@aofl/api-request
    TypeScript icon, indicating that this package has built-in type declarations

    3.13.1 • Public • Published

    @aofl/api-request

    A module for making api calls easier. Key features include caching responses and request/response formatters.

    Caching is implemented using @aofl/cache-manager and uses MemoryCache. @aofl/api-request caches the promises created by fetch api.

    Request/response formatting refers to constructing a request payload and parsing the response payload. A formatter object should implement pack() and unpack() properties. pack() is to construct the request payload and unpack() is used to parse the response. Any number of formatters can be added to apiRequestInstance using the addFromatter() method. E.g. GetFormatter, PostFormatter, CorsFormatter, FileUploadFormatter, GoogleMapsFormatter, ....

    Api Documentation

    Examples

    Installation

    npm i -S @aofl/api-request

    Usage

    import {ApiRequest} from '@aofl/api-request';
    
    class PostFormatter {
      static pack(payload) {
        const headers = new Headers();
        const body = new FormData();
    
        if (typeof payload !== 'undefined') {
          body.append('arguments', JSON.stringify(payload));
        }
    
        return {
          method: 'POST',
          headers,
          body,
          mode: 'cors',
          credentials: 'include'
        };
      }
    
      static unpack(response) {
        return response.json()
        .then((data) => {
          if (data.status !== 'success') {
            return Promise.reject(data);
          }
          return data;
        });
      }
    }
    
    const apiRequestInstance = new ApiRequest();
    apiRequestInstance.addFormatter('post', PostFormatter);
    
    apiRequestInstance.request('https://example.org/', {}, 'post', true, 'sample-namespace')
    .then((jsonResponse) => {
    })
    .catch((jsonResponse) => {
    });

    Install

    npm i @aofl/api-request

    DownloadsWeekly Downloads

    95

    Version

    3.13.1

    License

    MIT

    Unpacked Size

    27.5 kB

    Total Files

    8

    Last publish

    Collaborators

    • alexdinari
    • ariankhosravi