apiclient

0.2.5 • Public • Published

API client

Build Status

API client adalah request wrapper

Basically, ApiClient is just a request wrapper. It wrap all request to more structured way. The way we separate remote endpoint specification from coding overhead.

Because of apiclient is just wrapping request, any of request options can be used here.

Usage

To access these URLs, we use apiclient this way :

URLs:

var Apiclient = require('apiclient');
var seed = {
  base: {
    protocol: 'https',
    hostname: 'remote.apihost.com',
    port: 443,
    pathname: 'v1/api'
  },
  path: {
    GET: {
      user: {
        location: 'user/%(userid)d',
        query: {
          apikey: 'asdf2345kjhnkj'
        }
      }
    },
    POST: {
      user: {
        location: 'user',
        query: {
          apikey: 'asdf2345kjhnkj'
        }
      }
    },
    PUT: {
      user: {
        location: 'user/%(userid)d',
        query: {
          apikey: 'asdf2345kjhnkj'
        }
      }
    },
    DELETE: {
      user: {
        location: 'user/%(userid)d',
        query: {
          apikey: 'asdf2345kjhnkj'
        }
      }
    }
  }
}
 
var Api = new Apiclient(seed);
 
Api.get('user', {userid: 1}, {}, function (error, response, body) {
  console.log(error, response, body);
});
 
Api.post('user', {}, {
  body: {
    username: 'John',
    password: 'John123'
  }
}, function (error, response, body) {
  console.log(error, response, body);
});
 
Api.put('user', {userid: 1}, {}, function (error, response, body) {
  console.log(error, response, body);
});
 
Api.delete('user', {userid: 1}, {}, function (error, response, body) {
  console.log(error, response, body);
});

Functions

ApiClient(data)

ApiClient constructor

isEmptyObject(obj)Boolean

Check if is object empty

ApiClient(data)

ApiClient constructor

Kind: global function

Param Type Description
data Object Seed data.

apiClient.download(api, param, options, callback) ⇒ Void

Download file from server

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

Param Type Description
api String Endpoint API.
param Object Parameter url object.
options Object Request option object.
callback function Callback function.

apiClient.get(api, param, options, callback) ⇒ Void

Get thing from server using method HTTP GET

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

Param Type Description
api String Endpoint API.
param Object Parameter url object.
options Object Request option object.
callback function Callback function.

apiClient.postUpload(api, param, options, callback) ⇒ Void

Upload file to server using POST method with attached file on body.

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

Param Type Description
api String Endpoint API.
param Object Parameter url object.
options Object Request option object.
callback function Callback function.

apiClient.postForm(api, param, options, callback) ⇒ Void

Upload file to server using POST method with attached file on body, formatted as multipart form.

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

Param Type Description
api String Endpoint API.
param Object Parameter url object.
options Object Request option object.
callback function Callback function.

apiClient.post(api, param, options, callback) ⇒ Void

Send string via POST method. This API will keep body on memory, attaching big string more than 1MB is not recommended, as it will fill the memory so fast.

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

Param Type Description
api String Endpoint API.
param Object Parameter url object.
options Object Request option object.
callback function Callback function.

apiClient.putForm(api, param, options, callback) ⇒ Void

Send data using PUT method with multipart/form-data format body.

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

Param Type Description
api String Endpoint API.
param Object Parameter url object.
options Object Request option object.
callback function Callback function.

apiClient.put(api, param, options, callback) ⇒ Void

Send data using PUT method with raw string body.

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

Param Type Description
api String Endpoint API.
param Object Parameter url object.
options Object Request option object.
callback function Callback function.

apiClient.delete(api, param, options, callback) ⇒ Void

Send data using DELETE method with raw string body.

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

Param Type Description
api String Endpoint API.
param Object Parameter url object.
options Object Request option object.
callback function Callback function.

isEmptyObject(obj) ⇒ Boolean

Check if is object empty

Kind: global function
Returns: Boolean - Return true if empty or false otherwise.

Param Type Description
obj Object Object to be Check

Readme

Keywords

Package Sidebar

Install

npm i apiclient

Weekly Downloads

70

Version

0.2.5

License

MIT

Last publish

Collaborators

  • septianw