js-api-service

1.0.7 • Public • Published

js-api-service

A simple way to make API request from the client end

Installation

Use the package manager npm to install js-api-service.

npm install js-api-service

Explanation

ApiService.request(method, path, callback, errorCallback, payload, exectuteWhileLoading);

  1. method: this is any of the HTTP request method - get, delete, head, options, posst, put, patch, purge, link, unlink
  2. path: this is the relative path to the needed resource - '/user'
  3. callback: this is a function that gets executed when the request is successful - (response) => console.log('response from API -> ', response)
  4. errorCallback: this is a error function that gets executed when the request fails - (error) => console.log('error from API -> ', error)
  5. payload (optional): this is an object that needs you to send data to the API - { key: value }
  6. exectuteWhileLoading (optional): this is a function that gets executed while the request is loading - NB: you can show a toast or upload progress

Usage

import { ApiService } from 'js-api-service';

const userApiService = new ApiService('https://jsonplaceholder.typicode.com');
// or
// const userApiService = new ApiService('https://jsonplaceholder.typicode.com', 'Bearer **token**');
// or
// const userApiService = new ApiService('https://jsonplaceholder.typicode.com', 'Bearer **token**', 'application/json');
userApiService.request(
    'get', 
    '/users',
    response => {
        console.log(response);
        return response;
    },
    error => {
        console.log(error);
        return error;
    },
    null,
    console.log('Loading all users...'),
);

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

GNU

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i js-api-service

    Weekly Downloads

    5

    Version

    1.0.7

    License

    GPL-3.0-or-later

    Unpacked Size

    39.7 kB

    Total Files

    4

    Last publish

    Collaborators

    • akinolu52