-
Notifications
You must be signed in to change notification settings - Fork 0
/
EcommerceClient.js
161 lines (137 loc) · 4.14 KB
/
EcommerceClient.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
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
class EcommerceClient {
constructor(config = {}) {
this.baseURL = config.baseURL || 'https://your-api-endpoint.amazonaws.com';
this.timeout = config.timeout || 5000;
this.headers = {
'Content-Type': 'application/json',
...(config.headers || {})
};
}
// Helper method to handle API calls
async fetchWithTimeout(endpoint, options = {}) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
try {
const response = await fetch(`${this.baseURL}${endpoint}`, {
...options,
headers: this.headers,
signal: controller.signal
});
const responseData = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(`API Error: ${response.status} - ${responseData.error || response.statusText || 'Unknown error'}
\nEndpoint: ${endpoint}
\nResponse: ${JSON.stringify(responseData)}`);
}
return responseData.data;
} finally {
clearTimeout(timeoutId);
}
}
// Users
async getAllUsers() {
return this.fetchWithTimeout('/users', {
method: 'GET'
});
}
// Sales
async getSales() {
return this.fetchWithTimeout('/sales', {
method: 'GET'
});
}
// Inventory
async getInventory() {
return this.fetchWithTimeout('/inventory', {
method: 'GET'
});
}
async updateInventory(productId, warehouseId, quantity) {
return this.fetchWithTimeout('/inventory', {
method: 'PUT',
body: JSON.stringify({
product_id: productId,
warehouse_id: warehouseId,
quantity
})
});
}
// Fulfillment
async createFulfillment(orderId, warehouseId, items) {
return this.fetchWithTimeout('/fulfillment', {
method: 'POST',
body: JSON.stringify({
order_id: orderId,
warehouse_id: warehouseId,
items
})
});
}
async getFulfillmentStatus(fulfillmentId) {
return this.fetchWithTimeout(`/fulfillment/${fulfillmentId}`, {
method: 'GET'
});
}
}
// Example usage
const runExample = async () => {
const client = new EcommerceClient({
baseURL: 'https://your-api-endpoint.amazonaws.com',
timeout: 5000,
headers: {
'Authorization': 'Bearer your-token-here'
}
});
try {
// Get all users
const users = await client.getAllUsers();
console.log('Users:', users);
// Get sales data
const sales = await client.getSales();
console.log('Sales:', sales);
// Check inventory
const inventory = await client.getInventory();
console.log('Inventory:', inventory);
// Update inventory
const updatedInventory = await client.updateInventory(1, 1, 10);
console.log('Updated Inventory:', updatedInventory);
// Create a fulfillment
const fulfillment = await client.createFulfillment('ORD-2024-009', 1, [
{ product_id: 1, quantity: 1 },
{ product_id: 2, quantity: 2 }
]);
console.log('Created Fulfillment:', fulfillment);
// Check fulfillment status
const status = await client.getFulfillmentStatus(fulfillment.id);
console.log('Fulfillment Status:', status);
} catch (error) {
console.error('Error:', error.message);
}
};
// Test with fake data
const testClient = async () => {
const client = new EcommerceClient({
baseURL: 'https://fake-api.example.com'
});
// Mock response for testing
globalThis.fetch = async (url, options) => {
const mockData = {
data: {
id: 1,
status: 'success',
timestamp: new Date().toISOString()
}
};
return {
ok: true,
json: async () => mockData
};
};
try {
const result = await client.getSales();
console.log('Test result:', result);
} catch (error) {
console.error('Test error:', error);
}
};
module.exports = EcommerceClient;