Skip to content

Commit 15aab35

Browse files
authored
(feat): Add extra field support for CollectionItem (#137)
1 parent 3c0af9f commit 15aab35

File tree

3 files changed

+137
-2
lines changed

3 files changed

+137
-2
lines changed

src/wrapper/CollectionsClient.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Collections } from "../api/resources/collections/client/Client";
2+
import { Client as Items } from "./ItemsClient";
3+
4+
// Client adapts the base client to permit extra properties in
5+
// the client.Collections.Items.createItem request.
6+
export class Client extends Collections {
7+
constructor(protected readonly _options: Collections.Options) {
8+
super(_options);
9+
}
10+
11+
protected _items: Items | undefined;
12+
13+
public get items(): Items {
14+
return (this._items ??= new Items(this._options));
15+
}
16+
}

src/wrapper/ItemsClient.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

src/wrapper/WebflowClient.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
import qs from "qs";
12
import { WebflowClient as FernClient } from "../Client";
23
import { OauthScope } from "../api/types";
3-
import * as errors from "../errors";
44
import * as core from "../core";
5-
import qs from "qs";
5+
import * as errors from "../errors";
6+
import { Client as Collections } from "./CollectionsClient";
67

78
export class WebflowClient extends FernClient {
9+
constructor(protected readonly _options: FernClient.Options) {
10+
super(_options);
11+
}
12+
13+
protected _collections: Collections | undefined;
14+
15+
public get collections(): Collections {
16+
return (this._collections ??= new Collections(this._options));
17+
}
18+
819
/**
920
* @param clientId The OAuth client ID
1021
* @param state The state

0 commit comments

Comments
 (0)