biteship-wrapper
v2.0.0
Published
Biteship API wrapper library for NodeJS
Maintainers
Readme
biteship-wrapper
Biteship API wrapper library for NodeJS
| Version | Node.JS | Redis Client Library Version | |---|---|---| | 1.x | 16, 18, 20 | 4.x | | 2.x | 20, 22 | 5.x |
Features
- Async Await or Promise Support
- Typescript Support
- Built-in Cache (Memory, File or Redis)
Install
npm install biteship-wrapperUsage
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 actionretrieve.
But, its won't running automatically even you have already set thecache_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- Please see Redis Client Configuration Options.
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:
statuscould be 2xx or 3xx.successalways truecodemust be a number and its from Biteship.
Error Response
{
"status": 400,
"success": false,
"error": "",
"code": 40000001
}Note:
statuscould be 4xx or 5xx.successalways falsecodemust be a number and its from Biteship.
Methods
action(name: string): thismaps(payload: object): thisrates(payload: object): thislocations(payload?: object | null, id?: string): thisdraftOrders(payload?: object | null, id?: string, customPath?: string): thisorders(payload?: object | null, id?: string): thisordersCancellationReasons(lang: string): thiscouriers(): thistrackings(id: string): thispublicTrackings(waybillId: string, courierId: string): thiscache(ttl: number): thissend(callback: (error: any, response?: any) => void): voidsendAsync(): 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: "[email protected]",
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: "[email protected]",
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: "[email protected]",
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);
});Cancel
biteship.action('cancel').orders({
code: 'change_courier',
reason: 'Want to change courier'
}, '5dd599ebdefcd4158eb8470b')
.send(function(err, res) {
if(err) return console.log(err);
console.log(res);
});Note:
- Please see the payload to cancel orders here.
Using Orders Cancellation Reasons API
Retrieve
biteship.action('retrieve').ordersCancellationReasons('en')
.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);
});