Skip to content

Latest commit

 

History

History
62 lines (48 loc) · 2.15 KB

resolvers.md

File metadata and controls

62 lines (48 loc) · 2.15 KB

API Map

Resolvers

What is a resolver?

A resolver is a function that creates a request, and returns a promise that will eventually resolve with a response object, or fail with an error object.

Here's what a simple one looks like:

function(url, method, data, options) {

    // process the parameters as needed
    if (method === 'GET' && typeof data === 'object') {
        let paramString = '?' + Object.keys(data).map(key => `${key}=${data[key]}`).join('&');
        url = url + paramString;
    }

    // create a promise that will eventually return the result of a request, based on the given parameters
    let promise = fetch(url, {
        method: method,
        body: method != 'GET' ? data : undefined,
        headers: options.headers,
        redirect: options.redirect
    });

    // add any post-processing or filtering
    promise = promise.then(res => res.json());

    return promise;
}

A resolver essentially describes how to make a requst in your app. This function becomes the resolver for all subsequent API calls that your app makes. As such, feel free to implement any API-wide features you want here, such as global error handleing, automatically adding an authentication header, etc.

Extending Resolvers

Sometimes you'll want to use someone else's resolver, but you'll want to add your own functionality to it. For example, if we wanted to extend the above resolver, we could do something like:

var resolver = require('resolver-above'); // include some awesome resolver from NPM

// create a function that extends the included resolver.  This will be the resolver you use to set up api-map.
function extendedResolver(url, method, data, options) {
    resolver(url, method, data, options)
    .then(res => {
        // handle responses in however you want
        console.log('There was a response!', res);

        // modify the response
        return {
            message: 'success',
            data: res
        }
    }, err => {
        // handle errors globally
        console.error('hey, there was an error', err);

        err.myCustomProp = 'What have you done to my request!?!?';

        return err;
    })
}