1.0.0 • Published 4 years ago

custom-fetch v1.0.0

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
4 years ago

Custom Fetch

Creates a function with the same API as WHATWG's fetch, but with your own custom behaviour. Ideal for testing or for modifying an HTTP request before sending.

import CustomFetch from 'custom-fetch';
import * as NodeFetch from 'node-fecth';

// You need to bring your own Request and Response constructors
// There are already libraries that implement these and I didn't want
// to re-implement them just for the sake of it.
const { Request, Response } = NodeFetch;

const fetch = new CustomFetch(async (request, signal) => {
    // `request` is an instance of the provided Request class,
    // constructed based on the arguments passed to `fetch`.
    // `signal` is the value of `signal` on the fetch options object
    // (or undefined if not provided)

    switch(request.url) {
        case 'http://horses.example/':
            // You can return just a response body and it will be
            // used as the first argument to the Response constructor
            return 'Welcome to horses.example';
        case 'http://flowers.example/foxglove.json':
            // You can also return a preconstructed Response object
            // (as long as it's of the same class as the given
            // Response constructor)
            return new Response(
                JSON.stringify({
                    name: 'Foxglove',
                    scientificName: 'Digitalis purpurea',
                    gbifId: 5414995
                }),
                {
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }
            )
        default:
            // Since NodeFetch.fetch returns a Response object,
            // we can just forward the request on if we like,
            // or we could make modifications to the request
            // before doing so.
            request = new Request(request, { headers: { 'X-Favourite-Color': 'blue' } })
            return NodeFetch.fetch(request, { signal });
},
// Let CustomFetch know how to construct the request and response objects
{ Request, Response }
);

export default fetch('http://horses.example/');

Dependencies

None

custom-fetch

module.exports ⏏

Custom Fetch constructor

Kind: Exported class

new module.exports(fetchHandler, fetchConstructors)

ParamTypeDescription
fetchHandlerfetchHandlerCallback for handling fetch request.
fetchConstructorsobjectObject containing constructors for creating fetch Request and Response classes
fetchConstructors.RequestRequestConstructor for a fetch Request object
fetchConstructors.ResponseResponseConstructor for a fetch Response object

module.exports~Request : function

Kind: inner class of module.exports

new Request(url, options)
ParamType
urlstring
optionsobject

module.exports~Response : function

Kind: inner class of module.exports

new Response(body, options)
ParamType
bodystring
optionsobject

module.exports~fetchHandler ⇒ Response | string

Kind: inner typedef of module.exports

ParamType
requestRequest
signalSignal
1.0.0

4 years ago