Skip to content

Commit 0a34f5f

Browse files
committed
Improve code validation information
1 parent 01dbafd commit 0a34f5f

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

__tests__/airWaybills.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ describe('AirWaybills', () => {
2222

2323
expect(() => airWaybills.createAnAirWayBill({})).toThrow('Awb number cannot be empty');
2424
});
25+
26+
it('should throw an error when awb_number is formatted incorrectly', () => {
27+
const apiKey = 'your-api-key';
28+
const airWaybills = new AirWaybills(apiKey);
29+
30+
expect(() => airWaybills.createAnAirWayBill({awb_number: '123456'})).toThrow('The air waybill number format is invalid and can only be 12 digits in length');
31+
});
2532

2633
it('should send a POST request to /awb with awb_number', () => {
2734
const apiKey = 'your-api-key';
2835
const airWaybills = new AirWaybills(apiKey);
2936
const sendApiRequestMock = jest.spyOn(Request, 'sendApiRequest').mockReturnValue({});
3037

31-
const params = { awb_number: 'your-awb-number' };
38+
const params = { awb_number: '235-69030430' };
3239
airWaybills.createAnAirWayBill(params);
3340

3441
expect(sendApiRequestMock).toHaveBeenCalledWith('/awb', apiKey, 'POST', params);

__tests__/tracking.test.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ describe('Trackings', () => {
2020
const apiKey = 'your-api-key';
2121
const trackings = new Trackings(apiKey);
2222

23-
expect(() => trackings.createTracking({})).toThrow('Tracking number cannot be empty');
23+
expect(() => trackings.createTracking({ tracking_number: '', courier_code: 'usps' })).toThrow('Tracking number cannot be empty');
2424
});
2525

2626
it('should throw an error when courier_code is missing', () => {
2727
const apiKey = 'your-api-key';
2828
const trackings = new Trackings(apiKey);
2929

30-
expect(() => trackings.createTracking({ tracking_number: 'your-tracking-number' })).toThrow('Courier Code cannot be empty');
30+
expect(() => trackings.createTracking({ tracking_number: '9400111899562537624646', courier_code: '' })).toThrow('Courier Code cannot be empty');
3131
});
3232

3333
it('should send a POST request to /trackings/create with params', () => {
3434
const apiKey = 'your-api-key';
3535
const trackings = new Trackings(apiKey);
3636
const sendApiRequestMock = jest.spyOn(Request, 'sendApiRequest').mockReturnValue({});
3737

38-
const params = { tracking_number: 'your-tracking-number', courier_code: 'your-courier-code' };
38+
const params = { tracking_number: '9400111899562537624646', courier_code: 'usps' };
3939
trackings.createTracking(params);
4040

4141
expect(sendApiRequestMock).toHaveBeenCalledWith('trackings/create', apiKey, 'POST', params);
@@ -48,7 +48,7 @@ describe('Trackings', () => {
4848
const trackings = new Trackings(apiKey);
4949
const sendApiRequestMock = jest.spyOn(Request, 'sendApiRequest').mockReturnValue({});
5050

51-
const params = { tracking_number: 'your-tracking-number' };
51+
const params = { tracking_numbers: '9400111899562537624646' };
5252
const paramsValue = new URLSearchParams(params).toString()
5353
trackings.getTrackingResults(params);
5454

@@ -65,13 +65,29 @@ describe('Trackings', () => {
6565

6666
expect(() => trackings.batchCreateTrackings(params)).toThrow('Max. 40 tracking numbers create in one call');
6767
});
68+
69+
it('should throw an error when tracking_number is missing', () => {
70+
const apiKey = 'your-api-key';
71+
const trackings = new Trackings(apiKey);
72+
73+
const params = [{ tracking_number: '', courier_code: 'usps' }];
74+
expect(() => trackings.batchCreateTrackings(params)).toThrow('Tracking number cannot be empty');
75+
});
76+
77+
it('should throw an error when courier_code is missing', () => {
78+
const apiKey = 'your-api-key';
79+
const trackings = new Trackings(apiKey);
80+
81+
const params = [{ tracking_number: '9400111899562537624646', courier_code: '' }];
82+
expect(() => trackings.batchCreateTrackings(params)).toThrow('Courier Code cannot be empty');
83+
});
6884

6985
it('should send a POST request to /trackings/batch with params', () => {
7086
const apiKey = 'your-api-key';
7187
const trackings = new Trackings(apiKey);
7288
const sendApiRequestMock = jest.spyOn(Request, 'sendApiRequest').mockReturnValue({});
7389

74-
const params = [{ tracking_number: 'your-tracking-number', courier_code: 'your-courier-code' }];
90+
const params = [{ tracking_number: '9400111899562537624646', courier_code: 'usps' }];
7591
trackings.batchCreateTrackings(params);
7692

7793
expect(sendApiRequestMock).toHaveBeenCalledWith('trackings/batch', apiKey, 'POST', params);

src/tracking/airWaybills.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ class AirWaybills {
1414
if (!params.awb_number) {
1515
throw new Error('Awb number cannot be empty')
1616
}
17-
const apiPath = "awb"
18-
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, "POST", params)
17+
if (params.awb_number.length != 12) {
18+
throw new Error('The air waybill number format is invalid and can only be 12 digits in length')
19+
}
20+
const apiPath = 'awb'
21+
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, 'POST', params)
1922
return response
2023
}
2124

src/tracking/trackings.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,59 @@ class Trackings {
1717
if(!params.courier_code) {
1818
throw new Error('Courier Code cannot be empty')
1919
}
20-
const apiPath = "create"
21-
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, "POST", params)
20+
const apiPath = 'create'
21+
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, 'POST', params)
2222
return response
2323
}
2424

2525
getTrackingResults(params) {
2626
const paramsValue = new URLSearchParams(params).toString()
27-
const apiPath = "get?" + paramsValue
28-
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, "GET")
27+
const apiPath = 'get?' + paramsValue
28+
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, 'GET')
2929
return response
3030
}
3131

3232
batchCreateTrackings(params) {
3333
if(params.length > 40) {
3434
throw new Error('Max. 40 tracking numbers create in one call')
3535
}
36-
const apiPath = "batch"
37-
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, "POST", params)
36+
for(let i=0;i<params.length;i++){
37+
if(!params[i]['tracking_number']){
38+
throw new Error('Tracking number cannot be empty')
39+
}
40+
if(!params[i]['courier_code']){
41+
throw new Error('Courier Code cannot be empty')
42+
}
43+
}
44+
const apiPath = 'batch'
45+
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, 'POST', params)
3846
return response
3947
}
4048

4149
updateTrackingByID(idString, params) {
4250
if(!idString) {
4351
throw new Error('Id cannot be empty')
4452
}
45-
const apiPath = "update/"+ idString
46-
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, "PUT", params)
53+
const apiPath = 'update/'+ idString
54+
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, 'PUT', params)
4755
return response
4856
}
4957

5058
deleteTrackingByID(idString) {
5159
if(!idString) {
5260
throw new Error('Id cannot be empty')
5361
}
54-
const apiPath = "delete/"+ idString
55-
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, "DELETE")
62+
const apiPath = 'delete/'+ idString
63+
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, 'DELETE')
5664
return response
5765
}
5866

5967
retrackTrackingByID(idString) {
6068
if(!idString) {
6169
throw new Error('Id cannot be empty')
6270
}
63-
const apiPath = "retrack/"+ idString
64-
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, "POST")
71+
const apiPath = 'retrack/'+ idString
72+
const response = Request.sendApiRequest(this.apiModule + '/' + apiPath, this.apiKey, 'POST')
6573
return response
6674
}
6775

0 commit comments

Comments
 (0)