Skip to content

Commit bb269ca

Browse files
author
John Agan
authored
V1.1.2 (#71)
* cleaned-up tests * add + corrected membership endpoints
1 parent 0084d68 commit bb269ca

31 files changed

+564
-483
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ const updatedItem = await webflow.updateItem({
207207

208208
### Memberships
209209
```javascript
210-
// Get the all users for a site
210+
// Get a site's users from the site
211+
const users = await site.users();
212+
213+
// Get a site's users with a site id
211214
const users = await webflow.users({
212215
siteId: "[SITE ID]"
213216
});
@@ -217,6 +220,14 @@ const user = await site.user({
217220
siteId: "[SITE ID]",
218221
userId: "[USER ID]"
219222
});
223+
224+
// Get a site's access groups
225+
const accessGroups = await site.accessGroups();
226+
227+
// Get a site's access groups with a site id
228+
const accessGroups = await webflow.accessGroups({
229+
siteId: "[SITE ID]"
230+
});
220231
```
221232

222233
### Webhooks

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "webflow-api",
33
"description": "Webflow's official Node.js SDK for Data APIs",
4-
"version": "1.1.1",
4+
"version": "1.1.2",
55
"types": "dist/index.d.ts",
66
"main": "dist/index.js",
77
"contributors": [

src/api/item.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Client, PaginatedData, QueryString, requireArgs } from "../core";
1+
import { Client, PaginatedData, requireArgs } from "../core";
22

33
/**************************************************************
44
* Interfaces

src/api/membership.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Client, PaginatedData, QueryString, requireArgs } from "../core";
1+
import { Client, PaginatedData, requireArgs } from "../core";
22

33
/**************************************************************
44
* Interfaces
@@ -11,6 +11,14 @@ export interface IUser {
1111
data: any;
1212
}
1313

14+
export interface IAccessGroup {
15+
_id: string;
16+
name: string;
17+
shortId: string;
18+
slug: string;
19+
createdOn: string;
20+
}
21+
1422
export interface IUserDelete {
1523
deleted: number;
1624
}
@@ -22,6 +30,10 @@ export type PaginatedUsers = PaginatedData & {
2230
users: IUser[];
2331
};
2432

33+
export type PaginatedAccessGroups = PaginatedData & {
34+
accessGroups: IAccessGroup[];
35+
};
36+
2537
export type UserIdParam = { siteId: string; userId: string };
2638

2739
/**************************************************************
@@ -78,9 +90,8 @@ export function update(
7890
{
7991
siteId,
8092
userId,
81-
data,
93+
...data
8294
}: {
83-
data: object;
8495
siteId: string;
8596
userId: string;
8697
}
@@ -123,3 +134,22 @@ export function remove(
123134
const path = `/sites/${siteId}/users/${userId}`;
124135
return client.delete<IUserDelete>(path);
125136
}
137+
138+
/**
139+
* Get a list of Access Groups
140+
* @param client The Webflow client
141+
* @param params The params for the request
142+
* @param params.siteId The Site ID
143+
* @param params.limit The number of items to return (optional)
144+
* @param params.offset The number of items to skip (optional)
145+
* @returns A list of Access Groups
146+
*/
147+
export function accessGroups(
148+
client: Client,
149+
{ siteId, limit, offset }: { siteId: string; limit?: number; offset?: number }
150+
) {
151+
requireArgs({ siteId });
152+
const params = { limit, offset };
153+
const path = `/sites/${siteId}/users/accessgroups`;
154+
return client.get<PaginatedAccessGroups>(path, { params });
155+
}

src/core/client.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,32 @@ export interface PaginatedData {
1717
total: number;
1818
}
1919

20-
export interface WebflowOptions {
21-
host?: string;
22-
token?: string;
23-
version?: string;
24-
headers?: Record<string, string>;
20+
/**************************************************************
21+
* Functions
22+
**************************************************************/
23+
24+
/**
25+
* Transforms JSON response to an object
26+
* if the response is a Webflow error, it will throw an error
27+
* @param data JSON response
28+
* @returns response object
29+
*/
30+
function transformResponse(data: any = {}) {
31+
// parse json if string
32+
if (String(data) === data) data = JSON.parse(data);
33+
34+
// throw an error if Webflow returns an error obejct
35+
if (data.err) throw new RequestError(data);
36+
return data;
37+
}
38+
39+
/**
40+
* Transforms POST/PUT/PATCH request data to JSON
41+
* @param data A JavaScript object
42+
* @returns JSON string
43+
*/
44+
function transformRequest(data: any = {}) {
45+
return JSON.stringify(data);
2546
}
2647

2748
/**************************************************************
@@ -35,7 +56,8 @@ export class Client extends Axios {
3556
token,
3657
}: Options = {}) {
3758
super({
38-
transformRequest: [(data) => JSON.stringify(data)],
59+
transformRequest: [transformRequest],
60+
transformResponse: [transformResponse],
3961
baseURL: `https://api.${host}/`,
4062
headers: {
4163
"Content-Type": "application/json",
@@ -46,22 +68,6 @@ export class Client extends Axios {
4668
});
4769

4870
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;
6571
}
6672

6773
// set the Authorization header

src/webflow.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ export class Webflow {
265265
* @param params The Item information
266266
* @param params.collectionId The Collection ID
267267
* @param params.itemId The Item ID
268-
* @param query The query parameters (optional)
269268
* @returns The updated Collection Item
270269
*/
271270
updateItem({
@@ -319,8 +318,7 @@ export class Webflow {
319318
* @param params The Item information
320319
* @param params.collectionId The Collection ID
321320
* @param params.itemId The Item ID
322-
* @param query The query parameters (optional)
323-
* @param query.live Update the live version
321+
* @param params.live Update the live version
324322
* @returns The unpublished Collection Item result
325323
*/
326324
deleteItems({
@@ -340,8 +338,7 @@ export class Webflow {
340338
* @param params The Item information
341339
* @param params.collectionId The Collection ID
342340
* @param params.itemId The Item ID
343-
* @param query The query parameters (optional)
344-
* @param query.live Update the live version
341+
* @param params.live Update the live version
345342
* @returns The Published Collection Item result
346343
*/
347344
publishItems({
@@ -366,6 +363,8 @@ export class Webflow {
366363
* @param params The Site information
367364
* @param params.siteId The Site ID
368365
* @param pageParams The pagination information (optional)
366+
* @param pageParams.limit The number of results to return
367+
* @param pageParams.offset The number of results to skip
369368
* @returns A list of User accounts
370369
*/
371370
async users({ siteId }: { siteId: string }, pageParams?: PaginationFilter) {
@@ -425,6 +424,16 @@ export class Webflow {
425424
return MembershipWrapper.remove(this.client, { siteId, userId });
426425
}
427426

427+
/**
428+
* Get a list of Access Groups
429+
* @param params The Site and User information
430+
* @param params.siteId The Site ID
431+
* @returns The result from the remove request
432+
*/
433+
accessGroups({ siteId }: { siteId: string }) {
434+
return MembershipWrapper.accessGroups(this.client, { siteId });
435+
}
436+
428437
/**************************************************************
429438
* Webhook Endpoints
430439
**************************************************************/

src/wrapper/collection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ResponseWrapper, ItemWrapper } from ".";
22
import { Client, QueryString } from "../core";
3-
import { Collection, Item } from "../api";
3+
import { Collection } from "../api";
44

55
export class CollectionWrapper implements Collection.ICollection {
66
fields: Collection.CollectionField[];

src/wrapper/membership.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class MembershipWrapper implements Membership.IUser {
7171
client: Client,
7272
{ userId, siteId, data }: { userId: string; siteId: string; data: any }
7373
) {
74-
const res = await Membership.update(client, { userId, siteId, data });
74+
const res = await Membership.update(client, { userId, siteId, ...data });
7575
const user = ResponseWrapper<typeof res.data>(res);
7676
return new MembershipWrapper(client, siteId, user);
7777
}
@@ -92,6 +92,31 @@ export class MembershipWrapper implements Membership.IUser {
9292
return ResponseWrapper<typeof res.data>(res);
9393
}
9494

95+
/**
96+
* Get a list of Access Groups
97+
* @param client The Webflow client
98+
* @param params The params for the request
99+
* @param params.siteId The Site ID
100+
* @param params.limit The number of items to return (optional)
101+
* @param params.offset The number of items to skip (optional)
102+
* @returns A list of Access Groups
103+
*/
104+
static async accessGroups(
105+
client: Client,
106+
{
107+
siteId,
108+
limit,
109+
offset,
110+
}: { siteId: string; limit?: number; offset?: number }
111+
) {
112+
const res = await Membership.accessGroups(client, {
113+
siteId,
114+
limit,
115+
offset,
116+
});
117+
return ResponseWrapper<typeof res.data>(res);
118+
}
119+
95120
/**
96121
* Remove a User
97122
* @param client The Webflow client

src/wrapper/site.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CollectionWrapper,
44
ResponseWrapper,
55
WebhookWrapper,
6+
MembershipWrapper,
67
} from ".";
78
import { Client, QueryString } from "../core";
89
import { Site } from "../api";
@@ -161,4 +162,67 @@ export class SiteWrapper implements Site.ISite {
161162
const _params = { url, siteId: this._id, triggerType, filter };
162163
return WebhookWrapper.create(this.client, _params);
163164
}
165+
166+
/**
167+
* Get a list of Users
168+
* @param params The params for the request
169+
* @param params.limit The number of items to return (optional)
170+
* @param params.offset The number of items to skip (optional)
171+
* @returns A list of Users
172+
*/
173+
async users({ limit, offset }: { limit?: number; offset?: number } = {}) {
174+
return MembershipWrapper.list(this.client, {
175+
siteId: this._id,
176+
limit,
177+
offset,
178+
});
179+
}
180+
181+
/**
182+
* Get a single User
183+
* @param params The params for the request
184+
* @param params.userId The user ID
185+
* @returns A single User
186+
*/
187+
async user({ userId }: { userId: string }) {
188+
return MembershipWrapper.getOne(this.client, { siteId: this._id, userId });
189+
}
190+
191+
/**
192+
* Invite a User to a site
193+
* @param params The params for the request
194+
* @param params.email The email address of the user to invite
195+
* @returns The newly created User
196+
*/
197+
async inviteUser({ email }: { email: string }) {
198+
return MembershipWrapper.invite(this.client, { siteId: this._id, email });
199+
}
200+
201+
/**
202+
* Get a list of Access Groups
203+
* @param params The params for the request
204+
* @param params.limit The number of items to return (optional)
205+
* @param params.offset The number of items to skip (optional)
206+
* @returns A list of Access Groups
207+
*/
208+
async accessGroups({
209+
limit,
210+
offset,
211+
}: { limit?: number; offset?: number } = {}) {
212+
return MembershipWrapper.accessGroups(this.client, {
213+
siteId: this._id,
214+
limit,
215+
offset,
216+
});
217+
}
218+
219+
/**
220+
* Remove a User
221+
* @param params The params for the request
222+
* @param params.userId The user ID
223+
* @returns The result of the remove
224+
*/
225+
async removeUser({ userId }: { userId: string }) {
226+
return MembershipWrapper.remove(this.client, { siteId: this._id, userId });
227+
}
164228
}

src/wrapper/webhook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ResponseWrapper } from ".";
21
import { Client, QueryString } from "../core";
2+
import { ResponseWrapper } from ".";
33
import { Webhook } from "../api";
44

55
export type CreateWebhookParams = {

0 commit comments

Comments
 (0)