All files / src backend.js

78.95% Statements 15/19
100% Branches 4/4
60% Functions 6/10
100% Lines 13/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 671x                         1x             2x 2x             2x           2x   2x                                       1x 2x 2x 2x 2x   2x        
const BACKEND_URL = 'http://localhost:7000/greetings';
 
/**
 * Saves a new greeting on the server using an HTTP POST request
 * @param {greeting} greetingToBeSaved - The Greeting object that should be inserted.
 * The object must not be null and must not contain an id property. The id will be generated on the server.
 * @param {function} onSuccess - A Callback function that is invoked when a successful response is received from the
 * server. The callback receives an object as its only parameter that has a property "id" that contains the server
 * generated id for the new greeting.
 * @param onFailure {function} onFailure - Callback function that is invoked when the server called failed
 * (due to technical or other reasons, like invalid request parameters). The callback will be invoked with one
 * parameter that contains an error message (string)
 */
export const saveToServer = (greetingToBeSaved, onSuccess, onFailure) => {
    // Four potential return "scenarios":
    // SCENARIO 1: Server responded, HTTP 201 => OK, as expected
    // SCENARIO 2: Server responded, HTTP != 201 => Server error (e.g. invalid data posted)
    // SCENARIO 3. Server does NOT respond at all (technical problems etc)
    // SCENARIO 4: An error occurs during the response handling (=> catch-handler will be invoked)
 
    const handleServerResponse = response => response.json()
        .then(json => response.status === 201 ?
            /* SCENARIO 1 */ onSuccess(json)
            :
            /* SCENARIO 2 */ onFailure(json.error)
        );
 
    /* SCENARIO 3 */
    const handleServerError = err => onFailure(err.message);
 
    /* SCENARIO 4
     * (might be the same as Scenario 3 in real life, just
     * to make it more explicit here as own scenario)
     */
    const handleUnexpectedError = err => onFailure('Unexpected error: ' + err);
 
    return fetch(BACKEND_URL, {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(greetingToBeSaved)
    })
        .then(handleServerResponse, handleServerError)
        .catch(handleUnexpectedError)
    ;
};
 
/**
 * Loads a list of all greetings from the server
 * @param onSuccess {function} A Callback function that is invoked when a successful response is received from the server.
 * The callback is invoked with one parameter: an Array of all greetings
 * @param onFailure {function} The callback function is invoked when the server call failed. The callback is invoked
 * with one parameter: a string with an error message
 */
export const loadFromServer = (onSuccess, onFailure) => {
    const handleServerResponse = response => response.json()
        .then(json => response.status === 200 ? onSuccess(json) : onFailure(json.error));
    const handleServerError = err => onFailure(err.message);
    const handleUnexpectedError = err => onFailure('Unexpected error: ' + err);
 
    return fetch(BACKEND_URL)
        .then(handleServerResponse, handleServerError)
        .catch(handleUnexpectedError)
    ;
};