webflow-api 1.0.3 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +4 -5
  2. package/dist/api/collection.d.ts +51 -0
  3. package/dist/api/collection.js +35 -0
  4. package/dist/api/index.d.ts +7 -0
  5. package/dist/api/index.js +33 -0
  6. package/dist/api/item.d.ts +143 -0
  7. package/dist/api/item.js +131 -0
  8. package/dist/api/membership.d.ts +91 -0
  9. package/dist/api/membership.js +80 -0
  10. package/dist/api/meta.d.ts +51 -0
  11. package/dist/api/meta.js +24 -0
  12. package/dist/api/oauth.d.ts +67 -0
  13. package/dist/api/oauth.js +65 -0
  14. package/dist/api/site.d.ts +65 -0
  15. package/dist/api/site.js +59 -0
  16. package/dist/api/webhook.d.ts +80 -0
  17. package/dist/api/webhook.js +67 -0
  18. package/dist/core/client.d.ts +40 -0
  19. package/dist/core/client.js +49 -0
  20. package/dist/core/error.d.ts +19 -0
  21. package/dist/core/error.js +23 -0
  22. package/dist/core/index.d.ts +3 -0
  23. package/dist/core/index.js +19 -0
  24. package/dist/core/options.d.ts +8 -0
  25. package/dist/core/options.js +5 -0
  26. package/dist/index.d.ts +2 -0
  27. package/dist/index.js +2 -9
  28. package/dist/webflow.d.ts +366 -0
  29. package/dist/webflow.js +388 -0
  30. package/dist/wrapper/collection.d.ts +85 -0
  31. package/dist/wrapper/collection.js +94 -0
  32. package/dist/wrapper/index.d.ts +6 -0
  33. package/dist/wrapper/index.js +22 -0
  34. package/dist/wrapper/item.d.ts +140 -0
  35. package/dist/wrapper/item.js +153 -0
  36. package/dist/wrapper/membership.d.ts +91 -0
  37. package/dist/wrapper/membership.js +106 -0
  38. package/dist/wrapper/response.d.ts +16 -0
  39. package/dist/wrapper/response.js +17 -0
  40. package/dist/wrapper/site.d.ts +119 -0
  41. package/dist/wrapper/site.js +136 -0
  42. package/dist/wrapper/webhook.d.ts +78 -0
  43. package/dist/wrapper/webhook.js +82 -0
  44. package/package.json +16 -17
  45. package/src/api/collection.ts +87 -0
  46. package/src/api/index.ts +7 -0
  47. package/src/api/item.ts +231 -0
  48. package/src/api/membership.ts +125 -0
  49. package/src/api/meta.ts +61 -0
  50. package/src/api/oauth.ts +119 -0
  51. package/src/api/site.ts +86 -0
  52. package/src/api/webhook.ts +125 -0
  53. package/src/core/client.ts +76 -0
  54. package/src/core/error.ts +32 -0
  55. package/src/core/index.ts +3 -0
  56. package/src/core/options.ts +9 -0
  57. package/src/index.ts +3 -0
  58. package/src/webflow.ts +487 -0
  59. package/src/wrapper/collection.ts +115 -0
  60. package/src/wrapper/index.ts +6 -0
  61. package/src/wrapper/item.ts +218 -0
  62. package/src/wrapper/membership.ts +138 -0
  63. package/src/wrapper/response.ts +25 -0
  64. package/src/wrapper/site.ts +164 -0
  65. package/src/wrapper/webhook.ts +116 -0
  66. package/yarn.lock +388 -1473
  67. package/dist/ResponseWrapper.js +0 -135
  68. package/dist/Webflow.js +0 -344
  69. package/dist/WebflowClient.js +0 -115
  70. package/index.d.ts +0 -430
  71. package/src/ResponseWrapper.js +0 -103
  72. package/src/Webflow.js +0 -301
  73. package/src/WebflowClient.js +0 -98
  74. package/src/index.js +0 -3
