-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathClient.ts
More file actions
319 lines (306 loc) · 14.4 KB
/
Client.ts
File metadata and controls
319 lines (306 loc) · 14.4 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// This file was auto-generated by Fern from our API Definition.
import type { BaseClientOptions, BaseRequestOptions } from "../../../../BaseClient";
import { type NormalizedClientOptionsWithAuth, normalizeClientOptionsWithAuth } from "../../../../BaseClient";
import * as core from "../../../../core";
import { mergeHeaders } from "../../../../core/headers";
import * as environments from "../../../../environments";
import { handleNonStatusCodeError } from "../../../../errors/handleNonStatusCodeError";
import * as errors from "../../../../errors/index";
import * as serializers from "../../../../serialization/index";
import * as Webflow from "../../../index";
export declare namespace InventoryClient {
export type Options = BaseClientOptions;
export interface RequestOptions extends BaseRequestOptions {}
}
/**
* Inventory is the stock of e-commerce items in your Webflow site.
*/
export class InventoryClient {
protected readonly _options: NormalizedClientOptionsWithAuth<InventoryClient.Options>;
constructor(options: InventoryClient.Options) {
this._options = normalizeClientOptionsWithAuth(options);
}
/**
* List the current inventory levels for a particular SKU item.
*
* Required scope | `ecommerce:read`
*
* @param {string} sku_collection_id - Unique identifier for a SKU collection. Use the List Collections API to find this ID.
* @param {string} sku_id - Unique identifier for a SKU
* @param {InventoryClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link Webflow.BadRequestError}
* @throws {@link Webflow.UnauthorizedError}
* @throws {@link Webflow.ForbiddenError}
* @throws {@link Webflow.NotFoundError}
* @throws {@link Webflow.ConflictError}
* @throws {@link Webflow.TooManyRequestsError}
* @throws {@link Webflow.InternalServerError}
*
* @example
* await client.inventory.list("6377a7c4b7a79608c34a46f7", "5e8518516e147040726cc415")
*/
public list(
sku_collection_id: string,
sku_id: string,
requestOptions?: InventoryClient.RequestOptions,
): core.HttpResponsePromise<Webflow.InventoryItem> {
return core.HttpResponsePromise.fromPromise(this.__list(sku_collection_id, sku_id, requestOptions));
}
private async __list(
sku_collection_id: string,
sku_id: string,
requestOptions?: InventoryClient.RequestOptions,
): Promise<core.WithRawResponse<Webflow.InventoryItem>> {
const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
_authRequest.headers,
this._options?.headers,
requestOptions?.headers,
);
const _response = await core.fetcher({
url: core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
((await core.Supplier.get(this._options.environment)) ?? environments.WebflowEnvironment.DataApi)
.base,
`collections/${core.url.encodePathParam(sku_collection_id)}/items/${core.url.encodePathParam(sku_id)}/inventory`,
),
method: "GET",
headers: _headers,
queryParameters: requestOptions?.queryParams,
timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
abortSignal: requestOptions?.abortSignal,
fetchFn: this._options?.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return {
data: serializers.InventoryItem.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
rawResponse: _response.rawResponse,
};
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new Webflow.BadRequestError(_response.error.body, _response.rawResponse);
case 401:
throw new Webflow.UnauthorizedError(
serializers.Error_.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
_response.rawResponse,
);
case 403:
throw new Webflow.ForbiddenError(_response.error.body, _response.rawResponse);
case 404:
throw new Webflow.NotFoundError(
serializers.Error_.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
_response.rawResponse,
);
case 409:
throw new Webflow.ConflictError(_response.error.body, _response.rawResponse);
case 429:
throw new Webflow.TooManyRequestsError(
serializers.Error_.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
_response.rawResponse,
);
case 500:
throw new Webflow.InternalServerError(
serializers.Error_.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
_response.rawResponse,
);
default:
throw new errors.WebflowError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
}
return handleNonStatusCodeError(
_response.error,
_response.rawResponse,
"GET",
"/collections/{sku_collection_id}/items/{sku_id}/inventory",
);
}
/**
* Updates the current inventory levels for a particular SKU item.
*
* Updates may be given in one or two methods, absolutely or incrementally.
* - Absolute updates are done by setting `quantity` directly.
* - Incremental updates are by specifying the inventory delta in `updateQuantity` which is then added to the `quantity` stored on the server.
*
* Required scope | `ecommerce:write`
*
* @param {string} sku_collection_id - Unique identifier for a SKU collection. Use the List Collections API to find this ID.
* @param {string} sku_id - Unique identifier for a SKU
* @param {Webflow.InventoryUpdateRequest} request
* @param {InventoryClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link Webflow.BadRequestError}
* @throws {@link Webflow.UnauthorizedError}
* @throws {@link Webflow.ForbiddenError}
* @throws {@link Webflow.NotFoundError}
* @throws {@link Webflow.ConflictError}
* @throws {@link Webflow.TooManyRequestsError}
* @throws {@link Webflow.InternalServerError}
*
* @example
* await client.inventory.update("6377a7c4b7a79608c34a46f7", "5e8518516e147040726cc415", {
* inventoryType: "infinite"
* })
*/
public update(
sku_collection_id: string,
sku_id: string,
request: Webflow.InventoryUpdateRequest,
requestOptions?: InventoryClient.RequestOptions,
): core.HttpResponsePromise<Webflow.InventoryItem> {
return core.HttpResponsePromise.fromPromise(this.__update(sku_collection_id, sku_id, request, requestOptions));
}
private async __update(
sku_collection_id: string,
sku_id: string,
request: Webflow.InventoryUpdateRequest,
requestOptions?: InventoryClient.RequestOptions,
): Promise<core.WithRawResponse<Webflow.InventoryItem>> {
const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
_authRequest.headers,
this._options?.headers,
requestOptions?.headers,
);
const _response = await core.fetcher({
url: core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
((await core.Supplier.get(this._options.environment)) ?? environments.WebflowEnvironment.DataApi)
.base,
`collections/${core.url.encodePathParam(sku_collection_id)}/items/${core.url.encodePathParam(sku_id)}/inventory`,
),
method: "PATCH",
headers: _headers,
contentType: "application/json",
queryParameters: requestOptions?.queryParams,
requestType: "json",
body: serializers.InventoryUpdateRequest.jsonOrThrow(request, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
omitUndefined: true,
}),
timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
abortSignal: requestOptions?.abortSignal,
fetchFn: this._options?.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return {
data: serializers.InventoryItem.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
rawResponse: _response.rawResponse,
};
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new Webflow.BadRequestError(_response.error.body, _response.rawResponse);
case 401:
throw new Webflow.UnauthorizedError(
serializers.Error_.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
_response.rawResponse,
);
case 403:
throw new Webflow.ForbiddenError(_response.error.body, _response.rawResponse);
case 404:
throw new Webflow.NotFoundError(
serializers.Error_.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
_response.rawResponse,
);
case 409:
throw new Webflow.ConflictError(_response.error.body, _response.rawResponse);
case 429:
throw new Webflow.TooManyRequestsError(
serializers.Error_.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
_response.rawResponse,
);
case 500:
throw new Webflow.InternalServerError(
serializers.Error_.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
_response.rawResponse,
);
default:
throw new errors.WebflowError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
}
return handleNonStatusCodeError(
_response.error,
_response.rawResponse,
"PATCH",
"/collections/{sku_collection_id}/items/{sku_id}/inventory",
);
}
}