1.1.7 • Published 6 years ago

@vladrozhnev/call-api v1.1.7

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

@vladrozhnev/call-api

Simple utils library for API calls.

npm version npm downloads npm license


Instalation

To install @vladrozhnev/call-api - you can use npm or yarn package manager.
npm install @vladrozhnev/call-api
yarn add @vladrozhnev/call-api

Documentation

The @vladrozhnev/call-api library includes two functions: callAPI and callAPIFactory. Below you can find the documentation for both functions.


The callAPI function

The simple method for API calls. Currently is a build on native fetch API.
The callAPI function accept next arguments:
NameType
urlstring
optionsobject
eventsobject
The options parameter may includes the following fields:
NameTypeDefault value
methodstringGET
modestringsame-origin
credentialsstringomit
redirectstringfollowing
cachestringdefault
headersobjectHeaders
queryobject{}
bodyobject{}
The events parameter may includes the following fields:
NameTypeDescriptions
onRequestfunctionCalled before the request. Gets options as the first argument to modify it.
onResponsefunctionCalled after the request. Gets response as the first argument.

The callAPIFactory function

Factory for custom callAPI function. The difference betwee factory function and custom function is compatibilities with versions. You don't need to implement different API calls methods for different API versions. You may use only one callAPI for different API versions.
The callAPIFactory function accept next arguments:
NameType
optionsobject / aray of objects

The options parameter may includes the following fields:
NameTypeDefault value
baseURLstring
versionstring / numbernone
methodstringGET
modestringsame-origin
credentialsstringomit
redirectstringfollowing
cachestringdefault
headersobjectHeaders
queryobject{}
bodyobject{}
evensobject{}
The events parameter may includes the following fields:
NameTypeDescriptions
onRequestfunctionCalled before the request. Gets options as the first argument to modify it.
onResponsefunctionCalled after the request. Gets response as the first argument.

Examples

import { callAPI } from '@vladrozhnev/call-api';

function someContext() {
  // Promise style
  // Will make request to https://example.com/api/1/public/users?limit=10 | GET
  callAPI('https://example.com/api/1/public/users', {
    method: 'GET',
    query: { limit: 10 }
  })
  .then((response) => console.log(response))
  .catch((error) => console.error(error));
}

async function someContext() {
  // Asyncfunction style
  // Will make request to https://example.com/api/1/public/users?limit=10 | GET
  const response = await callAPI('https://example.com/api/1/public/users', {
    method: 'GET',
    query: { limit: 10 }
  });
}

// Generator style
// Will make request to https://example.com/api/1/public/users | POST
function* someContext() {
  const response = yield callAPI('https://example.com/api/1/public/users', {
    method: 'POST',
    body: { userName: 'Bob' }
  });
}

import { callAPI } from '@vladrozhnev/call-api';

// Events usage
async function someContext() {
  const url = 'https://example.com/api/1/secure/users';
  const options = { query: { limit: 10 } };
  const events = {
    onRequest: (options) => {
      const token = getSomeToken();
      options.headers.append('Authorization', `Bearer ${token}`);
    }
  }

  // Will make request to https://example.com/api/1/public/users?limit=10 | GET
  const response = await callAPI(url, options, events);
}

import { callAPIFactory } from '@vladrozhnev/call-api';

async function someContext() {
  const callAPI = callAPIFactory({
    baseURL: 'https://example.com/api/1/public',
    version: 'none' // You don't need to provide a version for only one option.
  });

  // Will make request to https://example.com/api/1/public/users?page=1 | GET
  const response = await callAPI('/users', { query: { page: 1 } });
}

async function someContext() {
  const callAPI = callAPIFactory([
    {
      baseURL: 'https://example.com/api/1/public',
      version: 1
    },
    {
      baseURL: 'https://example.com/api/1.1/public',
      version: 1.1
    },
    {
      baseURL: 'https://example.com/api/2/public',
      version: 2
    },
    {
      baseURL: 'https://example.com/api/3/secure',
      version: 'secure',
      events: {
        onRequest: (options) => {
          const token = getSomeToken();
          options.headers.append('Authorization', `Bearer ${token}`);
        }
      }
    }
  ]);

  // Will make request to https://example.com/api/1/public/users?page=1 | GET
  const response1 = await callAPI(1, '/users', { query: { page: 1 } });

  // Will make request to https://example.com/api/1.1/public/users?page=2 | GET
  const response11 = await callAPI(1.1, '/users', { query: { page: 2 } });

  // Will make request to https://example.com/api/2/public/users?page=3 | GET
  const response2 = await callAPI(2, '/users', { query: { page: 3 } });

  // Will make request to https://example.com/api/3/secure/users?page=4 | GET
  const response3 = await callAPI('secure', '/users', { query: { page: 4 } });
}
1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago