1.1.1 • Published 6 months ago

biteship-wrapper v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

biteship-wrapper

Types npm version CircleCI Known Vulnerabilities NPM download/month NPM download total
Biteship API wrapper library for NodeJS

Features

  • Async Await or Promise Support
  • Typescript Support
  • Built-in Cache (Memory, File or Redis)

Install

npm install biteship-wrapper

Usage

This library was created refer to Biteship API documentation version 1.

Set Config

const Biteship = require('biteship-wrapper');
// or
// import Biteship from 'biteship-wrapper';

const config = {
  api_key: "YOUR_API_KEY"
};

const biteship = new Biteship(config);

Setup Cache Config

// simple cache with memory or file
const config = {
  api_key: "YOUR_API_KEY",
  cache_config: {
    namespace: 'biteship',
    engine: 'memory', // you can use "memory", "file" or "redis"
  }
};

// cache using redis
// Example url connection redis:
// redis[s]://[[username][:password]@][host][:port][/db-number]
const config = {
  api_key: "YOUR_API_KEY",
  cache_config: {
    namespace: 'biteship',
    engine: 'redis',
    url: 'redis://127.0.0.1:6379' // this url is required for redis connection.
  }
};

Note:

  • Cache will work only for any request with method GET. In this case, it will work for most action retrieve.
    But, its won't running automatically even you have already set the cache_config.
  • If the response is false or error, then it would not getting cached.
  • This library is not included with Redis Client library, you might required to install manually.
npm install redis

Example to use Maps API

Using Callback

biteship.action('retrieve').maps({ input: 'jakarta selatan' }).send(function(err, res) {
  if(err) return console.log(err);
  console.log(res);
});

// with cache for 1 hour or 3600 seconds.
biteship.action('retrieve').maps({ input: 'jakarta selatan' }).cache(3600).send(function(err, res) {
  if(err) return console.log(err);
  console.log(res);
});

Using Promise

biteship.action('retrieve').maps({ input: 'jakarta selatan' }).sendAsync()
  .then(res => {
    console.log(res);
  })
  .catch(err => {
    console.log(err);
  });

// with cache for 1 hour or 3600 seconds.
biteship.action('retrieve').maps({ input: 'jakarta selatan' }).cache(3600).sendAsync()
  .then(res => {
    console.log(res);
  })
  .catch(err => {
    console.log(err);
  });

Using Async Await

try {
  const res = await biteship.action('retrieve').maps({ input: 'jakarta selatan' }).sendAsync();
  console.log(res);
} catch(err) {
  console.log(err);
}

// with cache for 1 hour or 3600 seconds.
try {
  const res = await biteship.action('retrieve').maps({ input: 'jakarta selatan' }).cache(3600).sendAsync();
  console.log(res);
} catch(err) {
  console.log(err);
}

Output Response

Here is the output response standard in Biteship API.

Success Response

{
  "status": 200,
  "success": true,
  "message": "",
  "code": 20001007,
  "data": []
}

Note:

  • status could be 2xx or 3xx.
  • success always true
  • code must be a number and its from Biteship.

Error Response

{
  "status": 400,
  "success": false,
  "error": "",
  "code": 40000001
}

Note:

  • status could be 4xx or 5xx.
  • success always false
  • code must be a number and its from Biteship.

Methods

  • action(name: string): this
  • maps(payload: object): this
  • rates(payload: object): this
  • locations(payload?: object | null, id?: string): this
  • draftOrders(payload?: object | null, id?: string, customPath?: string): this
  • orders(payload?: object | null, id?: string): this
  • couriers(): this
  • trackings(id: string): this
  • publicTrackings(waybillId: string, courierId: string): this
  • cache(ttl: number): this
  • send(callback: (error: any, response?: any) => void): void
  • sendAsync(): Promise<{status: number;success: boolean;[key: string]: any;}>

Using Rates API

biteship.action('retrieve').rates({
  origin_area_id: "IDNP6IDNC148IDND836IDZ12410",
  destination_area_id: "IDNP6IDNC148IDND836IDZ12430",
  couriers: "paxel,jne,sicepat",
  items: [
    {
      name: "Shoes",
      description: "Black colored size 45",
      value: 199000,
      length: 30,
      width: 15,
      height: 20,
      weight: 200,
      quantity: 2
    }
  ]
}).send(function(err, res) {
  if(err) return console.log(err);
  console.log(res);
});

Note:

  • Please see the payload to retrieve rates here.

Using Locations API

Create

biteship.action('create').locations({
  name: "Apotik Gambir",
  contact_name: "Ahmad",
  contact_phone: "08123456789",
  address: "Jl. Gambir Selatan no 5. Blok F 92. Jakarta Pusat.",
  note: "Dekat tulisan warung Bu Indah",
  postal_code: 10110,
  latitude: -6.232123121,
  longitude: 102.22189911,
  type: "origin",
}).send(function(err, res) {
  if(err) return console.log(err);
  console.log(res);
});

Note:

  • Please see the payload to create locations here.

Retrieve

