@zapier/zapier-sdk-cli 0.6.3 → 0.6.4
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 +9 -0
- package/README.md +115 -12
- package/bin/{zapier-sdk.js → zapier-sdk.mjs} +1 -1
- package/dist/cli.cjs +1711 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/{cli.js → cli.mjs} +204 -456
- package/dist/index.cjs +770 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.mjs +739 -0
- package/dist/package.json +67 -0
- package/dist/src/cli.js +8 -14
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -1
- package/dist/src/plugins/bundleCode/index.d.ts +15 -0
- package/dist/src/{commands/bundle-code → plugins/bundleCode}/index.js +19 -1
- package/dist/src/{commands/bundle-code → plugins/bundleCode}/schemas.d.ts +8 -8
- package/dist/src/{commands/bundle-code → plugins/bundleCode}/schemas.js +4 -4
- package/dist/src/plugins/generateTypes/index.d.ts +21 -0
- package/dist/src/{commands/generate-types → plugins/generateTypes}/index.js +18 -0
- package/dist/src/{commands/generate-types → plugins/generateTypes}/schemas.d.ts +0 -3
- package/dist/src/{commands/generate-types → plugins/generateTypes}/schemas.js +1 -2
- package/dist/src/plugins/getConfigPath/index.d.ts +15 -0
- package/dist/src/plugins/getConfigPath/index.js +19 -0
- package/dist/src/plugins/getConfigPath/schemas.d.ts +3 -0
- package/dist/src/plugins/getConfigPath/schemas.js +5 -0
- package/dist/src/plugins/index.d.ts +6 -0
- package/dist/src/plugins/index.js +6 -0
- package/dist/src/plugins/login/index.d.ts +15 -0
- package/dist/src/plugins/login/index.js +26 -0
- package/dist/src/plugins/login/schemas.d.ts +9 -0
- package/dist/src/plugins/login/schemas.js +10 -0
- package/dist/src/plugins/logout/index.d.ts +15 -0
- package/dist/src/plugins/logout/index.js +18 -0
- package/dist/src/plugins/logout/schemas.d.ts +3 -0
- package/dist/src/plugins/logout/schemas.js +5 -0
- package/dist/src/plugins/mcp/index.d.ts +15 -0
- package/dist/src/plugins/mcp/index.js +24 -0
- package/dist/src/plugins/mcp/schemas.d.ts +9 -0
- package/dist/src/plugins/mcp/schemas.js +10 -0
- package/dist/src/sdk.d.ts +9 -0
- package/dist/src/sdk.js +22 -0
- package/dist/src/utils/cli-generator.js +4 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +19 -8
- package/src/cli.ts +8 -21
- package/src/index.ts +2 -0
- package/src/{commands/bundle-code → plugins/bundleCode}/index.ts +39 -2
- package/src/{commands/bundle-code → plugins/bundleCode}/schemas.ts +4 -4
- package/src/{commands/generate-types → plugins/generateTypes}/index.ts +56 -3
- package/src/{commands/generate-types → plugins/generateTypes}/schemas.ts +0 -4
- package/src/plugins/getConfigPath/index.ts +42 -0
- package/src/plugins/getConfigPath/schemas.ts +8 -0
- package/src/plugins/index.ts +6 -0
- package/src/plugins/login/index.ts +48 -0
- package/src/plugins/login/schemas.ts +13 -0
- package/src/plugins/logout/index.ts +37 -0
- package/src/plugins/logout/schemas.ts +8 -0
- package/src/plugins/mcp/index.ts +43 -0
- package/src/plugins/mcp/schemas.ts +13 -0
- package/src/sdk.ts +43 -0
- package/src/utils/cli-generator.ts +5 -0
- package/tsconfig.build.json +15 -3
- package/tsconfig.json +2 -2
- package/tsup.config.ts +10 -4
- package/bin/zsdk.js +0 -4
- package/dist/index.js +0 -0
- package/dist/src/commands/bundle-code/cli.d.ts +0 -2
- package/dist/src/commands/bundle-code/cli.js +0 -77
- package/dist/src/commands/bundle-code/index.d.ts +0 -5
- package/dist/src/commands/configPath.d.ts +0 -2
- package/dist/src/commands/configPath.js +0 -9
- package/dist/src/commands/generate-types/cli.d.ts +0 -2
- package/dist/src/commands/generate-types/cli.js +0 -84
- package/dist/src/commands/generate-types/index.d.ts +0 -8
- package/dist/src/commands/index.d.ts +0 -6
- package/dist/src/commands/index.js +0 -6
- package/dist/src/commands/login.d.ts +0 -2
- package/dist/src/commands/login.js +0 -25
- package/dist/src/commands/logout.d.ts +0 -2
- package/dist/src/commands/logout.js +0 -16
- package/dist/src/commands/mcp.d.ts +0 -2
- package/dist/src/commands/mcp.js +0 -11
- package/src/commands/bundle-code/cli.ts +0 -118
- package/src/commands/configPath.ts +0 -10
- package/src/commands/generate-types/cli.ts +0 -126
- package/src/commands/index.ts +0 -6
- package/src/commands/login.ts +0 -34
- package/src/commands/logout.ts +0 -19
- package/src/commands/mcp.ts +0 -14
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,739 @@
|
|
|
1
|
+
import { createFunction, OutputPropertySchema, AuthenticationIdPropertySchema, AppKeyPropertySchema, createZapierSdkWithoutRegistry, registryPlugin } from '@zapier/zapier-sdk';
|
|
2
|
+
import open from 'open';
|
|
3
|
+
import crypto from 'crypto';
|
|
4
|
+
import express from 'express';
|
|
5
|
+
import pkceChallenge from 'pkce-challenge';
|
|
6
|
+
import ora from 'ora';
|
|
7
|
+
import chalk from 'chalk';
|
|
8
|
+
import { getLoggedInUser, logout, updateLogin, getConfigPath } from '@zapier/zapier-sdk-cli-login';
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
import { startMcpServerAsProcess } from '@zapier/zapier-sdk-mcp';
|
|
11
|
+
import * as fs from 'fs';
|
|
12
|
+
import * as path from 'path';
|
|
13
|
+
import { buildSync } from 'esbuild';
|
|
14
|
+
|
|
15
|
+
// src/sdk.ts
|
|
16
|
+
|
|
17
|
+
// src/utils/constants.ts
|
|
18
|
+
var ZAPIER_BASE = "https://zapier.com";
|
|
19
|
+
var LOGIN_CLIENT_ID = "K5eEnRE9TTmSFATdkkWhKF8NOKwoiOnYAyIqJjae";
|
|
20
|
+
var LOGIN_PORTS = [49505, 50575, 52804, 55981, 61010, 63851];
|
|
21
|
+
var LOGIN_TIMEOUT_MS = 3e5;
|
|
22
|
+
var AUTH_MODE_HEADER = "X-Auth";
|
|
23
|
+
var spinPromise = async (promise, text) => {
|
|
24
|
+
const spinner = ora(text).start();
|
|
25
|
+
try {
|
|
26
|
+
const result = await promise;
|
|
27
|
+
spinner.succeed();
|
|
28
|
+
return result;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
spinner.fail();
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var log = {
|
|
35
|
+
info: (message, ...args) => {
|
|
36
|
+
console.log(chalk.blue("\u2139"), message, ...args);
|
|
37
|
+
},
|
|
38
|
+
error: (message, ...args) => {
|
|
39
|
+
console.error(chalk.red("\u2716"), message, ...args);
|
|
40
|
+
},
|
|
41
|
+
success: (message, ...args) => {
|
|
42
|
+
console.log(chalk.green("\u2713"), message, ...args);
|
|
43
|
+
},
|
|
44
|
+
warn: (message, ...args) => {
|
|
45
|
+
console.log(chalk.yellow("\u26A0"), message, ...args);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var log_default = log;
|
|
49
|
+
|
|
50
|
+
// src/utils/api/client.ts
|
|
51
|
+
var createApiClient = () => {
|
|
52
|
+
const post = async (url, data, options = {}) => {
|
|
53
|
+
const { headers = {} } = options;
|
|
54
|
+
const response = await fetch(url, {
|
|
55
|
+
method: "POST",
|
|
56
|
+
headers: {
|
|
57
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
58
|
+
Connection: "close",
|
|
59
|
+
...headers
|
|
60
|
+
},
|
|
61
|
+
body: new URLSearchParams(data)
|
|
62
|
+
});
|
|
63
|
+
if (!response.ok) {
|
|
64
|
+
throw new Error(`${response.status} ${response.statusText}`);
|
|
65
|
+
}
|
|
66
|
+
const responseData = await response.json();
|
|
67
|
+
return {
|
|
68
|
+
data: responseData,
|
|
69
|
+
status: response.status
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
post
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
var api = createApiClient();
|
|
77
|
+
var client_default = api;
|
|
78
|
+
|
|
79
|
+
// src/utils/getCallablePromise.ts
|
|
80
|
+
var getCallablePromise = () => {
|
|
81
|
+
let resolve2 = () => {
|
|
82
|
+
};
|
|
83
|
+
let reject = () => {
|
|
84
|
+
};
|
|
85
|
+
const promise = new Promise((_resolve, _reject) => {
|
|
86
|
+
resolve2 = _resolve;
|
|
87
|
+
reject = _reject;
|
|
88
|
+
});
|
|
89
|
+
return {
|
|
90
|
+
promise,
|
|
91
|
+
resolve: resolve2,
|
|
92
|
+
reject
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
var getCallablePromise_default = getCallablePromise;
|
|
96
|
+
var findAvailablePort = () => {
|
|
97
|
+
return new Promise((resolve2, reject) => {
|
|
98
|
+
let portIndex = 0;
|
|
99
|
+
const tryPort = (port) => {
|
|
100
|
+
const server = express().listen(port, () => {
|
|
101
|
+
server.close();
|
|
102
|
+
resolve2(port);
|
|
103
|
+
});
|
|
104
|
+
server.on("error", (err) => {
|
|
105
|
+
if (err.code === "EADDRINUSE") {
|
|
106
|
+
if (portIndex < LOGIN_PORTS.length) {
|
|
107
|
+
tryPort(LOGIN_PORTS[portIndex++]);
|
|
108
|
+
} else {
|
|
109
|
+
reject(
|
|
110
|
+
new Error(
|
|
111
|
+
`All configured OAuth callback ports are busy: ${LOGIN_PORTS.join(", ")}. Please try again later or close applications using these ports.`
|
|
112
|
+
)
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
reject(err);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
if (LOGIN_PORTS.length > 0) {
|
|
121
|
+
tryPort(LOGIN_PORTS[portIndex++]);
|
|
122
|
+
} else {
|
|
123
|
+
reject(new Error("No OAuth callback ports configured"));
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
var generateRandomString = () => {
|
|
128
|
+
const array = new Uint32Array(28);
|
|
129
|
+
crypto.getRandomValues(array);
|
|
130
|
+
return Array.from(
|
|
131
|
+
array,
|
|
132
|
+
(dec) => ("0" + dec.toString(16)).substring(-2)
|
|
133
|
+
).join("");
|
|
134
|
+
};
|
|
135
|
+
var login = async (timeoutMs = LOGIN_TIMEOUT_MS) => {
|
|
136
|
+
logout();
|
|
137
|
+
const availablePort = await findAvailablePort();
|
|
138
|
+
const redirectUri = `http://localhost:${availablePort}/oauth`;
|
|
139
|
+
log_default.info(`Using port ${availablePort} for OAuth callback`);
|
|
140
|
+
const { promise: promisedCode, resolve: setCode } = getCallablePromise_default();
|
|
141
|
+
const app = express();
|
|
142
|
+
app.get("/oauth", (req, res) => {
|
|
143
|
+
setCode(String(req.query.code));
|
|
144
|
+
res.setHeader("Connection", "close");
|
|
145
|
+
res.end("You can now close this tab and return to the CLI.");
|
|
146
|
+
});
|
|
147
|
+
const server = app.listen(availablePort);
|
|
148
|
+
const connections = /* @__PURE__ */ new Set();
|
|
149
|
+
server.on("connection", (conn) => {
|
|
150
|
+
connections.add(conn);
|
|
151
|
+
conn.on("close", () => connections.delete(conn));
|
|
152
|
+
});
|
|
153
|
+
const cleanup = () => {
|
|
154
|
+
server.close();
|
|
155
|
+
log_default.info("\n\u274C Login cancelled by user");
|
|
156
|
+
process.exit(0);
|
|
157
|
+
};
|
|
158
|
+
process.on("SIGINT", cleanup);
|
|
159
|
+
process.on("SIGTERM", cleanup);
|
|
160
|
+
const { code_verifier: codeVerifier, code_challenge: codeChallenge } = await pkceChallenge();
|
|
161
|
+
const authUrl = `${ZAPIER_BASE}/oauth/authorize/?${new URLSearchParams({
|
|
162
|
+
response_type: "code",
|
|
163
|
+
client_id: LOGIN_CLIENT_ID,
|
|
164
|
+
redirect_uri: redirectUri,
|
|
165
|
+
scope: "internal offline_access",
|
|
166
|
+
state: generateRandomString(),
|
|
167
|
+
code_challenge: codeChallenge,
|
|
168
|
+
code_challenge_method: "S256"
|
|
169
|
+
}).toString()}`;
|
|
170
|
+
log_default.info("Opening your browser to log in.");
|
|
171
|
+
log_default.info("If it doesn't open, visit:", authUrl);
|
|
172
|
+
open(authUrl);
|
|
173
|
+
try {
|
|
174
|
+
await spinPromise(
|
|
175
|
+
Promise.race([
|
|
176
|
+
promisedCode,
|
|
177
|
+
new Promise(
|
|
178
|
+
(_resolve, reject) => setTimeout(() => {
|
|
179
|
+
reject(
|
|
180
|
+
new Error(
|
|
181
|
+
`Login timed out after ${Math.round(timeoutMs / 1e3)} seconds.`
|
|
182
|
+
)
|
|
183
|
+
);
|
|
184
|
+
}, timeoutMs)
|
|
185
|
+
)
|
|
186
|
+
]),
|
|
187
|
+
"Waiting for you to login and authorize"
|
|
188
|
+
);
|
|
189
|
+
} finally {
|
|
190
|
+
process.off("SIGINT", cleanup);
|
|
191
|
+
process.off("SIGTERM", cleanup);
|
|
192
|
+
await new Promise((resolve2) => {
|
|
193
|
+
const timeout = setTimeout(() => {
|
|
194
|
+
log_default.info("Server close timed out, forcing connection shutdown...");
|
|
195
|
+
connections.forEach((conn) => conn.destroy());
|
|
196
|
+
resolve2();
|
|
197
|
+
}, 1e3);
|
|
198
|
+
server.close(() => {
|
|
199
|
+
clearTimeout(timeout);
|
|
200
|
+
resolve2();
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
log_default.info("Exchanging authorization code for tokens...");
|
|
205
|
+
const { data } = await client_default.post(
|
|
206
|
+
`${ZAPIER_BASE}/oauth/token/`,
|
|
207
|
+
{
|
|
208
|
+
grant_type: "authorization_code",
|
|
209
|
+
code: await promisedCode,
|
|
210
|
+
redirect_uri: redirectUri,
|
|
211
|
+
client_id: LOGIN_CLIENT_ID,
|
|
212
|
+
code_verifier: codeVerifier
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
headers: {
|
|
216
|
+
[AUTH_MODE_HEADER]: "no",
|
|
217
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
);
|
|
221
|
+
updateLogin(data);
|
|
222
|
+
log_default.info("Token exchange completed successfully");
|
|
223
|
+
return data.access_token;
|
|
224
|
+
};
|
|
225
|
+
var login_default = login;
|
|
226
|
+
var LoginSchema = z.object({
|
|
227
|
+
timeout: z.string().optional().describe("Login timeout in seconds (default: 300)")
|
|
228
|
+
}).describe("Log in to Zapier to access your account");
|
|
229
|
+
|
|
230
|
+
// src/plugins/login/index.ts
|
|
231
|
+
var loginWithSdk = createFunction(async function loginWithSdk2(options) {
|
|
232
|
+
const timeoutSeconds = options.timeout ? parseInt(options.timeout, 10) : 300;
|
|
233
|
+
if (isNaN(timeoutSeconds) || timeoutSeconds <= 0) {
|
|
234
|
+
throw new Error("Timeout must be a positive number");
|
|
235
|
+
}
|
|
236
|
+
await login_default(timeoutSeconds * 1e3);
|
|
237
|
+
const user = await getLoggedInUser();
|
|
238
|
+
console.log(`\u2705 Successfully logged in as ${user.email}`);
|
|
239
|
+
setTimeout(() => process.exit(0), 100);
|
|
240
|
+
}, LoginSchema);
|
|
241
|
+
var loginPlugin = () => ({
|
|
242
|
+
login: loginWithSdk,
|
|
243
|
+
context: {
|
|
244
|
+
meta: {
|
|
245
|
+
login: {
|
|
246
|
+
categories: ["account"],
|
|
247
|
+
inputSchema: LoginSchema
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
var LogoutSchema = z.object({}).describe("Log out of your Zapier account");
|
|
253
|
+
|
|
254
|
+
// src/plugins/logout/index.ts
|
|
255
|
+
var logoutWithSdk = createFunction(async function logoutWithSdk2(_options) {
|
|
256
|
+
logout();
|
|
257
|
+
console.log("\u2705 Successfully logged out");
|
|
258
|
+
}, LogoutSchema);
|
|
259
|
+
var logoutPlugin = () => ({
|
|
260
|
+
logout: logoutWithSdk,
|
|
261
|
+
context: {
|
|
262
|
+
meta: {
|
|
263
|
+
logout: {
|
|
264
|
+
categories: ["account"],
|
|
265
|
+
inputSchema: LogoutSchema
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
var McpSchema = z.object({
|
|
271
|
+
port: z.string().optional().describe("Port to listen on (for future HTTP transport)")
|
|
272
|
+
}).describe("Start MCP server for Zapier SDK");
|
|
273
|
+
|
|
274
|
+
// src/plugins/mcp/index.ts
|
|
275
|
+
var mcpPlugin = ({ context }) => {
|
|
276
|
+
const mcpWithSdk = createFunction(async function mcpWithSdk2(options) {
|
|
277
|
+
const mcpOptions = {
|
|
278
|
+
...options,
|
|
279
|
+
debug: context.options.debug
|
|
280
|
+
};
|
|
281
|
+
return await startMcpServerAsProcess(mcpOptions);
|
|
282
|
+
}, McpSchema);
|
|
283
|
+
return {
|
|
284
|
+
mcp: mcpWithSdk,
|
|
285
|
+
context: {
|
|
286
|
+
meta: {
|
|
287
|
+
mcp: {
|
|
288
|
+
categories: ["utility"],
|
|
289
|
+
inputSchema: McpSchema
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
};
|
|
295
|
+
var GenerateTypesSchema = z.object({
|
|
296
|
+
appKey: AppKeyPropertySchema.describe("App key to generate SDK code for"),
|
|
297
|
+
authenticationId: AuthenticationIdPropertySchema.optional(),
|
|
298
|
+
output: OutputPropertySchema.optional().describe(
|
|
299
|
+
"Output file path (defaults to generated/<appKey>.ts)"
|
|
300
|
+
),
|
|
301
|
+
lockFilePath: z.string().optional().describe("Path to the .zapierrc lock file (defaults to .zapierrc)")
|
|
302
|
+
}).describe("Generate TypeScript SDK code for a specific app");
|
|
303
|
+
var generateTypesPlugin = ({ sdk }) => {
|
|
304
|
+
const generateTypesWithSdk = createFunction(
|
|
305
|
+
async function generateTypesWithSdk2(options) {
|
|
306
|
+
return await generateTypes({ ...options, sdk });
|
|
307
|
+
},
|
|
308
|
+
GenerateTypesSchema
|
|
309
|
+
);
|
|
310
|
+
return {
|
|
311
|
+
generateTypes: generateTypesWithSdk,
|
|
312
|
+
context: {
|
|
313
|
+
meta: {
|
|
314
|
+
generateTypes: {
|
|
315
|
+
categories: ["utility"],
|
|
316
|
+
inputSchema: GenerateTypesSchema
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
};
|
|
322
|
+
function generateFetchMethodSignature() {
|
|
323
|
+
return ` /** Make authenticated HTTP requests through Zapier's Relay service */
|
|
324
|
+
fetch: (options: Omit<z.infer<typeof RelayFetchSchema>, 'authenticationId'>) => Promise<Response>`;
|
|
325
|
+
}
|
|
326
|
+
async function generateTypes(options) {
|
|
327
|
+
const {
|
|
328
|
+
appKey,
|
|
329
|
+
authenticationId,
|
|
330
|
+
output = `./types/${appKey}.d.ts`,
|
|
331
|
+
sdk
|
|
332
|
+
} = options;
|
|
333
|
+
const { app, version } = parseAppIdentifier(appKey);
|
|
334
|
+
const actionsResult = await sdk.listActions({
|
|
335
|
+
appKey: app
|
|
336
|
+
});
|
|
337
|
+
const actions = actionsResult.data;
|
|
338
|
+
if (actions.length === 0) {
|
|
339
|
+
const typeDefinitions2 = generateEmptyTypesFile(app, version);
|
|
340
|
+
if (output) {
|
|
341
|
+
fs.mkdirSync(path.dirname(output), { recursive: true });
|
|
342
|
+
fs.writeFileSync(output, typeDefinitions2, "utf8");
|
|
343
|
+
}
|
|
344
|
+
return typeDefinitions2;
|
|
345
|
+
}
|
|
346
|
+
const actionsWithFields = [];
|
|
347
|
+
if (authenticationId) {
|
|
348
|
+
for (const action of actions) {
|
|
349
|
+
try {
|
|
350
|
+
const manifestEntry = sdk.getContext().getManifestEntry(appKey);
|
|
351
|
+
const fieldsResult = await sdk.listInputFields({
|
|
352
|
+
// If the appKey is in the manifest, use the appKey so that the types are consistent with the manifest's version, otherwise use the action.app_key
|
|
353
|
+
appKey: manifestEntry ? appKey : action.app_key,
|
|
354
|
+
actionKey: action.key,
|
|
355
|
+
actionType: action.action_type,
|
|
356
|
+
authenticationId
|
|
357
|
+
});
|
|
358
|
+
const fields = fieldsResult.data.map((field) => {
|
|
359
|
+
const fieldObj = field;
|
|
360
|
+
return {
|
|
361
|
+
...fieldObj,
|
|
362
|
+
required: fieldObj.is_required || fieldObj.required || false
|
|
363
|
+
};
|
|
364
|
+
});
|
|
365
|
+
actionsWithFields.push({
|
|
366
|
+
...action,
|
|
367
|
+
inputFields: fields,
|
|
368
|
+
name: action.title || action.key
|
|
369
|
+
});
|
|
370
|
+
} catch {
|
|
371
|
+
actionsWithFields.push({
|
|
372
|
+
...action,
|
|
373
|
+
inputFields: [],
|
|
374
|
+
name: action.title || action.key
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
} else {
|
|
379
|
+
actions.forEach((action) => {
|
|
380
|
+
actionsWithFields.push({
|
|
381
|
+
...action,
|
|
382
|
+
inputFields: [],
|
|
383
|
+
name: action.title || action.key
|
|
384
|
+
});
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
const typeDefinitions = generateTypeDefinitions(
|
|
388
|
+
app,
|
|
389
|
+
actionsWithFields,
|
|
390
|
+
version
|
|
391
|
+
);
|
|
392
|
+
if (output) {
|
|
393
|
+
fs.mkdirSync(path.dirname(output), { recursive: true });
|
|
394
|
+
fs.writeFileSync(output, typeDefinitions, "utf8");
|
|
395
|
+
}
|
|
396
|
+
return typeDefinitions;
|
|
397
|
+
}
|
|
398
|
+
function parseAppIdentifier(identifier) {
|
|
399
|
+
const parts = identifier.split("@");
|
|
400
|
+
return {
|
|
401
|
+
app: parts[0],
|
|
402
|
+
version: parts[1]
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
function generateTypeDefinitions(appKey, actions, version) {
|
|
406
|
+
if (actions.length === 0) {
|
|
407
|
+
return generateEmptyTypesFile(appKey, version);
|
|
408
|
+
}
|
|
409
|
+
const actionsByType = actions.reduce(
|
|
410
|
+
(acc, action) => {
|
|
411
|
+
if (!acc[action.action_type]) {
|
|
412
|
+
acc[action.action_type] = [];
|
|
413
|
+
}
|
|
414
|
+
acc[action.action_type].push(action);
|
|
415
|
+
return acc;
|
|
416
|
+
},
|
|
417
|
+
{}
|
|
418
|
+
);
|
|
419
|
+
const appName = capitalize(appKey);
|
|
420
|
+
const versionComment = version ? ` * Generated for ${appKey}@${version}` : ` * Generated for ${appKey}`;
|
|
421
|
+
let output = `/* eslint-disable @typescript-eslint/naming-convention */
|
|
422
|
+
/**
|
|
423
|
+
* Auto-generated TypeScript types for Zapier ${appKey} actions
|
|
424
|
+
${versionComment}
|
|
425
|
+
* Generated on: ${(/* @__PURE__ */ new Date()).toISOString()}
|
|
426
|
+
*
|
|
427
|
+
* Usage:
|
|
428
|
+
* import type { ${appName}Sdk } from './path/to/this/file'
|
|
429
|
+
* const sdk = createZapierSdk() as unknown as ${appName}Sdk
|
|
430
|
+
*
|
|
431
|
+
* // Direct usage (per-call auth):
|
|
432
|
+
* await sdk.apps.${appKey}.search.user_by_email({ authenticationId: 123, inputs: { email } })
|
|
433
|
+
*
|
|
434
|
+
* // Factory usage (pinned auth):
|
|
435
|
+
* const my${appName} = sdk.apps.${appKey}({ authenticationId: 123 })
|
|
436
|
+
* await my${appName}.search.user_by_email({ inputs: { email } })
|
|
437
|
+
*/
|
|
438
|
+
|
|
439
|
+
import type { ActionExecutionOptions, ActionExecutionResult } from '@zapier/zapier-sdk'
|
|
440
|
+
import { z } from 'zod'
|
|
441
|
+
import { RelayFetchSchema } from '@zapier/zapier-sdk'
|
|
442
|
+
|
|
443
|
+
`;
|
|
444
|
+
actions.forEach((action) => {
|
|
445
|
+
if (action.inputFields.length > 0) {
|
|
446
|
+
const inputTypeName = `${appName}${capitalize(action.action_type)}${capitalize(
|
|
447
|
+
sanitizeActionName(action.key)
|
|
448
|
+
)}Inputs`;
|
|
449
|
+
output += `interface ${inputTypeName} {
|
|
450
|
+
`;
|
|
451
|
+
action.inputFields.forEach((field) => {
|
|
452
|
+
const isOptional = !field.required;
|
|
453
|
+
const fieldType = mapFieldTypeToTypeScript(field);
|
|
454
|
+
const description = field.helpText ? ` /** ${escapeComment(field.helpText)} */
|
|
455
|
+
` : "";
|
|
456
|
+
output += `${description} ${sanitizeFieldName(field.key)}${isOptional ? "?" : ""}: ${fieldType}
|
|
457
|
+
`;
|
|
458
|
+
});
|
|
459
|
+
output += `}
|
|
460
|
+
|
|
461
|
+
`;
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
Object.entries(actionsByType).forEach(([actionType, typeActions]) => {
|
|
465
|
+
const typeName = `${appName}${capitalize(actionType)}Actions`;
|
|
466
|
+
output += `interface ${typeName} {
|
|
467
|
+
`;
|
|
468
|
+
typeActions.forEach((action) => {
|
|
469
|
+
const actionName = sanitizeActionName(action.key);
|
|
470
|
+
const description = action.description ? ` /** ${escapeComment(action.description)} */
|
|
471
|
+
` : "";
|
|
472
|
+
if (action.inputFields.length > 0) {
|
|
473
|
+
const inputTypeName = `${appName}${capitalize(action.action_type)}${capitalize(
|
|
474
|
+
sanitizeActionName(action.key)
|
|
475
|
+
)}Inputs`;
|
|
476
|
+
output += `${description} ${actionName}: (options: { inputs: ${inputTypeName} } & Omit<ActionExecutionOptions, 'inputs'>) => Promise<ActionExecutionResult>
|
|
477
|
+
`;
|
|
478
|
+
} else {
|
|
479
|
+
output += `${description} ${actionName}: (options?: { inputs?: Record<string, any> } & ActionExecutionOptions) => Promise<ActionExecutionResult>
|
|
480
|
+
`;
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
output += `}
|
|
484
|
+
|
|
485
|
+
`;
|
|
486
|
+
});
|
|
487
|
+
output += `interface ${appName}AppProxy {
|
|
488
|
+
`;
|
|
489
|
+
Object.keys(actionsByType).forEach((actionType) => {
|
|
490
|
+
const typeName = `${appName}${capitalize(actionType)}Actions`;
|
|
491
|
+
output += ` ${actionType}: ${typeName}
|
|
492
|
+
`;
|
|
493
|
+
});
|
|
494
|
+
output += generateFetchMethodSignature() + "\n";
|
|
495
|
+
output += `}
|
|
496
|
+
|
|
497
|
+
`;
|
|
498
|
+
output += `interface ${appName}AppFactory {
|
|
499
|
+
`;
|
|
500
|
+
output += ` (options: { authenticationId: number }): ${appName}AppProxy
|
|
501
|
+
`;
|
|
502
|
+
output += `}
|
|
503
|
+
|
|
504
|
+
`;
|
|
505
|
+
output += `type ${appName}AppWithFactory = ${appName}AppFactory & ${appName}AppProxy
|
|
506
|
+
|
|
507
|
+
`;
|
|
508
|
+
output += `export interface ${appName}Sdk {
|
|
509
|
+
`;
|
|
510
|
+
output += ` apps: {
|
|
511
|
+
`;
|
|
512
|
+
output += ` ${appKey}: ${appName}AppWithFactory
|
|
513
|
+
`;
|
|
514
|
+
output += ` }
|
|
515
|
+
`;
|
|
516
|
+
output += `}
|
|
517
|
+
`;
|
|
518
|
+
return output;
|
|
519
|
+
}
|
|
520
|
+
function generateEmptyTypesFile(appKey, version) {
|
|
521
|
+
const appName = capitalize(appKey);
|
|
522
|
+
const versionComment = version ? ` * Generated for ${appKey}@${version}` : ` * Generated for ${appKey}`;
|
|
523
|
+
return `/* eslint-disable @typescript-eslint/naming-convention */
|
|
524
|
+
/**
|
|
525
|
+
* Auto-generated TypeScript types for Zapier ${appKey} actions
|
|
526
|
+
${versionComment}
|
|
527
|
+
* Generated on: ${(/* @__PURE__ */ new Date()).toISOString()}
|
|
528
|
+
*
|
|
529
|
+
* No actions found for this app.
|
|
530
|
+
*/
|
|
531
|
+
|
|
532
|
+
import type { ActionExecutionOptions, ActionExecutionResult } from '@zapier/zapier-sdk'
|
|
533
|
+
import { z } from 'zod'
|
|
534
|
+
import { RelayFetchSchema } from '@zapier/zapier-sdk'
|
|
535
|
+
|
|
536
|
+
interface ${appName}AppProxy {
|
|
537
|
+
// No actions available
|
|
538
|
+
${generateFetchMethodSignature()}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
interface ${appName}AppFactory {
|
|
542
|
+
(options: { authenticationId: number }): ${appName}AppProxy
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
type ${appName}AppWithFactory = ${appName}AppFactory & ${appName}AppProxy
|
|
546
|
+
|
|
547
|
+
export interface ${appName}Sdk {
|
|
548
|
+
apps: {
|
|
549
|
+
${appKey}: ${appName}AppWithFactory
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
`;
|
|
553
|
+
}
|
|
554
|
+
function capitalize(str) {
|
|
555
|
+
return str.charAt(0).toUpperCase() + str.slice(1).replace(/[-_]/g, "");
|
|
556
|
+
}
|
|
557
|
+
function sanitizeActionName(actionKey) {
|
|
558
|
+
let sanitized = actionKey.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
559
|
+
if (/^[0-9]/.test(sanitized)) {
|
|
560
|
+
sanitized = "_" + sanitized;
|
|
561
|
+
}
|
|
562
|
+
return sanitized;
|
|
563
|
+
}
|
|
564
|
+
function sanitizeFieldName(fieldKey) {
|
|
565
|
+
let sanitized = fieldKey.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
566
|
+
if (/^[0-9]/.test(sanitized)) {
|
|
567
|
+
sanitized = "_" + sanitized;
|
|
568
|
+
}
|
|
569
|
+
return sanitized;
|
|
570
|
+
}
|
|
571
|
+
function escapeComment(comment) {
|
|
572
|
+
return comment.replace(/\*\//g, "*\\/").replace(/\r?\n/g, " ");
|
|
573
|
+
}
|
|
574
|
+
function mapFieldTypeToTypeScript(field) {
|
|
575
|
+
if (field.choices && field.choices.length > 0) {
|
|
576
|
+
const choiceValues = field.choices.filter(
|
|
577
|
+
(choice) => choice.value !== void 0 && choice.value !== null && choice.value !== ""
|
|
578
|
+
).map(
|
|
579
|
+
(choice) => typeof choice.value === "string" ? `"${choice.value}"` : choice.value
|
|
580
|
+
);
|
|
581
|
+
if (choiceValues.length > 0) {
|
|
582
|
+
return choiceValues.join(" | ");
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
switch (field.type?.toLowerCase()) {
|
|
586
|
+
case "string":
|
|
587
|
+
case "text":
|
|
588
|
+
case "email":
|
|
589
|
+
case "url":
|
|
590
|
+
case "password":
|
|
591
|
+
return "string";
|
|
592
|
+
case "integer":
|
|
593
|
+
case "number":
|
|
594
|
+
return "number";
|
|
595
|
+
case "boolean":
|
|
596
|
+
return "boolean";
|
|
597
|
+
case "datetime":
|
|
598
|
+
case "date":
|
|
599
|
+
return "string";
|
|
600
|
+
// ISO date strings
|
|
601
|
+
case "file":
|
|
602
|
+
return "string";
|
|
603
|
+
// File URL or content
|
|
604
|
+
case "array":
|
|
605
|
+
return "any[]";
|
|
606
|
+
case "object":
|
|
607
|
+
return "Record<string, any>";
|
|
608
|
+
default:
|
|
609
|
+
return "string | number | boolean";
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
var BundleCodeSchema = z.object({
|
|
613
|
+
input: z.string().min(1).describe("Input TypeScript file path to bundle"),
|
|
614
|
+
output: OutputPropertySchema.optional().describe(
|
|
615
|
+
"Output file path (defaults to input with .js extension)"
|
|
616
|
+
),
|
|
617
|
+
string: z.boolean().optional().describe("Return bundled code as string instead of writing to file"),
|
|
618
|
+
minify: z.boolean().optional().describe("Minify the bundled output"),
|
|
619
|
+
target: z.string().optional().describe("ECMAScript target version"),
|
|
620
|
+
cjs: z.boolean().optional().describe("Output CommonJS format instead of ESM")
|
|
621
|
+
}).describe("Bundle TypeScript code into executable JavaScript");
|
|
622
|
+
var bundleCodePlugin = () => {
|
|
623
|
+
const bundleCodeWithSdk = createFunction(async function bundleCodeWithSdk2(options) {
|
|
624
|
+
return await bundleCode(options);
|
|
625
|
+
}, BundleCodeSchema);
|
|
626
|
+
return {
|
|
627
|
+
bundleCode: bundleCodeWithSdk,
|
|
628
|
+
context: {
|
|
629
|
+
meta: {
|
|
630
|
+
bundleCode: {
|
|
631
|
+
categories: ["utility"],
|
|
632
|
+
inputSchema: BundleCodeSchema
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
};
|
|
638
|
+
var ZapierBundleError = class extends Error {
|
|
639
|
+
constructor(message, details, originalError) {
|
|
640
|
+
super(message);
|
|
641
|
+
this.code = "ZAPIER_BUNDLE_ERROR";
|
|
642
|
+
this.name = "ZapierBundleError";
|
|
643
|
+
this.details = details;
|
|
644
|
+
this.originalError = originalError;
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
async function bundleCode(options) {
|
|
648
|
+
const {
|
|
649
|
+
input,
|
|
650
|
+
output,
|
|
651
|
+
target = "es2020",
|
|
652
|
+
cjs = false,
|
|
653
|
+
minify = false,
|
|
654
|
+
string: returnString = false
|
|
655
|
+
} = options;
|
|
656
|
+
const resolvedInput = path.resolve(process.cwd(), input);
|
|
657
|
+
try {
|
|
658
|
+
const result = buildSync({
|
|
659
|
+
entryPoints: [resolvedInput],
|
|
660
|
+
bundle: true,
|
|
661
|
+
platform: "node",
|
|
662
|
+
target,
|
|
663
|
+
format: cjs ? "cjs" : "esm",
|
|
664
|
+
minify,
|
|
665
|
+
write: false,
|
|
666
|
+
external: [],
|
|
667
|
+
// Bundle everything
|
|
668
|
+
banner: {
|
|
669
|
+
js: "#!/usr/bin/env node"
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
if (result.errors.length > 0) {
|
|
673
|
+
const errorMessages = result.errors.map((e) => e.text);
|
|
674
|
+
throw new ZapierBundleError(
|
|
675
|
+
`Bundle failed: ${errorMessages.join(", ")}`,
|
|
676
|
+
errorMessages
|
|
677
|
+
);
|
|
678
|
+
}
|
|
679
|
+
const bundledCode = result.outputFiles?.[0]?.text;
|
|
680
|
+
if (!bundledCode) {
|
|
681
|
+
throw new ZapierBundleError("No output generated");
|
|
682
|
+
}
|
|
683
|
+
let finalOutput = bundledCode;
|
|
684
|
+
if (returnString) {
|
|
685
|
+
finalOutput = JSON.stringify(bundledCode);
|
|
686
|
+
}
|
|
687
|
+
if (output) {
|
|
688
|
+
fs.mkdirSync(path.dirname(output), { recursive: true });
|
|
689
|
+
fs.writeFileSync(output, finalOutput, "utf8");
|
|
690
|
+
}
|
|
691
|
+
return finalOutput;
|
|
692
|
+
} catch (error) {
|
|
693
|
+
if (error instanceof ZapierBundleError) {
|
|
694
|
+
throw error;
|
|
695
|
+
}
|
|
696
|
+
throw new ZapierBundleError(
|
|
697
|
+
`Bundle failed: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
698
|
+
void 0,
|
|
699
|
+
error instanceof Error ? error : void 0
|
|
700
|
+
);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
var GetConfigPathSchema = z.object({}).describe("Show the path to the configuration file");
|
|
704
|
+
var getConfigPathPlugin = () => {
|
|
705
|
+
const getConfigPathWithSdk = createFunction(
|
|
706
|
+
async function getConfigPathWithSdk2(_options) {
|
|
707
|
+
return getConfigPath();
|
|
708
|
+
},
|
|
709
|
+
GetConfigPathSchema
|
|
710
|
+
);
|
|
711
|
+
return {
|
|
712
|
+
getConfigPath: getConfigPathWithSdk,
|
|
713
|
+
context: {
|
|
714
|
+
meta: {
|
|
715
|
+
getConfigPath: {
|
|
716
|
+
categories: ["utility"],
|
|
717
|
+
inputSchema: GetConfigPathSchema
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
// src/sdk.ts
|
|
725
|
+
function createZapierCliSdk(options = {}) {
|
|
726
|
+
let sdk = createZapierSdkWithoutRegistry({
|
|
727
|
+
debug: options.debug
|
|
728
|
+
});
|
|
729
|
+
sdk = sdk.addPlugin(generateTypesPlugin);
|
|
730
|
+
sdk = sdk.addPlugin(bundleCodePlugin);
|
|
731
|
+
sdk = sdk.addPlugin(getConfigPathPlugin);
|
|
732
|
+
sdk = sdk.addPlugin(mcpPlugin);
|
|
733
|
+
sdk = sdk.addPlugin(loginPlugin);
|
|
734
|
+
sdk = sdk.addPlugin(logoutPlugin);
|
|
735
|
+
const finalSdk = sdk.addPlugin(registryPlugin);
|
|
736
|
+
return finalSdk;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
export { createZapierCliSdk };
|