Skip to content

Commit 844d435

Browse files
committed
first commit
1 parent 97be758 commit 844d435

File tree

12 files changed

+875
-2
lines changed

12 files changed

+875
-2
lines changed

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Ignore system and temporary files
2+
.DS_Store
3+
Thumbs.db
4+
*.swp
5+
*.bak
6+
*.tmp
7+
8+
# Ignore files generated by editor/IDE
9+
.vscode/
10+
.idea/
11+
*.sublime-project
12+
*.sublime-workspace
13+
14+
# Ignore files or directories generated by dependency management tools
15+
/node_modules/
16+
/vendor/
17+
18+
# Ignore files generated by compilation
19+
/build/
20+
/dist/
21+
22+
# Ignore configuration files
23+
.env
24+
*.env.local
25+
.env.*.local
26+
.env.development
27+
.env.local
28+
.env.test
29+
.env.production
30+
31+
# Ignore npm/yarn files
32+
package-lock.json
33+
yarn.lock
34+
35+
# Ignore log files
36+
*.log

README.md

Lines changed: 234 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,234 @@
1-
# trackingmore-sdk-nodejs
2-
node.js SDK for TrackingMore API
1+
Trackingmore-sdk-nodejs
2+
=================
3+
4+
node.js SDK for Trackingmore API
5+
6+
Contact: <manage@trackingmore.org>
7+
8+
## Official document
9+
10+
[Document](https://www.trackingmore.com/docs/trackingmore/d5ac362fc3cda-api-quick-start)
11+
12+
## Index
13+
1. [Installation](https://github.com/TrackingMores/trackingmore-sdk-nodejs#installation)
14+
2. [Testing](https://github.com/TrackingMores/trackingmore-sdk-nodejs#testing)
15+
3. SDK
16+
1. [Couriers](https://github.com/TrackingMores/trackingmore-sdk-nodejs#couriers)
17+
2. [Trackings](https://github.com/TrackingMores/trackingmore-sdk-nodejs#trackings)
18+
3. [Air Waybill](https://github.com/TrackingMores/trackingmore-sdk-nodejs#air-waybill)
19+
20+
21+
## Installation
22+
```
23+
npm install trackingmore-sdk-nodejs
24+
```
25+
26+
## Quick Start
27+
28+
```javascript
29+
const TrackingMore = require('trackingmore-sdk-nodejs')
30+
const key = 'you api key'
31+
const trackingmore = new TrackingMore(key)
32+
try {
33+
// Get all couriers (couriers/all)
34+
trackingmore.couriers.getAllCouriers()
35+
.then(result => console.log(result))
36+
.catch(e => console.log(e))
37+
} catch (error) {
38+
console.error('An error occurred:', error.message)
39+
}
40+
```
41+
42+
## Test
43+
```
44+
npm run test or npm test
45+
```
46+
47+
## Error handling
48+
49+
Simply add a try-catch block
50+
51+
```javascript
52+
try {
53+
// Get all couriers (couriers/all)
54+
trackingmore.couriers.getAllCouriers()
55+
.then(result => console.log(result))
56+
.catch(e => console.log(e))
57+
} catch (error) {
58+
console.error('An error occurred:', error.message)
59+
}
60+
61+
```
62+
63+
## Couriers
64+
##### Return a list of all supported couriers.
65+
https://api.trackingmore.com/v4/couriers/all
66+
```javascript
67+
const key = 'you api key'
68+
const trackingmore = new TrackingMore(key)
69+
// Get all couriers (couriers/all)
70+
trackingmore.couriers.getAllCouriers()
71+
.then(result => console.log(result))
72+
.catch(e => console.log(e))
73+
```
74+
75+
##### Return a list of matched couriers based on submitted tracking number.
76+
https://api.trackingmore.com/v4/couriers/detect
77+
```javascript
78+
const key = 'you api key'
79+
const trackingmore = new TrackingMore(key)
80+
// Detect courier (couriers/detect)
81+
const params = {'tracking_number': '9400111899562537624326'}
82+
trackingmore.couriers.detect(params)
83+
.then(result => console.log(result))
84+
.catch(e => console.log(e))
85+
```
86+
87+
## Trackings
88+
##### Create a tracking.
89+
https://api.trackingmore.com/v4/trackings/create
90+
```javascript
91+
const key = 'you api key'
92+
const trackingmore = new TrackingMore(key)
93+
// Create a Tracking (trackings/create)
94+
const params = {
95+
'tracking_number': '9400111899562537624326',
96+
'courier_code': 'usps',
97+
'order_number': '',
98+
'customer_name': '',
99+
'title': '',
100+
'language': 'en',
101+
'note': 'test Order'
102+
}
103+
trackingmore.trackings.createTracking(params)
104+
.then(result => console.log(result))
105+
.catch(e => console.log(e))
106+
```
107+
108+
##### Get tracking results of multiple trackings.
109+
https://api.trackingmore.com/v4/trackings/get
110+
```javascript
111+
const key = 'you api key'
112+
const trackingmore = new TrackingMore(key)
113+
114+
// Get results (trackings/get)
115+
// Perform queries based on various conditions
116+
// const params = {
117+
// 'tracking_numbers': '9400111899562537624656',
118+
// 'courier_code': 'usps',
119+
// }
120+
// const params = {
121+
// 'tracking_numbers': '9400111899562539126562,9400111899562537624656',
122+
// 'courier_code': 'usps',
123+
// }
124+
const params = {
125+
'created_date_min': '2023-08-23T06:00:00+00:00',
126+
'created_date_max': '2023-09-05T07:20:42+00:00',
127+
}
128+
trackingmore.trackings.getTrackingResults(params)
129+
.then(result => console.log(result))
130+
.catch(e => console.log(e))
131+
```
132+
133+
##### Create multiple trackings (Max. 40 tracking numbers create in one call).
134+
https://api.trackingmore.com/v4/trackings/batch
135+
```javascript
136+
const key = 'you api key'
137+
const trackingmore = new TrackingMore(key)
138+
// Create trackings (trackings/batch)
139+
const params = [{
140+
'tracking_number': '9400111899562537680047',
141+
'courier_code':'usps'
142+
},{
143+
'tracking_number': '9400111899562537680048',
144+
'courier_code':'usps'
145+
}]
146+
trackingmore.trackings.batchCreateTrackings(params)
147+
.then(result => console.log(result))
148+
.catch(e => console.log(e))
149+
```
150+
151+
##### Update a tracking by ID.
152+
https://api.trackingmore.com/v4/trackings/update/{id}
153+
```javascript
154+
const key = 'you api key'
155+
const trackingmore = new TrackingMore(key)
156+
// Update a Tracking by ID (trackings/update)
157+
const params = {
158+
'customer_name': 'New name',
159+
'note':'New test order note'
160+
}
161+
const idString = "9a135b15b5d983e1d8950d99022db0c7"
162+
trackingmore.trackings.updateTrackingByID(idString, params)
163+
.then(result => console.log(result))
164+
.catch(e => console.log(e))
165+
```
166+
167+
##### Delete a tracking by ID.
168+
https://api.trackingmore.com/v4/trackings/delete/{id}
169+
```javascript
170+
const key = 'you api key'
171+
const trackingmore = new TrackingMore(key)
172+
// Delete Tracking by ID (trackings/delete)
173+
const idString = "9a135b15b5d983e1d8950d99022db0c7"
174+
trackingmore.trackings.deleteTrackingByID(idString)
175+
.then(result => console.log(result))
176+
.catch(e => console.log(e))
177+
```
178+
179+
##### Retrack expired tracking by ID.
180+
https://api.trackingmore.com/v4/trackings/retrack/{id}
181+
```javascript
182+
const key = 'you api key'
183+
const trackingmore = new TrackingMore(key)
184+
// Retrack expired Tracking by ID (trackings/retrack)
185+
const idString = "99f4ed7fc73aa83fe68fd69ab6458b28"
186+
trackingmore.trackings.retrackTrackingByID(idString)
187+
.then(result => console.log(result))
188+
.catch(e => console.log(e))
189+
```
190+
## Air Waybill
191+
##### Create an air waybill.
192+
https://api.trackingmore.com/v4/awb
193+
```javascript
194+
const key = 'you api key'
195+
const trackingmore = new TrackingMore(key)
196+
// Create an air waybill (awb)
197+
const params = {
198+
'awb_number': '235-69030430',
199+
}
200+
trackingmore.airWaybills.createAnAirWayBill(params)
201+
.then(result => console.log(result))
202+
.catch(e => console.log(e))
203+
```
204+
205+
## Response Code
206+
207+
Trackingmore uses conventional HTTP response codes to indicate success or failure of an API request. In general, codes in the 2xx range indicate success, codes in the 4xx range indicate an error that resulted from the provided information (e.g. a required parameter was missing, a charge failed, etc.), and codes in the 5xx range indicate an TrackingMore's server error.
208+
209+
210+
Http CODE|META CODE|TYPE | MESSAGE
211+
----|-----|--------------|-------------------------------
212+
200 |200 | <code>Success</code> | Request response is successful
213+
400 |400 | <code>BadRequest</code> | Request type error. Please check the API documentation for the request type of this API.
214+
400 |4101 | <code>BadRequest</code> | Tracking No. already exists.
215+
400 |4102 | <code>BadRequest</code> | Tracking No. no exists. Please use 「Create a tracking」 API first to create shipment.
216+
400 |4103 | <code>BadRequest</code> | You have exceeded the shipment quantity of API call. The maximum quantity is 40 shipments per call.
217+
400 |4110 | <code>BadRequest</code> | The value of tracking_number is invalid.
218+
400 |4111 | <code>BadRequest</code> | Tracking_number is required.
219+
400 |4112 | <code>BadRequest</code> | Invalid Tracking ID.
220+
400 |4113 | <code>BadRequest</code> | Retrack is not allowed. You can only retrack an expired tracking.
221+
400 |4120 | <code>BadRequest</code> | The value of courier_code is invalid.
222+
400 |4121 | <code>BadRequest</code> | Cannot detect courier.
223+
400 |4122 | <code>BadRequest</code> | Missing or invalid value of the special required fields for this courier.
224+
400 |4130 | <code>BadRequest</code> | The format of Field name is invalid.
225+
400 |4160 | <code>BadRequest</code> | The awb_number is required or invaild format.
226+
400 |4161 | <code>BadRequest</code> | The awb airline does not support yet.
227+
400 |4190 | <code>BadRequest</code> | You are reaching the maximum quota limitation, please upgrade your current plan.
228+
401 |401 | <code>Unauthorized</code> | Authentication failed or has no permission. Please check and ensure your API Key is correct.
229+
403 |403 | <code>Forbidden</code> | Access prohibited. The request has been refused or access is not allowed.
230+
404 |404 | <code>NotFound</code> | Page does not exist. Please check and ensure your link is correct.
231+
429 |429 | <code>TooManyRequests</code>| Exceeded API request limits, please try again later. Please check the API documentation for the limit of this API.
232+
500 |511 | <code>ServerError</code> | Server error. Please contact us: service@trackingmore.org.
233+
500 |512 | <code>ServerError</code> | Server error. Please contact us: service@trackingmore.org.
234+
500 |513 | <code>ServerError</code> | Server error. Please contact us: service@trackingmore.org.

__tests__/airWaybills.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const AirWaybills = require('../src/tracking/airWaybills')
2+
const Request = require('../src/lib/request')
3+
4+
describe('AirWaybills', () => {
5+
describe('constructor', () => {
6+
it('should throw an error when apiKey is missing', () => {
7+
expect(() => new AirWaybills()).toThrow('API Key is missing');
8+
});
9+
10+
it('should create an AirWaybills instance with apiKey', () => {
11+
const apiKey = 'your-api-key';
12+
const airWaybills = new AirWaybills(apiKey);
13+
expect(airWaybills.apiKey).toBe(apiKey);
14+
expect(airWaybills.apiModule).toBe('');
15+
});
16+
});
17+
18+
describe('createAnAirWayBill', () => {
19+
it('should throw an error when awb_number is missing', () => {
20+
const apiKey = 'your-api-key';
21+
const airWaybills = new AirWaybills(apiKey);
22+
23+
expect(() => airWaybills.createAnAirWayBill({})).toThrow('Awb number cannot be empty');
24+
});
25+
26+
it('should send a POST request to /awb with awb_number', () => {
27+
const apiKey = 'your-api-key';
28+
const airWaybills = new AirWaybills(apiKey);
29+
const sendApiRequestMock = jest.spyOn(Request, 'sendApiRequest').mockReturnValue({});
30+
31+
const params = { awb_number: 'your-awb-number' };
32+
airWaybills.createAnAirWayBill(params);
33+
34+
expect(sendApiRequestMock).toHaveBeenCalledWith('/awb', apiKey, 'POST', params);
35+
});
36+
});
37+
});

__tests__/couriers.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const Couriers = require('../src/tracking/couriers')
2+
const Request = require('../src/lib/request')
3+
4+
describe('Couriers', () => {
5+
describe('constructor', () => {
6+
it('should throw an error when apiKey is missing', () => {
7+
expect(() => new Couriers()).toThrow('API Key is missing')
8+
})
9+
10+
it('should create a Couriers instance with apiKey', () => {
11+
const apiKey = 'your-api-key'
12+
const couriers = new Couriers(apiKey)
13+
expect(couriers.apiKey).toBe(apiKey)
14+
expect(couriers.apiModule).toBe('couriers')
15+
})
16+
})
17+
18+
describe('getAllCouriers', () => {
19+
it('should send a GET request to /couriers/all', () => {
20+
const apiKey = 'your-api-key'
21+
const couriers = new Couriers(apiKey)
22+
const sendApiRequestMock = jest.spyOn(Request, 'sendApiRequest').mockReturnValue({})
23+
24+
couriers.getAllCouriers()
25+
26+
expect(sendApiRequestMock).toHaveBeenCalledWith('couriers/all', apiKey, 'GET')
27+
})
28+
})
29+
30+
describe('detect', () => {
31+
it('should throw an error when tracking_number is missing', () => {
32+
const apiKey = 'your-api-key'
33+
const couriers = new Couriers(apiKey)
34+
35+
expect(() => couriers.detect({})).toThrow('Tracking number cannot be empty')
36+
})
37+
38+
it('should send a POST request to /couriers/detect with tracking_number', () => {
39+
const apiKey = 'your-api-key'
40+
const couriers = new Couriers(apiKey)
41+
const sendApiRequestMock = jest.spyOn(Request, 'sendApiRequest').mockReturnValue({})
42+
43+
const params = { tracking_number: 'your-tracking-number' }
44+
couriers.detect(params)
45+
46+
expect(sendApiRequestMock).toHaveBeenCalledWith('couriers/detect', apiKey, 'POST', params)
47+
})
48+
})
49+
})

0 commit comments

Comments
 (0)