|
| 1 | +import urlJoin from "url-join"; |
| 2 | +import { Pages } from "../api/resources/pages/client/Client"; |
| 3 | +import * as core from "../core"; |
| 4 | +import * as Webflow from "../api"; |
| 5 | +import * as environments from "../environments"; |
| 6 | +import * as errors from "../errors"; |
| 7 | +import * as serializers from "../serialization"; |
| 8 | +import { mergeHeaders, mergeOnlyDefinedHeaders } from "../core/headers.js"; |
| 9 | +import * as SchemaOverrides from "./schemas"; |
| 10 | + |
| 11 | +declare module "../api/resources/pages/client/Client" { |
| 12 | + export namespace Pages {} |
| 13 | +} |
| 14 | + |
| 15 | +// Client adapts the base client to override extra properties in |
| 16 | +// the client.pages.updatePageSettings request. |
| 17 | +// Overriding to preserve ability to use `body` in request to prevent breaking change |
| 18 | +export class Client extends Pages { |
| 19 | + constructor(protected readonly _options: Pages.Options) { |
| 20 | + super(_options); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Update Page-level metadata, including SEO and Open Graph fields. |
| 25 | + * |
| 26 | + * <Note> |
| 27 | + * Note: When updating Page Metadata in secondary locales, you may only add `slug` to the request if your Site has the [Advanced or Enterprise Localization](https://webflow.com/localization) add-on. |
| 28 | + * </Note> |
| 29 | + * |
| 30 | + * Required scope | `pages:write` |
| 31 | + * |
| 32 | + * @param {string} pageId - Unique identifier for a Page |
| 33 | + * @param {Webflow.PageMetadataWrite | SchemaOverrides.PageMetadataWriteBody} request |
| 34 | + * @param {Pages.RequestOptions} requestOptions - Request-specific configuration. |
| 35 | + * |
| 36 | + * @throws {@link Webflow.BadRequestError} |
| 37 | + * @throws {@link Webflow.UnauthorizedError} |
| 38 | + * @throws {@link Webflow.NotFoundError} |
| 39 | + * @throws {@link Webflow.TooManyRequestsError} |
| 40 | + * @throws {@link Webflow.InternalServerError} |
| 41 | + * |
| 42 | + * @example |
| 43 | + * await client.pages.updatePageSettings("63c720f9347c2139b248e552", { |
| 44 | + * localeId: "65427cf400e02b306eaa04a0", |
| 45 | + * title: "Guide to the Galaxy", |
| 46 | + * slug: "guide-to-the-galaxy", |
| 47 | + * seo: { |
| 48 | + * title: "The Ultimate Hitchhiker's Guide to the Galaxy", |
| 49 | + * description: "Everything you need to know about the galaxy, from avoiding Vogon poetry to the importance of towels." |
| 50 | + * }, |
| 51 | + * openGraph: { |
| 52 | + * title: "Explore the Cosmos with The Ultimate Guide", |
| 53 | + * titleCopied: false, |
| 54 | + * description: "Dive deep into the mysteries of the universe with your guide to everything galactic.", |
| 55 | + * descriptionCopied: false |
| 56 | + * } |
| 57 | + * }) |
| 58 | + */ |
| 59 | + public updatePageSettings( |
| 60 | + pageId: string, |
| 61 | + request: (Webflow.PageMetadataWrite | SchemaOverrides.PageMetadataWriteBody) = {}, |
| 62 | + requestOptions?: Pages.RequestOptions, |
| 63 | + ): core.HttpResponsePromise<Webflow.Page> { |
| 64 | + return core.HttpResponsePromise.fromPromise(this.__updatePageSettingsOverride(pageId, request, requestOptions)); |
| 65 | + } |
| 66 | + |
| 67 | + private async __updatePageSettingsOverride( |
| 68 | + pageId: string, |
| 69 | + request: (Webflow.PageMetadataWrite | SchemaOverrides.PageMetadataWriteBody) = {}, |
| 70 | + requestOptions?: Pages.RequestOptions, |
| 71 | + ): Promise<core.WithRawResponse<Webflow.Page>> { |
| 72 | + const { localeId, ..._body } = request; |
| 73 | + const _queryParams: Record<string, string | string[] | object | object[] | null> = {}; |
| 74 | + if (localeId != null) { |
| 75 | + _queryParams["localeId"] = localeId; |
| 76 | + } |
| 77 | + |
| 78 | + const _response = await core.fetcher({ |
| 79 | + url: urlJoin( |
| 80 | + (await core.Supplier.get(this._options.baseUrl)) ?? |
| 81 | + ((await core.Supplier.get(this._options.environment)) ?? environments.WebflowEnvironment.DataApi) |
| 82 | + .base, |
| 83 | + `pages/${encodeURIComponent(pageId)}`, |
| 84 | + ), |
| 85 | + method: "PUT", |
| 86 | + headers: mergeHeaders( |
| 87 | + this._options?.headers, |
| 88 | + mergeOnlyDefinedHeaders({ Authorization: await this._getAuthorizationHeader() }), |
| 89 | + requestOptions?.headers, |
| 90 | + ), |
| 91 | + contentType: "application/json", |
| 92 | + queryParameters: _queryParams, |
| 93 | + requestType: "json", |
| 94 | + body: serializers.PageMetadataWrite.jsonOrThrow(_body, { |
| 95 | + unrecognizedObjectKeys: "passthrough", |
| 96 | + allowUnrecognizedUnionMembers: true, |
| 97 | + allowUnrecognizedEnumValues: true, |
| 98 | + }), |
| 99 | + timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, |
| 100 | + maxRetries: requestOptions?.maxRetries, |
| 101 | + abortSignal: requestOptions?.abortSignal, |
| 102 | + }); |
| 103 | + if (_response.ok) { |
| 104 | + return { |
| 105 | + data: serializers.Page.parseOrThrow(_response.body, { |
| 106 | + unrecognizedObjectKeys: "passthrough", |
| 107 | + allowUnrecognizedUnionMembers: true, |
| 108 | + allowUnrecognizedEnumValues: true, |
| 109 | + skipValidation: true, |
| 110 | + breadcrumbsPrefix: ["response"], |
| 111 | + }), |
| 112 | + rawResponse: _response.rawResponse, |
| 113 | + }; |
| 114 | + } |
| 115 | + |
| 116 | + if (_response.error.reason === "status-code") { |
| 117 | + switch (_response.error.statusCode) { |
| 118 | + case 400: |
| 119 | + throw new Webflow.BadRequestError(_response.error.body, _response.rawResponse); |
| 120 | + case 401: |
| 121 | + throw new Webflow.UnauthorizedError( |
| 122 | + serializers.Error_.parseOrThrow(_response.error.body, { |
| 123 | + unrecognizedObjectKeys: "passthrough", |
| 124 | + allowUnrecognizedUnionMembers: true, |
| 125 | + allowUnrecognizedEnumValues: true, |
| 126 | + skipValidation: true, |
| 127 | + breadcrumbsPrefix: ["response"], |
| 128 | + }), |
| 129 | + _response.rawResponse, |
| 130 | + ); |
| 131 | + case 404: |
| 132 | + throw new Webflow.NotFoundError( |
| 133 | + serializers.Error_.parseOrThrow(_response.error.body, { |
| 134 | + unrecognizedObjectKeys: "passthrough", |
| 135 | + allowUnrecognizedUnionMembers: true, |
| 136 | + allowUnrecognizedEnumValues: true, |
| 137 | + skipValidation: true, |
| 138 | + breadcrumbsPrefix: ["response"], |
| 139 | + }), |
| 140 | + _response.rawResponse, |
| 141 | + ); |
| 142 | + case 429: |
| 143 | + throw new Webflow.TooManyRequestsError( |
| 144 | + serializers.Error_.parseOrThrow(_response.error.body, { |
| 145 | + unrecognizedObjectKeys: "passthrough", |
| 146 | + allowUnrecognizedUnionMembers: true, |
| 147 | + allowUnrecognizedEnumValues: true, |
| 148 | + skipValidation: true, |
| 149 | + breadcrumbsPrefix: ["response"], |
| 150 | + }), |
| 151 | + _response.rawResponse, |
| 152 | + ); |
| 153 | + case 500: |
| 154 | + throw new Webflow.InternalServerError( |
| 155 | + serializers.Error_.parseOrThrow(_response.error.body, { |
| 156 | + unrecognizedObjectKeys: "passthrough", |
| 157 | + allowUnrecognizedUnionMembers: true, |
| 158 | + allowUnrecognizedEnumValues: true, |
| 159 | + skipValidation: true, |
| 160 | + breadcrumbsPrefix: ["response"], |
| 161 | + }), |
| 162 | + _response.rawResponse, |
| 163 | + ); |
| 164 | + default: |
| 165 | + throw new errors.WebflowError({ |
| 166 | + statusCode: _response.error.statusCode, |
| 167 | + body: _response.error.body, |
| 168 | + rawResponse: _response.rawResponse, |
| 169 | + }); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + switch (_response.error.reason) { |
| 174 | + case "non-json": |
| 175 | + throw new errors.WebflowError({ |
| 176 | + statusCode: _response.error.statusCode, |
| 177 | + body: _response.error.rawBody, |
| 178 | + rawResponse: _response.rawResponse, |
| 179 | + }); |
| 180 | + case "timeout": |
| 181 | + throw new errors.WebflowTimeoutError("Timeout exceeded when calling PUT /pages/{page_id}."); |
| 182 | + case "unknown": |
| 183 | + throw new errors.WebflowError({ |
| 184 | + message: _response.error.errorMessage, |
| 185 | + rawResponse: _response.rawResponse, |
| 186 | + }); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments