-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
320 lines (282 loc) · 10 KB
/
index.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import axios, { AxiosInstance } from 'axios';
export class TShipAPI {
private axiosInstance: AxiosInstance;
constructor(private secretKey: string) {
this.axiosInstance = axios.create({
baseURL: 'https://api.terminal.africa/v1',
headers: {
'Authorization': `Bearer ${this.secretKey}`,
'Content-Type': 'application/json'
}
});
}
public async getCarriers(active?: boolean, type?: string, perPage: number = 100, page: number = 1): Promise<any> {
try {
const response = await this.axiosInstance.get('/carriers', {
params: { active, type, perPage, page }
});
return response.data;
} catch (error) {
console.error('Error fetching carriers:', error);
throw error;
}
}
public async getCarrier(carrierId: string): Promise<any> {
try {
const response = await this.axiosInstance.get(`/carriers/${carrierId}`);
return response.data;
} catch (error) {
console.error(`Error fetching carrier with ID ${carrierId}:`, error);
throw error;
}
}
public async getDropOffLocations(country: string, state?: string, city?: string, carrier?: number): Promise<any> {
try {
const response = await this.axiosInstance.get('/locations/drop-off', {
params: { country, state, city, carrier }
});
return response.data;
} catch (error) {
console.error('Error fetching drop-off locations:', error);
throw error;
}
}
// Add this new method to the TShipAPI class
public async getShipmentRates(options: {
currency?: string,
deliveryAddress?: string,
pickupAddress?: string,
parcelId?: string,
shipmentId?: string,
cashOnDelivery?: boolean
}): Promise<any> {
try {
const params = {
currency: options.currency,
delivery_address: options.deliveryAddress,
pickup_address: options.pickupAddress,
parcel: options.parcelId,
shipment_id: options.shipmentId,
cash_on_delivery: options.cashOnDelivery
};
const response = await this.axiosInstance.get('/rates/shipment', { params });
return response.data;
} catch (error) {
console.error('Error fetching shipment rates:', error);
throw error;
}
}
// Add this new method to the TShipAPI class
public async getShipmentQuotes(options: {
pickupAddress: object,
deliveryAddress: object,
parcel: object,
carrierId?: string,
currency?: string,
cashOnDelivery?: boolean
}): Promise<any> {
try {
const data = {
pickup_address: options.pickupAddress,
delivery_address: options.deliveryAddress,
parcel: options.parcel,
carrier_id: options.carrierId,
currency: options.currency,
cash_on_delivery: options.cashOnDelivery
};
const response = await this.axiosInstance.post('/rates/shipment/quotes', data);
return response.data;
} catch (error) {
console.error('Error fetching shipment quotes:', error);
throw error;
}
}
// Add this new method to the TShipAPI class
public async getUserRates(perPage: number = 100, page: number = 1): Promise<any> {
try {
const params = { perPage, page };
const response = await this.axiosInstance.get('/rates', { params });
return response.data;
} catch (error) {
console.error('Error fetching user rates:', error);
throw error;
}
}
public async getRate(rateId: string, currency?: string): Promise<any> {
try {
const params = { currency };
const response = await this.axiosInstance.get(`/rates/${rateId}`, { params });
return response.data;
} catch (error) {
console.error('Error fetching rate:', error);
throw error;
}
}
public async createParcel(parcelData: {
description?: string,
items: Array<object>,
metadata?: object,
packaging: string,
weight_unit: string
}): Promise<any> {
try {
const response = await this.axiosInstance.post('/parcels', parcelData);
return response.data;
} catch (error) {
console.error('Error creating parcel:', error);
throw error;
}
}
public async updateParcel(parcelId: string, updateData: {
description?: string,
items?: Array<object>,
metadata?: object,
packaging?: string
}): Promise<any> {
try {
const response = await this.axiosInstance.put(`/parcels/${parcelId}`, updateData);
return response.data;
} catch (error) {
console.error('Error updating parcel:', error);
throw error;
}
}
public async getParcels(perPage: number = 100, page: number = 1): Promise<any> {
try {
const params = { perPage, page };
const response = await this.axiosInstance.get('/parcels', { params });
return response.data;
} catch (error) {
console.error('Error fetching parcels:', error);
throw error;
}
}
public async getParcel(parcelId: string): Promise<any> {
try {
const response = await this.axiosInstance.get(`/parcels/${parcelId}`);
return response.data;
} catch (error) {
console.error('Error fetching parcel details:', error);
throw error;
}
}
public async createShipment(shipmentData: {
address_from: string,
address_to: string,
address_return?: string,
metadata?: object,
parcel?: string,
parcels?: Array<string>,
shipment_purpose: string,
shipment_type?: string
}): Promise<any> {
try {
const response = await this.axiosInstance.post('/shipments', shipmentData);
return response.data;
} catch (error) {
console.error('Error creating shipment:', error);
throw error;
}
}
public async createQuickShipment(quickShipmentData: {
pickup_address: object,
delivery_address: object,
parcel?: object,
parcels?: Array<object>,
metadata?: string,
shipment_purpose: string,
shipment_type?: string
}): Promise<any> {
try {
const response = await this.axiosInstance.post('/shipments/quick', quickShipmentData);
return response.data;
} catch (error) {
console.error('Error creating quick shipment:', error);
throw error;
}
}
public async updateShipment(shipmentId: string, updateData: {
address_to?: string,
address_from?: string,
address_return?: string,
metadata?: object,
parcel?: string,
parcels?: Array<string>,
shipment_purpose?: string
}): Promise<any> {
try {
const response = await this.axiosInstance.put(`/shipments/${shipmentId}`, updateData);
return response.data;
} catch (error) {
console.error('Error updating shipment:', error);
throw error;
}
}
public async getShipments(queryParams: {
perPage?: string,
page?: string,
populate?: boolean,
status?: string,
}): Promise<any> {
try {
const response = await this.axiosInstance.get('/shipments', { params: queryParams });
return response.data;
} catch (error) {
console.error('Error fetching shipments:', error);
throw error;
}
}
public async getShipment(shipmentId: string): Promise<any> {
try {
const response = await this.axiosInstance.get(`/shipments/${shipmentId}`);
return response.data;
} catch (error) {
console.error('Error fetching shipment details:', error);
throw error;
}
}
public async trackShipment(shipmentId: string): Promise<any> {
try {
const response = await this.axiosInstance.get(`/shipments/track/${shipmentId}`);
return response.data;
} catch (error) {
console.error('Error tracking shipment:', error);
throw error;
}
}
public async cancelShipment(shipmentId: string): Promise<any> {
try {
const response = await this.axiosInstance.post('/shipments/cancel', { shipment_id: shipmentId });
return response.data;
} catch (error) {
console.error('Error canceling shipment:', error);
throw error;
}
}
public async deleteShipment(shipmentId: string): Promise<any> {
try {
const response = await this.axiosInstance.delete('/shipments', {
data: { shipment_id: shipmentId },
});
return response.data;
} catch (error) {
console.error('Error deleting shipment:', error);
throw error;
}
}
public async arrangePickupAndDelivery(rateId: string, shipmentId?: string, purchaseInsurance?: boolean, cashToCollect?: number): Promise<any> {
try {
const requestData = {
rate_id: rateId,
shipment_id: shipmentId,
purchase_insurance: purchaseInsurance,
cash_to_collect: cashToCollect,
};
const response = await this.axiosInstance.post('/shipments/pickup', requestData);
return response.data;
} catch (error) {
console.error('Error arranging pickup and delivery:', error);
throw error;
}
}
}