Skip to content

Commit

Permalink
added entries for the users-api
Browse files Browse the repository at this point in the history
  • Loading branch information
John Agan committed Jun 16, 2022
1 parent d7f7927 commit 6ad68a7
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*]
max_line_length = 80
41 changes: 40 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@ declare class Webflow {
query?: Webflow.WebflowQueryArg
): Promise<Webflow.WebflowApiModel.Collection>;

// Users
users(
data: {
siteId: string;
},
query?: Webflow.WebflowQueryArg
): Promise<Webflow.WebflowApiModel.User[]>;

user(
data: {
siteId: string;
userId: string;
},
query?: Webflow.WebflowQueryArg
): Promise<Webflow.WebflowApiModel.User>;

updateUser(
data: { siteId: string; userId: string } & Record<string, any>,
query?: Webflow.WebflowQueryArg
): Promise<Webflow.WebflowApiModel.User>;

removeUser(
data: { siteId: string; userId: string },
query?: Webflow.WebflowQueryArg
): Promise<{ deleted: number }>;

inviteUser(
data: { siteId: string; email: string },
query?: Webflow.WebflowQueryArg
): Promise<Webflow.WebflowApiModel.User>;

// Items

items(
Expand Down Expand Up @@ -213,6 +244,14 @@ declare namespace Webflow {
name: string;
}

interface User {
_id: string;
lastUpdated: string;
createdOn: string;
emailVerified: boolean;
fields: object;
}

/**
* https://developers.webflow.com/?javascript#collections
*/
Expand Down Expand Up @@ -301,7 +340,7 @@ declare namespace Webflow {
rateLimit: {
limit: number;
remaining: number;
}
};
}

interface ItemsResponse {
Expand Down
48 changes: 41 additions & 7 deletions src/ResponseWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,28 @@ export default class ResponseWrapper {

items: this.api.items.bind(this.api, { collectionId: collection._id }),
item(first, ...rest) {
return this.api.item({ ...first, collectionId: collection._id }, ...rest);
return this.api.item(
{ ...first, collectionId: collection._id },
...rest
);
},
createItem(first, ...rest) {
return this.api.createItem({ ...first, collectionId: collection._id }, ...rest);
return this.api.createItem(
{ ...first, collectionId: collection._id },
...rest
);
},
updateItem(first, ...rest) {
return this.api.updateItem({ ...first, collectionId: collection._id }, ...rest);
return this.api.updateItem(
{ ...first, collectionId: collection._id },
...rest
);
},
removeItem(first, ...rest) {
return this.api.removeItem({ ...first, collectionId: collection._id }, ...rest);
return this.api.removeItem(
{ ...first, collectionId: collection._id },
...rest
);
},
};
}
Expand All @@ -56,17 +68,39 @@ export default class ResponseWrapper {
...item,

update(first, ...rest) {
return this.api.updateItem({ ...first, collectionId, itemId: item._id }, ...rest);
return this.api.updateItem(
{ ...first, collectionId, itemId: item._id },
...rest
);
},
remove: this.api.updateItem.bind(this.api, {
collectionId,
itemId: item._id,
}),
};
}

user(user, siteId) {
return {
...user,

update(first, ...rest) {
return this.api.updateUser({ ...first, siteId }, ...rest);
},
remove(first, ...rest) {
return this.api.removeUser({ ...first, siteId }, ...rest);
},
remove: this.api.updateItem.bind(this.api, { collectionId, itemId: item._id }),
};
}

webhook(webhook, siteId) {
return {
...webhook,

remove: this.api.removeWebhook.bind(this.api, { siteId, webhookId: webhook._id }),
remove: this.api.removeWebhook.bind(this.api, {
siteId,
webhookId: webhook._id,
}),
};
}
}
114 changes: 89 additions & 25 deletions src/Webflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export default class Webflow {
};

this.authenticatedFetch = (method, path, data, query) => {
const queryString = query && !isObjectEmpty(query) ? `?${qs.stringify(query)}` : "";
const queryString =
query && !isObjectEmpty(query) ? `?${qs.stringify(query)}` : "";

const uri = `${this.endpoint}${path}${queryString}`;
const opts = {
Expand Down Expand Up @@ -116,13 +117,17 @@ export default class Webflow {
// Sites

sites(query = {}) {
return this.get("/sites", query).then((sites) => sites.map((site) => this.responseWrapper.site(site)));
return this.get("/sites", query).then((sites) =>
sites.map((site) => this.responseWrapper.site(site))
);
}

site({ siteId }, query = {}) {
if (!siteId) return Promise.reject(buildRequiredArgError("siteId"));

return this.get(`/sites/${siteId}`, query).then((site) => this.responseWrapper.site(site));
return this.get(`/sites/${siteId}`, query).then((site) =>
this.responseWrapper.site(site)
);
}

publishSite({ siteId, domains }) {
Expand All @@ -148,12 +153,15 @@ export default class Webflow {
if (!siteId) return Promise.reject(buildRequiredArgError("siteId"));

return this.get(`/sites/${siteId}/collections`, query).then((collections) =>
collections.map((collection) => this.responseWrapper.collection(collection))
collections.map((collection) =>
this.responseWrapper.collection(collection)
)
);
}

collection({ collectionId }, query = {}) {
if (!collectionId) return Promise.reject(buildRequiredArgError("collectionId"));
if (!collectionId)
return Promise.reject(buildRequiredArgError("collectionId"));

return this.get(`/collections/${collectionId}`, query).then((collection) =>
this.responseWrapper.collection(collection)
Expand All @@ -163,56 +171,112 @@ export default class Webflow {
// Items

items({ collectionId }, query = {}) {
if (!collectionId) return Promise.reject(buildRequiredArgError("collectionId"));
if (!collectionId)
return Promise.reject(buildRequiredArgError("collectionId"));

return this.get(`/collections/${collectionId}/items`, query).then((res) => ({
...res,
return this.get(`/collections/${collectionId}/items`, query).then(
(res) => ({
...res,

items: res.items.map((item) => this.responseWrapper.item(item, collectionId)),
}));
items: res.items.map((item) =>
this.responseWrapper.item(item, collectionId)
),
})
);
}

item({ collectionId, itemId }, query = {}) {
if (!collectionId) return Promise.reject(buildRequiredArgError("collectionId"));
if (!collectionId)
return Promise.reject(buildRequiredArgError("collectionId"));
if (!itemId) return Promise.reject(buildRequiredArgError("itemId"));

return this.get(`/collections/${collectionId}/items/${itemId}`, query).then((res) =>
this.responseWrapper.item(res.items[0], collectionId)
return this.get(`/collections/${collectionId}/items/${itemId}`, query).then(
(res) => this.responseWrapper.item(res.items[0], collectionId)
);
}

createItem({ collectionId, ...data }, query = {}) {
if (!collectionId) return Promise.reject(buildRequiredArgError("collectionId"));
if (!collectionId)
return Promise.reject(buildRequiredArgError("collectionId"));

return this.post(`/collections/${collectionId}/items`, data, query).then((item) =>
this.responseWrapper.item(item, collectionId)
return this.post(`/collections/${collectionId}/items`, data, query).then(
(item) => this.responseWrapper.item(item, collectionId)
);
}

updateItem({ collectionId, itemId, ...data }, query = {}) {
if (!collectionId) return Promise.reject(buildRequiredArgError("collectionId"));
if (!collectionId)
return Promise.reject(buildRequiredArgError("collectionId"));
if (!itemId) return Promise.reject(buildRequiredArgError("itemId"));

return this.put(`/collections/${collectionId}/items/${itemId}`, data, query);
return this.put(
`/collections/${collectionId}/items/${itemId}`,
data,
query
);
}

removeItem({ collectionId, itemId }, query = {}) {
if (!collectionId) return Promise.reject(buildRequiredArgError("collectionId"));
if (!collectionId)
return Promise.reject(buildRequiredArgError("collectionId"));
if (!itemId) return Promise.reject(buildRequiredArgError("itemId"));

return this.delete(`/collections/${collectionId}/items/${itemId}`, query);
}

patchItem({ collectionId, itemId, ...data }, query = {}) {
if (!collectionId) return Promise.reject(buildRequiredArgError("collectionId"));
if (!collectionId)
return Promise.reject(buildRequiredArgError("collectionId"));
if (!itemId) return Promise.reject(buildRequiredArgError("itemId"));

return this.patch(`/collections/${collectionId}/items/${itemId}`, data, query);
return this.patch(
`/collections/${collectionId}/items/${itemId}`,
data,
query
);
}

// Users

users({ siteId }, query = {}) {
if (!siteId) return Promise.reject(buildRequiredArgError("siteId"));

return this.get(`/sites/${siteId}/users`, query).then((res) =>
res.users.map((user) => this.responseWrapper.user(user))
);
}

user({ siteId, userId }, query = {}) {
if (!siteId) return Promise.reject(buildRequiredArgError("siteId"));
if (!userId) return Promise.reject(buildRequiredArgError("userId"));

return this.get(`/sites/${siteId}/users/${userId}`, query).then((user) =>
this.responseWrapper.user(user, siteId)
);
}

updateUser({ siteId, userId, ...data }, query = {}) {
if (!siteId) return Promise.reject(buildRequiredArgError("siteId"));
if (!userId) return Promise.reject(buildRequiredArgError("userId"));

return this.patch(`/sites/${siteId}/users/${userId}`, data, query);
}

inviteUser({ siteId, email }, query = {}) {
if (!siteId) return Promise.reject(buildRequiredArgError("siteId"));
if (!email) return Promise.reject(buildRequiredArgError("email"));

return this.post(`/sites/${siteId}/users/invite`, { email }, query).then(
(user) => this.responseWrapper.user(user, siteId)
);
}

// Images
removeUser({ siteId, userId }, query = {}) {
if (!siteId) return Promise.reject(buildRequiredArgError("siteId"));
if (!userId) return Promise.reject(buildRequiredArgError("userId"));

// TODO
return this.delete(`/sites/${siteId}/users/${userId}`, query);
}

// Webhooks

Expand All @@ -228,8 +292,8 @@ export default class Webflow {
if (!siteId) return Promise.reject(buildRequiredArgError("siteId"));
if (!webhookId) return Promise.reject(buildRequiredArgError("webhookId"));

return this.get(`/sites/${siteId}/webhooks/${webhookId}`, query).then((webhook) =>
this.responseWrapper.webhook(webhook, siteId)
return this.get(`/sites/${siteId}/webhooks/${webhookId}`, query).then(
(webhook) => this.responseWrapper.webhook(webhook, siteId)
);
}

Expand Down

0 comments on commit 6ad68a7

Please sign in to comment.