|
1
|
import { HttpTypes } from "@medusajs/types" |
|
2
|
import { Client } from "../client.js" |
|
3
|
import { ClientHeaders } from "../types.js" |
|
4
|
|
|
5
|
export class ApiKey { |
|
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 methods retrieves a paginated list of API keys. It sends a request to the |
|
19
|
* [List API Keys](https://docs.medusajs.com/api/admin/api-keys/list-api-keys) API route. |
|
20
|
* |
|
21
|
* @param queryParams - Filters and pagination configurations. |
|
22
|
* @param headers - Headers to pass in the request. |
|
23
|
* @returns The paginated list of API keys. |
|
24
|
* |
|
25
|
* @example |
|
26
|
* To retrieve the list of API keys: |
|
27
|
* |
|
28
|
* ```ts |
|
29
|
* sdk.admin.apiKey.list() |
|
30
|
* .then(({ api_keys, count, limit, offset }) => { |
|
31
|
* console.log(api_keys) |
|
32
|
* }) |
|
33
|
* ``` |
|
34
|
* |
|
35
|
* To configure the pagination, pass the `limit` and `offset` query parameters. |
|
36
|
* |
|
37
|
* For example, to retrieve only 10 items and skip 10 items: |
|
38
|
* |
|
39
|
* ```ts |
|
40
|
* sdk.admin.apiKey.list({ |
|
41
|
* limit: 10, |
|
42
|
* offset: 10 |
|
43
|
* }) |
|
44
|
* .then(({ api_keys, count, limit, offset }) => { |
|
45
|
* console.log(api_keys) |
|
46
|
* }) |
|
47
|
* ``` |
|
48
|
* |
|
49
|
* Using the `fields` query parameter, you can specify the fields and relations to retrieve |
|
50
|
* in each API key: |
|
51
|
* |
|
52
|
* ```ts |
|
53
|
* sdk.admin.apiKey.list({ |
|
54
|
* fields: "id,*sales_channels" |
|
55
|
* }) |
|
56
|
* .then(({ api_keys, count, limit, offset }) => { |
|
57
|
* console.log(api_keys) |
|
58
|
* }) |
|
59
|
* ``` |
|
60
|
* |
|
61
|
* Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations). |
|
62
|
*/ |
|
63
|
async list( |
|
64
|
queryParams?: HttpTypes.AdminGetApiKeysParams, |
|
65
|
headers?: ClientHeaders |
|
66
|
) { |
|
67
|
return await this.client.fetch< |
|
68
|
HttpTypes.AdminApiKeyListResponse |
|
69
|
>(`/admin/api-keys`, { |
|
70
|
query: queryParams, |
|
71
|
headers, |
|
72
|
}) |
|
73
|
} |
|
74
|
|
|
75
|
/** |
|
76
|
* This method creates an API key. It sends a request to the [Create API Key](https://docs.medusajs.com/api/admin/api-keys/create-api-key) |
|
77
|
* API route. |
|
78
|
* |
|
79
|
* @param body - The API key's details. |
|
80
|
* @param query - Configure the fields to retrieve in the created API key. |
|
81
|
* @param headers - Headers to pass in the request |
|
82
|
* @returns The created API key |
|
83
|
* |
|
84
|
* @example |
|
85
|
* sdk.admin.apiKey.create({ |
|
86
|
* title: "Development", |
|
87
|
* type: "publishable" |
|
88
|
* }) |
|
89
|
* .then(({ api_key }) => { |
|
90
|
* console.log(api_key) |
|
91
|
* }) |
|
92
|
*/ |
|
93
|
async create( |
|
94
|
body: HttpTypes.AdminCreateApiKey, |
|
95
|
query?: HttpTypes.AdminGetApiKeysParams, |
|
96
|
headers?: ClientHeaders |
|
97
|
) { |
|
98
|
return await this.client.fetch<HttpTypes.AdminApiKeyResponse>( |
|
99
|
`/admin/api-keys`, |
|
100
|
{ |
|
101
|
method: "POST", |
|
102
|
headers, |
|
103
|
body, |
|
104
|
query, |
|
105
|
} |
|
106
|
) |
|
107
|
} |
|
108
|
|
|
109
|
/** |
|
110
|
* This method revokes an API key. It sends a request to the |
|
111
|
* [Revoke API Key](https://docs.medusajs.com/api/admin/api-keys/revoke-api-key) API route. |
|
112
|
* |
|
113
|
* @param id - The API key's ID. |
|
114
|
* @param headers - Headers to pass in the request. |
|
115
|
* @returns The API key's details. |
|
116
|
* |
|
117
|
* @example |
|
118
|
* sdk.admin.apiKey.revoke("apk_123") |
|
119
|
* .then(({ api_key }) => { |
|
120
|
* console.log(api_key) |
|
121
|
* }) |
|
122
|
*/ |
|
123
|
async revoke(id: string, headers?: ClientHeaders) { |
|
124
|
return await this.client.fetch<HttpTypes.AdminApiKeyResponse>( |
|
125
|
`/admin/api-keys/${id}/revoke`, |
|
126
|
{ |
|
127
|
method: "POST", |
|
128
|
headers, |
|
129
|
} |
|
130
|
) |
|
131
|
} |
|
132
|
|
|
133
|
/** |
|
134
|
* This method retrieves an API key's details. It sends a request to the |
|
135
|
* [Get API key](https://docs.medusajs.com/api/admin/api-keys/get-api-key) API route. |
|
136
|
* |
|
137
|
* @param id - The API key's ID. |
|
138
|
* @param headers - Headers to pass in the request. |
|
139
|
* @returns The API key's details. |
|
140
|
* |
|
141
|
* @example |
|
142
|
* sdk.admin.apiKey.retrieve("apk_123") |
|
143
|
* .then(({ api_key }) => { |
|
144
|
* console.log(api_key) |
|
145
|
* }) |
|
146
|
*/ |
|
147
|
async retrieve(id: string, headers?: ClientHeaders) { |
|
148
|
return await this.client.fetch<HttpTypes.AdminApiKeyResponse>( |
|
149
|
`/admin/api-keys/${id}`, |
|
150
|
{ |
|
151
|
headers, |
|
152
|
} |
|
153
|
) |
|
154
|
} |
|
155
|
|
|
156
|
/** |
|
157
|
* This method updates an API key's details. It sends a request to the |
|
158
|
* [Update API Key](https://docs.medusajs.com/api/admin/api-keys/update-an-api-key) API route. |
|
159
|
* |
|
160
|
* @param id - The API key's ID. |
|
161
|
* @param body - The data to update in the API key. |
|
162
|
* @param query - Configure the fields to retrieve in the API key. |
|
163
|
* @param headers - Headers to pass in the request. |
|
164
|
* @returns The API key's details. |
|
165
|
* |
|
166
|
* @example |
|
167
|
* sdk.admin.apiKey.update("apk_123", { |
|
168
|
* title: "Development" |
|
169
|
* }) |
|
170
|
* .then(({ api_key }) => { |
|
171
|
* console.log(api_key) |
|
172
|
* }) |
|
173
|
*/ |
|
174
|
async update( |
|
175
|
id: string, |
|
176
|
body: HttpTypes.AdminUpdateApiKey, |
|
177
|
query?: HttpTypes.AdminGetApiKeysParams, |
|
178
|
headers?: ClientHeaders |
|
179
|
) { |
|
180
|
return await this.client.fetch<HttpTypes.AdminApiKeyResponse>( |
|
181
|
`/admin/api-keys/${id}`, |
|
182
|
{ |
|
183
|
method: "POST", |
|
184
|
headers, |
|
185
|
body, |
|
186
|
query, |
|
187
|
} |
|
188
|
) |
|
189
|
} |
|
190
|
|
|
191
|
/** |
|
192
|
* This method deletes an API key by its ID. It sends a request to the |
|
193
|
* [Delete API Key](https://docs.medusajs.com/api/admin/api-keys/delete-an-api-key) API route. |
|
194
|
* |
|
195
|
* @param id - The API key's ID. |
|
196
|
* @param headers - Headers to pass in the request. |
|
197
|
* @returns The deletion's details. |
|
198
|
* |
|
199
|
* @example |
|
200
|
* sdk.admin.apiKey.delete("apk_123") |
|
201
|
* .then(({ deleted }) => { |
|
202
|
* console.log(deleted) |
|
203
|
* }) |
|
204
|
*/ |
|
205
|
async delete(id: string, headers?: ClientHeaders) { |
|
206
|
return await this.client.fetch<HttpTypes.AdminApiKeyDeleteResponse>( |
|
207
|
`/admin/api-keys/${id}`, |
|
208
|
{ |
|
209
|
method: "DELETE", |
|
210
|
headers, |
|
211
|
} |
|
212
|
) |
|
213
|
} |
|
214
|
|
|
215
|
/** |
|
216
|
* This method manages the sales channels associated with a publishable API key to either add |
|
217
|
* or remove associations. It sends a request to the [Manage Sales Channels](https://docs.medusajs.com/api/admin/api-keys/manage-sales-channels) |
|
218
|
* API route. |
|
219
|
* |
|
220
|
* @param id - The API key's ID. |
|
221
|
* @param body - The sales channels to add or remove from the API key. |
|
222
|
* @param headers - Headers to pass in the request. |
|
223
|
* @returns The API key's details. |
|
224
|
* |
|
225
|
* @example |
|
226
|
* sdk.admin.apiKey.batchSalesChannels("apk_123", { |
|
227
|
* add: ["sc_123"], |
|
228
|
* remove: ["sc_321"] |
|
229
|
* }) |
|
230
|
* .then(({ api_key }) => { |
|
231
|
* console.log(api_key) |
|
232
|
* }) |
|
233
|
*/ |
|
234
|
async batchSalesChannels( |
|
235
|
id: string, |
|
236
|
body: HttpTypes.AdminBatchLink, |
|
237
|
headers?: ClientHeaders |
|
238
|
) { |
|
239
|
return await this.client.fetch<HttpTypes.AdminApiKeyResponse>( |
|
240
|
`/admin/api-keys/${id}/sales-channels`, |
|
241
|
{ |
|
242
|
method: "POST", |
|
243
|
headers, |
|
244
|
body, |
|
245
|
} |
|
246
|
) |
|
247
|
} |
|
248
|
} |