|
1
|
import { HttpTypes } from "@medusajs/types" |
|
2
|
import { Client } from "../client.js" |
|
3
|
import { ClientHeaders } from "../types.js" |
|
4
|
|
|
5
|
export class SalesChannel { |
|
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 new sales channel. It sends a request to the |
|
19
|
* [Create Sales Channel](https://docs.medusajs.com/api/admin/sales-channels/create-sales-channel) |
|
20
|
* API route. |
|
21
|
* |
|
22
|
* @param body - The details of the sales channel to create. |
|
23
|
* @param query - Configure the fields and relations to retrieve in the sales channel. |
|
24
|
* @param headers - Headers to pass in the request. |
|
25
|
* @returns The sales channel's details. |
|
26
|
* |
|
27
|
* @example |
|
28
|
* sdk.admin.salesChannel.create({ |
|
29
|
* name: "Storefront", |
|
30
|
* }) |
|
31
|
* .then(({ salesChannel }) => { |
|
32
|
* console.log(salesChannel) |
|
33
|
* }) |
|
34
|
*/ |
|
35
|
async create( |
|
36
|
body: HttpTypes.AdminCreateSalesChannel, |
|
37
|
query?: HttpTypes.SelectParams, |
|
38
|
headers?: ClientHeaders |
|
39
|
) { |
|
40
|
return await this.client.fetch<HttpTypes.AdminSalesChannelResponse>( |
|
41
|
`/admin/sales-channels`, |
|
42
|
{ |
|
43
|
method: "POST", |
|
44
|
headers, |
|
45
|
body, |
|
46
|
query, |
|
47
|
} |
|
48
|
) |
|
49
|
} |
|
50
|
|
|
51
|
/** |
|
52
|
* This method updates a sales channel. It sends a request to the |
|
53
|
* [Update Sales Channel](https://docs.medusajs.com/api/admin/sales-channels/update-a-sales-channel) |
|
54
|
* API route. |
|
55
|
* |
|
56
|
* @param id - The ID of the sales channel to update. |
|
57
|
* @param body - The details of the sales channel to update. |
|
58
|
* @param query - Configure the fields and relations to retrieve in the sales channel. |
|
59
|
* @param headers - Headers to pass in the request. |
|
60
|
* @returns The sales channel's details. |
|
61
|
* |
|
62
|
* @example |
|
63
|
* sdk.admin.salesChannel.update( |
|
64
|
* "sc_123", |
|
65
|
* { |
|
66
|
* name: "Storefront", |
|
67
|
* } |
|
68
|
* ) |
|
69
|
* .then(({ salesChannel }) => { |
|
70
|
* console.log(salesChannel) |
|
71
|
* }) |
|
72
|
*/ |
|
73
|
async update( |
|
74
|
id: string, |
|
75
|
body: HttpTypes.AdminUpdateSalesChannel, |
|
76
|
query?: HttpTypes.SelectParams, |
|
77
|
headers?: ClientHeaders |
|
78
|
) { |
|
79
|
return await this.client.fetch<HttpTypes.AdminSalesChannelResponse>( |
|
80
|
`/admin/sales-channels/${id}`, |
|
81
|
{ |
|
82
|
method: "POST", |
|
83
|
headers, |
|
84
|
body, |
|
85
|
query, |
|
86
|
} |
|
87
|
) |
|
88
|
} |
|
89
|
|
|
90
|
/** |
|
91
|
* This method deletes a sales channel. It sends a request to the |
|
92
|
* [Delete Sales Channel](https://docs.medusajs.com/api/admin/sales-channels/delete-a-sales-channel) |
|
93
|
* API route. |
|
94
|
* |
|
95
|
* @param id - The ID of the sales channel to delete. |
|
96
|
* @param headers - Headers to pass in the request. |
|
97
|
* @returns The deletion's details. |
|
98
|
* |
|
99
|
* @example |
|
100
|
* sdk.admin.salesChannel.delete("sc_123") |
|
101
|
* .then(({ deleted }) => { |
|
102
|
* console.log(deleted) |
|
103
|
* }) |
|
104
|
*/ |
|
105
|
async delete(id: string, headers?: ClientHeaders) { |
|
106
|
return await this.client.fetch<HttpTypes.AdminSalesChannelDeleteResponse>( |
|
107
|
`/admin/sales-channels/${id}`, |
|
108
|
{ |
|
109
|
method: "DELETE", |
|
110
|
headers, |
|
111
|
} |
|
112
|
) |
|
113
|
} |
|
114
|
|
|
115
|
/** |
|
116
|
* This method retrieves a sales channel. It sends a request to the |
|
117
|
* [Retrieve Sales Channel](https://docs.medusajs.com/api/admin/sales-channels/get-a-sales-channel) |
|
118
|
* API route. |
|
119
|
* |
|
120
|
* @param id - The ID of the sales channel to retrieve. |
|
121
|
* @param query - Configure the fields and relations to retrieve in the sales channel. |
|
122
|
* @param headers - Headers to pass in the request. |
|
123
|
* @returns The sales channel's details. |
|
124
|
* |
|
125
|
* @example |
|
126
|
* To retrieve a sales channel by its ID: |
|
127
|
* |
|
128
|
* ```ts |
|
129
|
* sdk.admin.salesChannel.retrieve("sc_123") |
|
130
|
* .then(({ sales_channel }) => { |
|
131
|
* console.log(sales_channel) |
|
132
|
* }) |
|
133
|
* ``` |
|
134
|
* |
|
135
|
* To specify the fields and relations to retrieve: |
|
136
|
* |
|
137
|
* ```ts |
|
138
|
* sdk.admin.salesChannel.retrieve("sc_123", { |
|
139
|
* fields: "id,*products" |
|
140
|
* }) |
|
141
|
* .then(({ sales_channel }) => { |
|
142
|
* console.log(sales_channel) |
|
143
|
* }) |
|
144
|
* ``` |
|
145
|
* |
|
146
|
* Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations). |
|
147
|
*/ |
|
148
|
async retrieve( |
|
149
|
id: string, |
|
150
|
query?: HttpTypes.SelectParams, |
|
151
|
headers?: ClientHeaders |
|
152
|
) { |
|
153
|
return await this.client.fetch<HttpTypes.AdminSalesChannelResponse>( |
|
154
|
`/admin/sales-channels/${id}`, |
|
155
|
{ |
|
156
|
method: "GET", |
|
157
|
headers, |
|
158
|
query, |
|
159
|
} |
|
160
|
) |
|
161
|
} |
|
162
|
|
|
163
|
/** |
|
164
|
* This method retrieves a list of sales channels. It sends a request to the |
|
165
|
* [List Sales Channels](https://docs.medusajs.com/api/admin/sales-channels/list-sales-channels) |
|
166
|
* API route. |
|
167
|
* |
|
168
|
* @param query - Filters and pagination configurations. |
|
169
|
* @param headers - Headers to pass in the request. |
|
170
|
* @returns The list of sales channels. |
|
171
|
* |
|
172
|
* @example |
|
173
|
* To retrieve the list of sales channels: |
|
174
|
* |
|
175
|
* ```ts |
|
176
|
* sdk.admin.salesChannel.list() |
|
177
|
* .then(({ sales_channels, count, limit, offset }) => { |
|
178
|
* console.log(sales_channels) |
|
179
|
* }) |
|
180
|
* ``` |
|
181
|
* |
|
182
|
* To configure the pagination, pass the `limit` and `offset` query parameters. |
|
183
|
* |
|
184
|
* For example, to retrieve only 10 items and skip 10 items: |
|
185
|
* |
|
186
|
* ```ts |
|
187
|
* sdk.admin.salesChannel.list({ |
|
188
|
* limit: 10, |
|
189
|
* offset: 10 |
|
190
|
* }) |
|
191
|
* .then(({ sales_channels, count, limit, offset }) => { |
|
192
|
* console.log(sales_channels) |
|
193
|
* }) |
|
194
|
* ``` |
|
195
|
* |
|
196
|
* Using the `fields` query parameter, you can specify the fields and relations to retrieve |
|
197
|
* in each sales channel: |
|
198
|
* |
|
199
|
* ```ts |
|
200
|
* sdk.admin.salesChannel.list({ |
|
201
|
* fields: "id,*products" |
|
202
|
* }) |
|
203
|
* .then(({ sales_channels, count, limit, offset }) => { |
|
204
|
* console.log(sales_channels) |
|
205
|
* }) |
|
206
|
* ``` |
|
207
|
* |
|
208
|
* Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations). |
|
209
|
*/ |
|
210
|
async list( |
|
211
|
query?: HttpTypes.AdminSalesChannelListParams, |
|
212
|
headers?: ClientHeaders |
|
213
|
) { |
|
214
|
return await this.client.fetch<HttpTypes.AdminSalesChannelListResponse>( |
|
215
|
`/admin/sales-channels`, |
|
216
|
{ |
|
217
|
method: "GET", |
|
218
|
headers, |
|
219
|
query, |
|
220
|
} |
|
221
|
) |
|
222
|
} |
|
223
|
|
|
224
|
/** |
|
225
|
* This method manages the products in a sales channel to add or remove them. It sends a request to the |
|
226
|
* [Manage Products in Sales Channel](https://docs.medusajs.com/api/admin/sales-channels/manage-products) |
|
227
|
* API route. |
|
228
|
* |
|
229
|
* @param id - The ID of the sales channel to manage the products for. |
|
230
|
* @param body - The details of the products to add or remove from the sales channel. |
|
231
|
* @param headers - Headers to pass in the request. |
|
232
|
* @returns The sales channel's details. |
|
233
|
* |
|
234
|
* @example |
|
235
|
* sdk.admin.salesChannel.updateProducts("sc_123", { |
|
236
|
* add: ["prod_123", "prod_456"], |
|
237
|
* remove: ["prod_789"] |
|
238
|
* }) |
|
239
|
* .then(({ sales_channel }) => { |
|
240
|
* console.log(sales_channel) |
|
241
|
* }) |
|
242
|
* |
|
243
|
* @deprecated Use {@link batchProducts} instead |
|
244
|
*/ |
|
245
|
async updateProducts( |
|
246
|
id: string, |
|
247
|
body: HttpTypes.AdminUpdateSalesChannelProducts, |
|
248
|
headers?: ClientHeaders |
|
249
|
) { |
|
250
|
return await this.client.fetch<HttpTypes.AdminSalesChannelResponse>( |
|
251
|
`/admin/sales-channels/${id}/products`, |
|
252
|
{ |
|
253
|
method: "POST", |
|
254
|
headers, |
|
255
|
body, |
|
256
|
} |
|
257
|
) |
|
258
|
} |
|
259
|
|
|
260
|
/** |
|
261
|
* This method manages the products in a sales channel to add or remove them. It sends a request to the |
|
262
|
* [Manage Products in Sales Channel](https://docs.medusajs.com/api/admin/sales-channels/manage-products) |
|
263
|
* API route. |
|
264
|
* |
|
265
|
* @param id - The ID of the sales channel to manage the products for. |
|
266
|
* @param body - The details of the products to add or remove from the sales channel. |
|
267
|
* @param headers - Headers to pass in the request. |
|
268
|
* @returns The sales channel's details. |
|
269
|
* |
|
270
|
* @example |
|
271
|
* sdk.admin.salesChannel.batchProducts("sc_123", { |
|
272
|
* add: ["prod_123", "prod_456"], |
|
273
|
* remove: ["prod_789"] |
|
274
|
* }) |
|
275
|
* .then(({ sales_channel }) => { |
|
276
|
* console.log(sales_channel) |
|
277
|
* }) |
|
278
|
*/ |
|
279
|
async batchProducts( |
|
280
|
id: string, |
|
281
|
body: HttpTypes.AdminUpdateSalesChannelProducts, |
|
282
|
headers?: ClientHeaders |
|
283
|
) { |
|
284
|
return await this.client.fetch<HttpTypes.AdminSalesChannelResponse>( |
|
285
|
`/admin/sales-channels/${id}/products`, |
|
286
|
{ |
|
287
|
method: "POST", |
|
288
|
headers, |
|
289
|
body, |
|
290
|
} |
|
291
|
) |
|
292
|
} |
|
293
|
} |