|
1
|
import { HttpTypes } from "@medusajs/types" |
|
2
|
import { Client } from "../client.js" |
|
3
|
import { ClientHeaders } from "../types.js" |
|
4
|
|
|
5
|
export class User { |
|
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 updates a user. It sends a request to the |
|
19
|
* [Update User](https://docs.medusajs.com/api/admin/users/update-a-user) |
|
20
|
* API route. |
|
21
|
* |
|
22
|
* @param id - The ID of the user to update. |
|
23
|
* @param body - The details of the user to update. |
|
24
|
* @param query - Configure the fields and relations to retrieve in the tax region. |
|
25
|
* @param headers - Headers to pass in the request. |
|
26
|
* @returns The user's details. |
|
27
|
* |
|
28
|
* @example |
|
29
|
* sdk.admin.user.update("user_123", { |
|
30
|
* first_name: "John", |
|
31
|
* last_name: "Doe", |
|
32
|
* }) |
|
33
|
* .then(({ user }) => { |
|
34
|
* console.log(user) |
|
35
|
* }) |
|
36
|
*/ |
|
37
|
async update( |
|
38
|
id: string, |
|
39
|
body: HttpTypes.AdminUpdateUser, |
|
40
|
query?: HttpTypes.AdminUserParams, |
|
41
|
headers?: ClientHeaders |
|
42
|
) { |
|
43
|
return this.client.fetch<HttpTypes.AdminUserResponse>( |
|
44
|
`/admin/users/${id}`, |
|
45
|
{ |
|
46
|
method: "POST", |
|
47
|
headers, |
|
48
|
body, |
|
49
|
query, |
|
50
|
} |
|
51
|
) |
|
52
|
} |
|
53
|
|
|
54
|
/** |
|
55
|
* This method retrieves a list of users. It sends a request to the |
|
56
|
* [List Users](https://docs.medusajs.com/api/admin/users/list-users) |
|
57
|
* API route. |
|
58
|
* |
|
59
|
* @param queryParams - Filters and pagination configurations. |
|
60
|
* @param headers - Headers to pass in the request. |
|
61
|
* @returns The list of users. |
|
62
|
* |
|
63
|
* @example |
|
64
|
* To retrieve the list of users: |
|
65
|
* |
|
66
|
* ```ts |
|
67
|
* sdk.admin.user.list() |
|
68
|
* .then(({ users, count, limit, offset }) => { |
|
69
|
* console.log(users) |
|
70
|
* }) |
|
71
|
* ``` |
|
72
|
* |
|
73
|
* To configure the pagination, pass the `limit` and `offset` query parameters. |
|
74
|
* |
|
75
|
* For example, to retrieve only 10 items and skip 10 items: |
|
76
|
* |
|
77
|
* ```ts |
|
78
|
* sdk.admin.user.list({ |
|
79
|
* limit: 10, |
|
80
|
* offset: 10 |
|
81
|
* }) |
|
82
|
* .then(({ users, count, limit, offset }) => { |
|
83
|
* console.log(users) |
|
84
|
* }) |
|
85
|
* ``` |
|
86
|
* |
|
87
|
* Using the `fields` query parameter, you can specify the fields and relations to retrieve |
|
88
|
* in each user: |
|
89
|
* |
|
90
|
* ```ts |
|
91
|
* sdk.admin.user.list({ |
|
92
|
* fields: "id,email" |
|
93
|
* }) |
|
94
|
* .then(({ users, count, limit, offset }) => { |
|
95
|
* console.log(users) |
|
96
|
* }) |
|
97
|
* ``` |
|
98
|
* |
|
99
|
* Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations). |
|
100
|
*/ |
|
101
|
async list( |
|
102
|
queryParams?: HttpTypes.AdminUserListParams, |
|
103
|
headers?: ClientHeaders |
|
104
|
) { |
|
105
|
return this.client.fetch<HttpTypes.AdminUserListResponse>(`/admin/users`, { |
|
106
|
headers, |
|
107
|
query: queryParams, |
|
108
|
}) |
|
109
|
} |
|
110
|
|
|
111
|
/** |
|
112
|
* This method retrieves a user. It sends a request to the |
|
113
|
* [Get User](https://docs.medusajs.com/api/admin/users/get-a-user) |
|
114
|
* API route. |
|
115
|
* |
|
116
|
* @param id - The ID of the user to retrieve. |
|
117
|
* @param query - Configure the fields and relations to retrieve in the user. |
|
118
|
* @param headers - Headers to pass in the request. |
|
119
|
* @returns The user's details. |
|
120
|
* |
|
121
|
* @example |
|
122
|
* To retrieve a user by its ID: |
|
123
|
* |
|
124
|
* ```ts |
|
125
|
* sdk.admin.user.retrieve("user_123") |
|
126
|
* .then(({ user }) => { |
|
127
|
* console.log(user) |
|
128
|
* }) |
|
129
|
* ``` |
|
130
|
* |
|
131
|
* To specify the fields and relations to retrieve: |
|
132
|
* |
|
133
|
* ```ts |
|
134
|
* sdk.admin.user.retrieve("user_123", { |
|
135
|
* fields: "id,email" |
|
136
|
* }) |
|
137
|
* .then(({ user }) => { |
|
138
|
* console.log(user) |
|
139
|
* }) |
|
140
|
* ``` |
|
141
|
* |
|
142
|
* Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations). |
|
143
|
*/ |
|
144
|
async retrieve( |
|
145
|
id: string, |
|
146
|
query?: HttpTypes.AdminUserParams, |
|
147
|
headers?: ClientHeaders |
|
148
|
) { |
|
149
|
return this.client.fetch<HttpTypes.AdminUserResponse>( |
|
150
|
`/admin/users/${id}`, |
|
151
|
{ |
|
152
|
query, |
|
153
|
headers, |
|
154
|
} |
|
155
|
) |
|
156
|
} |
|
157
|
|
|
158
|
/** |
|
159
|
* This method retrieves the auth providers a user can authenticate with. It sends a |
|
160
|
* request to the List User Auth Providers API route. |
|
161
|
* |
|
162
|
* @param id - The user's ID. |
|
163
|
* @param headers - Headers to pass in the request. |
|
164
|
* @returns The IDs of the user's auth providers. |
|
165
|
* |
|
166
|
* @example |
|
167
|
* sdk.admin.user.listAuthProviders("user_123") |
|
168
|
* .then(({ providers }) => { |
|
169
|
* console.log(providers) |
|
170
|
* }) |
|
171
|
* |
|
172
|
* @since 2.20.0 |
|
173
|
*/ |
|
174
|
async listAuthProviders(id: string, headers?: ClientHeaders) { |
|
175
|
return this.client.fetch<HttpTypes.AdminUserAuthProvidersResponse>( |
|
176
|
`/admin/users/${id}/auth-providers`, |
|
177
|
{ |
|
178
|
headers, |
|
179
|
} |
|
180
|
) |
|
181
|
} |
|
182
|
|
|
183
|
/** |
|
184
|
* This method generates a reset password token for a user. It sends a request to the |
|
185
|
* Generate Reset Password Token for a User API route. |
|
186
|
* |
|
187
|
* Useful for constructing the reset password link without sending an email. |
|
188
|
* |
|
189
|
* The token is short-lived, can only be used once, and generating it invalidates |
|
190
|
* any reset password token previously issued for the user. |
|
191
|
* |
|
192
|
* @param id - The ID of the user to generate the token for. |
|
193
|
* @param headers - Headers to pass in the request. |
|
194
|
* @returns The generated reset password token. |
|
195
|
* |
|
196
|
* @example |
|
197
|
* sdk.admin.user.generateResetPasswordToken("user_123") |
|
198
|
* .then(({ token }) => { |
|
199
|
* console.log(token) |
|
200
|
* }) |
|
201
|
* |
|
202
|
* @since 2.20.0 |
|
203
|
*/ |
|
204
|
async generateResetPasswordToken(id: string, headers?: ClientHeaders) { |
|
205
|
return this.client.fetch<HttpTypes.AdminUserResetPasswordTokenResponse>( |
|
206
|
`/admin/users/${id}/reset-password`, |
|
207
|
{ |
|
208
|
method: "POST", |
|
209
|
headers, |
|
210
|
} |
|
211
|
) |
|
212
|
} |
|
213
|
|
|
214
|
/** |
|
215
|
* This method retrieves a paginated list of roles assigned to a user. It sends a request to the |
|
216
|
* List User Roles API route. |
|
217
|
* |
|
218
|
* @param id - The user's ID. |
|
219
|
* @param queryParams - Filters and pagination configurations. |
|
220
|
* @param headers - Headers to pass in the request. |
|
221
|
* @returns The list of roles. |
|
222
|
* |
|
223
|
* @example |
|
224
|
* sdk.admin.user.listRoles("user_123") |
|
225
|
* .then(({ roles, count, limit, offset }) => { |
|
226
|
* console.log(roles) |
|
227
|
* }) |
|
228
|
*/ |
|
229
|
async listRoles( |
|
230
|
id: string, |
|
231
|
queryParams?: HttpTypes.AdminRbacRoleUserListParams, |
|
232
|
headers?: ClientHeaders |
|
233
|
) { |
|
234
|
return this.client.fetch<HttpTypes.AdminGetUserRolesParams>( |
|
235
|
`/admin/users/${id}/roles`, |
|
236
|
{ |
|
237
|
query: queryParams, |
|
238
|
headers, |
|
239
|
} |
|
240
|
) |
|
241
|
} |
|
242
|
|
|
243
|
/** |
|
244
|
* This method assigns roles to a user. It sends a request to the |
|
245
|
* Assign User Roles API route. |
|
246
|
* |
|
247
|
* @param id - The user's ID. |
|
248
|
* @param body - The roles to assign. |
|
249
|
* @param headers - Headers to pass in the request. |
|
250
|
* @returns The user's roles. |
|
251
|
* |
|
252
|
* @example |
|
253
|
* sdk.admin.user.addRoles("user_123", { |
|
254
|
* roles: ["role_123"] |
|
255
|
* }) |
|
256
|
* .then(({ roles }) => { |
|
257
|
* console.log(roles) |
|
258
|
* }) |
|
259
|
*/ |
|
260
|
async addRoles( |
|
261
|
id: string, |
|
262
|
body: HttpTypes.AdminAssignUserRoles, |
|
263
|
headers?: ClientHeaders |
|
264
|
) { |
|
265
|
return this.client.fetch<HttpTypes.AdminUserRolesResponse>( |
|
266
|
`/admin/users/${id}/roles`, |
|
267
|
{ |
|
268
|
method: "POST", |
|
269
|
headers, |
|
270
|
body, |
|
271
|
} |
|
272
|
) |
|
273
|
} |
|
274
|
|
|
275
|
/** |
|
276
|
* This method removes a role from a user. It sends a request to the |
|
277
|
* Remove User Role API route. |
|
278
|
* |
|
279
|
* @param id - The user's ID. |
|
280
|
* @param roleId - The role's ID. |
|
281
|
* @param headers - Headers to pass in the request. |
|
282
|
* @returns The removal's details. |
|
283
|
* |
|
284
|
* @example |
|
285
|
* sdk.admin.user.removeRole("user_123", "role_123") |
|
286
|
* .then(({ deleted }) => { |
|
287
|
* console.log(deleted) |
|
288
|
* }) |
|
289
|
*/ |
|
290
|
async removeRole(id: string, roleId: string, headers?: ClientHeaders) { |
|
291
|
return this.client.fetch<HttpTypes.AdminUserRoleDeleteResponse>( |
|
292
|
`/admin/users/${id}/roles/${roleId}`, |
|
293
|
{ |
|
294
|
method: "DELETE", |
|
295
|
headers, |
|
296
|
} |
|
297
|
) |
|
298
|
} |
|
299
|
|
|
300
|
/** |
|
301
|
* This method removes roles from a user. It sends a request to the |
|
302
|
* Remove User Roles API route. |
|
303
|
* |
|
304
|
* @param id - The user's ID. |
|
305
|
* @param body - The roles to remove. |
|
306
|
* @param headers - Headers to pass in the request. |
|
307
|
* @returns The removal's details. |
|
308
|
* |
|
309
|
* @example |
|
310
|
* sdk.admin.user.removeRoles("user_123", { |
|
311
|
* roles: ["role_123"] |
|
312
|
* }) |
|
313
|
* .then(({ deleted }) => { |
|
314
|
* console.log(deleted) |
|
315
|
* }) |
|
316
|
*/ |
|
317
|
async removeRoles( |
|
318
|
id: string, |
|
319
|
body: HttpTypes.AdminRemoveUserRoles, |
|
320
|
headers?: ClientHeaders |
|
321
|
) { |
|
322
|
return this.client.fetch<HttpTypes.AdminUserRolesDeleteResponse>( |
|
323
|
`/admin/users/${id}/roles`, |
|
324
|
{ |
|
325
|
method: "DELETE", |
|
326
|
headers, |
|
327
|
body, |
|
328
|
} |
|
329
|
) |
|
330
|
} |
|
331
|
|
|
332
|
/** |
|
333
|
* This method deletes a user. It sends a request to the |
|
334
|
* [Delete User](https://docs.medusajs.com/api/admin/users/delete-a-user) |
|
335
|
* API route. |
|
336
|
* |
|
337
|
* @param id - The ID of the user to delete. |
|
338
|
* @param headers - Headers to pass in the request. |
|
339
|
* @returns The deletion's details. |
|
340
|
* |
|
341
|
* @example |
|
342
|
* sdk.admin.user.delete("user_123") |
|
343
|
* .then(({ deleted }) => { |
|
344
|
* console.log(deleted) |
|
345
|
* }) |
|
346
|
*/ |
|
347
|
async delete(id: string, headers?: ClientHeaders) { |
|
348
|
return this.client.fetch<HttpTypes.AdminUserDeleteResponse>( |
|
349
|
`/admin/users/${id}`, |
|
350
|
{ |
|
351
|
method: "DELETE", |
|
352
|
headers, |
|
353
|
} |
|
354
|
) |
|
355
|
} |
|
356
|
|
|
357
|
/** |
|
358
|
* This method retrieves the currently authenticated user. It sends a request to the |
|
359
|
* [Get Logged-In User](https://docs.medusajs.com/api/admin/users/get-logged-in-user) |
|
360
|
* API route. |
|
361
|
* |
|
362
|
* @param query - Configure the fields and relations to retrieve in the user. |
|
363
|
* @param headers - Headers to pass in the request. |
|
364
|
* @returns The user's details. |
|
365
|
* |
|
366
|
* @example |
|
367
|
* To retrieve the currently authenticated user: |
|
368
|
* |
|
369
|
* ```ts |
|
370
|
* sdk.admin.user.me() |
|
371
|
* .then(({ user }) => { |
|
372
|
* console.log(user) |
|
373
|
* }) |
|
374
|
* ``` |
|
375
|
* |
|
376
|
* To specify the fields and relations to retrieve: |
|
377
|
* |
|
378
|
* ```ts |
|
379
|
* sdk.admin.user.me({ |
|
380
|
* fields: "id,email" |
|
381
|
* }) |
|
382
|
* .then(({ user }) => { |
|
383
|
* console.log(user) |
|
384
|
* }) |
|
385
|
* ``` |
|
386
|
* |
|
387
|
* Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations). |
|
388
|
*/ |
|
389
|
async me(query?: HttpTypes.AdminUserParams, headers?: ClientHeaders) { |
|
390
|
return this.client.fetch<HttpTypes.AdminUserResponse>(`/admin/users/me`, { |
|
391
|
query, |
|
392
|
headers, |
|
393
|
}) |
|
394
|
} |
|
395
|
} |