@moqada/simple-api-client

Simple API Client for JavaScript

Stats

StarsIssuesVersionUpdatedCreatedSize
@moqada/simple-api-client
1210.5.03 years ago5 years agoMinified + gzip package size for @moqada/simple-api-client in KB

Readme

simple-api-client

Greenkeeper badge

NPM version Build Status Coverage Status Dependency Status DevDependency Status License

Simple API Client for JavaScript.

WIP

Installation

npm install --save @moqada/simple-api-client

Usage

JavaScript

import SimpleAPIClient from '@moqada/simple-api-client';

class APIClinet extends SimpleAPIClient {

  constructor({token, custom}: {token: string, custom: string}) {
    super({endpoint: 'http://api.example.com/v1'});
    this.token = token;
    this.custom = custom;
  }

  getDefaultOptions(): Object {
    return {
      headers: {
        'Authorization': `Bearer ${this.token}`,
        'X-Custom-Header': `${this.custom}`
      }
    };
  }

  toResponse(error: ?Object, response: ?Object): Object {
    if (error) {
      return {error};
    }
    return {
      body: response.body
    };
  }

  getUsers(query): Promise<{body: Object}, {error: Object}> {
    return this.get('/users', {query});
  }
}

const clinet = new APIClinet({token: 'xxxxxxxyyyyy', custom: 'foobar'});
client.getUsers({offset: 20, limit: 10}).then(({body}) => {
  console.log(body);
}).catch(({error}) => {
  console.error(error);
}):

TypeScript

A import style is different from JavaScript.

import {SimpleAPIClient} from '@moqada/simple-api-client';

Todo

  • Test
  • Support TypeScript (experimental)

Related

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.