diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 00000000..7deba337 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,338 @@ +declare class Webflow { + constructor(options: Webflow.WebflowOptions); + + get( + path: string, + query?: Webflow.WebflowQueryArg + ): Promise; + post( + path: string, + data?: Data, + query?: Webflow.WebflowQueryArg + ): Promise; + put( + path: string, + data?: Data, + query?: Webflow.WebflowQueryArg + ): Promise; + patch( + path: string, + data?: Data, + query?: Webflow.WebflowQueryArg + ): Promise; + delete( + path: string, + query?: Webflow.WebflowQueryArg + ): Promise; + + info(query?: Webflow.WebflowQueryArg): Promise; + + // sites + + sites( + query?: Webflow.WebflowQueryArg + ): Promise; + + site( + params: { + siteId: string; + }, + query?: Webflow.WebflowQueryArg + ): Promise; + + publishSite( + data: { + siteId: string; + domains: string[]; + }, + query?: Webflow.WebflowQueryArg + ): Promise<{ queued: boolean }>; + + // Domains + + domains( + data: { + siteId: string; + }, + query?: Webflow.WebflowQueryArg + ): Promise; + + // Collections + + collections( + data: { + siteId: string; + }, + query?: Webflow.WebflowQueryArg + ): Promise; + collection( + data: { + collectionId: string; + }, + query?: Webflow.WebflowQueryArg + ): Promise; + + // Items + + items( + data: { + collectionId: string; + }, + query?: Webflow.WebflowQueryArg + ): Promise; + + item( + data: { + collectionId: string; + itemId: string; + }, + query?: Webflow.WebflowQueryArg + ): Promise; + + createItem( + // TODO: add a better data type + data: { collectionId: string } & Record, + query?: Webflow.WebflowQueryArg + ): Promise; + + updateItem( + // TODO: add a better data type + data: { collectionId: string; itemId: string } & Record, + query?: Webflow.WebflowQueryArg + ): Promise; + + removeItem( + data: { collectionId: string; itemId: string }, + query?: Webflow.WebflowQueryArg + ): Promise<{ deleted: number }>; + + patchItem( + // TODO: add a better data type + data: { collectionId: string; itemId: string } & Record, + query?: Webflow.WebflowQueryArg + ): Promise; + + // Webhooks + + webhooks( + data: { siteId: string }, + query?: Webflow.WebflowQueryArg + ): Promise; + + webhook( + data: { siteId: string; webhookId: string }, + query?: Webflow.WebflowQueryArg + ): Promise; + + createWebhook( + // TODO: add a better data type + data: { siteId: string } & Record, + query?: Webflow.WebflowQueryArg + ): Promise; + + removeWebhook( + data: { siteId: string; webhookId: string }, + query?: Webflow.WebflowQueryArg + ): Promise<{ deleted: number }>; +} + +declare namespace Webflow { + // other exported properties + class WebflowError extends Error {} + + // helper types / namespaces + type WebflowQueryArg = Record; + + interface WebflowOptions { + token: string; + endpoint?: string; + version?: string; + } + + namespace WebflowApiModel { + interface InfoApplication { + _id: string; + description: string; + homepage: string; + name: string; + owner: string; + ownerType: string; + } + + /** + * https://developers.webflow.com/?javascript#get-current-authorization-info + */ + interface Info { + _id: string; + createdOn: string; + grantType: string; + lastUsed: string; + sites: string[]; + orgs: string[]; + users: string[]; + rateLimit: number; + status: string; + application: InfoApplication; + } + /** + * https://developers.webflow.com/?javascript#sites + */ + interface Site { + _id: string; + createdOn: string; + name: string; + shortName: string; + lastPublished: string; + previewUrl: string; + timezone: string; + database: string; + + collections: OmitFirstArgOfFunction; + webhooks: OmitFirstArgOfFunction; + domains: OmitFirstArgOfFunction; + webhook: OmitPropertyFromFirstArgOfFunction; + createWebhook: OmitPropertyFromFirstArgOfFunction< + Webflow["createWebhook"], + "siteId" + >; + removeWebhook: OmitPropertyFromFirstArgOfFunction< + Webflow["removeWebhook"], + "siteId" + >; + publishSite: OmitPropertyFromFirstArgOfFunction< + Webflow["publishSite"], + "siteId" + >; + } + + /** + * https://developers.webflow.com/?javascript#domains + */ + interface Domain { + _id: string; + name: string; + } + + /** + * https://developers.webflow.com/?javascript#collections + */ + interface Collection { + _id: string; + lastUpdated: string; + createdOn: string; + name: string; + slug: string; + singularName: string; + fields: CollectionField[]; + + items: OmitFirstArgOfFunction; + item: OmitPropertyFromFirstArgOfFunction; + createItem: OmitPropertyFromFirstArgOfFunction< + Webflow["createItem"], + "collectionId" + >; + updateItem: OmitPropertyFromFirstArgOfFunction< + Webflow["updateItem"], + "collectionId" + >; + removeItem: OmitPropertyFromFirstArgOfFunction< + Webflow["removeItem"], + "collectionId" + >; + } + + type CollectionFieldType = + | "Bool" + | "Color" + | "Date" + | "ExtFileRef" + | "Set" + | "ImageRef" + | "Set" + | "ItemRef" + | "ItemRefSet" + | "Link" + | "Number" + | "Option" + | "PlainText" + | "RichText" + | "Video" + | "User"; + + /** + * https://developers.webflow.com/?javascript#fields + */ + interface CollectionField { + id: string; + type: CollectionFieldType; + slug: string; + name: string; + required: boolean; + editable: boolean; + // TODO: add a better type + validations: any; + } + + /** + * https://developers.webflow.com/?javascript#items + */ + interface CollectionItem extends Record { + _archived: boolean; + _draft: boolean; + _id: string; + _cid: string; + name: string; + slug: string; + "updated-on": string; + "created-on": string; + "published-on": string; + "updated-by": string; + "created-by": string; + "published-by": string; + + update: OmitPropertyFromFirstArgOfFunction< + Webflow["updateItem"], + "collectionId" | "itemId" + >; + remove: OmitFirstArgOfFunction; + } + + type WebhookTriggerType = + | "form_submission" + | "site_publish" + | "ecomm_new_order" + | "ecomm_order_changed" + | "ecomm_inventory_changed" + | "collection_item_created" + | "collection_item_changed" + | "collection_item_deleted"; + + interface Webhook { + _id: string; + triggerType: WebhookTriggerType; + triggerId: string; + site: string; + filter: string; + lastUsed: string; + createdOn: string; + + remove: OmitFirstArgOfFunction; + } + } +} + +export = Webflow; + +type OmitFirstArgOfFunction = Fn extends ( + x: any, + ...args: infer Args +) => infer R + ? (...args: Args) => R + : never; + +type OmitPropertyFromFirstArgOfFunction = Fn extends ( + x: infer A, + ...args: infer Args +) => infer R + ? (x: Omit, ...args: Args) => R + : never; diff --git a/package.json b/package.json index 67bd1714..b6f4c1f5 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "type": "git" }, "license": "MIT", + "types": "index.d.ts", "files": [ "dist", "src",