|
1
|
import { HttpTypes, SelectParams } from "@medusajs/types" |
|
2
|
import { Client } from "../client.js" |
|
3
|
import { ClientHeaders } from "../types.js" |
|
4
|
|
|
5
|
export class Customer { |
|
6
|
/** |
|
7
|
* @ignore |
|
8
|
*/ |
|
9
|
private client: Client |
|
10
|
/** |
|
11
|
* @ignore |
|
12
|
*/ |
|
13
|
constructor(client: Client) { |
|
14
|
this.client = client |
|
15
|
} |
|
16
|
|
|
17
|
/** |
|
18
|
* This method creates a customer. It sends a request to the |
|
19
|
* [Create Customer](https://docs.medusajs.com/api/admin/customers/create-customer) API route. |
|
20
|
* |
|
21
|
* @param body - The customer's details. |
|
22
|
* @param query - Configure the fields to retrieve in the customer. |
|
23
|
* @param headers - Headers to pass in the request. |
|
24
|
* @returns The customer's details. |
|
25
|
* |
|
26
|
* @example |
|
27
|
* sdk.admin.customer.create({ |
|
28
|
* email: "customer@gmail.com" |
|
29
|
* }) |
|
30
|
* .then(({ customer }) => { |
|
31
|
* console.log(customer) |
|
32
|
* }) |
|
33
|
*/ |
|
34
|
async create( |
|
35
|
body: HttpTypes.AdminCreateCustomer, |
|
36
|
query?: SelectParams, |
|
37
|
headers?: ClientHeaders |
|
38
|
) { |
|
39
|
return this.client.fetch<HttpTypes.AdminCustomerResponse>( |
|
40
|
`/admin/customers`, |
|
41
|
{ |
|
42
|
method: "POST", |
|
43
|
headers, |
|
44
|
body, |
|
45
|
query, |
|
46
|
} |
|
47
|
) |
|
48
|
} |
|
49
|
|
|
50
|
/** |
|
51
|
* This method updates a customer's details. It sends a request to the |
|
52
|
* [Update Customer](https://docs.medusajs.com/api/admin/customers/update-a-customer) API route. |
|
53
|
* |
|
54
|
* @param id - The customer's ID. |
|
55
|
* @param body - The details to update of the customer. |
|
56
|
* @param query - Configure the fields to retrieve in the customer. |
|
57
|
* @param headers - Headers to pass in the request. |
|
58
|
* @returns The customer's details. |
|
59
|
* |
|
60
|
* @example |
|
61
|
* sdk.admin.customer.update("cus_123", { |
|
62
|
* first_name: "John" |
|
63
|
* }) |
|
64
|
* .then(({ customer }) => { |
|
65
|
* console.log(customer) |
|
66
|
* }) |
|
67
|
*/ |
|
68
|
async update( |
|
69
|
id: string, |
|
70
|
body: HttpTypes.AdminUpdateCustomer, |
|
71
|
query?: SelectParams, |
|
72
|
headers?: ClientHeaders |
|
73
|
) { |
|
74
|
return this.client.fetch<HttpTypes.AdminCustomerResponse>( |
|
75
|
`/admin/customers/${id}`, |
|
76
|
{ |
|
77
|
method: "POST", |
|
78
|
headers, |
|
79
|
body, |
|
80
|
query, |
|
81
|
} |
|
82
|
) |
|
83
|
} |
|
84
|
|
|
85
|
/** |
|
86
|
* This method retrieves a paginated list of customers. It sends a request to the |
|
87
|
* [List Customers](https://docs.medusajs.com/api/admin/customers/list-customers) |
|
88
|
* API route. |
|
89
|
* |
|
90
|
* @param queryParams - Filters and pagination configurations. |
|
91
|
* @param headers - Headers to pass in the request. |
|
92
|
* @returns The paginated list of customers. |
|
93
|
* |
|
94
|
* @example |
|
95
|
* To retrieve the list of customers: |
|
96
|
* |
|
97
|
* ```ts |
|
98
|
* sdk.admin.customer.list() |
|
99
|
* .then(({ customers, count, limit, offset }) => { |
|
100
|
* console.log(customers) |
|
101
|
* }) |
|
102
|
* ``` |
|
103
|
* |
|
104
|
* To configure the pagination, pass the `limit` and `offset` query parameters. |
|
105
|
* |
|
106
|
* For example, to retrieve only 10 items and skip 10 items: |
|
107
|
* |
|
108
|
* ```ts |
|
109
|
* sdk.admin.customer.list({ |
|
110
|
* limit: 10, |
|
111
|
* offset: 10 |
|
112
|
* }) |
|
113
|
* .then(({ customers, count, limit, offset }) => { |
|
114
|
* console.log(customers) |
|
115
|
* }) |
|
116
|
* ``` |
|
117
|
* |
|
118
|
* Using the `fields` query parameter, you can specify the fields and relations to retrieve |
|
119
|
* in each customer: |
|
120
|
* |
|
121
|
* ```ts |
|
122
|
* sdk.admin.customer.list({ |
|
123
|
* fields: "id,*groups" |
|
124
|
* }) |
|
125
|
* .then(({ customers, count, limit, offset }) => { |
|
126
|
* console.log(customers) |
|
127
|
* }) |
|
128
|
* ``` |
|
129
|
* |
|
130
|
* Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations). |
|
131
|
*/ |
|
132
|
async list( |
|
133
|
queryParams?: HttpTypes.AdminCustomerFilters, |
|
134
|
headers?: ClientHeaders |
|
135
|
) { |
|
136
|
return this.client.fetch<HttpTypes.AdminCustomerListResponse>( |
|
137
|
`/admin/customers`, |
|
138
|
{ |
|
139
|
headers, |
|
140
|
query: queryParams, |
|
141
|
} |
|
142
|
) |
|
143
|
} |
|
144
|
|
|
145
|
/** |
|
146
|
* This method retrieves a customer by its ID. It sends a request to the |
|
147
|
* [Get Customer](https://docs.medusajs.com/api/admin/customers/get-a-customer) |
|
148
|
* API route. |
|
149
|
* |
|
150
|
* @param id - The customer's ID. |
|
151
|
* @param query - Configure the fields to retrieve in the customer. |
|
152
|
* @param headers - Headers to pass in the request. |
|
153
|
* @returns The customer's details. |
|
154
|
* |
|
155
|
* @example |
|
156
|
* To retrieve a customer by its ID: |
|
157
|
* |
|
158
|
* ```ts |
|
159
|
* sdk.admin.customer.retrieve("cus_123") |
|
160
|
* .then(({ customer }) => { |
|
161
|
* console.log(customer) |
|
162
|
* }) |
|
163
|
* ``` |
|
164
|
* |
|
165
|
* To specify the fields and relations to retrieve: |
|
166
|
* |
|
167
|
* ```ts |
|
168
|
* sdk.admin.customer.retrieve("cus_123", { |
|
169
|
* fields: "id,*groups" |
|
170
|
* }) |
|
171
|
* .then(({ customer }) => { |
|
172
|
* console.log(customer) |
|
173
|
* }) |
|
174
|
* ``` |
|
175
|
* |
|
176
|
* Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations). |
|
177
|
*/ |
|
178
|
async retrieve(id: string, query?: SelectParams, headers?: ClientHeaders) { |
|
179
|
return this.client.fetch<HttpTypes.AdminCustomerResponse>( |
|
180
|
`/admin/customers/${id}`, |
|
181
|
{ |
|
182
|
query, |
|
183
|
headers, |
|
184
|
} |
|
185
|
) |
|
186
|
} |
|
187
|
|
|
188
|
/** |
|
189
|
* This method deletes a customer by its ID. It sends a request to the |
|
190
|
* [Delete Customer](https://docs.medusajs.com/api/admin/customers/delete-a-customer) |
|
191
|
* API route. |
|
192
|
* |
|
193
|
* @param id - The customer's ID. |
|
194
|
* @param headers - Headers to pass in the request. |
|
195
|
* @returns The deletion's details. |
|
196
|
* |
|
197
|
* @example |
|
198
|
* sdk.admin.customer.delete("cus_123") |
|
199
|
* .then(({ deleted }) => { |
|
200
|
* console.log(deleted) |
|
201
|
* }) |
|
202
|
*/ |
|
203
|
async delete(id: string, headers?: ClientHeaders) { |
|
204
|
return this.client.fetch<HttpTypes.AdminCustomerDeleteResponse>( |
|
205
|
`/admin/customers/${id}`, |
|
206
|
{ |
|
207
|
method: "DELETE", |
|
208
|
headers, |
|
209
|
} |
|
210
|
) |
|
211
|
} |
|
212
|
|
|
213
|
/** |
|
214
|
* This method manages customer groups for a customer. |
|
215
|
* It sends a request to the [Manage Customers](https://docs.medusajs.com/api/admin/customers/manage-customer-groups) |
|
216
|
* API route. |
|
217
|
* |
|
218
|
* @param id - The customer's ID. |
|
219
|
* @param body - The groups to add customer to or remove customer from. |
|
220
|
* @param headers - Headers to pass in the request |
|
221
|
* @returns The customers details. |
|
222
|
* |
|
223
|
* @example |
|
224
|
* sdk.admin.customer.batchCustomerGroups("cus_123", { |
|
225
|
* add: ["cusgroup_123"], |
|
226
|
* remove: ["cusgroup_321"] |
|
227
|
* }) |
|
228
|
* .then(({ customer }) => { |
|
229
|
* console.log(customer) |
|
230
|
* }) |
|
231
|
*/ |
|
232
|
async batchCustomerGroups( |
|
233
|
id: string, |
|
234
|
body: HttpTypes.AdminBatchLink, |
|
235
|
headers?: ClientHeaders |
|
236
|
) { |
|
237
|
return await this.client.fetch<HttpTypes.AdminCustomerResponse>( |
|
238
|
`/admin/customers/${id}/customer-groups`, |
|
239
|
{ |
|
240
|
method: "POST", |
|
241
|
headers, |
|
242
|
body, |
|
243
|
} |
|
244
|
) |
|
245
|
} |
|
246
|
|
|
247
|
/** |
|
248
|
* This method creates a customer address. It sends a request to the |
|
249
|
* [Create Customer Address](https://docs.medusajs.com/api/admin/customers/add-address) |
|
250
|
* API route. |
|
251
|
* |
|
252
|
* @param id - The customer's ID. |
|
253
|
* @param body - The customer address's details. |
|
254
|
* @param headers - Headers to pass in the request. |
|
255
|
* @returns The customer address's details. |
|
256
|
* |
|
257
|
* @example |
|
258
|
* sdk.admin.customer.createAddress("cus_123", { |
|
259
|
* address_1: "123 Main St", |
|
260
|
* city: "Anytown", |
|
261
|
* country_code: "US", |
|
262
|
* postal_code: "12345" |
|
263
|
* }) |
|
264
|
* .then(({ customer }) => { |
|
265
|
* console.log(customer) |
|
266
|
* }) |
|
267
|
*/ |
|
268
|
async createAddress( |
|
269
|
id: string, |
|
270
|
body: HttpTypes.AdminCreateCustomerAddress, |
|
271
|
headers?: ClientHeaders |
|
272
|
) { |
|
273
|
return await this.client.fetch<HttpTypes.AdminCustomerResponse>( |
|
274
|
`/admin/customers/${id}/addresses`, |
|
275
|
{ |
|
276
|
method: "POST", |
|
277
|
headers, |
|
278
|
body, |
|
279
|
} |
|
280
|
) |
|
281
|
} |
|
282
|
|
|
283
|
/** |
|
284
|
* This method updates a customer address. It sends a request to the |
|
285
|
* [Update Customer Address](https://docs.medusajs.com/api/admin/customers/update-address) |
|
286
|
* API route. |
|
287
|
* |
|
288
|
* @param id - The customer's ID. |
|
289
|
* @param addressId - The customer address's ID. |
|
290
|
* @param body - The customer address's details. |
|
291
|
* @param headers - Headers to pass in the request. |
|
292
|
* @returns The customer address's details. |
|
293
|
* |
|
294
|
* @example |
|
295
|
* sdk.admin.customer.updateAddress("cus_123", "cus_addr_123", { |
|
296
|
* address_1: "123 Main St", |
|
297
|
* city: "Anytown", |
|
298
|
* country_code: "US", |
|
299
|
* postal_code: "12345" |
|
300
|
* }) |
|
301
|
* .then(({ customer }) => { |
|
302
|
* console.log(customer) |
|
303
|
* }) |
|
304
|
*/ |
|
305
|
async updateAddress( |
|
306
|
id: string, |
|
307
|
addressId: string, |
|
308
|
body: HttpTypes.AdminUpdateCustomerAddress, |
|
309
|
headers?: ClientHeaders |
|
310
|
) { |
|
311
|
return await this.client.fetch<HttpTypes.AdminCustomerResponse>( |
|
312
|
`/admin/customers/${id}/addresses/${addressId}`, |
|
313
|
{ |
|
314
|
method: "POST", |
|
315
|
headers, |
|
316
|
body, |
|
317
|
} |
|
318
|
) |
|
319
|
} |
|
320
|
|
|
321
|
/** |
|
322
|
* This method deletes a customer address. It sends a request to the |
|
323
|
* [Delete Customer Address](https://docs.medusajs.com/api/admin/customers/remove-an-address-from-customer) |
|
324
|
* API route. |
|
325
|
* |
|
326
|
* @param id - The customer's ID. |
|
327
|
* @param addressId - The customer address's ID. |
|
328
|
* @param headers - Headers to pass in the request. |
|
329
|
* @returns The customer address's details. |
|
330
|
* |
|
331
|
* @example |
|
332
|
* sdk.admin.customer.deleteAddress("cus_123", "cus_addr_123") |
|
333
|
* .then(({ customer }) => { |
|
334
|
* console.log(customer) |
|
335
|
* }) |
|
336
|
*/ |
|
337
|
async deleteAddress(id: string, addressId: string, headers?: ClientHeaders) { |
|
338
|
return await this.client.fetch<HttpTypes.AdminCustomerResponse>( |
|
339
|
`/admin/customers/${id}/addresses/${addressId}`, |
|
340
|
{ |
|
341
|
method: "DELETE", |
|
342
|
headers, |
|
343
|
} |
|
344
|
) |
|
345
|
} |
|
346
|
|
|
347
|
/** |
|
348
|
* This method retrieves a customer address by its ID. It sends a request to the |
|
349
|
* [Get Customer Address](https://docs.medusajs.com/api/admin/customers/list-addresses-2) |
|
350
|
* API route. |
|
351
|
* |
|
352
|
* @param id - The customer's ID. |
|
353
|
* @param addressId - The customer address's ID. |
|
354
|
* @param headers - Headers to pass in the request. |
|
355
|
* @returns The customer address's details. |
|
356
|
* |
|
357
|
* @example |
|
358
|
* sdk.admin.customer.retrieveAddress("cus_123", "cus_addr_123") |
|
359
|
* .then(({ address }) => { |
|
360
|
* console.log(address) |
|
361
|
* }) |
|
362
|
*/ |
|
363
|
async retrieveAddress( |
|
364
|
id: string, |
|
365
|
addressId: string, |
|
366
|
headers?: ClientHeaders |
|
367
|
) { |
|
368
|
return await this.client.fetch<HttpTypes.AdminCustomerAddressResponse>( |
|
369
|
`/admin/customers/${id}/addresses/${addressId}`, |
|
370
|
{ |
|
371
|
headers, |
|
372
|
} |
|
373
|
) |
|
374
|
} |
|
375
|
|
|
376
|
/** |
|
377
|
* This method retrieves a list of customer addresses. It sends a request to the |
|
378
|
* [List Customer Addresses](https://docs.medusajs.com/api/admin/customers/list-addresses) |
|
379
|
* API route. |
|
380
|
* |
|
381
|
* @param id - The customer's ID. |
|
382
|
* @param headers - Headers to pass in the request. |
|
383
|
* @returns The list of customer addresses. |
|
384
|
* |
|
385
|
* @example |
|
386
|
* sdk.admin.customer.listAddresses("cus_123") |
|
387
|
* .then(({ addresses }) => { |
|
388
|
* console.log(addresses) |
|
389
|
* }) |
|
390
|
*/ |
|
391
|
async listAddresses(id: string, headers?: ClientHeaders) { |
|
392
|
return await this.client.fetch<HttpTypes.AdminCustomerAddressListResponse>( |
|
393
|
`/admin/customers/${id}/addresses`, |
|
394
|
{ |
|
395
|
headers, |
|
396
|
} |
|
397
|
) |
|
398
|
} |
|
399
|
} |