Fetcher

Fetcher Wrapper of the native Fetch API

Constructor

new Fetcher(url, headersopt)

Construcotr

Parameters:
NameTypeAttributesDescription
urlstring

Base url

headersHeaders<optional>

Header data.

Examples

Using callbacks

const fetcher = new Fetcher( 'https://api.example.com' );
fetcher.get( '/books', function( err, result ) { } );

Using await

const fetcher = new Fetcher( 'https://api.example.com' );
try
{
    const result = await fetcher.get( '/books' );
}
catch( e )
{
    // Handle error
}

Methods

(async) delete(endpoint, cbopt) → {Promise.<(any|undefined)>}

DELETE call

Parameters:
NameTypeAttributesDefaultDescription
endpointstring

API endpoint

cbfunction<optional>
null

Callback function

Returns:
Type: 
Promise.<(any|undefined)>

(async) get(endpoint, cbopt) → {Promise.<(any|undefined)>}

GET call

Parameters:
NameTypeAttributesDefaultDescription
endpointstring

API endpoint

cbfunction<optional>
null

Callback function

Returns:
Type: 
Promise.<(any|undefined)>

(async) patch(endpoint, dataopt, cbopt) → {Promise.<(any|undefined)>}

PATCH call

Parameters:
NameTypeAttributesDefaultDescription
endpointstring

API endpoint

dataobject<optional>
{}

Patch data

cbfunction<optional>
null

Callback function

Returns:
Type: 
Promise.<(any|undefined)>

(async) post(endpoint, dataopt, cbopt) → {Promise.<(any|undefined)>}

POST call

Parameters:
NameTypeAttributesDefaultDescription
endpointstring

API endpoint

dataobject<optional>
{}

Post data

cbfunction<optional>
null

Callback function

Returns:
Type: 
Promise.<(any|undefined)>

(async) put(endpoint, dataopt, cbopt) → {Promise.<(any|undefined)>}

PUT call

Parameters:
NameTypeAttributesDefaultDescription
endpointstring

API endpoint

dataobject<optional>
{}

PUT data

cbfunction<optional>
null

Callback function

Returns:
Type: 
Promise.<(any|undefined)>