@zapier/zapier-sdk 0.13.5 → 0.13.7
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.
- package/CHANGELOG.md +15 -0
- package/dist/api/schemas.d.ts +38 -38
- package/dist/index.cjs +1 -1
- package/dist/index.d.mts +170 -177
- package/dist/index.mjs +1 -1
- package/dist/plugins/fetch/schemas.d.ts +4 -4
- package/dist/plugins/getAction/schemas.d.ts +2 -2
- package/dist/plugins/listActions/schemas.d.ts +6 -6
- package/dist/plugins/listApps/schemas.d.ts +6 -6
- package/dist/plugins/listAuthentications/schemas.d.ts +4 -4
- package/dist/plugins/listInputFieldChoices/schemas.d.ts +8 -8
- package/dist/plugins/listInputFields/schemas.d.ts +8 -8
- package/dist/plugins/request/schemas.d.ts +8 -8
- package/dist/plugins/runAction/schemas.d.ts +8 -8
- package/dist/schemas/Action.d.ts +2 -2
- package/dist/schemas/App.d.ts +8 -8
- package/dist/types/sdk.d.ts +2 -8
- package/dist/types/sdk.d.ts.map +1 -1
- package/package.json +8 -3
- package/src/api/auth.ts +0 -28
- package/src/api/client.ts +0 -491
- package/src/api/debug.test.ts +0 -76
- package/src/api/debug.ts +0 -154
- package/src/api/index.ts +0 -90
- package/src/api/polling.test.ts +0 -405
- package/src/api/polling.ts +0 -253
- package/src/api/schemas.ts +0 -465
- package/src/api/types.ts +0 -152
- package/src/auth.ts +0 -72
- package/src/constants.ts +0 -16
- package/src/index.ts +0 -111
- package/src/plugins/api/index.ts +0 -43
- package/src/plugins/apps/index.ts +0 -203
- package/src/plugins/apps/schemas.ts +0 -64
- package/src/plugins/eventEmission/builders.ts +0 -115
- package/src/plugins/eventEmission/index.test.ts +0 -169
- package/src/plugins/eventEmission/index.ts +0 -294
- package/src/plugins/eventEmission/transport.test.ts +0 -214
- package/src/plugins/eventEmission/transport.ts +0 -135
- package/src/plugins/eventEmission/types.ts +0 -58
- package/src/plugins/eventEmission/utils.ts +0 -121
- package/src/plugins/fetch/index.ts +0 -83
- package/src/plugins/fetch/schemas.ts +0 -37
- package/src/plugins/findFirstAuthentication/index.test.ts +0 -209
- package/src/plugins/findFirstAuthentication/index.ts +0 -68
- package/src/plugins/findFirstAuthentication/schemas.ts +0 -47
- package/src/plugins/findUniqueAuthentication/index.test.ts +0 -197
- package/src/plugins/findUniqueAuthentication/index.ts +0 -77
- package/src/plugins/findUniqueAuthentication/schemas.ts +0 -49
- package/src/plugins/getAction/index.test.ts +0 -239
- package/src/plugins/getAction/index.ts +0 -75
- package/src/plugins/getAction/schemas.ts +0 -41
- package/src/plugins/getApp/index.test.ts +0 -181
- package/src/plugins/getApp/index.ts +0 -60
- package/src/plugins/getApp/schemas.ts +0 -33
- package/src/plugins/getAuthentication/index.test.ts +0 -294
- package/src/plugins/getAuthentication/index.ts +0 -95
- package/src/plugins/getAuthentication/schemas.ts +0 -38
- package/src/plugins/getProfile/index.ts +0 -60
- package/src/plugins/getProfile/schemas.ts +0 -24
- package/src/plugins/listActions/index.test.ts +0 -526
- package/src/plugins/listActions/index.ts +0 -132
- package/src/plugins/listActions/schemas.ts +0 -55
- package/src/plugins/listApps/index.test.ts +0 -378
- package/src/plugins/listApps/index.ts +0 -159
- package/src/plugins/listApps/schemas.ts +0 -41
- package/src/plugins/listAuthentications/index.test.ts +0 -739
- package/src/plugins/listAuthentications/index.ts +0 -152
- package/src/plugins/listAuthentications/schemas.ts +0 -77
- package/src/plugins/listInputFieldChoices/index.test.ts +0 -653
- package/src/plugins/listInputFieldChoices/index.ts +0 -173
- package/src/plugins/listInputFieldChoices/schemas.ts +0 -125
- package/src/plugins/listInputFields/index.test.ts +0 -439
- package/src/plugins/listInputFields/index.ts +0 -294
- package/src/plugins/listInputFields/schemas.ts +0 -68
- package/src/plugins/manifest/index.test.ts +0 -776
- package/src/plugins/manifest/index.ts +0 -461
- package/src/plugins/manifest/schemas.ts +0 -60
- package/src/plugins/registry/index.ts +0 -160
- package/src/plugins/request/index.test.ts +0 -333
- package/src/plugins/request/index.ts +0 -105
- package/src/plugins/request/schemas.ts +0 -69
- package/src/plugins/runAction/index.test.ts +0 -388
- package/src/plugins/runAction/index.ts +0 -215
- package/src/plugins/runAction/schemas.ts +0 -60
- package/src/resolvers/actionKey.ts +0 -37
- package/src/resolvers/actionType.ts +0 -34
- package/src/resolvers/appKey.ts +0 -7
- package/src/resolvers/authenticationId.ts +0 -54
- package/src/resolvers/index.ts +0 -11
- package/src/resolvers/inputFieldKey.ts +0 -70
- package/src/resolvers/inputs.ts +0 -69
- package/src/schemas/Action.ts +0 -52
- package/src/schemas/App.ts +0 -45
- package/src/schemas/Auth.ts +0 -59
- package/src/schemas/Field.ts +0 -169
- package/src/schemas/Run.ts +0 -40
- package/src/schemas/UserProfile.ts +0 -60
- package/src/sdk.test.ts +0 -212
- package/src/sdk.ts +0 -178
- package/src/types/domain.test.ts +0 -50
- package/src/types/domain.ts +0 -66
- package/src/types/errors.ts +0 -278
- package/src/types/events.ts +0 -43
- package/src/types/functions.ts +0 -28
- package/src/types/optional-zapier-sdk-cli-login.d.ts +0 -37
- package/src/types/plugin.ts +0 -125
- package/src/types/properties.ts +0 -80
- package/src/types/sdk.ts +0 -117
- package/src/types/telemetry-events.ts +0 -85
- package/src/utils/array-utils.test.ts +0 -131
- package/src/utils/array-utils.ts +0 -41
- package/src/utils/domain-utils.test.ts +0 -433
- package/src/utils/domain-utils.ts +0 -267
- package/src/utils/file-utils.test.ts +0 -73
- package/src/utils/file-utils.ts +0 -94
- package/src/utils/function-utils.test.ts +0 -141
- package/src/utils/function-utils.ts +0 -245
- package/src/utils/pagination-utils.test.ts +0 -620
- package/src/utils/pagination-utils.ts +0 -242
- package/src/utils/schema-utils.ts +0 -207
- package/src/utils/string-utils.test.ts +0 -45
- package/src/utils/string-utils.ts +0 -54
- package/src/utils/validation.test.ts +0 -51
- package/src/utils/validation.ts +0 -44
- package/tsconfig.build.json +0 -18
- package/tsconfig.json +0 -20
- package/tsconfig.tsbuildinfo +0 -1
- package/tsup.config.ts +0 -23
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from "vitest";
|
|
2
|
-
import { resolve, writeFile, readFile } from "./file-utils";
|
|
3
|
-
|
|
4
|
-
// Mock the dynamic imports to simulate missing modules
|
|
5
|
-
vi.mock("fs/promises", () => {
|
|
6
|
-
throw new Error("Module not found");
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
vi.mock("path", () => {
|
|
10
|
-
throw new Error("Module not found");
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
describe("file-utils", () => {
|
|
14
|
-
describe("resolve", () => {
|
|
15
|
-
it("should handle absolute paths", async () => {
|
|
16
|
-
expect(await resolve("/absolute/path")).toBe("/absolute/path");
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("should handle relative paths with ./", async () => {
|
|
20
|
-
expect(await resolve("./relative/path")).toBe("/relative/path");
|
|
21
|
-
expect(await resolve("./relative/path", "/base")).toBe(
|
|
22
|
-
"/base/relative/path",
|
|
23
|
-
);
|
|
24
|
-
expect(await resolve("./relative/path", "/base/")).toBe(
|
|
25
|
-
"/base/relative/path",
|
|
26
|
-
);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it("should handle parent directory paths with ../", async () => {
|
|
30
|
-
expect(await resolve("../parent/path")).toBe("/parent/path");
|
|
31
|
-
expect(await resolve("../../nested/parent/path")).toBe(
|
|
32
|
-
"/nested/parent/path",
|
|
33
|
-
);
|
|
34
|
-
expect(await resolve("../parent/path", "/base")).toBe(
|
|
35
|
-
"/base/parent/path",
|
|
36
|
-
);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("should handle paths without prefix", async () => {
|
|
40
|
-
expect(await resolve("simple/path")).toBe("/simple/path");
|
|
41
|
-
expect(await resolve("simple/path", "/base")).toBe("/base/simple/path");
|
|
42
|
-
expect(await resolve("simple/path", "/base/")).toBe("/base/simple/path");
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
describe("writeFile and readFile with in-memory filesystem", () => {
|
|
47
|
-
it("should write and read files using in-memory filesystem when fs is not available", async () => {
|
|
48
|
-
const filePath = "/test/file.txt";
|
|
49
|
-
const content = "Hello, world!";
|
|
50
|
-
|
|
51
|
-
await writeFile(filePath, content);
|
|
52
|
-
|
|
53
|
-
const readContent = await readFile(filePath);
|
|
54
|
-
expect(readContent).toBe(content);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it("should throw error when reading non-existent file", async () => {
|
|
58
|
-
await expect(readFile("/non/existent/file.txt")).rejects.toThrow(
|
|
59
|
-
"File not found: /non/existent/file.txt",
|
|
60
|
-
);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it("should handle multiple files in in-memory filesystem", async () => {
|
|
64
|
-
await writeFile("/file1.txt", "content1");
|
|
65
|
-
await writeFile("/file2.txt", "content2");
|
|
66
|
-
await writeFile("/dir/file3.txt", "content3");
|
|
67
|
-
|
|
68
|
-
expect(await readFile("/file1.txt")).toBe("content1");
|
|
69
|
-
expect(await readFile("/file2.txt")).toBe("content2");
|
|
70
|
-
expect(await readFile("/dir/file3.txt")).toBe("content3");
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
});
|
package/src/utils/file-utils.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
// In-memory filesystem fallback for browser environments
|
|
2
|
-
const inMemoryFiles: { [path: string]: string } = {};
|
|
3
|
-
|
|
4
|
-
// Lazy-loaded modules
|
|
5
|
-
let fsPromises: any = null;
|
|
6
|
-
let pathModule: any = null;
|
|
7
|
-
|
|
8
|
-
async function loadFsPromises() {
|
|
9
|
-
if (fsPromises) return fsPromises;
|
|
10
|
-
|
|
11
|
-
try {
|
|
12
|
-
fsPromises = await import("fs/promises");
|
|
13
|
-
return fsPromises;
|
|
14
|
-
} catch {
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async function loadPathModule() {
|
|
20
|
-
if (pathModule) return pathModule;
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
pathModule = await import("path");
|
|
24
|
-
return pathModule;
|
|
25
|
-
} catch {
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export async function resolve(
|
|
31
|
-
path: string,
|
|
32
|
-
basePath: string = "/",
|
|
33
|
-
): Promise<string> {
|
|
34
|
-
const pathModule = await loadPathModule();
|
|
35
|
-
|
|
36
|
-
if (pathModule) {
|
|
37
|
-
return pathModule.resolve(path);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Simple fallback path resolution assuming root "/" location
|
|
41
|
-
if (path.startsWith("/")) {
|
|
42
|
-
return path;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (path.startsWith("./")) {
|
|
46
|
-
// Remove ./ prefix and join to base
|
|
47
|
-
const cleanPath = path.slice(2);
|
|
48
|
-
return basePath.endsWith("/")
|
|
49
|
-
? basePath + cleanPath
|
|
50
|
-
: basePath + "/" + cleanPath;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (path.startsWith("../")) {
|
|
54
|
-
// Remove ../ prefix since we're already at root
|
|
55
|
-
const cleanPath = path.replace(/^(\.\.\/)+/, "");
|
|
56
|
-
return basePath.endsWith("/")
|
|
57
|
-
? basePath + cleanPath
|
|
58
|
-
: basePath + "/" + cleanPath;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// No slash prefix, join to base
|
|
62
|
-
return basePath.endsWith("/") ? basePath + path : basePath + "/" + path;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export async function writeFile(
|
|
66
|
-
filePath: string,
|
|
67
|
-
content: string,
|
|
68
|
-
): Promise<void> {
|
|
69
|
-
const fs = await loadFsPromises();
|
|
70
|
-
|
|
71
|
-
if (fs) {
|
|
72
|
-
await fs.writeFile(filePath, content, "utf8");
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Fallback to in-memory filesystem
|
|
77
|
-
inMemoryFiles[filePath] = content;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export async function readFile(filePath: string): Promise<string> {
|
|
81
|
-
const fs = await loadFsPromises();
|
|
82
|
-
|
|
83
|
-
if (fs) {
|
|
84
|
-
return await fs.readFile(filePath, "utf8");
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Fallback to in-memory filesystem
|
|
88
|
-
const content = inMemoryFiles[filePath];
|
|
89
|
-
if (content !== undefined) {
|
|
90
|
-
return content;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
throw new Error(`File not found: ${filePath}`);
|
|
94
|
-
}
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
import { createFunction, createPaginatedFunction } from "./function-utils";
|
|
3
|
-
import { ZapierValidationError, ZapierUnknownError } from "../types/errors";
|
|
4
|
-
import { ALL_ITEMS, listTestItems } from "./pagination-utils.test";
|
|
5
|
-
|
|
6
|
-
describe("createFunction", () => {
|
|
7
|
-
it("should return result from core function", async () => {
|
|
8
|
-
const mockCoreFn = async (options: { name: string }) => {
|
|
9
|
-
return `Hello, ${options.name}!`;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const wrappedFn = createFunction(mockCoreFn);
|
|
13
|
-
const result = await wrappedFn({ name: "World" });
|
|
14
|
-
|
|
15
|
-
expect(result).toBe("Hello, World!");
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
describe.each([
|
|
20
|
-
{ name: "createFunction", createFn: createFunction },
|
|
21
|
-
{ name: "createPaginatedFunction", createFn: createPaginatedFunction },
|
|
22
|
-
])("$name", ({ createFn }) => {
|
|
23
|
-
it("should preserve function name", async () => {
|
|
24
|
-
async function testCoreFn(options: { value: number }) {
|
|
25
|
-
return options.value * 2;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const wrappedFn = createFn(testCoreFn);
|
|
29
|
-
|
|
30
|
-
expect(wrappedFn.name).toBe("testCoreFn");
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("should normalize and rethrow ZapierError instances", async () => {
|
|
34
|
-
const mockError = new ZapierValidationError("Validation failed", {
|
|
35
|
-
details: { field: "name" },
|
|
36
|
-
});
|
|
37
|
-
const mockCoreFn = async () => {
|
|
38
|
-
throw mockError;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const wrappedFn = createFn(mockCoreFn);
|
|
42
|
-
|
|
43
|
-
await expect(wrappedFn({})).rejects.toBe(mockError);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("should normalize generic Error to ZapierUnknownError", async () => {
|
|
47
|
-
const mockCoreFn = async () => {
|
|
48
|
-
throw new Error("Something went wrong");
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const wrappedFn = createFn(mockCoreFn);
|
|
52
|
-
|
|
53
|
-
await expect(wrappedFn({})).rejects.toThrow(ZapierUnknownError);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it("should normalize non-Error values to ZapierUnknownError", async () => {
|
|
57
|
-
const mockCoreFn = async () => {
|
|
58
|
-
throw "String error";
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const wrappedFn = createFn(mockCoreFn);
|
|
62
|
-
|
|
63
|
-
await expect(wrappedFn({})).rejects.toThrow(ZapierUnknownError);
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
describe("createPaginatedFunction", () => {
|
|
68
|
-
it("should wrap non-array result in {data: [result]}", async () => {
|
|
69
|
-
const mockCoreFn = async (options: { name: string }) => {
|
|
70
|
-
return `Hello, ${options.name}!`;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const wrappedFn = createPaginatedFunction(mockCoreFn);
|
|
74
|
-
const result = await wrappedFn({ name: "World" });
|
|
75
|
-
|
|
76
|
-
expect(result).toEqual({ data: ["Hello, World!"] });
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it("should wrap array result in {data: result}", async () => {
|
|
80
|
-
const mockCoreFn = async (options: { count: number }) => {
|
|
81
|
-
return Array.from({ length: options.count }, (_, i) => `Item ${i + 1}`);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const wrappedFn = createPaginatedFunction(mockCoreFn);
|
|
85
|
-
const result = await wrappedFn({ count: 3 });
|
|
86
|
-
|
|
87
|
-
expect(result).toEqual({ data: ["Item 1", "Item 2", "Item 3"] });
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it("should return {data} object unchanged", async () => {
|
|
91
|
-
const mockCoreFn = async (options: { items: string[] }) => {
|
|
92
|
-
return { data: options.items };
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const wrappedFn = createPaginatedFunction(mockCoreFn);
|
|
96
|
-
const result = await wrappedFn({ items: ["A", "B", "C"] });
|
|
97
|
-
|
|
98
|
-
expect(result).toEqual({ data: ["A", "B", "C"] });
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it("should allow for await iteration over pages", async () => {
|
|
102
|
-
const listItems = createPaginatedFunction(listTestItems);
|
|
103
|
-
|
|
104
|
-
const pages = [];
|
|
105
|
-
for await (const page of listItems({ pageSize: 3 })) {
|
|
106
|
-
pages.push(page);
|
|
107
|
-
if (pages.length >= 3) break;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Should have collected 3 pages
|
|
111
|
-
expect(pages).toHaveLength(3);
|
|
112
|
-
|
|
113
|
-
// Check first page structure
|
|
114
|
-
expect(pages[0]).toEqual({
|
|
115
|
-
data: ALL_ITEMS.slice(0, 3),
|
|
116
|
-
nextCursor: "cursor-3",
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
// Check second page
|
|
120
|
-
expect(pages[1]).toEqual({
|
|
121
|
-
data: ALL_ITEMS.slice(3, 6),
|
|
122
|
-
nextCursor: "cursor-6",
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it("should allow for await iteration over individual items", async () => {
|
|
127
|
-
const listItems = createPaginatedFunction(listTestItems);
|
|
128
|
-
|
|
129
|
-
const items = [];
|
|
130
|
-
for await (const item of listItems({ pageSize: 3 }).items()) {
|
|
131
|
-
items.push(item);
|
|
132
|
-
if (items.length >= 7) break; // Get first 7 items across pages
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Should have collected 7 individual items
|
|
136
|
-
expect(items).toHaveLength(7);
|
|
137
|
-
|
|
138
|
-
// Should be the first 7 items from ALL_ITEMS (flattened from pages)
|
|
139
|
-
expect(items).toEqual(ALL_ITEMS.slice(0, 7));
|
|
140
|
-
});
|
|
141
|
-
});
|
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generic utility functions for creating paginated SDK functions
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type { z } from "zod";
|
|
6
|
-
import { ZapierError, ZapierUnknownError } from "../types/errors";
|
|
7
|
-
import { paginate } from "./pagination-utils";
|
|
8
|
-
import { createValidator, validateOptions } from "./validation";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Helper function to extract cursor from pagination data
|
|
12
|
-
*/
|
|
13
|
-
export function extractCursor(
|
|
14
|
-
data: { next?: string | null } | null | undefined,
|
|
15
|
-
): string | undefined {
|
|
16
|
-
if (!data?.next) {
|
|
17
|
-
return undefined;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
const urlObj = new URL(data.next);
|
|
22
|
-
const offset = urlObj.searchParams.get("offset");
|
|
23
|
-
return offset || undefined;
|
|
24
|
-
} catch {
|
|
25
|
-
return undefined;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Helper function to convert unknown errors to SDK errors
|
|
31
|
-
*/
|
|
32
|
-
function normalizeError(error: unknown): ZapierError {
|
|
33
|
-
if (error instanceof ZapierError) {
|
|
34
|
-
return error;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
38
|
-
return new ZapierUnknownError(`Unknown error: ${message}`, { cause: error });
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Higher-order function that creates a function from a core function
|
|
43
|
-
*
|
|
44
|
-
* @param coreFn - Function that returns T directly or throws errors
|
|
45
|
-
* @param schema - Optional Zod schema for validation
|
|
46
|
-
* @returns A function that normalizes and rethrows errors
|
|
47
|
-
*/
|
|
48
|
-
export function createFunction<
|
|
49
|
-
TOptions,
|
|
50
|
-
TResult,
|
|
51
|
-
TSchemaOptions extends TOptions = TOptions,
|
|
52
|
-
>(
|
|
53
|
-
coreFn: (options: TOptions) => Promise<TResult>,
|
|
54
|
-
schema?: z.ZodSchema<TSchemaOptions>,
|
|
55
|
-
) {
|
|
56
|
-
const functionName = coreFn.name;
|
|
57
|
-
|
|
58
|
-
// Create a named function using dynamic property access
|
|
59
|
-
const namedFunctions = {
|
|
60
|
-
[functionName]: async function (options?: TOptions): Promise<TResult> {
|
|
61
|
-
try {
|
|
62
|
-
// Convert undefined options to empty object
|
|
63
|
-
const normalizedOptions = (options ?? {}) as TOptions;
|
|
64
|
-
|
|
65
|
-
if (schema) {
|
|
66
|
-
const validatedOptions = validateOptions(schema, normalizedOptions);
|
|
67
|
-
return await coreFn({
|
|
68
|
-
...normalizedOptions,
|
|
69
|
-
...validatedOptions,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
return await coreFn(normalizedOptions);
|
|
73
|
-
} catch (error) {
|
|
74
|
-
// Catch and normalize any errors, then rethrow
|
|
75
|
-
throw normalizeError(error);
|
|
76
|
-
}
|
|
77
|
-
},
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
return namedFunctions[functionName];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Higher-order function that creates a page function that wraps results in {data}
|
|
85
|
-
* Internal utility used by createPaginatedFunction
|
|
86
|
-
*/
|
|
87
|
-
function createPageFunction<TOptions, TResult>(
|
|
88
|
-
coreFn: (options: TOptions) => Promise<TResult>,
|
|
89
|
-
) {
|
|
90
|
-
const functionName = coreFn.name + "Page";
|
|
91
|
-
|
|
92
|
-
// Create a named function using dynamic property access
|
|
93
|
-
const namedFunctions = {
|
|
94
|
-
[functionName]: async function (
|
|
95
|
-
options: TOptions & {
|
|
96
|
-
cursor?: string;
|
|
97
|
-
maxItems?: number;
|
|
98
|
-
pageSize?: number;
|
|
99
|
-
},
|
|
100
|
-
): Promise<{ data: any[]; nextCursor?: string }> {
|
|
101
|
-
try {
|
|
102
|
-
const result = await coreFn(options);
|
|
103
|
-
|
|
104
|
-
// If result already has a data property, ensure it's an array
|
|
105
|
-
if (result && typeof result === "object" && "data" in result) {
|
|
106
|
-
const data = (result as any).data;
|
|
107
|
-
return {
|
|
108
|
-
data: Array.isArray(data) ? data : [data],
|
|
109
|
-
nextCursor: (result as any).nextCursor,
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// If result is an array, wrap in {data: result}
|
|
114
|
-
if (Array.isArray(result)) {
|
|
115
|
-
return { data: result };
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// If result is non-array, wrap in {data: [result]}
|
|
119
|
-
return { data: [result] };
|
|
120
|
-
} catch (error) {
|
|
121
|
-
// Catch and normalize any errors, then rethrow
|
|
122
|
-
throw normalizeError(error);
|
|
123
|
-
}
|
|
124
|
-
},
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
return namedFunctions[functionName];
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Higher-order function that creates a paginated function that wraps results in {data}
|
|
132
|
-
*
|
|
133
|
-
* @param coreFn - Function that returns T directly or throws errors
|
|
134
|
-
* @returns A function that normalizes errors and wraps results in {data}
|
|
135
|
-
*/
|
|
136
|
-
type PaginatedResult<TResult> = TResult extends {
|
|
137
|
-
data: any;
|
|
138
|
-
nextCursor?: string;
|
|
139
|
-
}
|
|
140
|
-
? TResult
|
|
141
|
-
: { data: TResult; nextCursor?: string };
|
|
142
|
-
|
|
143
|
-
type ItemType<TResult> = TResult extends { data: infer TData }
|
|
144
|
-
? TData extends readonly (infer TItem)[]
|
|
145
|
-
? TItem
|
|
146
|
-
: TData
|
|
147
|
-
: TResult extends readonly (infer TItem)[]
|
|
148
|
-
? TItem
|
|
149
|
-
: TResult;
|
|
150
|
-
|
|
151
|
-
export function createPaginatedFunction<
|
|
152
|
-
TOptions,
|
|
153
|
-
TResult,
|
|
154
|
-
TSchemaOptions extends TOptions = TOptions,
|
|
155
|
-
>(
|
|
156
|
-
coreFn: (options: TOptions & { pageSize: number }) => Promise<TResult>,
|
|
157
|
-
schema?: z.ZodSchema<TSchemaOptions>,
|
|
158
|
-
): (
|
|
159
|
-
options?: TOptions & {
|
|
160
|
-
cursor?: string;
|
|
161
|
-
pageSize?: number;
|
|
162
|
-
maxItems?: number;
|
|
163
|
-
},
|
|
164
|
-
) => Promise<PaginatedResult<TResult>> &
|
|
165
|
-
AsyncIterable<PaginatedResult<TResult>> & {
|
|
166
|
-
items(): AsyncIterable<ItemType<TResult>>;
|
|
167
|
-
} {
|
|
168
|
-
const pageFunction = createPageFunction(coreFn);
|
|
169
|
-
const functionName = coreFn.name;
|
|
170
|
-
const validator = schema ? createValidator(schema) : null;
|
|
171
|
-
|
|
172
|
-
// Create the main paginated function
|
|
173
|
-
const namedFunctions = {
|
|
174
|
-
[functionName]: function (
|
|
175
|
-
options?: TOptions & {
|
|
176
|
-
cursor?: string;
|
|
177
|
-
pageSize?: number;
|
|
178
|
-
maxItems?: number;
|
|
179
|
-
},
|
|
180
|
-
) {
|
|
181
|
-
// Convert undefined options to empty object
|
|
182
|
-
const normalizedOptions = (options ?? {}) as TOptions & {
|
|
183
|
-
cursor?: string;
|
|
184
|
-
pageSize?: number;
|
|
185
|
-
maxItems?: number;
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
// Validate options if schema provided (validated fields take precedence)
|
|
189
|
-
const validatedOptions = {
|
|
190
|
-
...normalizedOptions,
|
|
191
|
-
...(validator ? validator(normalizedOptions) : normalizedOptions),
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
// Set default pageSize if not provided
|
|
195
|
-
const pageSize = validatedOptions.pageSize || 100;
|
|
196
|
-
const optimizedOptions = {
|
|
197
|
-
...validatedOptions,
|
|
198
|
-
pageSize,
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
const iterator = paginate(pageFunction, optimizedOptions as any);
|
|
202
|
-
|
|
203
|
-
// Get a promise for the first iteration
|
|
204
|
-
const firstPagePromise = iterator.next().then((result) => {
|
|
205
|
-
if (result.done) {
|
|
206
|
-
throw new Error("Paginate should always iterate at least once");
|
|
207
|
-
}
|
|
208
|
-
return result.value;
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
// Add Symbol.asyncIterator to make the promise iterable
|
|
212
|
-
return Object.assign(firstPagePromise, {
|
|
213
|
-
[Symbol.asyncIterator]: async function* () {
|
|
214
|
-
// Yield the first page (await the promise)
|
|
215
|
-
yield await firstPagePromise;
|
|
216
|
-
|
|
217
|
-
// Yield the rest of the pages
|
|
218
|
-
for await (const page of iterator) {
|
|
219
|
-
yield page;
|
|
220
|
-
}
|
|
221
|
-
},
|
|
222
|
-
items: function () {
|
|
223
|
-
return {
|
|
224
|
-
[Symbol.asyncIterator]: async function* () {
|
|
225
|
-
// Yield the first page items
|
|
226
|
-
const firstPage = await firstPagePromise;
|
|
227
|
-
for (const item of firstPage.data) {
|
|
228
|
-
yield item;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// Yield items from the rest of the pages
|
|
232
|
-
for await (const page of iterator) {
|
|
233
|
-
for (const item of page.data) {
|
|
234
|
-
yield item;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
},
|
|
238
|
-
};
|
|
239
|
-
},
|
|
240
|
-
});
|
|
241
|
-
},
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
return namedFunctions[functionName] as any;
|
|
245
|
-
}
|