|
1 | | -import { Client, QueryString, requireArgs } from "../core"; |
| 1 | +import { AxiosInstance } from "axios"; |
| 2 | +import { requireArgs, WebflowRecord } from "../core"; |
| 3 | +import { Item } from "."; |
2 | 4 |
|
3 | 5 | /************************************************************** |
4 | 6 | * Types |
@@ -47,41 +49,106 @@ export interface ICollection { |
47 | 49 | } |
48 | 50 |
|
49 | 51 | /************************************************************** |
50 | | - * Functions |
| 52 | + * Class |
51 | 53 | **************************************************************/ |
| 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; |
52 | 62 |
|
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 | + } |
70 | 141 |
|
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 | + } |
87 | 154 | } |
0 commit comments