@@ -0,0 +1,125 @@
1
+ import { Client, PaginatedData, QueryString, requireArgs } from "../core";
2
+
3
+ /**************************************************************
4
+ * Interfaces
5
+ **************************************************************/
6
+ export interface IUser {
7
+ emailVerified: boolean;
8
+ lastUpdated?: string;
9
+ createdOn: string;
10
+ _id: string;
11
+ data: any;
12
+ }
13
+
14
+ export interface IUserDelete {
15
+ deleted: number;
16
+ }
17
+
18
+ /**************************************************************
19
+ * Types
20
+ **************************************************************/
21
+ export type PaginatedUsers = PaginatedData & {
22
+ users: IUser[];
23
+ };
24
+
25
+ export type UserIdParam = { siteId: string; userId: string };
26
+
27
+ /**************************************************************
28
+ * Functions
29
+ **************************************************************/
30
+
31
+ /**
32
+ * Get a list of Users
33
+ * @param client The Webflow client
34
+ * @param params The params for the request
35
+ * @param params.siteId The site ID
36
+ * @param params.limit The number of items to return (optional)
37
+ * @param params.offset The number of items to skip (optional)
38
+ * @returns A list of Users
39
+ */
40
+ export function list(
41
+ client: Client,
42
+ { siteId, limit, offset }: { siteId: string; limit?: number; offset?: number }
43
+ ) {
44
+ requireArgs({ siteId });
45
+ const params = { limit, offset };
46
+ const path = `/sites/${siteId}/users`;
47
+ return client.get<PaginatedUsers>(path, { params });
48
+ }
49
+
50
+ /**
51
+ * Get a single User
52
+ * @param client The Webflow client
53
+ * @param params The params for the request
54
+ * @param params.siteId The site ID
55
+ * @param params.userId The user ID
56
+ * @returns A single User
57
+ */
58
+ export function getOne(
59
+ client: Client,
60
+ { siteId, userId }: { siteId: string; userId: string }
61
+ ) {
62
+ requireArgs({ siteId, userId });
63
+ const path = `/sites/${siteId}/users/${userId}`;
64
+ return client.get<IUser>(path);
65
+ }
66
+
67
+ /**
68
+ * Update a User
69
+ * @param client The Webflow client
70
+ * @param params The params for the request
71
+ * @param params.siteId The site ID
72
+ * @param params.userId The user ID
73
+ * @param params.data The data to update
74
+ * @returns The updated User
75
+ */
76
+ export function update(
77
+ client: Client,
78
+ {
79
+ siteId,
80
+ userId,
81
+ data,
82
+ }: {
83
+ data: object;
84
+ siteId: string;
85
+ userId: string;
86
+ }
87
+ ) {
88
+ requireArgs({ siteId, userId });
89
+ const path = `/sites/${siteId}/users/${userId}`;
90
+ return client.patch<IUser>(path, data);
91
+ }
92
+
93
+ /**
94
+ * Invite a User to a site
95
+ * @param client The Webflow client
96
+ * @param params The params for the request
97
+ * @param params.siteId The site ID
98
+ * @param params.email The email address of the user to invite
99
+ * @returns The newly created User
100
+ */
101
+ export async function invite(
102
+ client: Client,
103
+ { siteId, email }: { siteId: string; email: string }
104
+ ) {
105
+ requireArgs({ siteId, email });
106
+ const path = `/sites/${siteId}/users/invite`;
107
+ return client.post<IUser>(path, { email });
108
+ }
109
+
110
+ /**
111
+ * Remove a User
112
+ * @param client The Webflow client
113
+ * @param params The params for the request
114
+ * @param params.siteId The site ID
115
+ * @param params.userId The user ID
116
+ * @returns The result of the remove
117
+ */
118
+ export function remove(
119
+ client: Client,
120
+ { siteId, userId }: { siteId: string; userId: string }
121
+ ) {
122
+ requireArgs({ siteId, userId });
123
+ const path = `/sites/${siteId}/users/${userId}`;
124
+ return client.delete<IUserDelete>(path);
125
+ }
@@ -0,0 +1,61 @@
1
+ import { Client } from "../core";
2
+
3
+ /**************************************************************
4
+ * Types
5
+ **************************************************************/
6
+ export type InfoApplication = {
7
+ description: string;
8
+ ownerType: string;
9
+ homepage: string;
10
+ owner: string;
11
+ name: string;
12
+ _id: string;
13
+ };
14
+
15
+ /**************************************************************
16
+ * Interfaces
17
+ **************************************************************/
18
+ export interface IAuthenticatedUser {
19
+ user: {
20
+ firstName: string;
21
+ lastName: string;
22
+ email: string;
23
+ _id: string;
24
+ };
25
+ }
26
+
27
+ export interface IAuthentiationInfo {
28
+ application: InfoApplication;
29
+ workspaces: string[];
30
+ rateLimit: number;
31
+ createdOn: string;
32
+ grantType: string;
33
+ lastUsed: string;
34
+ sites: string[];
35
+ users: string[];
36
+ orgs: string[];
37
+ status: string;
38
+ _id: string;
39
+ }
40
+
41
+ /**************************************************************
42
+ * Functions
43
+ **************************************************************/
44
+
45
+ /**
46
+ * Get the authentication info for the current token
47
+ * @param client The Webflow client
48
+ * @returns The authentication info
49
+ */
50
+ export function info(client: Client) {
51
+ return client.get<IAuthentiationInfo>("/info");
52
+ }
53
+
54
+ /**
55
+ * Get the authenticated user
56
+ * @param client The Webflow client
57
+ * @returns The authenticated user
58
+ */
59
+ export function user(client: Client) {
60
+ return client.get<IAuthenticatedUser>("/user");
61
+ }
@@ -0,0 +1,119 @@
1
+ import { Client, requireArgs } from "../core";
2
+
3
+ /**************************************************************
4
+ * Interfaces
5
+ **************************************************************/
6
+ export interface IAuthorizeUrlParams {
7
+ state?: string;
8
+ scope?: string;
9
+ client_id: string;
10
+ redirect_uri?: string;
11
+ response_type?: string;
12
+ }
13
+
14
+ export interface IRevokeTokenParams {
15
+ client_secret: string;
16
+ access_token: string;
17
+ client_id: string;
18
+ }
19
+
20
+ export interface IAccessTokenParams {
21
+ code: string;
22
+ client_id: string;
23
+ grant_type?: string;
24
+ client_secret: string;
25
+ redirect_uri?: string;
26
+ }
27
+
28
+ export interface IAccessToken {
29
+ token_type: string;
30
+ access_token: string;
31
+ }
32
+
33
+ export interface IRevokeToken {
34
+ didRevoke: boolean;
35
+ }
36
+
37
+ /**************************************************************
38
+ * Functions
39
+ **************************************************************/
40
+
41
+ /**
42
+ * Get the URL to authorize a user
43
+ * @param client The Webflow client
44
+ * @param params The params for the request
45
+ * @param params.client_id The OAuth client ID
46
+ * @param params.scope The scope (optional)
47
+ * @param params.state The state (optional)
48
+ * @param params.redirect_uri The redirect URI (optional)
49
+ * @param params.response_type The response type (default: code)
50
+ * @returns The URL to authorize a user
51
+ */
52
+ export function authorizeUrl(
53
+ client: Client,
54
+ {
55
+ response_type = "code",
56
+ redirect_uri,
57
+ client_id,
58
+ state,
59
+ scope,
60
+ }: IAuthorizeUrlParams
61
+ ) {
62
+ requireArgs({ client_id });
63
+
64
+ const params = { response_type, client_id };
65
+ if (redirect_uri) params["redirect_uri"] = redirect_uri;
66
+ if (state) params["state"] = state;
67
+ if (scope) params["scope"] = scope;
68
+
69
+ const url = "/oauth/authorize";
70
+ return client.getUri({ url, method: "GET", params });
71
+ }
72
+
73
+ /**
74
+ * Get an access token
75
+ * @param client The Webflow client
76
+ * @param params The params for the request
77
+ * @param params.code The OAuth code
78
+ * @param params.client_id The OAuth client ID
79
+ * @param params.client_secret The OAuth client secret
80
+ * @param params.redirect_uri The redirect URI (optional)
81
+ * @param params.grant_type The grant type (default: "authorization_code")
82
+ * @returns An access token
83
+ */
84
+ export function accessToken(
85
+ client: Client,
86
+ {
87
+ grant_type = "authorization_code",
88
+ client_secret,
89
+ redirect_uri,
90
+ client_id,
91
+ code,
92
+ }: IAccessTokenParams
93
+ ) {
94
+ requireArgs({ client_id, client_secret, code });
95
+
96
+ const path = "/oauth/access_token";
97
+ const data = { client_secret, redirect_uri, grant_type, client_id, code };
98
+ return client.post<IAccessToken>(path, data);
99
+ }
100
+
101
+ /**
102
+ * Revoke an access token
103
+ * @param client The Webflow client
104
+ * @param params The params for the request
105
+ * @param params.client_id The OAuth client ID
106
+ * @param params.client_secret The OAuth client secret
107
+ * @param params.access_token The OAuth access token
108
+ * @returns The result of the revoke
109
+ */
110
+ export function revokeToken(
111
+ client: Client,
112
+ { client_secret, access_token, client_id }: IRevokeTokenParams
113
+ ) {
114
+ requireArgs({ client_id, client_secret, access_token });
115
+
116
+ const path = "/oauth/revoke_authorization";
117
+ const data = { client_secret, access_token, client_id };
118
+ return client.post<IRevokeToken>(path, data);
119
+ }
@@ -0,0 +1,86 @@
1
+ import { Client, QueryString, requireArgs } from "../core";
2
+
3
+ /**************************************************************
4
+ * Interfaces
5
+ *************************************************************/
6
+ export interface IDomain {
7
+ _id: string;
8
+ name: string;
9
+ }
10
+
11
+ export interface ISite {
12
+ lastPublished: string;
13
+ previewUrl: string;
14
+ createdOn: string;
15
+ shortName: string;
16
+ timezone: string;
17
+ database: string;
18
+ name: string;
19
+ _id: string;
20
+ }
21
+
22
+ export interface IPublishSite {
23
+ queued: boolean;
24
+ }
25
+
26
+ /**************************************************************
27
+ * Functions
28
+ **************************************************************/
29
+
30
+ /**
31
+ * Get a list of Sites
32
+ * @param client The Webflow client
33
+ * @param params The query string parameters (optional)
34
+ * @returns
35
+ */
36
+ export function list(client: Client, params?: QueryString) {
37
+ const path = "/sites";
38
+ return client.get<ISite[]>(path, { params });
39
+ }
40
+
41
+ /**
42
+ * Get a single Site
43
+ * @param client The Webflow client
44
+ * @param params The params for the request
45
+ * @param params.siteId The site ID
46
+ * @param params.params The query string parameters (optional)
47
+ * @returns A single Site
48
+ */
49
+ export function getOne(
50
+ client: Client,
51
+ { siteId, params }: { siteId: string; params?: QueryString }
52
+ ) {
53
+ requireArgs({ siteId });
54
+ const path = `/sites/${siteId}`;
55
+ return client.get<ISite>(path, { params });
56
+ }
57
+
58
+ /**
59
+ * Publish a site
60
+ * @param client The Webflow client
61
+ * @param params The params for the request
62
+ * @param params.siteId The site ID
63
+ * @param params.domains The domains to publish to
64
+ * @returns The publish result
65
+ */
66
+ export function publish(
67
+ client: Client,
68
+ { siteId, domains }: { siteId: string; domains: string[] }
69
+ ) {
70
+ requireArgs({ siteId, domains });
71
+ const path = `/sites/${siteId}/publish`;
72
+ return client.post<IPublishSite>(path, { domains });
73
+ }
74
+
75
+ /**
76
+ * Get a list of domains for a site
77
+ * @param client The Webflow client
78
+ * @param params The params for the request
79
+ * @param params.siteId The site ID
80
+ * @returns A list of domains
81
+ */
82
+ export function domains(client: Client, { siteId }: { siteId: string }) {
83
+ requireArgs({ siteId });
84
+ const path = `/sites/${siteId}/domains`;
85
+ return client.get<IDomain[]>(path);
86
+ }
@@ -0,0 +1,125 @@
1
+ import { Client, QueryString, requireArgs } from "../core";
2
+
3
+ /**************************************************************
4
+ * Types
5
+ **************************************************************/
6
+ export type TriggerType =
7
+ | "form_submission"
8
+ | "site_publish"
9
+ | "ecomm_new_order"
10
+ | "ecomm_order_changed"
11
+ | "ecomm_inventory_changed"
12
+ | "collection_item_created"
13
+ | "collection_item_changed"
14
+ | "collection_item_deleted"
15
+ | string;
16
+
17
+ export type Filter = {
18
+ name: string;
19
+ };
20
+
21
+ /**************************************************************
22
+ * Interfaces
23
+ **************************************************************/
24
+ export interface IWebhook {
25
+ triggerType: TriggerType;
26
+ triggerId: string;
27
+ createdOn: string;
28
+ lastUsed?: string;
29
+ site: string;
30
+ _id: string;
31
+ filter?: {
32
+ name: string;
33
+ };
34
+ }
35
+
36
+ export interface IRemoveResult {
37
+ deleted: number;
38
+ }
39
+
40
+ /**************************************************************
41
+ * Functions
42
+ **************************************************************/
43
+
44
+ /**
45
+ * Get a list of Webhooks
46
+ * @param client The Webflow client
47
+ * @param params1 The params for the request
48
+ * @param params1.siteId The site ID
49
+ * @param params The query string parameters (optional)
50
+ * @returns A list of Webhooks
51
+ */
52
+ export function list(
53
+ client: Client,
54
+ { siteId }: { siteId: string },
55
+ params?: QueryString
56
+ ) {
57
+ requireArgs({ siteId });
58
+ const path = `/sites/${siteId}/webhooks`;
59
+ return client.get<IWebhook[]>(path, { params });
60
+ }
61
+
62
+ /**
63
+ * Get a single Webhook
64
+ * @param client The Webflow client
65
+ * @param params The params for the request
66
+ * @param params.siteId The site ID
67
+ * @param params.webhookId The webhook ID
68
+ * @returns A single Webhook
69
+ */
70
+ export function getOne(
71
+ client: Client,
72
+ { siteId, webhookId }: { siteId: string; webhookId: string }
73
+ ) {
74
+ requireArgs({ siteId, webhookId });
75
+ const path = `/sites/${siteId}/webhooks/${webhookId}`;
76
+ return client.get<IWebhook>(path);
77
+ }
78
+
79
+ /**
80
+ * Create a new Webhook
81
+ * @param client The Webflow client
82
+ * @param params The params for the request
83
+ * @param params.siteId The site ID
84
+ * @param params.url The URL to send the webhook to
85
+ * @param params.triggerType The event to trigger the webhook
86
+ * @param params.filter The filter to apply to the webhook (optional: form_submission only)
87
+ * @param params.params The query string parameters (optional)
88
+ * @returns The created webhook
89
+ */
90
+ export function create(
91
+ client: Client,
92
+ {
93
+ triggerType,
94
+ siteId,
95
+ filter,
96
+ url,
97
+ }: {
98
+ url: string;
99
+ siteId: string;
100
+ filter?: Filter;
101
+ triggerType: TriggerType;
102
+ }
103
+ ) {
104
+ requireArgs({ siteId, triggerType, url });
105
+ const path = `/sites/${siteId}/webhooks`;
106
+ const data = { triggerType, url, filter };
107
+ return client.post<IWebhook>(path, data);
108
+ }
109
+
110
+ /**
111
+ * Remove a Webhook
112
+ * @param client The Webflow client
113
+ * @param params The query string parameters (optional)
114
+ * @param params.webhookId The Webhook ID
115
+ * @param params.siteId The Site ID
116
+ * @returns The result of the removal
117
+ */
118
+ export function remove(
119
+ client: Client,
120
+ { siteId, webhookId }: { siteId: string; webhookId: string }
121
+ ) {
122
+ requireArgs({ siteId, webhookId });
123
+ const path = `/sites/${siteId}/webhooks/${webhookId}`;
124
+ return client.delete<IRemoveResult>(path);
125
+ }
@@ -0,0 +1,76 @@
1
+ import { Axios } from "axios";
2
+ import { DEFAULT_HOST, USER_AGENT, Options, RequestError } from "../core";
3
+
4
+ /**************************************************************
5
+ * Types
6
+ **************************************************************/
7
+ export type QueryString = Record<string, any>;
8
+ export type PaginationFilter = { limit?: number; offset?: number };
9
+
10
+ /**************************************************************
11
+ * Interfaces
12
+ ************************************************************* */
13
+ export interface PaginatedData {
14
+ count: number;
15
+ limit: number;
16
+ offset: number;
17
+ total: number;
18
+ }
19
+
20
+ export interface WebflowOptions {
21
+ host?: string;
22
+ token?: string;
23
+ version?: string;
24
+ headers?: Record<string, string>;
25
+ }
26
+
27
+ /**************************************************************
28
+ * Classes
29
+ **************************************************************/
30
+ export class Client extends Axios {
31
+ constructor({
32
+ host = DEFAULT_HOST,
33
+ headers = {},
34
+ version,
35
+ token,
36
+ }: Options = {}) {
37
+ super({
38
+ transformRequest: [(data) => JSON.stringify(data)],
39
+ baseURL: `https://api.${host}/`,
40
+ headers: {
41
+ "Content-Type": "application/json",
42
+ "User-Agent": USER_AGENT,
43
+ "Accept-Version": version,
44
+ ...headers,
45
+ },
46
+ });
47
+
48
+ if (token) this.token = token;
49
+
50
+ // check for webflow errors
51
+ this.defaults.transformResponse = [this.transformResponse];
52
+ }
53
+
54
+ /**
55
+ * Transforms JSON response to an object
56
+ * if the response is a Webflow error, it will throw an error
57
+ * @param data JSON response
58
+ * @returns response object
59
+ */
60
+ private transformResponse(data: any = {}) {
61
+ // parse json if string
62
+ if (String(data) === data) data = JSON.parse(data);
63
+ if (data.err) throw new RequestError(data);
64
+ return data;
65
+ }
66
+
67
+ // set the Authorization header
68
+ set token(value: string) {
69
+ this.defaults.headers["Authorization"] = `Bearer ${value}`;
70
+ }
71
+
72
+ // clear the Authorization header
73
+ clearToken() {
74
+ delete this.defaults.headers["Authorization"];
75
+ }
76
+ }
@@ -0,0 +1,32 @@
1
+ export interface IRequestError {
2
+ msg: string;
3
+ code: number;
4
+ name: string;
5
+ path: string;
6
+ err: string;
7
+ }
8
+
9
+ export class RequestError extends Error implements IRequestError {
10
+ msg: string;
11
+ code: number;
12
+ name: string;
13
+ path: string;
14
+ err: string;
15
+
16
+ constructor(error: IRequestError) {
17
+ super(error.err ? error.err : "Unknown error occured");
18
+ Object.assign(this, error);
19
+ }
20
+ }
21
+
22
+ export class ArgumentError extends Error {
23
+ constructor(name: string) {
24
+ super(`Argument '${name}' is required but was not present`);
25
+ }
26
+ }
27
+
28
+ export function requireArgs(args: object) {
29
+ for (const key in args) {
30
+ if (!args[key]) throw new ArgumentError(key);
31
+ }
32
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./client";
2
+ export * from "./error";
3
+ export * from "./options";
@@ -0,0 +1,9 @@
1
+ export const DEFAULT_HOST = "webflow.com";
2
+ export const USER_AGENT = "Webflow Javascript SDK / 1.0";
3
+
4
+ export interface Options {
5
+ host?: string;
6
+ token?: string;
7
+ version?: string;
8
+ headers?: Record<string, string>;
9
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { Webflow } from "./webflow";
2
+
3
+ export = Webflow;