@zapier/zapier-sdk 0.0.2 → 0.1.0

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 (167) 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 -76
  12. package/dist/api/types.js +9 -0
  13. package/dist/functions/bundleCode/index.d.ts +11 -0
  14. package/dist/functions/bundleCode/index.js +91 -0
  15. package/dist/functions/bundleCode/info.d.ts +27 -0
  16. package/dist/functions/bundleCode/info.js +11 -0
  17. package/dist/functions/bundleCode/schemas.d.ts +27 -0
  18. package/dist/functions/bundleCode/schemas.js +22 -0
  19. package/dist/functions/generateTypes/index.d.ts +11 -0
  20. package/dist/functions/generateTypes/index.js +305 -0
  21. package/dist/functions/generateTypes/info.d.ts +21 -0
  22. package/dist/functions/generateTypes/info.js +11 -0
  23. package/dist/functions/generateTypes/schemas.d.ts +30 -0
  24. package/dist/functions/generateTypes/schemas.js +14 -0
  25. package/dist/functions/getAction/index.d.ts +12 -0
  26. package/dist/functions/getAction/index.js +26 -0
  27. package/dist/functions/getAction/info.d.ts +18 -0
  28. package/dist/functions/getAction/info.js +11 -0
  29. package/dist/functions/getAction/schemas.d.ts +30 -0
  30. package/dist/functions/getAction/schemas.js +13 -0
  31. package/dist/functions/getApp/index.d.ts +12 -0
  32. package/dist/functions/getApp/index.js +37 -0
  33. package/dist/functions/getApp/info.d.ts +12 -0
  34. package/dist/functions/getApp/info.js +11 -0
  35. package/dist/functions/getApp/schemas.d.ts +24 -0
  36. package/dist/functions/getApp/schemas.js +11 -0
  37. package/dist/functions/listActions/index.d.ts +12 -0
  38. package/dist/functions/listActions/index.js +128 -0
  39. package/dist/functions/listActions/info.d.ts +15 -0
  40. package/dist/functions/listActions/info.js +11 -0
  41. package/dist/functions/listActions/schemas.d.ts +27 -0
  42. package/dist/functions/listActions/schemas.js +14 -0
  43. package/dist/functions/listApps/index.d.ts +12 -0
  44. package/dist/functions/listApps/index.js +50 -0
  45. package/dist/functions/listApps/info.d.ts +18 -0
  46. package/dist/functions/listApps/info.js +11 -0
  47. package/dist/functions/listApps/schemas.d.ts +30 -0
  48. package/dist/functions/listApps/schemas.js +15 -0
  49. package/dist/functions/listAuths/index.d.ts +12 -0
  50. package/dist/functions/listAuths/index.js +118 -0
  51. package/dist/functions/listAuths/info.d.ts +24 -0
  52. package/dist/functions/listAuths/info.js +11 -0
  53. package/dist/functions/listAuths/schemas.d.ts +36 -0
  54. package/dist/functions/listAuths/schemas.js +17 -0
  55. package/dist/functions/listFields/index.d.ts +12 -0
  56. package/dist/functions/listFields/index.js +65 -0
  57. package/dist/functions/listFields/info.d.ts +24 -0
  58. package/dist/functions/listFields/info.js +11 -0
  59. package/dist/functions/listFields/schemas.d.ts +36 -0
  60. package/dist/functions/listFields/schemas.js +17 -0
  61. package/dist/functions/runAction/index.d.ts +12 -0
  62. package/dist/functions/runAction/index.js +157 -0
  63. package/dist/functions/runAction/info.d.ts +24 -0
  64. package/dist/functions/runAction/info.js +11 -0
  65. package/dist/functions/runAction/schemas.d.ts +36 -0
  66. package/dist/functions/runAction/schemas.js +15 -0
  67. package/dist/index.d.ts +15 -3
  68. package/dist/index.js +27 -4
  69. package/dist/plugins/apps/index.d.ts +8 -0
  70. package/dist/plugins/apps/index.js +77 -0
  71. package/dist/plugins/apps/info.d.ts +6 -0
  72. package/dist/plugins/apps/info.js +13 -0
  73. package/dist/plugins/apps/types.d.ts +21 -0
  74. package/dist/plugins/apps/types.js +2 -0
  75. package/dist/resolvers/actionKey.d.ts +8 -0
  76. package/dist/resolvers/actionKey.js +20 -0
  77. package/dist/resolvers/actionType.d.ts +8 -0
  78. package/dist/resolvers/actionType.js +21 -0
  79. package/dist/resolvers/appKey.d.ts +6 -0
  80. package/dist/resolvers/appKey.js +8 -0
  81. package/dist/resolvers/authenticationId.d.ts +8 -0
  82. package/dist/resolvers/authenticationId.js +29 -0
  83. package/dist/resolvers/index.d.ts +39 -0
  84. package/dist/resolvers/index.js +105 -0
  85. package/dist/resolvers/inputs.d.ts +7 -0
  86. package/dist/resolvers/inputs.js +15 -0
  87. package/dist/schema-utils.d.ts +39 -0
  88. package/dist/schema-utils.js +52 -0
  89. package/dist/schemas/Action.d.ts +21 -0
  90. package/dist/schemas/Action.js +31 -0
  91. package/dist/schemas/App.d.ts +19 -0
  92. package/dist/schemas/App.js +32 -0
  93. package/dist/schemas/Auth.d.ts +30 -0
  94. package/dist/schemas/Auth.js +49 -0
  95. package/dist/schemas/Field.d.ts +15 -0
  96. package/dist/schemas/Field.js +25 -0
  97. package/dist/sdk.d.ts +3 -4
  98. package/dist/sdk.js +96 -11
  99. package/dist/types/domain.d.ts +22 -0
  100. package/dist/types/domain.js +21 -0
  101. package/dist/types/properties.d.ts +21 -0
  102. package/dist/types/properties.js +45 -0
  103. package/dist/types/sdk.d.ts +21 -0
  104. package/dist/types/sdk.js +2 -0
  105. package/package.json +4 -2
  106. package/src/api/auth.ts +28 -0
  107. package/src/api/client.ts +148 -0
  108. package/src/api/debug.ts +58 -0
  109. package/src/api/index.ts +83 -0
  110. package/src/api/polling.ts +56 -0
  111. package/src/{types.ts → api/types.ts} +51 -118
  112. package/src/functions/bundleCode/index.ts +78 -0
  113. package/src/functions/bundleCode/info.ts +9 -0
  114. package/src/functions/bundleCode/schemas.ts +30 -0
  115. package/src/functions/generateTypes/index.ts +348 -0
  116. package/src/functions/generateTypes/info.ts +9 -0
  117. package/src/functions/generateTypes/schemas.ts +38 -0
  118. package/src/functions/getAction/index.ts +33 -0
  119. package/src/functions/getAction/info.ts +9 -0
  120. package/src/functions/getAction/schemas.ts +35 -0
  121. package/src/functions/getApp/index.ts +41 -0
  122. package/src/functions/getApp/info.ts +9 -0
  123. package/src/functions/getApp/schemas.ts +31 -0
  124. package/src/functions/listActions/index.ts +149 -0
  125. package/src/functions/listActions/info.ts +9 -0
  126. package/src/functions/listActions/schemas.ts +40 -0
  127. package/src/functions/listApps/index.ts +60 -0
  128. package/src/functions/listApps/info.ts +9 -0
  129. package/src/functions/listApps/schemas.ts +43 -0
  130. package/src/functions/listAuths/index.ts +153 -0
  131. package/src/functions/listAuths/info.ts +9 -0
  132. package/src/functions/listAuths/schemas.ts +48 -0
  133. package/src/functions/listFields/index.ts +86 -0
  134. package/src/functions/listFields/info.ts +9 -0
  135. package/src/functions/listFields/schemas.ts +46 -0
  136. package/src/functions/runAction/index.ts +258 -0
  137. package/src/functions/runAction/info.ts +9 -0
  138. package/src/functions/runAction/schemas.ts +41 -0
  139. package/src/index.ts +24 -4
  140. package/src/plugins/apps/index.ts +144 -0
  141. package/src/plugins/apps/info.ts +12 -0
  142. package/src/plugins/apps/types.ts +34 -0
  143. package/src/resolvers/actionKey.ts +33 -0
  144. package/src/resolvers/actionType.ts +30 -0
  145. package/src/resolvers/appKey.ts +11 -0
  146. package/src/resolvers/authenticationId.ts +38 -0
  147. package/src/resolvers/index.ts +117 -0
  148. package/src/resolvers/inputs.ts +23 -0
  149. package/src/schema-utils.ts +119 -0
  150. package/src/schemas/Action.ts +40 -0
  151. package/src/schemas/App.ts +43 -0
  152. package/src/schemas/Auth.ts +62 -0
  153. package/src/schemas/Field.ts +34 -0
  154. package/src/sdk.ts +153 -19
  155. package/src/types/domain.ts +54 -0
  156. package/src/types/properties.ts +67 -0
  157. package/src/types/sdk.ts +42 -0
  158. package/dist/actions-sdk.d.ts +0 -51
  159. package/dist/actions-sdk.js +0 -1194
  160. package/dist/output-schemas.d.ts +0 -95
  161. package/dist/output-schemas.js +0 -138
  162. package/dist/schemas.d.ts +0 -338
  163. package/dist/schemas.js +0 -336
  164. package/dist/types.js +0 -41
  165. package/src/actions-sdk.ts +0 -1685
  166. package/src/output-schemas.ts +0 -196
  167. package/src/schemas.ts +0 -467
@@ -1,3 +1,54 @@
1
+ /**
2
+ * API Client Type Definitions
3
+ *
4
+ * This module contains all type definitions related to the Zapier API client,
5
+ * including configuration options, client interfaces, request/response types,
6
+ * and API response models.
7
+ */
8
+
9
+ // ============================================================================
10
+ // API Client Infrastructure Types
11
+ // ============================================================================
12
+
13
+ export interface ApiClientOptions {
14
+ baseUrl: string;
15
+ token?: string;
16
+ debug?: boolean;
17
+ fetch?: typeof globalThis.fetch;
18
+ }
19
+
20
+ export interface ApiClient {
21
+ get: (path: string, options?: RequestOptions) => Promise<any>;
22
+ post: (path: string, data?: any, options?: RequestOptions) => Promise<any>;
23
+ put: (path: string, data?: any, options?: RequestOptions) => Promise<any>;
24
+ delete: (path: string, options?: RequestOptions) => Promise<any>;
25
+ poll: (path: string, options?: PollOptions) => Promise<any>;
26
+ }
27
+
28
+ export interface RequestOptions {
29
+ headers?: Record<string, string>;
30
+ searchParams?: Record<string, string>;
31
+ authRequired?: boolean;
32
+ customErrorHandler?: (response: Response) => Error | undefined;
33
+ }
34
+
35
+ export interface PollOptions extends RequestOptions {
36
+ maxAttempts?: number;
37
+ initialDelay?: number;
38
+ maxDelay?: number;
39
+ successStatus?: number;
40
+ pendingStatus?: number;
41
+ resultExtractor?: (response: any) => any;
42
+ }
43
+
44
+ export interface DebugLogger {
45
+ (message: string, data?: any): void;
46
+ }
47
+
48
+ // ============================================================================
49
+ // API Response Models
50
+ // ============================================================================
51
+
1
52
  export interface Integration {
2
53
  key: string;
3
54
  name: string;
@@ -49,19 +100,6 @@ export interface Choice {
49
100
  label: string;
50
101
  }
51
102
 
52
- export interface ListOptions {
53
- category?: string;
54
- appKey?: string;
55
- type?: string;
56
- limit?: number;
57
- offset?: number;
58
- }
59
-
60
- export interface ActionExecutionOptions {
61
- inputs?: Record<string, any>;
62
- authId?: number;
63
- }
64
-
65
103
  export interface ActionExecutionResult {
66
104
  success: boolean;
67
105
  data?: any;
@@ -108,40 +146,6 @@ export interface NeedsResponse {
108
146
  errors?: string[];
109
147
  }
110
148
 
111
- // Future extensibility types
112
- export interface Workflow {
113
- id: string;
114
- name: string;
115
- description: string;
116
- steps: WorkflowStep[];
117
- }
118
-
119
- export interface WorkflowStep {
120
- id: string;
121
- type: "trigger" | "action";
122
- appKey: string;
123
- actionKey?: string;
124
- triggerKey?: string;
125
- }
126
-
127
- export interface Interface {
128
- key: string;
129
- name: string;
130
- fields: Field[];
131
- }
132
-
133
- export interface Table {
134
- key: string;
135
- name: string;
136
- columns: TableColumn[];
137
- }
138
-
139
- export interface TableColumn {
140
- key: string;
141
- name: string;
142
- type: string;
143
- }
144
-
145
149
  export interface Authentication {
146
150
  id: number;
147
151
  date: string;
@@ -171,74 +175,3 @@ export interface AuthenticationsResponse {
171
175
  previous?: string | null;
172
176
  results: Authentication[];
173
177
  }
174
-
175
- export interface AuthenticationListOptions {
176
- account_id?: string;
177
- owner?: string;
178
- appKey?: string;
179
- limit?: number;
180
- offset?: number;
181
- }
182
-
183
- // Custom error classes for better error handling
184
- export class ZapierSdkError extends Error {
185
- constructor(
186
- message: string,
187
- public code?: string,
188
- ) {
189
- super(message);
190
- this.name = "ZapierSdkError";
191
- }
192
- }
193
-
194
- export class AppNotFoundError extends ZapierSdkError {
195
- constructor(appKey: string) {
196
- super(`App "${appKey}" not found`);
197
- this.name = "AppNotFoundError";
198
- this.code = "APP_NOT_FOUND";
199
- }
200
- }
201
-
202
- export class ActionNotFoundError extends ZapierSdkError {
203
- constructor(appKey: string, actionKey: string) {
204
- super(`Action "${actionKey}" not found in app "${appKey}"`);
205
- this.name = "ActionNotFoundError";
206
- this.code = "ACTION_NOT_FOUND";
207
- }
208
- }
209
-
210
- export class AuthenticationError extends ZapierSdkError {
211
- constructor(message: string) {
212
- super(message);
213
- this.name = "AuthenticationError";
214
- this.code = "AUTHENTICATION_ERROR";
215
- }
216
- }
217
-
218
- // Auth configuration types
219
- export interface AuthObject {
220
- id: number;
221
- }
222
-
223
- // Common SDK interfaces
224
- export interface BaseSdkOptions {
225
- token?: string;
226
- authentications?: {
227
- [appKey: string]: AuthObject[];
228
- };
229
- fetch?: typeof fetch;
230
- baseUrl?: string;
231
- debug?: boolean;
232
- }
233
-
234
- export interface CombinedSdk {
235
- [namespace: string]: any;
236
- }
237
-
238
- // Factory function type for creating individual SDKs
239
- export type SdkFactory<T = any> = (options: BaseSdkOptions) => T;
240
-
241
- // Utility function to combine multiple SDKs
242
- export function combineSdks(sdks: Record<string, any>): CombinedSdk {
243
- return { ...sdks };
244
- }
@@ -0,0 +1,78 @@
1
+ import type { BundleCodeOptions } from "./schemas";
2
+
3
+ /**
4
+ * Bundle TypeScript code into executable JavaScript
5
+ *
6
+ * This function can be used standalone without instantiating a full SDK,
7
+ * which enables better tree-shaking in applications that only need this functionality.
8
+ *
9
+ * @param options - Bundling configuration options
10
+ * @returns Promise<string> - Bundled JavaScript code
11
+ */
12
+ export async function bundleCode(options: BundleCodeOptions): Promise<string> {
13
+ const {
14
+ input,
15
+ output,
16
+ target = "es2020",
17
+ cjs = false,
18
+ minify = false,
19
+ string: returnString = false,
20
+ } = options;
21
+
22
+ // Dynamically import esbuild
23
+ const { buildSync } = await import("esbuild");
24
+ const fs = await import("fs");
25
+ const path = await import("path");
26
+
27
+ // Resolve input path
28
+ const resolvedInput = path.resolve(process.cwd(), input);
29
+
30
+ try {
31
+ // Bundle with esbuild
32
+ const result = buildSync({
33
+ entryPoints: [resolvedInput],
34
+ bundle: true,
35
+ platform: "node",
36
+ target: target,
37
+ format: cjs ? "cjs" : "esm",
38
+ minify: minify,
39
+ write: false,
40
+ external: [], // Bundle everything
41
+ banner: {
42
+ js: "#!/usr/bin/env node",
43
+ },
44
+ });
45
+
46
+ if (result.errors.length > 0) {
47
+ throw new Error(
48
+ `Bundle failed: ${result.errors.map((e) => e.text).join(", ")}`,
49
+ );
50
+ }
51
+
52
+ const bundledCode = result.outputFiles?.[0]?.text;
53
+ if (!bundledCode) {
54
+ throw new Error("No output generated");
55
+ }
56
+
57
+ let finalOutput = bundledCode;
58
+
59
+ if (returnString) {
60
+ // Output as quoted string for node -e using JSON.stringify
61
+ finalOutput = JSON.stringify(bundledCode);
62
+ }
63
+
64
+ // Write to file if output path specified
65
+ if (output) {
66
+ fs.mkdirSync(path.dirname(output), { recursive: true });
67
+ fs.writeFileSync(output, finalOutput, "utf8");
68
+ }
69
+
70
+ return finalOutput;
71
+ } catch (error) {
72
+ throw new Error(
73
+ `Bundle failed: ${
74
+ error instanceof Error ? error.message : "Unknown error"
75
+ }`,
76
+ );
77
+ }
78
+ }
@@ -0,0 +1,9 @@
1
+ import { bundleCode } from "./index";
2
+ import { BundleCodeSchema } from "./schemas";
3
+
4
+ // Function registry info - imports both function and schema
5
+ export const bundleCodeInfo = {
6
+ name: bundleCode.name,
7
+ inputSchema: BundleCodeSchema,
8
+ implementation: bundleCode,
9
+ };
@@ -0,0 +1,30 @@
1
+ import { z } from "zod";
2
+ import { OutputPropertySchema } from "../../types/properties";
3
+
4
+ // Pure Zod schema - no resolver metadata!
5
+ export const BundleCodeSchema = z
6
+ .object({
7
+ input: z.string().min(1).describe("Input TypeScript file path to bundle"),
8
+ output: OutputPropertySchema.optional().describe(
9
+ "Output file path (defaults to input with .js extension)",
10
+ ),
11
+ string: z
12
+ .boolean()
13
+ .default(false)
14
+ .describe("Return bundled code as string instead of writing to file"),
15
+ minify: z.boolean().default(false).describe("Minify the bundled output"),
16
+ target: z.string().default("es2017").describe("ECMAScript target version"),
17
+ cjs: z
18
+ .boolean()
19
+ .default(false)
20
+ .describe("Output CommonJS format instead of ESM"),
21
+ })
22
+ .describe("Bundle TypeScript code into executable JavaScript");
23
+
24
+ // Type inferred from schema (no additional function config needed for bundleCode)
25
+ export type BundleCodeOptions = z.infer<typeof BundleCodeSchema>;
26
+
27
+ // SDK function interface - ready to be mixed into main SDK interface
28
+ export interface BundleCodeSdkFunction {
29
+ bundleCode: (options: BundleCodeOptions) => Promise<string>;
30
+ }
@@ -0,0 +1,348 @@
1
+ import type { Action, ActionField } from "../../types/domain";
2
+ import { listActions } from "../listActions";
3
+ import { listFields } from "../listFields";
4
+ import type { GenerateTypesOptions } from "./schemas";
5
+
6
+ interface ActionWithActionFields extends Omit<Action, "inputFields"> {
7
+ inputFields: ActionField[];
8
+ }
9
+
10
+ /**
11
+ * Generate TypeScript types for a specific app
12
+ *
13
+ * This function can be used standalone without instantiating a full SDK,
14
+ * which enables better tree-shaking in applications that only need this functionality.
15
+ *
16
+ * @param options - App key, authentication ID, output path, and API configuration options
17
+ * @returns Promise<string> - Generated TypeScript code
18
+ */
19
+ export async function generateTypes(
20
+ options: GenerateTypesOptions,
21
+ ): Promise<string> {
22
+ const {
23
+ appKey,
24
+ authenticationId,
25
+ output = `./types/${appKey}.d.ts`,
26
+ } = options;
27
+
28
+ // Parse app identifier (support app@version format)
29
+ const { app, version } = parseAppIdentifier(appKey);
30
+
31
+ // Fetch all actions for the app
32
+ const actions = await listActions({
33
+ ...options,
34
+ appKey: app,
35
+ });
36
+
37
+ if (actions.length === 0) {
38
+ const typeDefinitions = generateEmptyTypesFile(app, version);
39
+
40
+ if (output) {
41
+ const fs = await import("fs");
42
+ const path = await import("path");
43
+ fs.mkdirSync(path.dirname(output), { recursive: true });
44
+ fs.writeFileSync(output, typeDefinitions, "utf8");
45
+ }
46
+
47
+ return typeDefinitions;
48
+ }
49
+
50
+ // Fetch input fields for each action
51
+ const actionsWithFields: ActionWithActionFields[] = [];
52
+
53
+ if (authenticationId) {
54
+ for (const action of actions) {
55
+ try {
56
+ const fields = await listFields({
57
+ ...options,
58
+ appKey: action.appKey,
59
+ actionKey: action.key,
60
+ actionType: action.type,
61
+ authenticationId: authenticationId,
62
+ });
63
+ actionsWithFields.push({ ...action, inputFields: fields });
64
+ } catch {
65
+ // If we can't get fields for an action, include it without fields
66
+ actionsWithFields.push({ ...action, inputFields: [] });
67
+ }
68
+ }
69
+ } else {
70
+ // Convert actions to have empty input fields (will generate generic types)
71
+ actions.forEach((action) => {
72
+ actionsWithFields.push({ ...action, inputFields: [] });
73
+ });
74
+ }
75
+
76
+ // Generate TypeScript types
77
+ const typeDefinitions = generateTypeDefinitions(
78
+ app,
79
+ actionsWithFields,
80
+ version,
81
+ );
82
+
83
+ // Write to file if output path specified
84
+ if (output) {
85
+ const fs = await import("fs");
86
+ const path = await import("path");
87
+ fs.mkdirSync(path.dirname(output), { recursive: true });
88
+ fs.writeFileSync(output, typeDefinitions, "utf8");
89
+ }
90
+
91
+ return typeDefinitions;
92
+ }
93
+
94
+ function parseAppIdentifier(identifier: string): {
95
+ app: string;
96
+ version?: string;
97
+ } {
98
+ const parts = identifier.split("@");
99
+ return {
100
+ app: parts[0],
101
+ version: parts[1],
102
+ };
103
+ }
104
+
105
+ function generateTypeDefinitions(
106
+ appKey: string,
107
+ actions: ActionWithActionFields[],
108
+ version?: string,
109
+ ): string {
110
+ // Handle empty actions
111
+ if (actions.length === 0) {
112
+ return generateEmptyTypesFile(appKey, version);
113
+ }
114
+
115
+ // Group actions by type
116
+ const actionsByType = actions.reduce(
117
+ (acc, action) => {
118
+ if (!acc[action.type]) {
119
+ acc[action.type] = [];
120
+ }
121
+ acc[action.type].push(action);
122
+ return acc;
123
+ },
124
+ {} as Record<string, ActionWithActionFields[]>,
125
+ );
126
+
127
+ const appName = capitalize(appKey);
128
+ const versionComment = version
129
+ ? ` * Generated for ${appKey}@${version}`
130
+ : ` * Generated for ${appKey}`;
131
+
132
+ let output = `/* eslint-disable @typescript-eslint/naming-convention */
133
+ /**
134
+ * Auto-generated TypeScript types for Zapier ${appKey} actions
135
+ ${versionComment}
136
+ * Generated on: ${new Date().toISOString()}
137
+ *
138
+ * Usage:
139
+ * import type { ${appName}Sdk } from './path/to/this/file'
140
+ * const sdk = createZapierSdk() as unknown as ${appName}Sdk
141
+ *
142
+ * // Direct usage (per-call auth):
143
+ * await sdk.apps.${appKey}.search.user_by_email({ authenticationId: 123, inputs: { email } })
144
+ *
145
+ * // Factory usage (pinned auth):
146
+ * const my${appName} = sdk.apps.${appKey}({ authenticationId: 123 })
147
+ * await my${appName}.search.user_by_email({ inputs: { email } })
148
+ */
149
+
150
+ import type { ActionExecutionOptions, ActionExecutionResult } from '@zapier/zapier-sdk'
151
+
152
+ `;
153
+
154
+ // Generate input types for each action
155
+ actions.forEach((action) => {
156
+ if (action.inputFields.length > 0) {
157
+ const inputTypeName = `${appName}${capitalize(action.type)}${capitalize(
158
+ sanitizeActionName(action.key),
159
+ )}Inputs`;
160
+
161
+ output += `interface ${inputTypeName} {\n`;
162
+
163
+ action.inputFields.forEach((field) => {
164
+ const isOptional = !field.required;
165
+ const fieldType = mapFieldTypeToTypeScript(field);
166
+ const description = field.helpText
167
+ ? ` /** ${escapeComment(field.helpText)} */\n`
168
+ : "";
169
+
170
+ output += `${description} ${sanitizeFieldName(field.key)}${
171
+ isOptional ? "?" : ""
172
+ }: ${fieldType}\n`;
173
+ });
174
+
175
+ output += `}\n\n`;
176
+ }
177
+ });
178
+
179
+ // Generate action type interfaces for each action type
180
+ Object.entries(actionsByType).forEach(([actionType, typeActions]) => {
181
+ const typeName = `${appName}${capitalize(actionType)}Actions`;
182
+
183
+ output += `interface ${typeName} {\n`;
184
+
185
+ typeActions.forEach((action: ActionWithActionFields) => {
186
+ const actionName = sanitizeActionName(action.key);
187
+ const description = action.description
188
+ ? ` /** ${escapeComment(action.description)} */\n`
189
+ : "";
190
+
191
+ // Generate type-safe action method signature
192
+ if (action.inputFields.length > 0) {
193
+ const inputTypeName = `${appName}${capitalize(action.type)}${capitalize(
194
+ sanitizeActionName(action.key),
195
+ )}Inputs`;
196
+ output += `${description} ${actionName}: (options: { inputs: ${inputTypeName} } & Omit<ActionExecutionOptions, 'inputs'>) => Promise<ActionExecutionResult>\n`;
197
+ } else {
198
+ // No specific input fields available - use generic Record<string, any> for inputs
199
+ output += `${description} ${actionName}: (options?: { inputs?: Record<string, any> } & ActionExecutionOptions) => Promise<ActionExecutionResult>\n`;
200
+ }
201
+ });
202
+
203
+ output += `}\n\n`;
204
+ });
205
+
206
+ // Generate the main app SDK interface with factory pattern support
207
+
208
+ // Generate the app proxy interface (actions grouped by type)
209
+ output += `interface ${appName}AppProxy {\n`;
210
+ Object.keys(actionsByType).forEach((actionType) => {
211
+ const typeName = `${appName}${capitalize(actionType)}Actions`;
212
+ output += ` ${actionType}: ${typeName}\n`;
213
+ });
214
+ output += `}\n\n`;
215
+
216
+ // Generate the factory function interface
217
+ output += `interface ${appName}AppFactory {\n`;
218
+ output += ` (options: { authenticationId: number }): ${appName}AppProxy\n`;
219
+ output += `}\n\n`;
220
+
221
+ // Combine factory and direct access
222
+ output += `type ${appName}AppWithFactory = ${appName}AppFactory & ${appName}AppProxy\n\n`;
223
+
224
+ // Generate the main SDK interface
225
+ output += `export interface ${appName}Sdk {\n`;
226
+ output += ` apps: {\n`;
227
+ output += ` ${appKey}: ${appName}AppWithFactory\n`;
228
+ output += ` }\n`;
229
+ output += `}\n`;
230
+
231
+ return output;
232
+ }
233
+
234
+ function generateEmptyTypesFile(appKey: string, version?: string): string {
235
+ const appName = capitalize(appKey);
236
+ const versionComment = version
237
+ ? ` * Generated for ${appKey}@${version}`
238
+ : ` * Generated for ${appKey}`;
239
+
240
+ return `/* eslint-disable @typescript-eslint/naming-convention */
241
+ /**
242
+ * Auto-generated TypeScript types for Zapier ${appKey} actions
243
+ ${versionComment}
244
+ * Generated on: ${new Date().toISOString()}
245
+ *
246
+ * No actions found for this app.
247
+ */
248
+
249
+ import type { ActionExecutionOptions, ActionExecutionResult } from '@zapier/zapier-sdk'
250
+
251
+ interface ${appName}AppProxy {
252
+ // No actions available
253
+ }
254
+
255
+ interface ${appName}AppFactory {
256
+ (options: { authenticationId: number }): ${appName}AppProxy
257
+ }
258
+
259
+ type ${appName}AppWithFactory = ${appName}AppFactory & ${appName}AppProxy
260
+
261
+ export interface ${appName}Sdk {
262
+ apps: {
263
+ ${appKey}: ${appName}AppWithFactory
264
+ }
265
+ }
266
+ `;
267
+ }
268
+
269
+ function capitalize(str: string): string {
270
+ return str.charAt(0).toUpperCase() + str.slice(1).replace(/[-_]/g, "");
271
+ }
272
+
273
+ function sanitizeActionName(actionKey: string): string {
274
+ // Ensure the action name is a valid TypeScript identifier
275
+ let sanitized = actionKey.replace(/[^a-zA-Z0-9_$]/g, "_");
276
+
277
+ // If it starts with a number, prepend an underscore
278
+ if (/^[0-9]/.test(sanitized)) {
279
+ sanitized = "_" + sanitized;
280
+ }
281
+
282
+ return sanitized;
283
+ }
284
+
285
+ function sanitizeFieldName(fieldKey: string): string {
286
+ // Ensure the field name is a valid TypeScript identifier
287
+ let sanitized = fieldKey.replace(/[^a-zA-Z0-9_$]/g, "_");
288
+
289
+ // If it starts with a number, prepend an underscore
290
+ if (/^[0-9]/.test(sanitized)) {
291
+ sanitized = "_" + sanitized;
292
+ }
293
+
294
+ return sanitized;
295
+ }
296
+
297
+ function escapeComment(comment: string): string {
298
+ // Escape comment text to prevent breaking the JSDoc comment
299
+ return comment.replace(/\*\//g, "*\\/").replace(/\r?\n/g, " ");
300
+ }
301
+
302
+ function mapFieldTypeToTypeScript(field: ActionField): string {
303
+ // Handle choices (enum-like fields)
304
+ if (field.choices && field.choices.length > 0) {
305
+ const choiceValues = field.choices
306
+ .filter(
307
+ (choice) =>
308
+ choice.value !== undefined &&
309
+ choice.value !== null &&
310
+ choice.value !== "",
311
+ )
312
+ .map((choice) =>
313
+ typeof choice.value === "string" ? `"${choice.value}"` : choice.value,
314
+ );
315
+
316
+ if (choiceValues.length > 0) {
317
+ return choiceValues.join(" | ");
318
+ }
319
+ // If all choices were filtered out, fall through to default type handling
320
+ }
321
+
322
+ // Map Zapier field types to TypeScript types
323
+ switch (field.type?.toLowerCase()) {
324
+ case "string":
325
+ case "text":
326
+ case "email":
327
+ case "url":
328
+ case "password":
329
+ return "string";
330
+ case "integer":
331
+ case "number":
332
+ return "number";
333
+ case "boolean":
334
+ return "boolean";
335
+ case "datetime":
336
+ case "date":
337
+ return "string"; // ISO date strings
338
+ case "file":
339
+ return "string"; // File URL or content
340
+ case "array":
341
+ return "any[]";
342
+ case "object":
343
+ return "Record<string, any>";
344
+ default:
345
+ // Default to string for unknown types, with union for common cases
346
+ return "string | number | boolean";
347
+ }
348
+ }
@@ -0,0 +1,9 @@
1
+ import { generateTypes } from "./index";
2
+ import { GenerateTypesSchema } from "./schemas";
3
+
4
+ // Function registry info - imports both function and schema
5
+ export const generateTypesInfo = {
6
+ name: generateTypes.name,
7
+ inputSchema: GenerateTypesSchema,
8
+ implementation: generateTypes,
9
+ };
@@ -0,0 +1,38 @@
1
+ import { z } from "zod";
2
+ import {
3
+ AppKeyPropertySchema,
4
+ AuthenticationIdPropertySchema,
5
+ OutputPropertySchema,
6
+ DebugPropertySchema,
7
+ } from "../../types/properties";
8
+
9
+ // Pure Zod schema - no resolver metadata!
10
+ export const GenerateTypesSchema = z
11
+ .object({
12
+ appKey: AppKeyPropertySchema.describe("App key to generate SDK code for"),
13
+ authenticationId: AuthenticationIdPropertySchema.optional(),
14
+ output: OutputPropertySchema.optional().describe(
15
+ "Output file path (defaults to generated/<appKey>.ts)",
16
+ ),
17
+ debug: DebugPropertySchema.describe(
18
+ "Enable debug logging during generation",
19
+ ),
20
+ })
21
+ .describe("Generate TypeScript SDK code for a specific app");
22
+
23
+ // Type inferred from schema + function config
24
+ export type GenerateTypesOptions = z.infer<typeof GenerateTypesSchema> & {
25
+ /** Base URL for Zapier API */
26
+ baseUrl?: string;
27
+ /** Authentication token */
28
+ token?: string;
29
+ /** Optional pre-instantiated API client */
30
+ api?: any;
31
+ /** Custom fetch implementation */
32
+ fetch?: typeof globalThis.fetch;
33
+ };
34
+
35
+ // SDK function interface - ready to be mixed into main SDK interface
36
+ export interface GenerateTypesSdkFunction {
37
+ generateTypes: (options: GenerateTypesOptions) => Promise<string>;
38
+ }