npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

call-api-factory

v1.0.0

Published

Simple utils library for API calls.

Downloads

5

Readme

call-api-factory

Simple utils library for API calls.

npm version npm downloads npm license


Instalation

To install call-api-factory - you can use npm or yarn package manager.
npm install call-api-factory
yarn add call-api-factory

Documentation

The call-api-factory 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:

| Name | Type | | --- | --- | | url | string | | options | object | | events | object |

The options parameter may includes the following fields:

| Name | Type | Default value | | --- | --- | --- | | method | string | GET | | mode | string | same-origin | | credentials | string | omit | | redirect | string | following | | cache | string | default | | headers | object | Headers | | query | object | {} | | body | object | {} |

The events parameter may includes the following fields:

| Name | Type | Descriptions | | --- | --- | --- | | onRequest | function | Called before the request. Gets options as the first argument to modify it. | | onResponse | function | Called 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:

| Name | Type | | --- | --- | | options | object / aray of objects |

The options parameter may includes the following fields:

| Name | Type | Default value | | --- | --- | --- | | baseURL | string | | | version | string / number | none | | method | string | GET | | mode | string | same-origin | | credentials | string | omit | | redirect | string | following | | cache | string | default | | headers | object | Headers | | query | object | {} | | body | object | {} | | evens | object | {} |

The events parameter may includes the following fields:

| Name | Type | Descriptions | | --- | --- | --- | | onRequest | function | Called before the request. Gets options as the first argument to modify it. | | onResponse | function | Called after the request. Gets response as the first argument. |

Examples

import { callAPI } from 'call-api-factory';

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 'call-api-factory';

// 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 'call-api-factory';

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 } });
}