|
| 1 | +import urlJoin from "url-join"; |
| 2 | +import * as Webflow from "../api"; |
| 3 | +import { Items } from "../api/resources/collections/resources/items/client/Client"; |
| 4 | +import * as core from "../core"; |
| 5 | +import * as environments from "../environments"; |
| 6 | +import * as errors from "../errors"; |
| 7 | +import * as serializers from "../serialization"; |
| 8 | + |
| 9 | +// Client adapts the base client to permit extra properties in |
| 10 | +// the client.Collections.Items.createItem request. |
| 11 | +export class Client extends Items { |
| 12 | + constructor(protected readonly _options: Items.Options) { |
| 13 | + super(_options); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Create Item in a Collection.</br></br> To create items across multiple locales, <a href="https://developers.webflow.com/data/reference/create-item-for-multiple-locales"> please use this endpoint.</a> </br></br> Required scope | `CMS:write` |
| 18 | + * @throws {@link Webflow.BadRequestError} |
| 19 | + * @throws {@link Webflow.UnauthorizedError} |
| 20 | + * @throws {@link Webflow.NotFoundError} |
| 21 | + * @throws {@link Webflow.TooManyRequestsError} |
| 22 | + * @throws {@link Webflow.InternalServerError} |
| 23 | + * |
| 24 | + * @example |
| 25 | + * await webflow.collections.items.createItem("collection_id", { |
| 26 | + * id: "42b720ef280c7a7a3be8cabe", |
| 27 | + * cmsLocaleId: "653ad57de882f528b32e810e", |
| 28 | + * lastPublished: "2022-11-29T16:22:43.159Z", |
| 29 | + * lastUpdated: "2022-11-17T17:19:43.282Z", |
| 30 | + * createdOn: "2022-11-17T17:11:57.148Z", |
| 31 | + * isArchived: false, |
| 32 | + * isDraft: false, |
| 33 | + * fieldData: { |
| 34 | + * name: "Pan Galactic Gargle Blaster Recipe", |
| 35 | + * slug: "pan-galactic-gargle-blaster" |
| 36 | + * } |
| 37 | + * }) |
| 38 | + */ |
| 39 | + public async createItem( |
| 40 | + collectionId: string, |
| 41 | + request: Webflow.CollectionItem, |
| 42 | + requestOptions?: Items.RequestOptions |
| 43 | + ): Promise<void> { |
| 44 | + const _response = await core.fetcher({ |
| 45 | + url: urlJoin( |
| 46 | + (await core.Supplier.get(this._options.environment)) ?? environments.WebflowEnvironment.Default, |
| 47 | + `collections/${collectionId}/items` |
| 48 | + ), |
| 49 | + method: "POST", |
| 50 | + headers: { |
| 51 | + Authorization: await this._getAuthorizationHeader(), |
| 52 | + "X-Fern-Language": "JavaScript", |
| 53 | + "X-Fern-SDK-Name": "webflow-api", |
| 54 | + "X-Fern-SDK-Version": "v2.2.1", |
| 55 | + "X-Fern-Runtime": core.RUNTIME.type, |
| 56 | + "X-Fern-Runtime-Version": core.RUNTIME.version, |
| 57 | + }, |
| 58 | + contentType: "application/json", |
| 59 | + body: await serializers.CollectionItem.jsonOrThrow( |
| 60 | + request, { |
| 61 | + unrecognizedObjectKeys: "passthrough", |
| 62 | + allowUnrecognizedUnionMembers: true, |
| 63 | + allowUnrecognizedEnumValues: true, |
| 64 | + skipValidation: true, |
| 65 | + }, |
| 66 | + ), |
| 67 | + timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, |
| 68 | + maxRetries: requestOptions?.maxRetries, |
| 69 | + }); |
| 70 | + if (_response.ok) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (_response.error.reason === "status-code") { |
| 75 | + switch (_response.error.statusCode) { |
| 76 | + case 400: |
| 77 | + throw new Webflow.BadRequestError(_response.error.body); |
| 78 | + case 401: |
| 79 | + throw new Webflow.UnauthorizedError(_response.error.body); |
| 80 | + case 404: |
| 81 | + throw new Webflow.NotFoundError(_response.error.body); |
| 82 | + case 429: |
| 83 | + throw new Webflow.TooManyRequestsError(_response.error.body); |
| 84 | + case 500: |
| 85 | + throw new Webflow.InternalServerError(_response.error.body); |
| 86 | + default: |
| 87 | + throw new errors.WebflowError({ |
| 88 | + statusCode: _response.error.statusCode, |
| 89 | + body: _response.error.body, |
| 90 | + }); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + switch (_response.error.reason) { |
| 95 | + case "non-json": |
| 96 | + throw new errors.WebflowError({ |
| 97 | + statusCode: _response.error.statusCode, |
| 98 | + body: _response.error.rawBody, |
| 99 | + }); |
| 100 | + case "timeout": |
| 101 | + throw new errors.WebflowTimeoutError(); |
| 102 | + case "unknown": |
| 103 | + throw new errors.WebflowError({ |
| 104 | + message: _response.error.errorMessage, |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments