zitejs 0.9.39 → 0.9.40

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.
@@ -0,0 +1,64 @@
1
+ const TOKEN_PREFIXES = ["sk_prod_", "fillout_token_"];
2
+ function isValidToken(token) {
3
+ return TOKEN_PREFIXES.some((prefix) => token.startsWith(prefix));
4
+ }
5
+ const ziteError = async (verb, res) => {
6
+ const ct = res.headers.get("Content-Type")?.split(";")[0];
7
+ if (ct === "application/json") {
8
+ const body = await res.json();
9
+ if (body?.error?.message)
10
+ return new Error(body.error.message);
11
+ }
12
+ return new Error(`Failed to ${verb} with status code ${res.status}`);
13
+ };
14
+ export class Zite {
15
+ token;
16
+ baseUrl;
17
+ constructor(token) {
18
+ if (typeof token !== "string" || !isValidToken(token)) {
19
+ throw new Error("Invalid Zite API key. Visit https://build.fillout.com/home/settings/developer to create one.");
20
+ }
21
+ this.token = token;
22
+ this.baseUrl = "https://tables.fillout.com/api/v1";
23
+ }
24
+ request = async (method, url, verb, body) => {
25
+ const res = await fetch(this.baseUrl + url, {
26
+ method,
27
+ headers: {
28
+ Authorization: `Bearer ${this.token}`,
29
+ ...(body ? { "Content-Type": "application/json" } : {}),
30
+ },
31
+ body: body ? JSON.stringify(body) : undefined,
32
+ });
33
+ if (!res.ok)
34
+ throw await ziteError(verb, res);
35
+ const data = await res.json();
36
+ return data;
37
+ };
38
+ record = {
39
+ create: (databaseId, tableId, record) => this.request("POST", `/bases/${e(databaseId)}/tables/${e(tableId)}/records`, "create record", { record }),
40
+ bulkCreate: (databaseId, tableId, records, matchOn) => this.request("POST", `/bases/${e(databaseId)}/tables/${e(tableId)}/records/bulk`, "bulk create records", { records, matchOn }),
41
+ get: (databaseId, tableId, recordId) => this.request("GET", `/bases/${e(databaseId)}/tables/${e(tableId)}/records/${e(recordId)}`, "get record"),
42
+ update: (databaseId, tableId, recordId, record) => this.request("PATCH", `/bases/${e(databaseId)}/tables/${e(tableId)}/records/${e(recordId)}`, "update record", { record }),
43
+ bulkUpdate: (databaseId, tableId, records) => this.request("PUT", `/bases/${e(databaseId)}/tables/${e(tableId)}/records/bulk`, "bulk update records", { records }),
44
+ delete: (databaseId, tableId, recordId) => this.request("DELETE", `/bases/${e(databaseId)}/tables/${e(tableId)}/records/${e(recordId)}`, "delete record"),
45
+ list: (databaseId, tableId, options) => this.request("POST", `/bases/${e(databaseId)}/tables/${e(tableId)}/records/list`, "list records", options),
46
+ };
47
+ field = {
48
+ create: (databaseId, tableId, field) => this.request("POST", `/bases/${e(databaseId)}/tables/${e(tableId)}/fields`, "create field", field),
49
+ update: (databaseId, tableId, fieldId, field) => this.request("PATCH", `/bases/${e(databaseId)}/tables/${e(tableId)}/fields/${e(fieldId)}`, "update field", field),
50
+ delete: (databaseId, tableId, fieldId) => this.request("DELETE", `/bases/${e(databaseId)}/tables/${e(tableId)}/fields/${e(fieldId)}`, "delete field"),
51
+ };
52
+ table = {
53
+ create: (databaseId, table) => this.request("POST", `/bases/${e(databaseId)}/tables`, "create table", table),
54
+ update: (databaseId, tableId, table) => this.request("PATCH", `/bases/${e(databaseId)}/tables/${e(tableId)}`, "update table", table),
55
+ delete: (databaseId, tableId) => this.request("DELETE", `/bases/${e(databaseId)}/tables/${e(tableId)}`, "delete table"),
56
+ };
57
+ database = {
58
+ create: (database) => this.request("POST", `/bases`, "create database", database),
59
+ list: () => this.request("GET", `/bases`, "list databases"),
60
+ get: (databaseId) => this.request("GET", `/bases/${e(databaseId)}`, "get database"),
61
+ delete: (databaseId) => this.request("DELETE", `/bases/${e(databaseId)}`, "delete database"),
62
+ };
63
+ }
64
+ const e = encodeURIComponent;
@@ -0,0 +1,3 @@
1
+ export { Zite } from "./client/index.js";
2
+ export type { Record, RecordListOutput, RecordListOptions, FieldCreateInput, FieldUpdateInput, TableCreateInput, TableUpdateInput, DatabaseCreateInput, DatabaseListOutput, } from "./client/index.js";
3
+ export * from "./types/index.js";
@@ -0,0 +1,2 @@
1
+ export { Zite } from "./client/index.js";
2
+ export * from "./types/index.js";