Skip to content

Commit ea0e31a

Browse files
author
John Agan
authored
refactored wrapper logic into single model (#73)
* refactored wrapper logic into single model * updated test path in npmignore
1 parent bb269ca commit ea0e31a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2003
-2920
lines changed

.npmignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
src/
2-
test/
1+
tests/
2+
src/

package.json

Lines changed: 2 additions & 4 deletions
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.2",
4+
"version": "1.2.0",
55
"types": "dist/index.d.ts",
66
"main": "dist/index.js",
77
"contributors": [
@@ -20,15 +20,13 @@
2020
],
2121
"scripts": {
2222
"build": "yarn clean && tsc -p ./",
23-
"lint": "eslint . --ext .ts,.tsx",
23+
"lint": "eslint . --ext .ts",
2424
"prepublish": "yarn build",
2525
"watch": "tsc -watch -p ./",
2626
"clean": "rm -rf dist",
2727
"test": "jest"
2828
},
2929
"devDependencies": {
30-
"@jest/globals": "^29.2.2",
31-
"@types/isomorphic-fetch": "^0.0.36",
3230
"@types/jest": "^29.2.1",
3331
"@types/node": "^18.11.9",
3432
"@typescript-eslint/eslint-plugin": "^5.42.0",

src/api/collection.ts

Lines changed: 102 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Client, QueryString, requireArgs } from "../core";
1+
import { AxiosInstance } from "axios";
2+
import { requireArgs, WebflowRecord } from "../core";
3+
import { Item } from ".";
24

35
/**************************************************************
46
* Types
@@ -47,41 +49,106 @@ export interface ICollection {
4749
}
4850

4951
/**************************************************************
50-
* Functions
52+
* Class
5153
**************************************************************/
54+
export class Collection extends WebflowRecord<ICollection> implements ICollection {
55+
fields: CollectionField[];
56+
singularName: string;
57+
lastUpdated: string;
58+
createdOn: string;
59+
_id: string;
60+
name: string;
61+
slug: string;
5262

53-
/**
54-
* Get a list of Collections
55-
* @param client The Webflow client
56-
* @param params1 The params for the request
57-
* @param params1.siteId The site ID
58-
* @param params The query string parameters (optional)
59-
* @returns A list of Collections
60-
*/
61-
export function list(
62-
client: Client,
63-
{ siteId }: { siteId: string },
64-
params?: QueryString
65-
) {
66-
requireArgs({ siteId });
67-
const path = `/sites/${siteId}/collections`;
68-
return client.get<ICollection[]>(path, { params });
69-
}
63+
/**************************************************************
64+
* Static Methods
65+
**************************************************************/
66+
67+
/**
68+
* Get a list of Collections
69+
* @param params The params for the request
70+
* @param params.siteId The site ID
71+
* @param client The Axios client instance
72+
* @returns A list of Collections
73+
*/
74+
static list({ siteId }: { siteId: string }, client: AxiosInstance) {
75+
requireArgs({ siteId });
76+
const path = `/sites/${siteId}/collections`;
77+
return client.get<ICollection[]>(path);
78+
}
79+
80+
/**
81+
* Get a single Collection
82+
* @param params The params for the request
83+
* @param params.collectionId The collection ID
84+
* @param client The Axios client instance
85+
* @returns A single Collection
86+
*/
87+
static getOne({ collectionId }: { collectionId: string }, client: AxiosInstance) {
88+
requireArgs({ collectionId });
89+
const path = `/collections/${collectionId}`;
90+
return client.get<ICollection>(path);
91+
}
92+
93+
/**************************************************************
94+
* Instance Methods
95+
**************************************************************/
96+
97+
/**
98+
* Get a single Item
99+
* @param params The params for the request
100+
* @param params.itemId The Item ID
101+
* @returns A single Item
102+
*/
103+
async item({ itemId }: { itemId: string }) {
104+
const res = await Item.getOne({ itemId, collectionId: this._id }, this.client);
105+
const [item] = res.data.items.map((data) => new Item(this.client, { ...res, data }));
106+
return item;
107+
}
108+
109+
/**
110+
* Get a list of Items
111+
* @param params The params for the request
112+
* @param params.limit The number of items to return (optional)
113+
* @param params.offset The number of items to skip (optional)
114+
* @returns A list of Items
115+
*/
116+
async items({ limit, offset }: { limit?: number; offset?: number } = {}) {
117+
const res = await Item.list({ collectionId: this._id, limit, offset }, this.client);
118+
return res.data.items.map((data) => new Item(this.client, { ...res, data }));
119+
}
120+
121+
/**
122+
* Remove a single Item
123+
* @param params The params for the request
124+
* @param params.itemId The Item ID
125+
* @returns The result from the removal
126+
*/
127+
async removeItem({ itemId }: { itemId: string }) {
128+
const res = await Item.remove({ itemId, collectionId: this._id }, this.client);
129+
return res.data;
130+
}
131+
132+
/**
133+
* Create a new Item
134+
* @param fields The Item fields to create
135+
* @returns The created Item
136+
*/
137+
async createItem(fields: any) {
138+
const res = await Item.create({ collectionId: this._id, fields }, this.client);
139+
return new Item(this.client, res);
140+
}
70141

71-
/**
72-
* Get a single Collection
73-
* @param client The Webflow client
74-
* @param params The params for the request
75-
* @param params.collectionId The collection ID
76-
* @param params.params The query string parameters (optional)
77-
* @returns A single Collection
78-
*/
79-
export function getOne(
80-
client: Client,
81-
{ collectionId }: { collectionId: string },
82-
params?: QueryString
83-
) {
84-
requireArgs({ collectionId });
85-
const path = `/collections/${collectionId}`;
86-
return client.get<ICollection>(path, { params });
142+
/**
143+
* Update a single Item
144+
* @param params The params for the request
145+
* @param params.itemId The Item ID
146+
* @param params.fields The fields to update
147+
* @returns The updated Item
148+
*/
149+
async updateItem({ itemId, fields }: { itemId: string; fields: any }) {
150+
const params = { itemId, collectionId: this._id, fields };
151+
const res = await Item.update(params, this.client);
152+
return new Item(this.client, res);
153+
}
87154
}

src/api/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export * as Membership from "./membership";
2-
export * as Collection from "./collection";
3-
export * as Webhook from "./webhook";
4-
export * as OAuth from "./oauth";
5-
export * as Item from "./item";
6-
export * as Site from "./site";
7-
export * as Meta from "./meta";
1+
export * from "./collection";
2+
export * from "./user";
3+
export * from "./webhook";
4+
export * from "./item";
5+
export * from "./site";
6+
export * from "./oauth";
7+
export * from "./meta";

0 commit comments

Comments
 (0)