Code Monkey home page Code Monkey logo

api-wrappy's Introduction

Uniwrap

Install

npm install --save api-wrappy

Define

// contentType is for the Request header's 'Content-Type'
// responseType is for the Response header's 'Content-Type'
// cf: https://developer.mozilla.org/en-US/docs/Web/API/Response#Methods
// works for fetch or any other http client
const def = {
  basePath: 'http://api.example.com',
  prefix: '/v0', // not required
  routes: {
    'getUser': { // call name
      uri: '/user/:id', // :id auto processed by 'call' and it's parameters
      method: 'get',
      responseType: 'json',
    },
    'createUser': {
      uri: '/user',
      method: 'post',
      contentType: 'application/xml', // I know xml sucks, it's just an example
      responseType: 'json',
    },
    ...
  },
};

Instantiate

// app.config.js
import Wrappy from 'api-wrappy'; // wrapper inside node_modules

import def from './def.js';

const wrapper = new Wrappy(def);
// OR directly define, whatever
const wrapper = new Wrappy({...});

export default wrapper;

Use

import wrapper from './app.config.js';

async function apiCalls() {
  try {
    wrapper.call('getUser', {id: 24}).then((user) => {
      ...
    });
  } catch (err) {
    // handle error
    console.log('Error: ', err);
  }
  // OR
  wrapper.call('getUser', {id: 24}) // still works as async returns a promise
  .then((user) => {...})
  .catch((e) => {
    // handle not found or whatever
    console.log('Error: ', err);
  });
  // OR
  try {
    const user = await wrapper.call('getUser', {id: 24});
    const userList = await wrapper.call('createUser', {
      headers: {Authorization: 'Bearer <ANiceToken>'},
      body: {name: 'Foo'},
    });
    //OR
    const ret = wrapper.callMultiple([
      wrapper.call('getUser', {id: 24}),
      wrapper.call('createUser', {...}),
      ...
    ]); // ret = [response for getUser, response for createUser, ...]; <- Very powerful
  } catch(err) {
    console.log('Error: ', err); // Errors are caught, allelujah!
  }
}

api-wrappy's People

Contributors

ganitzsh avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.