Latest Socket ResearchMalicious Chrome Extension Performs Hidden Affiliate Hijacking.Details
Socket
Book a DemoInstallSign in
Socket

@single9/api-wrapper

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@single9/api-wrapper

Call your APIs like a function.

Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

API Wrapper

Call your APIs like a function.

Installation

npm i @single9/api-wrapper

Usage

const ApiWrapper = require('@single9/api-wrapper');

Create

const api = new ApiWrapper([
  {
    name: '<Api Name>',       // only allow certain words and digits
    path: '<Api Path>',       // e.g. /api/posts
    method: '<HTTP Method>',  // e.g. post
  },
], {
  baseUrl: '<Base URL of API>'   // e.g. https://jsonplaceholder.typicode.com
                              // Default: http://localhost:3000
  headers: {
    // The headers you want to send. e.g. 'authorization': 'Bearer SAdoweasd...',
  },
  auth: { // authorization
    username: 'username',
    password: 'password',
  }
})

Use

api.<api_name>(params)
  • api: Your ApiWrapper instance.
  • api_name: The name of the API that you set before.
  • params: Compatible with axios request config
    • queryString
    • pathParams

Params

params.queryString

Used for query string. e.g. /users?limit=100

api.test({
  queryString: {
    key: value
  }
})
api.test({
  queryString: [
    {
      name: string,
      value: string | number,
    }
  ]
})

params.pathParams

Used for path parameters. e.g. /user/:id

api.test({
  pathParams: {
    key: value
  }
})
api.test({
  pathParams: [
    {
      name: string,
      value: string | number,
    }
  ]
})

Example

const ApiWrapper = require('@single9/api-wrapper');

// Create your API schema
const schema = [
  {
    name: 'newPost',  // this is your api function name
    path: '/posts',
    method: 'post',
  },
  {
    name: 'getTodo',
    path: '/todos/:todoId',  // path parameter
    method: 'get',
  },
];

const api = new ApiWrapper(schema, {
  baseUrl: 'https://jsonplaceholder.typicode.com',
});

async function start() {
  try {
    const post = await api.newPost({
      // Post Body
      data: {
        title: 'foo!!!!!!',
        body: 'bar!!',
        userId: 1
      },
    });

    console.log(post.data);

    const get = await api.getTodo({
      pathParams: {
        todoId: 2, // replace `:todoId` with value 2.
      },
    });

    console.log(get.data);
  } catch (err) {
    console.error(err);
  }
}

start();

Keywords

restful

FAQs

Package last updated on 22 Oct 2021

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