biteship.action('retrieve').locations(null, '61d565c69a3211036a05f3f8')
  .send(function(err, res) {
    if(err) return console.log(err);
    console.log(res);
});

Update

biteship.action('update').locations({
  name: "Apotik Monas",
}, '61d565c69a3211036a05f3f8').send(function(err, res) {
  if(err) return console.log(err);
  console.log(res);
});

Note:

  • Please see the payload to update locations here.

Delete

biteship.action('delete').locations(null, '61d565c69a3211036a05f3f8')
  .send(function(err, res) {
    if(err) return console.log(err);
    console.log(res);
});

Using Draft Orders API

Create

biteship.action('create').draftOrders({
  origin_contact_name: "Amir",
  origin_contact_phone: "081234567890",
  origin_address: "Plaza Senayan, Jalan Asia Afrik...",
  origin_note: "Deket pintu masuk STC",
  origin_postal_code: 12440,
  destination_contact_name: "John Doe",
  destination_contact_phone: "088888888888",
  destination_contact_email: "johndoe@example.com",
  destination_address: "Lebak Bulus MRT...",
  destination_postal_code: 12950,
  destination_note: "Near the gas station",
  delivery_type: "now",
  order_note: "Please be careful",
  items: [
    {
      name: "Black L",
      description: "White Shirt",
      category: "fashion",
      value: 165000,
      quantity: 1,
      height: 10,
      length: 10,
      weight: 200,
      width: 10
    }
  ]
}).send(function(err, res) {
  if(err) return console.log(err);
  console.log(res);
});

Note:

  • Please see the payload to create draft orders here.

Retrieve

biteship.action('retrieve')
  .draftOrders(null, 'ef18275c-02a9-4887-a56b-f374edb96ec4')
  .send(function(err, res) {
    if(err) return console.log(err);
    console.log(res);
});

Retrieve with Rates

biteship.action('retrieve')
  .draftOrders(null, 'ef18275c-02a9-4887-a56b-f374edb96ec4', '/rates')
  .send(function(err, res) {
    if(err) return console.log(err);
    console.log(res);
});

Update

biteship.action('update').draftOrders({
  courier_company: "sicepat",
  courier_type: "reg"
}, 'ef18275c-02a9-4887-a56b-f374edb96ec4')
.send(function(err, res) {
  if(err) return console.log(err);
  console.log(res);
});

Note:

  • Please see the payload to update draft orders here.

Confirm

biteship.action('confirm')
  .draftOrders(null, 'ef18275c-02a9-4887-a56b-f374edb96ec4', '/confirm')
  .send(function(err, res) {
    if(err) return console.log(err);
    console.log(res);
});

Delete

biteship.action('delete')
  .draftOrders(null, 'ef18275c-02a9-4887-a56b-f374edb96ec4')
  .send(function(err, res) {
    if(err) return console.log(err);
    console.log(res);
});

Using Orders API

Create

biteship.action('create').orders({
  shipper_contact_name: "Amir",
  shipper_contact_phone: "088888888888",
  shipper_contact_email: "biteship@test.com",
  shipper_organization: "Biteship Org Test",
  origin_contact_name: "Amir",
  origin_contact_phone: "088888888888",
  origin_address: "Plaza Senayan, Jalan Asia Afrik...",
  origin_note: "Deket pintu masuk STC",
  origin_postal_code: 12440,
  destination_contact_name: "John Doe",
  destination_contact_phone: "088888888888",
  destination_contact_email: "jon@test.com",
  destination_address: "Lebak Bulus MRT...",
  destination_postal_code: 12950,
  destination_note: "Near the gas station",
  courier_company: "jne",
  courier_type: "reg",
  courier_insurance: 500000,
  delivery_type: "now",
  order_note: "Please be careful",
  metadata: {},
  items: [
    {
      name: "Black L",
      description: "White Shirt",
      category: "fashion",
      value: 165000,
      quantity: 1,
      height: 10,
      length: 10,
      weight: 200,
      width: 10
    }
  ]
}).send(function(err, res) {
  if(err) return console.log(err);
  console.log(res);
});

Note:

  • Please see the payload to create orders here.

Retrieve

biteship.action('retrieve').orders(null, '5dd599ebdefcd4158eb8470b')
  .send(function(err, res) {
    if(err) return console.log(err);
    console.log(res);
});

Delete

biteship.action('delete').orders(null, '5dd599ebdefcd4158eb8470b')
  .send(function(err, res) {
    if(err) return console.log(err);
    console.log(res);
});

Using Couriers API

Retrieve

biteship.action('retrieve').couriers()
  .send(function(err, res) {
    if(err) return console.log(err);
    console.log(res);
});

Using Trackings API

Retrieve

biteship.action('retrieve').trackings('6051861741a37414e6637fab')
  .send(function(err, res) {
    if(err) return console.log(err);
    console.log(res);
});

Using Public Trackings API

Retrieve

biteship.action('retrieve')
  .publicTrackings('0123082100003094', '6051861741a37414e6637fab')
  .send(function(err, res) {
    if(err) return console.log(err);
    console.log(res);
});
1.1.1

6 months ago

1.1.0

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago