@zapier/zapier-sdk 0.0.3 → 0.1.1

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.
Files changed (189) hide show
  1. package/dist/api/auth.d.ts +8 -0
  2. package/dist/api/auth.js +29 -0
  3. package/dist/api/client.d.ts +8 -0
  4. package/dist/api/client.js +102 -0
  5. package/dist/api/debug.d.ts +12 -0
  6. package/dist/api/debug.js +50 -0
  7. package/dist/api/index.d.ts +26 -0
  8. package/dist/api/index.js +51 -0
  9. package/dist/api/polling.d.ts +17 -0
  10. package/dist/api/polling.js +34 -0
  11. package/dist/{types.d.ts → api/types.d.ts} +37 -88
  12. package/dist/api/types.js +9 -0
  13. package/dist/functions/{bundleCode.d.ts → bundleCode/index.d.ts} +1 -8
  14. package/dist/functions/bundleCode/info.d.ts +27 -0
  15. package/dist/functions/bundleCode/info.js +11 -0
  16. package/dist/functions/bundleCode/schemas.d.ts +27 -0
  17. package/dist/functions/bundleCode/schemas.js +22 -0
  18. package/dist/functions/findFirstAuthentication/index.d.ts +12 -0
  19. package/dist/functions/findFirstAuthentication/index.js +21 -0
  20. package/dist/functions/findFirstAuthentication/info.d.ts +30 -0
  21. package/dist/functions/findFirstAuthentication/info.js +11 -0
  22. package/dist/functions/findFirstAuthentication/schemas.d.ts +42 -0
  23. package/dist/functions/findFirstAuthentication/schemas.js +25 -0
  24. package/dist/functions/findUniqueAuthentication/index.d.ts +13 -0
  25. package/dist/functions/findUniqueAuthentication/index.js +28 -0
  26. package/dist/functions/findUniqueAuthentication/info.d.ts +30 -0
  27. package/dist/functions/findUniqueAuthentication/info.js +11 -0
  28. package/dist/functions/findUniqueAuthentication/schemas.d.ts +42 -0
  29. package/dist/functions/findUniqueAuthentication/schemas.js +25 -0
  30. package/dist/functions/{generateTypes.d.ts → generateTypes/index.d.ts} +2 -7
  31. package/dist/functions/{generateTypes.js → generateTypes/index.js} +58 -24
  32. package/dist/functions/generateTypes/info.d.ts +21 -0
  33. package/dist/functions/generateTypes/info.js +11 -0
  34. package/dist/functions/generateTypes/schemas.d.ts +30 -0
  35. package/dist/functions/generateTypes/schemas.js +14 -0
  36. package/dist/functions/{getAction.d.ts → getAction/index.d.ts} +2 -6
  37. package/dist/functions/{getAction.js → getAction/index.js} +6 -5
  38. package/dist/functions/getAction/info.d.ts +18 -0
  39. package/dist/functions/getAction/info.js +11 -0
  40. package/dist/functions/getAction/schemas.d.ts +30 -0
  41. package/dist/functions/getAction/schemas.js +13 -0
  42. package/dist/functions/{getApp.d.ts → getApp/index.d.ts} +2 -4
  43. package/dist/functions/{getApp.js → getApp/index.js} +6 -10
  44. package/dist/functions/getApp/info.d.ts +12 -0
  45. package/dist/functions/getApp/info.js +11 -0
  46. package/dist/functions/getApp/schemas.d.ts +24 -0
  47. package/dist/functions/getApp/schemas.js +11 -0
  48. package/dist/functions/{listActions.d.ts → listActions/index.d.ts} +3 -6
  49. package/dist/functions/{listActions.js → listActions/index.js} +4 -3
  50. package/dist/functions/listActions/info.d.ts +15 -0
  51. package/dist/functions/listActions/info.js +11 -0
  52. package/dist/functions/listActions/schemas.d.ts +27 -0
  53. package/dist/functions/listActions/schemas.js +14 -0
  54. package/dist/functions/{listApps.d.ts → listApps/index.d.ts} +2 -6
  55. package/dist/functions/{listApps.js → listApps/index.js} +1 -1
  56. package/dist/functions/listApps/info.d.ts +18 -0
  57. package/dist/functions/listApps/info.js +11 -0
  58. package/dist/functions/listApps/schemas.d.ts +30 -0
  59. package/dist/functions/listApps/schemas.js +15 -0
  60. package/dist/functions/{listAuths.d.ts → listAuthentications/index.d.ts} +3 -9
  61. package/dist/functions/{listAuths.js → listAuthentications/index.js} +28 -12
  62. package/dist/functions/listAuthentications/info.d.ts +30 -0
  63. package/dist/functions/listAuthentications/info.js +11 -0
  64. package/dist/functions/listAuthentications/schemas.d.ts +42 -0
  65. package/dist/functions/listAuthentications/schemas.js +25 -0
  66. package/dist/functions/{listFields.d.ts → listFields/index.d.ts} +3 -9
  67. package/dist/functions/{listFields.js → listFields/index.js} +8 -10
  68. package/dist/functions/listFields/info.d.ts +24 -0
  69. package/dist/functions/listFields/info.js +11 -0
  70. package/dist/functions/listFields/schemas.d.ts +36 -0
  71. package/dist/functions/listFields/schemas.js +17 -0
  72. package/dist/functions/{runAction.d.ts → runAction/index.d.ts} +2 -8
  73. package/dist/functions/{runAction.js → runAction/index.js} +15 -14
  74. package/dist/functions/runAction/info.d.ts +24 -0
  75. package/dist/functions/runAction/info.js +11 -0
  76. package/dist/functions/runAction/schemas.d.ts +36 -0
  77. package/dist/functions/runAction/schemas.js +15 -0
  78. package/dist/index.d.ts +10 -4
  79. package/dist/index.js +17 -6
  80. package/dist/plugins/apps/index.d.ts +8 -0
  81. package/dist/plugins/apps/index.js +77 -0
  82. package/dist/plugins/apps/info.d.ts +6 -0
  83. package/dist/plugins/apps/info.js +13 -0
  84. package/dist/plugins/apps/types.d.ts +21 -0
  85. package/dist/plugins/apps/types.js +2 -0
  86. package/dist/resolvers/actionKey.d.ts +8 -0
  87. package/dist/resolvers/actionKey.js +20 -0
  88. package/dist/resolvers/actionType.d.ts +8 -0
  89. package/dist/resolvers/actionType.js +21 -0
  90. package/dist/resolvers/appKey.d.ts +6 -0
  91. package/dist/resolvers/appKey.js +8 -0
  92. package/dist/resolvers/authenticationId.d.ts +8 -0
  93. package/dist/resolvers/authenticationId.js +29 -0
  94. package/dist/resolvers/index.d.ts +39 -0
  95. package/dist/resolvers/index.js +105 -0
  96. package/dist/resolvers/inputs.d.ts +7 -0
  97. package/dist/resolvers/inputs.js +15 -0
  98. package/dist/schema-utils.d.ts +44 -0
  99. package/dist/schema-utils.js +76 -0
  100. package/dist/schemas/Action.d.ts +21 -0
  101. package/dist/schemas/Action.js +31 -0
  102. package/dist/schemas/App.d.ts +19 -0
  103. package/dist/schemas/App.js +32 -0
  104. package/dist/schemas/Auth.d.ts +27 -0
  105. package/dist/schemas/Auth.js +42 -0
  106. package/dist/schemas/Field.d.ts +15 -0
  107. package/dist/schemas/Field.js +25 -0
  108. package/dist/sdk.d.ts +3 -4
  109. package/dist/sdk.js +104 -11
  110. package/dist/types/domain.d.ts +22 -0
  111. package/dist/types/domain.js +21 -0
  112. package/dist/types/properties.d.ts +21 -0
  113. package/dist/types/properties.js +43 -0
  114. package/dist/types/sdk.d.ts +23 -0
  115. package/dist/types/sdk.js +2 -0
  116. package/package.json +4 -2
  117. package/src/api/auth.ts +28 -0
  118. package/src/api/client.ts +148 -0
  119. package/src/api/debug.ts +58 -0
  120. package/src/api/index.ts +83 -0
  121. package/src/api/polling.ts +56 -0
  122. package/src/api/types.ts +177 -0
  123. package/src/functions/{bundleCode.ts → bundleCode/index.ts} +1 -8
  124. package/src/functions/bundleCode/info.ts +9 -0
  125. package/src/functions/bundleCode/schemas.ts +30 -0
  126. package/src/functions/findFirstAuthentication/index.ts +24 -0
  127. package/src/functions/findFirstAuthentication/info.ts +9 -0
  128. package/src/functions/findFirstAuthentication/schemas.ts +60 -0
  129. package/src/functions/findUniqueAuthentication/index.ts +35 -0
  130. package/src/functions/findUniqueAuthentication/info.ts +9 -0
  131. package/src/functions/findUniqueAuthentication/schemas.ts +60 -0
  132. package/src/functions/{generateTypes.ts → generateTypes/index.ts} +70 -31
  133. package/src/functions/generateTypes/info.ts +9 -0
  134. package/src/functions/generateTypes/schemas.ts +38 -0
  135. package/src/functions/{getAction.ts → getAction/index.ts} +11 -12
  136. package/src/functions/getAction/info.ts +9 -0
  137. package/src/functions/getAction/schemas.ts +35 -0
  138. package/src/functions/{getApp.ts → getApp/index.ts} +9 -15
  139. package/src/functions/getApp/info.ts +9 -0
  140. package/src/functions/getApp/schemas.ts +31 -0
  141. package/src/functions/{listActions.ts → listActions/index.ts} +8 -10
  142. package/src/functions/listActions/info.ts +9 -0
  143. package/src/functions/listActions/schemas.ts +40 -0
  144. package/src/functions/{listApps.ts → listApps/index.ts} +3 -8
  145. package/src/functions/listApps/info.ts +9 -0
  146. package/src/functions/listApps/schemas.ts +43 -0
  147. package/src/functions/{listAuths.ts → listAuthentications/index.ts} +32 -23
  148. package/src/functions/listAuthentications/info.ts +9 -0
  149. package/src/functions/listAuthentications/schemas.ts +60 -0
  150. package/src/functions/{listFields.ts → listFields/index.ts} +11 -20
  151. package/src/functions/listFields/info.ts +9 -0
  152. package/src/functions/listFields/schemas.ts +46 -0
  153. package/src/functions/{runAction.ts → runAction/index.ts} +25 -23
  154. package/src/functions/runAction/info.ts +9 -0
  155. package/src/functions/runAction/schemas.ts +41 -0
  156. package/src/index.ts +19 -5
  157. package/src/plugins/apps/index.ts +144 -0
  158. package/src/plugins/apps/info.ts +12 -0
  159. package/src/plugins/apps/types.ts +34 -0
  160. package/src/resolvers/actionKey.ts +33 -0
  161. package/src/resolvers/actionType.ts +30 -0
  162. package/src/resolvers/appKey.ts +11 -0
  163. package/src/resolvers/authenticationId.ts +38 -0
  164. package/src/resolvers/index.ts +117 -0
  165. package/src/resolvers/inputs.ts +23 -0
  166. package/src/schema-utils.ts +154 -0
  167. package/src/schemas/Action.ts +40 -0
  168. package/src/schemas/App.ts +43 -0
  169. package/src/schemas/Auth.ts +54 -0
  170. package/src/schemas/Field.ts +34 -0
  171. package/src/sdk.ts +165 -19
  172. package/src/types/domain.ts +54 -0
  173. package/src/types/properties.ts +67 -0
  174. package/src/types/sdk.ts +46 -0
  175. package/dist/actions-sdk.d.ts +0 -47
  176. package/dist/actions-sdk.js +0 -208
  177. package/dist/api.d.ts +0 -62
  178. package/dist/api.js +0 -227
  179. package/dist/output-schemas.d.ts +0 -95
  180. package/dist/output-schemas.js +0 -138
  181. package/dist/schemas.d.ts +0 -338
  182. package/dist/schemas.js +0 -336
  183. package/dist/types.js +0 -41
  184. package/src/actions-sdk.ts +0 -356
  185. package/src/api.ts +0 -361
  186. package/src/output-schemas.ts +0 -196
  187. package/src/schemas.ts +0 -467
  188. package/src/types.ts +0 -257
  189. /package/dist/functions/{bundleCode.js → bundleCode/index.js} +0 -0
@@ -1,208 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.createActionsSdk = createActionsSdk;
18
- const api_1 = require("./api");
19
- const listAuths_1 = require("./functions/listAuths");
20
- const listApps_1 = require("./functions/listApps");
21
- const getApp_1 = require("./functions/getApp");
22
- const listActions_1 = require("./functions/listActions");
23
- const getAction_1 = require("./functions/getAction");
24
- const runAction_1 = require("./functions/runAction");
25
- const listFields_1 = require("./functions/listFields");
26
- const generateTypes_1 = require("./functions/generateTypes");
27
- const bundleCode_1 = require("./functions/bundleCode");
28
- __exportStar(require("./schemas"), exports);
29
- __exportStar(require("./output-schemas"), exports);
30
- function createActionsSdk(options = {}) {
31
- // Auto-load .env files (searches up directory tree)
32
- try {
33
- const { findUpSync } = require("find-up");
34
- const envPath = findUpSync(".env");
35
- if (envPath) {
36
- require("dotenv").config({ path: envPath, quiet: true });
37
- }
38
- }
39
- catch {
40
- // Silently fail if dotenv/find-up not available or .env not found
41
- }
42
- const { fetch: customFetch = globalThis.fetch, baseUrl = "https://zapier.com", token, authentications = {}, debug = false, } = options;
43
- // If no token provided, try to get it from environment variable
44
- const finalToken = token || process.env.ZAPIER_TOKEN;
45
- // Create the API client
46
- const api = (0, api_1.createZapierApi)({
47
- baseUrl,
48
- token: finalToken,
49
- debug,
50
- fetch: customFetch,
51
- });
52
- // Create SDK object - we'll populate services after creation to avoid circular dependency
53
- const sdk = {};
54
- const appsService = createAppsService({
55
- api,
56
- token: finalToken,
57
- authentications,
58
- sdk,
59
- });
60
- const actionsService = createActionsService({
61
- api,
62
- token: finalToken,
63
- authentications,
64
- sdk,
65
- });
66
- const authsService = createAuthsService({
67
- api,
68
- token: finalToken,
69
- authentications,
70
- sdk,
71
- });
72
- const fieldsService = createFieldsService({
73
- api,
74
- token: finalToken,
75
- authentications,
76
- sdk,
77
- });
78
- // Create root namespace tools
79
- const generateFunction = async (options) => {
80
- return (0, generateTypes_1.generateTypes)({ ...options, api, token: finalToken });
81
- };
82
- const bundleFunction = async (options) => {
83
- return (0, bundleCode_1.bundleCode)(options);
84
- };
85
- // Populate the SDK object
86
- sdk.apps = appsService;
87
- sdk.actions = actionsService;
88
- sdk.auths = authsService;
89
- sdk.fields = fieldsService;
90
- sdk.generate = generateFunction;
91
- sdk.bundle = bundleFunction;
92
- // Note: Debug logging for SDK initialization is now handled by the API client
93
- return sdk;
94
- }
95
- function createAppsService(options) {
96
- const { api, token } = options;
97
- return {
98
- async list(options) {
99
- return (0, listApps_1.listApps)({ ...options, api, token });
100
- },
101
- async get(options) {
102
- return (0, getApp_1.getApp)({ ...options, api, token });
103
- },
104
- };
105
- }
106
- function createAuthsService(options) {
107
- const { api, token, authentications } = options;
108
- return {
109
- async list(options) {
110
- return (0, listAuths_1.listAuths)({ ...options, api, token });
111
- },
112
- find(options) {
113
- if (!authentications) {
114
- throw new Error(`No authentication configured`);
115
- }
116
- const auths = authentications[options.appKey] || [];
117
- if (auths.length === 0) {
118
- throw new Error(`No authentication configured for app "${options.appKey}"`);
119
- }
120
- if (auths.length > 1) {
121
- throw new Error(`Multiple authentications found for app "${options.appKey}". Please specify which one to use.`);
122
- }
123
- return auths[0].id;
124
- },
125
- };
126
- }
127
- function createFieldsService(options) {
128
- const { api, token } = options;
129
- return {
130
- async list(options) {
131
- return (0, listFields_1.listFields)({ ...options, api, token });
132
- },
133
- };
134
- }
135
- function createActionsService(options) {
136
- const { api, token, authentications } = options;
137
- // Create the base service with named methods
138
- const baseService = {
139
- async list(options) {
140
- return (0, listActions_1.listActions)({ ...options, api, token });
141
- },
142
- async get(options) {
143
- return (0, getAction_1.getAction)({ ...options, api, token });
144
- },
145
- // run will be replaced with function/proxy pattern below
146
- };
147
- // Create the run function with proxy capabilities
148
- const runFunction = async (params) => {
149
- const { app, type, action, inputs, authId: providedAuthId } = params;
150
- // Resolve auth ID for this specific app
151
- let authId = providedAuthId;
152
- if (!authId && authentications) {
153
- const auths = authentications[app] || [];
154
- if (auths.length === 0) {
155
- throw new Error(`No authentication configured for app "${app}"`);
156
- }
157
- if (auths.length > 1) {
158
- throw new Error(`Multiple authentications found for app "${app}". Please specify which one to use.`);
159
- }
160
- authId = auths[0].id;
161
- }
162
- // Delegate to standalone runAction function
163
- return (0, runAction_1.runAction)({
164
- app,
165
- type,
166
- action,
167
- inputs,
168
- authId,
169
- api,
170
- token,
171
- });
172
- };
173
- // Create proxy wrapper for the run function
174
- const runWithProxy = new Proxy(runFunction, {
175
- get(target, prop) {
176
- if (typeof prop === "string") {
177
- // Return app-level proxy
178
- return new Proxy({}, {
179
- get(_target, actionType) {
180
- if (typeof actionType !== "string")
181
- return undefined;
182
- return new Proxy({}, {
183
- get(_target, actionKey) {
184
- if (typeof actionKey !== "string")
185
- return undefined;
186
- // Return function that calls the main run function
187
- return async (options = {}) => {
188
- return await runFunction({
189
- app: prop,
190
- type: actionType, // Cast because proxy string can't be typed as enum
191
- action: actionKey,
192
- inputs: options.inputs,
193
- authId: options.authId,
194
- });
195
- };
196
- },
197
- });
198
- },
199
- });
200
- }
201
- return target[prop];
202
- },
203
- });
204
- // Add the run function/proxy to the base service
205
- baseService.run = runWithProxy;
206
- // Return the service with the new run function/proxy
207
- return baseService;
208
- }
package/dist/api.d.ts DELETED
@@ -1,62 +0,0 @@
1
- /**
2
- * Zapier API Client Module
3
- *
4
- * This module provides a centralized API layer for all HTTP interactions
5
- * with Zapier's various APIs. It handles authentication, error handling,
6
- * polling, and provides consistent patterns across all services.
7
- */
8
- export interface ApiClientOptions {
9
- baseUrl: string;
10
- token?: string;
11
- debug?: boolean;
12
- fetch?: typeof globalThis.fetch;
13
- }
14
- export interface ApiClient {
15
- get: (path: string, options?: RequestOptions) => Promise<any>;
16
- post: (path: string, data?: any, options?: RequestOptions) => Promise<any>;
17
- put: (path: string, data?: any, options?: RequestOptions) => Promise<any>;
18
- delete: (path: string, options?: RequestOptions) => Promise<any>;
19
- poll: (path: string, options?: PollOptions) => Promise<any>;
20
- }
21
- export interface RequestOptions {
22
- headers?: Record<string, string>;
23
- searchParams?: Record<string, string>;
24
- authRequired?: boolean;
25
- customErrorHandler?: (response: Response) => Error | undefined;
26
- }
27
- export interface PollOptions extends RequestOptions {
28
- maxAttempts?: number;
29
- initialDelay?: number;
30
- maxDelay?: number;
31
- successStatus?: number;
32
- pendingStatus?: number;
33
- resultExtractor?: (response: any) => any;
34
- }
35
- export declare function isJwt(token: string): boolean;
36
- export declare function getAuthorizationHeader(token: string): string;
37
- export declare function pollUntilComplete(options: {
38
- fetch: typeof globalThis.fetch;
39
- url: string;
40
- headers?: Record<string, string>;
41
- maxAttempts?: number;
42
- initialDelay?: number;
43
- maxDelay?: number;
44
- successStatus?: number;
45
- pendingStatus?: number;
46
- resultExtractor?: (response: any) => any;
47
- }): Promise<any>;
48
- export declare function createZapierApi(options: ApiClientOptions): ApiClient;
49
- export declare function generateRequestId(): string;
50
- /**
51
- * Utility function to get or create an API client for standalone functions
52
- *
53
- * @param config - Configuration that may include an existing API client
54
- * @returns ApiClient instance
55
- */
56
- export declare function getOrCreateApiClient(config: {
57
- baseUrl?: string;
58
- token?: string;
59
- api?: ApiClient;
60
- debug?: boolean;
61
- fetch?: typeof globalThis.fetch;
62
- }): ApiClient;
package/dist/api.js DELETED
@@ -1,227 +0,0 @@
1
- "use strict";
2
- /**
3
- * Zapier API Client Module
4
- *
5
- * This module provides a centralized API layer for all HTTP interactions
6
- * with Zapier's various APIs. It handles authentication, error handling,
7
- * polling, and provides consistent patterns across all services.
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.isJwt = isJwt;
11
- exports.getAuthorizationHeader = getAuthorizationHeader;
12
- exports.pollUntilComplete = pollUntilComplete;
13
- exports.createZapierApi = createZapierApi;
14
- exports.generateRequestId = generateRequestId;
15
- exports.getOrCreateApiClient = getOrCreateApiClient;
16
- // ============================================================================
17
- // Authentication Utilities
18
- // ============================================================================
19
- function isJwt(token) {
20
- // JWT tokens have exactly 3 parts separated by dots
21
- const parts = token.split(".");
22
- if (parts.length !== 3) {
23
- return false;
24
- }
25
- // Each part should be base64url encoded (no padding)
26
- // Basic validation - each part should be non-empty and contain valid base64url characters
27
- const base64UrlPattern = /^[A-Za-z0-9_-]+$/;
28
- return parts.every((part) => part.length > 0 && base64UrlPattern.test(part));
29
- }
30
- function getAuthorizationHeader(token) {
31
- // Check if token is a JWT (has 3 parts separated by dots)
32
- if (isJwt(token)) {
33
- return `JWT ${token}`;
34
- }
35
- // Default to Bearer for other token types
36
- return `Bearer ${token}`;
37
- }
38
- // ============================================================================
39
- // Debug Logging
40
- // ============================================================================
41
- function createDebugLogger(enabled) {
42
- if (!enabled) {
43
- return () => { }; // No-op function when debug is disabled
44
- }
45
- return (message, data) => {
46
- console.log(`[Zapier SDK] ${message}`, data || "");
47
- };
48
- }
49
- function createDebugFetch(options) {
50
- const { originalFetch, debugLog } = options;
51
- return async (input, options) => {
52
- const startTime = Date.now();
53
- // Convert input to URL string for logging
54
- const url = typeof input === "string" ? input : input.toString();
55
- const method = options?.method || "GET";
56
- debugLog(`→ ${method} ${url}`, {
57
- headers: options?.headers,
58
- body: options?.body ? JSON.parse(options.body) : undefined,
59
- });
60
- try {
61
- const response = await originalFetch(input, options);
62
- const duration = Date.now() - startTime;
63
- debugLog(`← ${response.status} ${response.statusText} (${duration}ms)`, {
64
- url,
65
- method,
66
- status: response.status,
67
- });
68
- return response;
69
- }
70
- catch (error) {
71
- const duration = Date.now() - startTime;
72
- debugLog(`✖ Request failed (${duration}ms)`, {
73
- url,
74
- method,
75
- error: error instanceof Error ? error.message : error,
76
- });
77
- throw error;
78
- }
79
- };
80
- }
81
- // ============================================================================
82
- // Polling Utilities
83
- // ============================================================================
84
- async function pollUntilComplete(options) {
85
- const { fetch, url, headers = {}, maxAttempts = 30, initialDelay = 50, maxDelay = 1000, successStatus = 200, pendingStatus = 202, resultExtractor = (response) => response, } = options;
86
- let delay = initialDelay;
87
- for (let attempt = 0; attempt < maxAttempts; attempt++) {
88
- const response = await fetch(url, { headers });
89
- if (response.status === successStatus) {
90
- // Success - extract and return results
91
- const result = await response.json();
92
- return resultExtractor(result);
93
- }
94
- else if (response.status === pendingStatus) {
95
- // Still processing - wait and retry
96
- if (attempt < maxAttempts - 1) {
97
- await new Promise((resolve) => setTimeout(resolve, delay));
98
- delay = Math.min(delay * 2, maxDelay); // Exponential backoff
99
- continue;
100
- }
101
- }
102
- else {
103
- // Error occurred
104
- throw new Error(`Request failed: GET ${url} - ${response.status} ${response.statusText}`);
105
- }
106
- }
107
- throw new Error(`Operation timed out after ${maxAttempts} attempts`);
108
- }
109
- // ============================================================================
110
- // API Client Factory
111
- // ============================================================================
112
- function createZapierApi(options) {
113
- const { baseUrl, token, debug = false, fetch: originalFetch = globalThis.fetch, } = options;
114
- const debugLog = createDebugLogger(debug);
115
- const fetch = createDebugFetch({ originalFetch, debugLog });
116
- // Helper to build full URLs
117
- function buildUrl(path, searchParams) {
118
- const url = new URL(path, baseUrl);
119
- if (searchParams) {
120
- Object.entries(searchParams).forEach(([key, value]) => {
121
- url.searchParams.set(key, value);
122
- });
123
- }
124
- return url.toString();
125
- }
126
- // Helper to build headers
127
- function buildHeaders(options = {}) {
128
- const headers = {
129
- ...options.headers,
130
- };
131
- // Add auth header if token provided and not explicitly disabled
132
- if (token && options.authRequired !== false) {
133
- headers.Authorization = getAuthorizationHeader(token);
134
- }
135
- return headers;
136
- }
137
- // Helper to handle responses
138
- async function handleResponse(response, customErrorHandler) {
139
- if (!response.ok) {
140
- // Check for custom error handling first
141
- if (customErrorHandler) {
142
- const customError = customErrorHandler(response);
143
- if (customError) {
144
- throw customError;
145
- }
146
- }
147
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
148
- }
149
- // Try to parse JSON, fall back to text if that fails
150
- try {
151
- return await response.json();
152
- }
153
- catch {
154
- return await response.text();
155
- }
156
- }
157
- // Helper to perform HTTP requests with JSON handling
158
- async function fetchJson(method, path, data, options = {}) {
159
- const url = buildUrl(path, options.searchParams);
160
- const headers = buildHeaders(options);
161
- // Add Content-Type for JSON requests with body data
162
- if (data && typeof data === "object") {
163
- headers["Content-Type"] = "application/json";
164
- }
165
- const response = await fetch(url, {
166
- method,
167
- headers,
168
- body: data ? JSON.stringify(data) : undefined,
169
- });
170
- return handleResponse(response, options.customErrorHandler);
171
- }
172
- return {
173
- async get(path, options = {}) {
174
- return fetchJson("GET", path, undefined, options);
175
- },
176
- async post(path, data, options = {}) {
177
- return fetchJson("POST", path, data, options);
178
- },
179
- async put(path, data, options = {}) {
180
- return fetchJson("PUT", path, data, options);
181
- },
182
- async delete(path, options = {}) {
183
- return fetchJson("DELETE", path, undefined, options);
184
- },
185
- async poll(path, options = {}) {
186
- const url = buildUrl(path, options.searchParams);
187
- const headers = buildHeaders(options);
188
- return pollUntilComplete({
189
- fetch,
190
- url,
191
- headers,
192
- maxAttempts: options.maxAttempts,
193
- initialDelay: options.initialDelay,
194
- maxDelay: options.maxDelay,
195
- successStatus: options.successStatus,
196
- pendingStatus: options.pendingStatus,
197
- resultExtractor: options.resultExtractor,
198
- });
199
- },
200
- };
201
- }
202
- // ============================================================================
203
- // Utility Functions
204
- // ============================================================================
205
- function generateRequestId() {
206
- return Math.random().toString(36).substring(2) + Date.now().toString(36);
207
- }
208
- // ============================================================================
209
- // Utility Functions for Standalone Functions
210
- // ============================================================================
211
- /**
212
- * Utility function to get or create an API client for standalone functions
213
- *
214
- * @param config - Configuration that may include an existing API client
215
- * @returns ApiClient instance
216
- */
217
- function getOrCreateApiClient(config) {
218
- const { baseUrl = "https://zapier.com", token = process.env.ZAPIER_TOKEN, api: providedApi, debug = false, fetch: customFetch, } = config;
219
- // Use provided API client or create a new one
220
- return (providedApi ||
221
- createZapierApi({
222
- baseUrl,
223
- token,
224
- debug,
225
- fetch: customFetch,
226
- }));
227
- }
@@ -1,95 +0,0 @@
1
- import { z } from "zod";
2
- export interface FormattedItem {
3
- title: string;
4
- subtitle?: string;
5
- details: Array<{
6
- text: string;
7
- style: "normal" | "dim" | "accent" | "warning" | "success";
8
- }>;
9
- }
10
- export interface FormatMetadata {
11
- format: (item: any) => FormattedItem;
12
- }
13
- export declare function withOutputSchema<T extends z.ZodType>(inputSchema: T, outputSchema: z.ZodType): T;
14
- export declare function getFormatMetadata(schema: z.ZodType): FormatMetadata | undefined;
15
- export declare function getOutputSchema(inputSchema: z.ZodType): z.ZodType | undefined;
16
- export declare const AppItemSchema: z.ZodObject<{
17
- key: z.ZodString;
18
- name: z.ZodOptional<z.ZodString>;
19
- description: z.ZodOptional<z.ZodString>;
20
- category: z.ZodOptional<z.ZodString>;
21
- }, "strip", z.ZodTypeAny, {
22
- key: string;
23
- name?: string | undefined;
24
- description?: string | undefined;
25
- category?: string | undefined;
26
- }, {
27
- key: string;
28
- name?: string | undefined;
29
- description?: string | undefined;
30
- category?: string | undefined;
31
- }>;
32
- export declare const ActionItemSchema: z.ZodObject<{
33
- key: z.ZodString;
34
- name: z.ZodOptional<z.ZodString>;
35
- type: z.ZodString;
36
- appKey: z.ZodOptional<z.ZodString>;
37
- description: z.ZodOptional<z.ZodString>;
38
- }, "strip", z.ZodTypeAny, {
39
- key: string;
40
- type: string;
41
- name?: string | undefined;
42
- description?: string | undefined;
43
- appKey?: string | undefined;
44
- }, {
45
- key: string;
46
- type: string;
47
- name?: string | undefined;
48
- description?: string | undefined;
49
- appKey?: string | undefined;
50
- }>;
51
- export declare const AuthItemSchema: z.ZodObject<{
52
- id: z.ZodNumber;
53
- title: z.ZodOptional<z.ZodString>;
54
- label: z.ZodOptional<z.ZodString>;
55
- identifier: z.ZodOptional<z.ZodString>;
56
- account_id: z.ZodOptional<z.ZodString>;
57
- is_private: z.ZodOptional<z.ZodBoolean>;
58
- shared_with_all: z.ZodOptional<z.ZodBoolean>;
59
- marked_stale_at: z.ZodOptional<z.ZodString>;
60
- }, "strip", z.ZodTypeAny, {
61
- id: number;
62
- account_id?: string | undefined;
63
- title?: string | undefined;
64
- label?: string | undefined;
65
- identifier?: string | undefined;
66
- is_private?: boolean | undefined;
67
- shared_with_all?: boolean | undefined;
68
- marked_stale_at?: string | undefined;
69
- }, {
70
- id: number;
71
- account_id?: string | undefined;
72
- title?: string | undefined;
73
- label?: string | undefined;
74
- identifier?: string | undefined;
75
- is_private?: boolean | undefined;
76
- shared_with_all?: boolean | undefined;
77
- marked_stale_at?: string | undefined;
78
- }>;
79
- export declare const FieldItemSchema: z.ZodObject<{
80
- key: z.ZodString;
81
- name: z.ZodOptional<z.ZodString>;
82
- description: z.ZodOptional<z.ZodString>;
83
- }, "strip", z.ZodTypeAny, {
84
- key: string;
85
- name?: string | undefined;
86
- description?: string | undefined;
87
- }, {
88
- key: string;
89
- name?: string | undefined;
90
- description?: string | undefined;
91
- }>;
92
- export type AppItem = z.infer<typeof AppItemSchema>;
93
- export type ActionItem = z.infer<typeof ActionItemSchema>;
94
- export type AuthItem = z.infer<typeof AuthItemSchema>;
95
- export type FieldItem = z.infer<typeof FieldItemSchema>;