-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
130 lines (117 loc) · 3.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
const fetch = require('node-fetch');
const NodeCache = require('node-cache');
const qs = require('querystring');
const shipCache = new NodeCache({ stdTTL: 100, checkperiod: 120, errorOnMissing: true });
const headers = {
key: null
};
const requestData = {
APIHostName: 'http://api.rajaongkir.com',
APIUrl: {
city: '/starter/city',
province: '/starter/province',
cost: '/starter/cost'
}
};
exports.init = key => {
headers.key = key;
};
exports.getAPIResult = (apiPath, method, headers, result) => {
const hostName = `${requestData.APIHostName}${apiPath}`;
const options = {
method,
headers
};
let apiObject;
switch (apiPath) {
case requestData.APIUrl.province: apiObject = 'province'; break;
case requestData.APIUrl.city: apiObject = 'city'; break;
default: apiObject = '';
}
shipCache.get(apiObject, (err, value) => {
if (!err) {
result(value);
}
});
fetch(hostName, options)
.then(res => {
return res.json();
}).then(json => {
shipCache.set(apiObject, json.rajaongkir.results, 10000);
shipCache.get(apiObject, (err, val) => {
if (err) {
result(json.rajaongkir.results);
}
result(val);
});
});
};
exports.getAllProvince = result => {
this.getAPIResult(requestData.APIUrl.province, 'GET', headers, result);
};
exports.getProvinceById = (provinceId, result) => {
if (provinceId && Number.isInteger(provinceId)) {
this.getAPIResult(`${requestData.APIUrl.province}?id=${provinceId}`, 'GET', result);
} else {
console.error('provinceId must be filled and integer');
}
};
exports.getAllCity = result => {
this.getAPIResult(requestData.APIUrl.city, 'GET', headers, result);
};
exports.getCityById = (cityId, result) => {
if (cityId && Number.isInteger(cityId)) {
this.getAPIResult(`${requestData.APIUrl.city}?id=${cityId}`, 'GET', headers, result);
} else {
console.error('cityId must be filled and integer');
}
};
exports.getShippingCost = (origin, destination, weight, courier, result) => {
const costHeaders = Object.assign(headers, { 'content-type': 'application/x-www-form-urlencoded' });
const apiPath = requestData.APIUrl.cost;
const hostName = `${requestData.APIHostName}${apiPath}`;
const options = {
method: 'POST',
headers: costHeaders,
body: qs.stringify({
origin,
destination,
weight,
courier
})
};
fetch(hostName, options).then(res => {
return res.json();
}).then(json => {
const apiResult = {
origin_details: json.rajaongkir.origin_details,
destination_details: json.rajaongkir.destination_details,
results: json.rajaongkir.results
};
result(apiResult);
});
};
exports.checkPostalData = (postal, data, callback) => {
for (let a = 0; a < data.length; a++) {
if (data[a].postal_code == postal) {
callback(data[a]);
break;
}
}
};
exports.getCityByPostal = (postal, callback) => {
if (typeof postal === 'number') {
shipCache.get('city', (err, val) => {
if (err) {
this.getAllcity(results => {
this.checkPostalData(postal, results, callback);
});
} else {
this.checkPostalData(postal, val, callback);
}
});
} else {
console.error('Postal must be number');
}
};