New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@aofl/api-request

Package Overview
Dependencies
Maintainers
2
Versions
144
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aofl/api-request

A module for simplifying api calls

3.0.0-beta.11
Source
npm
Version published
Weekly downloads
72
33.33%
Maintainers
2
Weekly downloads
 
Created
Source

@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 referes 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, ....

Examples

  • Basic Example

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();
    const boundary = 'Boundary-' + Date.now();

    headers.append('Accept', '*/*');
    headers.append('Content-Type', 'multipart/form-data; boundary=' + boundary);

    if (typeof payload !== 'undefined') {
      body.append('arguments', JSON.stringify(payload));
    }

    return {
      method: 'POST',
      headers,
      body,
      mode: 'cors',
      credentails: '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) => {
});

Keywords

aofljs

FAQs

Package last updated on 08 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts