Skip to content

Commit 8577a27

Browse files
authored
Merge pull request #9 from thexdev/feature
feat(api): add delete order command
2 parents 1606bcd + 9b6ee66 commit 8577a27

File tree

6 files changed

+110
-2
lines changed

6 files changed

+110
-2
lines changed

src/commands/delete-order.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Command from './command';
2+
3+
export class DeleteOrder extends Command {
4+
execute(): Promise<Response> {
5+
return this.http
6+
.setBody({
7+
cancellation_reason: this.extra.cancellation_reason,
8+
})
9+
.delete(`v1/orders/${this.extra.id}`);
10+
}
11+
}

src/http.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ export class Http {
5555
const response = await this.setUrl(url).setMethod('POST').run();
5656
return await response.json();
5757
}
58+
59+
async delete(url: string) {
60+
const response = await this.setUrl(url).setMethod('DELETE').run();
61+
return await response.json();
62+
}
5863
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Biteship } from './biteship';
22
import { CreateOrder } from './commands/create-order';
3+
import { DeleteOrder } from './commands/delete-order';
34
import { RetrieveArea } from './commands/retrieve-area';
45
import { RetrieveCourierRates } from './commands/retrieve-courier-rates';
56
import { RetrieveOrder } from './commands/retrieve-order';
@@ -8,6 +9,7 @@ import { SearchRates } from './commands/search-rates';
89
export {
910
Biteship,
1011
CreateOrder,
12+
DeleteOrder,
1113
RetrieveArea,
1214
RetrieveOrder,
1315
RetrieveCourierRates,

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export type ApiKey = string;
2-
export type HTTPMethod = 'GET' | 'POST';
2+
export type HTTPMethod = 'GET' | 'POST' | 'DELETE';
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import Command from '../../src/commands/command';
2+
import { DeleteOrder } from '../../src';
3+
import { rest } from 'msw';
4+
import { baseUrl } from '../fixtures/base-url';
5+
import {
6+
invokerWithApiKey,
7+
invokerWithInvalidApiKey,
8+
invokerWithMissingApiKey,
9+
} from '../fixtures/invoker';
10+
import { server } from '../fixtures/server';
11+
12+
describe('create-order.ts', () => {
13+
it('should be able to instantiated', async () => {
14+
const command = new DeleteOrder();
15+
expect(command).toBeInstanceOf(Command);
16+
});
17+
18+
describe('when API_KEY is not provided', () => {
19+
it('should returns authorization failure', async () => {
20+
const payload = {
21+
success: true,
22+
error: 'Authorization failed',
23+
code: 40101001,
24+
};
25+
26+
server.use(
27+
rest.delete(baseUrl('orders/1'), (_, res, ctx) => {
28+
return res(ctx.status(401), ctx.json(payload));
29+
}),
30+
);
31+
32+
const command = new DeleteOrder({ id: 1 });
33+
34+
const response = await invokerWithMissingApiKey.send(command);
35+
36+
expect(response).toEqual(payload);
37+
});
38+
});
39+
40+
describe('when API_KEY is invalid', () => {
41+
it('should return invalid api key warnings', async () => {
42+
const payload = {
43+
success: false,
44+
error:
45+
'Authentication for your key is failed. Please make sure input the right key or contact info@biteship.com for more information.',
46+
code: 40000001,
47+
};
48+
49+
server.use(
50+
rest.delete(baseUrl('orders/1'), (_, res, ctx) => {
51+
return res(ctx.status(400), ctx.json(payload));
52+
}),
53+
);
54+
55+
const command = new DeleteOrder({ id: 1 });
56+
57+
const response = await invokerWithInvalidApiKey.send(command);
58+
59+
expect(response).toEqual(payload);
60+
});
61+
});
62+
63+
describe('when order successfully deleted', () => {
64+
it('should returns 200 OK', async () => {
65+
const payload = {
66+
success: true,
67+
message: 'Order successfully deleted',
68+
object: 'order',
69+
id: '5dd5a396248481164a225af4',
70+
status: 'cancelled',
71+
cancellation_reason: 'Ingin mengganti kurir',
72+
};
73+
74+
server.use(
75+
rest.delete(baseUrl('orders/1'), (_, res, ctx) => {
76+
return res(ctx.status(200), ctx.json(payload));
77+
}),
78+
);
79+
80+
const command = new DeleteOrder({
81+
id: 1,
82+
cancelllation_reason: 'Ingin mengganti kurir',
83+
});
84+
85+
const response = await invokerWithApiKey.send(command);
86+
87+
expect(response).toEqual(payload);
88+
});
89+
});
90+
});

tests/commands/retrieve-order.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('retrieve-order.ts', () => {
5151
}),
5252
);
5353

54-
const command = new RetrieveOrder({id: 123});
54+
const command = new RetrieveOrder({ id: 123 });
5555

5656
const response = await invokerWithInvalidApiKey.send(command);
5757

0 commit comments

Comments
 (0)