@zapier/zapier-sdk 0.2.0 → 0.3.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.
- package/dist/api/client.js +3 -2
- package/dist/api/types.d.ts +1 -1
- package/dist/auth.d.ts +59 -0
- package/dist/auth.js +261 -0
- package/dist/functions/getAction/info.d.ts +3 -3
- package/dist/functions/getAction/schemas.d.ts +3 -3
- package/dist/functions/getAuthentication/index.d.ts +13 -0
- package/dist/functions/getAuthentication/index.js +38 -0
- package/dist/functions/getAuthentication/info.d.ts +12 -0
- package/dist/functions/getAuthentication/info.js +11 -0
- package/dist/functions/getAuthentication/schemas.d.ts +26 -0
- package/dist/functions/getAuthentication/schemas.js +16 -0
- package/dist/functions/listActions/info.d.ts +3 -3
- package/dist/functions/listActions/schemas.d.ts +3 -3
- package/dist/functions/listFields/info.d.ts +3 -3
- package/dist/functions/listFields/schemas.d.ts +3 -3
- package/dist/functions/runAction/index.js +7 -76
- package/dist/functions/runAction/info.d.ts +3 -3
- package/dist/functions/runAction/schemas.d.ts +3 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -1
- package/dist/sdk.js +31 -39
- package/dist/types/domain.d.ts +2 -0
- package/dist/types/events.d.ts +37 -0
- package/dist/types/events.js +8 -0
- package/dist/types/properties.d.ts +1 -1
- package/dist/types/properties.js +10 -1
- package/dist/types/sdk.d.ts +2 -1
- package/package.json +4 -1
- package/src/api/client.ts +3 -2
- package/src/api/types.ts +9 -1
- package/src/auth.ts +340 -0
- package/src/functions/getAuthentication/index.ts +51 -0
- package/src/functions/getAuthentication/info.ts +9 -0
- package/src/functions/getAuthentication/schemas.ts +43 -0
- package/src/functions/runAction/index.ts +10 -135
- package/src/index.ts +4 -0
- package/src/sdk.ts +24 -61
- package/src/types/domain.ts +4 -0
- package/src/types/events.ts +43 -0
- package/src/types/properties.ts +10 -1
- package/src/types/sdk.ts +2 -0
- package/dist/utils/getTokenFromConfig.d.ts +0 -7
- package/dist/utils/getTokenFromConfig.js +0 -29
- package/src/utils/getTokenFromConfig.ts +0 -28
package/src/auth.ts
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK Authentication Utilities
|
|
3
|
+
*
|
|
4
|
+
* This module provides SDK-level authentication utilities, including
|
|
5
|
+
* token acquisition, refresh, and user information extraction.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Import type { RequestOptions } from "./api/types"; // Commented out - not used yet
|
|
9
|
+
|
|
10
|
+
let config: any;
|
|
11
|
+
|
|
12
|
+
// Import event types from common location
|
|
13
|
+
import type { EventCallback } from "./types/events";
|
|
14
|
+
|
|
15
|
+
// Re-export for backward compatibility
|
|
16
|
+
export type {
|
|
17
|
+
SdkEvent,
|
|
18
|
+
AuthEvent,
|
|
19
|
+
ApiEvent,
|
|
20
|
+
LoadingEvent,
|
|
21
|
+
EventCallback,
|
|
22
|
+
} from "./types/events";
|
|
23
|
+
|
|
24
|
+
// Options interfaces for auth functions
|
|
25
|
+
export interface AuthOptions {
|
|
26
|
+
onEvent?: EventCallback;
|
|
27
|
+
fetch?: typeof globalThis.fetch;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// JWT payload interfaces
|
|
31
|
+
interface JwtPayload {
|
|
32
|
+
payload: Record<string, any>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface LoginData {
|
|
36
|
+
access_token: string;
|
|
37
|
+
refresh_token: string;
|
|
38
|
+
expires_in: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Constants needed for token refresh
|
|
42
|
+
const ZAPIER_BASE = "https://zapier.com";
|
|
43
|
+
const LOGIN_CLIENT_ID = "K5eEnRE9TTmSFATdkkWhKF8NOKwoiOnYAyIqJjae";
|
|
44
|
+
const AUTH_MODE_HEADER = "X-Auth";
|
|
45
|
+
|
|
46
|
+
// Utility functions for config management
|
|
47
|
+
function getConfig() {
|
|
48
|
+
if (!config) {
|
|
49
|
+
const ConfModule = require("conf");
|
|
50
|
+
const Conf = ConfModule.default || ConfModule;
|
|
51
|
+
config = new Conf({ projectName: "zapier-sdk-cli" });
|
|
52
|
+
}
|
|
53
|
+
return config;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function updateLogin(loginData: LoginData): void {
|
|
57
|
+
const cfg = getConfig();
|
|
58
|
+
cfg.set("login_jwt", loginData.access_token);
|
|
59
|
+
cfg.set("login_refresh_token", loginData.refresh_token);
|
|
60
|
+
cfg.set("login_expires_at", Date.now() + loginData.expires_in * 1000);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function clearLogin(): void {
|
|
64
|
+
const cfg = getConfig();
|
|
65
|
+
cfg.delete("login_jwt");
|
|
66
|
+
cfg.delete("login_refresh_token");
|
|
67
|
+
cfg.delete("login_expires_at");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// JWT utility functions
|
|
71
|
+
function decodeJwtOrThrow(jwt: unknown): JwtPayload {
|
|
72
|
+
if (typeof jwt !== "string") {
|
|
73
|
+
throw new Error("Expected JWT to be a string");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let jsonwebtoken: any;
|
|
77
|
+
try {
|
|
78
|
+
jsonwebtoken = require("jsonwebtoken");
|
|
79
|
+
} catch {
|
|
80
|
+
throw new Error(
|
|
81
|
+
"jsonwebtoken not available - this function requires CLI dependencies",
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const decodedJwt = jsonwebtoken.decode(jwt, { complete: true });
|
|
86
|
+
|
|
87
|
+
if (!decodedJwt) {
|
|
88
|
+
throw new Error("Could not decode JWT");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (typeof decodedJwt.payload === "string") {
|
|
92
|
+
throw new Error("Did not expect JWT payload to be a string");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return decodedJwt;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Refreshes an expired JWT token using the refresh token.
|
|
100
|
+
* Returns the new access token or throws an error.
|
|
101
|
+
*/
|
|
102
|
+
async function refreshJwt(
|
|
103
|
+
refreshToken: string,
|
|
104
|
+
options: AuthOptions = {},
|
|
105
|
+
): Promise<string> {
|
|
106
|
+
const { onEvent, fetch = globalThis.fetch } = options;
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
onEvent?.({
|
|
110
|
+
type: "auth_refreshing",
|
|
111
|
+
payload: {
|
|
112
|
+
message: "Refreshing your token...",
|
|
113
|
+
operation: "token_refresh",
|
|
114
|
+
},
|
|
115
|
+
timestamp: Date.now(),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const response = await fetch(`${ZAPIER_BASE}/oauth/token/`, {
|
|
119
|
+
method: "POST",
|
|
120
|
+
headers: {
|
|
121
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
122
|
+
[AUTH_MODE_HEADER]: "no",
|
|
123
|
+
},
|
|
124
|
+
body: new URLSearchParams({
|
|
125
|
+
client_id: LOGIN_CLIENT_ID,
|
|
126
|
+
refresh_token: refreshToken,
|
|
127
|
+
grant_type: "refresh_token",
|
|
128
|
+
}),
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
if (!response.ok) {
|
|
132
|
+
throw new Error(
|
|
133
|
+
`Token refresh failed: ${response.status} ${response.statusText}`,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const data: LoginData = await response.json();
|
|
138
|
+
|
|
139
|
+
// Update stored login data
|
|
140
|
+
updateLogin(data);
|
|
141
|
+
|
|
142
|
+
onEvent?.({
|
|
143
|
+
type: "auth_success",
|
|
144
|
+
payload: {
|
|
145
|
+
message: "Token refreshed successfully",
|
|
146
|
+
operation: "token_refresh",
|
|
147
|
+
},
|
|
148
|
+
timestamp: Date.now(),
|
|
149
|
+
});
|
|
150
|
+
return data.access_token;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
// If refresh fails, clear stored login
|
|
153
|
+
clearLogin();
|
|
154
|
+
const errorMessage = `Token refresh failed: ${error instanceof Error ? error.message : "Unknown error"}`;
|
|
155
|
+
onEvent?.({
|
|
156
|
+
type: "auth_error",
|
|
157
|
+
payload: {
|
|
158
|
+
message: errorMessage,
|
|
159
|
+
error: errorMessage,
|
|
160
|
+
operation: "token_refresh",
|
|
161
|
+
},
|
|
162
|
+
timestamp: Date.now(),
|
|
163
|
+
});
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Attempts to read a valid JWT token from the CLI configuration.
|
|
170
|
+
* Automatically refreshes expired tokens when possible.
|
|
171
|
+
* Returns undefined if no valid token is found or refresh fails.
|
|
172
|
+
*/
|
|
173
|
+
export async function getTokenFromConfig(
|
|
174
|
+
options: AuthOptions = {},
|
|
175
|
+
): Promise<string | undefined> {
|
|
176
|
+
try {
|
|
177
|
+
const cfg = getConfig();
|
|
178
|
+
|
|
179
|
+
const jwt = cfg.get("login_jwt") as string | undefined;
|
|
180
|
+
const refreshToken = cfg.get("login_refresh_token") as string | undefined;
|
|
181
|
+
const expiresAt = cfg.get("login_expires_at") as number | undefined;
|
|
182
|
+
|
|
183
|
+
// Check if we have all required fields
|
|
184
|
+
if (!jwt || !refreshToken || !expiresAt) {
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Check if token is still valid (with 30 second buffer)
|
|
189
|
+
if (expiresAt > Date.now() + 30 * 1000) {
|
|
190
|
+
return jwt;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Token is expired - attempt to refresh
|
|
194
|
+
try {
|
|
195
|
+
return await refreshJwt(refreshToken, options);
|
|
196
|
+
} catch {
|
|
197
|
+
// If refresh fails, return undefined
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
} catch {
|
|
201
|
+
// If conf is not available or any other error occurs, return undefined
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Gets the ZAPIER_TOKEN from environment variables.
|
|
208
|
+
* Returns undefined if not set.
|
|
209
|
+
*/
|
|
210
|
+
export function getTokenFromEnv(): string | undefined {
|
|
211
|
+
return process.env.ZAPIER_TOKEN;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Attempts to get a token with the following precedence:
|
|
216
|
+
* 1. ZAPIER_TOKEN environment variable
|
|
217
|
+
* 2. Valid JWT from CLI configuration (with auto-refresh)
|
|
218
|
+
*
|
|
219
|
+
* Returns undefined if no valid token is found.
|
|
220
|
+
*/
|
|
221
|
+
export async function getTokenFromEnvOrConfig(
|
|
222
|
+
options: AuthOptions = {},
|
|
223
|
+
): Promise<string | undefined> {
|
|
224
|
+
// First priority: environment variable
|
|
225
|
+
const envToken = getTokenFromEnv();
|
|
226
|
+
if (envToken) {
|
|
227
|
+
return envToken;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Second priority: CLI configuration (with auto-refresh)
|
|
231
|
+
return getTokenFromConfig(options);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Gets the current JWT token, refreshing if necessary.
|
|
236
|
+
* Returns undefined if no valid token is available.
|
|
237
|
+
*/
|
|
238
|
+
export async function getValidJwt(
|
|
239
|
+
options: AuthOptions = {},
|
|
240
|
+
): Promise<string | undefined> {
|
|
241
|
+
try {
|
|
242
|
+
const cfg = getConfig();
|
|
243
|
+
|
|
244
|
+
const jwt = cfg.get("login_jwt") as string | undefined;
|
|
245
|
+
const refreshToken = cfg.get("login_refresh_token") as string | undefined;
|
|
246
|
+
const expiresAt = cfg.get("login_expires_at") as number | undefined;
|
|
247
|
+
|
|
248
|
+
// Check if we have all required fields
|
|
249
|
+
if (!jwt || !refreshToken || !expiresAt) {
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Check if token is still valid (with 30 second buffer)
|
|
254
|
+
if (expiresAt > Date.now() + 30 * 1000) {
|
|
255
|
+
return jwt;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Token is expired - attempt to refresh
|
|
259
|
+
return await refreshJwt(refreshToken, options);
|
|
260
|
+
} catch {
|
|
261
|
+
return undefined;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Gets the logged-in user information from JWT token.
|
|
267
|
+
* Automatically refreshes token if expired.
|
|
268
|
+
*/
|
|
269
|
+
export async function getLoggedInUser(options: AuthOptions = {}): Promise<{
|
|
270
|
+
accountId: number;
|
|
271
|
+
customUserId: number;
|
|
272
|
+
email: string;
|
|
273
|
+
}> {
|
|
274
|
+
const jwt = await getValidJwt(options);
|
|
275
|
+
|
|
276
|
+
if (!jwt) {
|
|
277
|
+
throw new Error(
|
|
278
|
+
"No valid authentication token available. Please login first.",
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
let decodedJwt = decodeJwtOrThrow(jwt);
|
|
283
|
+
|
|
284
|
+
if (decodedJwt.payload["sub_type"] == "service") {
|
|
285
|
+
decodedJwt = decodeJwtOrThrow(decodedJwt.payload["njwt"]);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (typeof decodedJwt.payload["zap:acc"] !== "string") {
|
|
289
|
+
throw new Error("JWT payload does not contain accountId");
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const accountId = parseInt(decodedJwt.payload["zap:acc"], 10);
|
|
293
|
+
if (isNaN(accountId)) {
|
|
294
|
+
throw new Error("JWT accountId is not a number");
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (
|
|
298
|
+
decodedJwt.payload["sub_type"] !== "customuser" ||
|
|
299
|
+
typeof decodedJwt.payload["sub"] !== "string"
|
|
300
|
+
) {
|
|
301
|
+
throw new Error("JWT payload does not contain customUserId");
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const customUserId = parseInt(decodedJwt.payload["sub"], 10);
|
|
305
|
+
if (isNaN(customUserId)) {
|
|
306
|
+
throw new Error("JWT customUserId is not a number");
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const email = decodedJwt.payload["zap:uname"];
|
|
310
|
+
if (typeof email !== "string") {
|
|
311
|
+
throw new Error("JWT payload does not contain email");
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return {
|
|
315
|
+
accountId,
|
|
316
|
+
customUserId,
|
|
317
|
+
email,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Clears stored login information.
|
|
323
|
+
*/
|
|
324
|
+
export function logout(options: Pick<AuthOptions, "onEvent"> = {}): void {
|
|
325
|
+
const { onEvent } = options;
|
|
326
|
+
clearLogin();
|
|
327
|
+
onEvent?.({
|
|
328
|
+
type: "auth_logout",
|
|
329
|
+
payload: { message: "Logged out successfully", operation: "logout" },
|
|
330
|
+
timestamp: Date.now(),
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Gets the path to the configuration file.
|
|
336
|
+
*/
|
|
337
|
+
export function getConfigPath(): string {
|
|
338
|
+
const cfg = getConfig();
|
|
339
|
+
return cfg.path;
|
|
340
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { getOrCreateApiClient } from "../../api";
|
|
2
|
+
import type { Authentication } from "../../types/domain";
|
|
3
|
+
import type { GetAuthenticationOptions } from "./schemas";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Get a specific authentication by ID
|
|
7
|
+
*
|
|
8
|
+
* This function can be used standalone without instantiating a full SDK,
|
|
9
|
+
* which enables better tree-shaking in applications that only need this functionality.
|
|
10
|
+
*
|
|
11
|
+
* @param options - Authentication ID and API configuration options
|
|
12
|
+
* @returns Promise<Authentication> - The authentication details
|
|
13
|
+
* @throws Error if authentication not found or access denied
|
|
14
|
+
*/
|
|
15
|
+
export async function getAuthentication(
|
|
16
|
+
options: GetAuthenticationOptions,
|
|
17
|
+
): Promise<Authentication> {
|
|
18
|
+
const { authenticationId } = options;
|
|
19
|
+
const api = getOrCreateApiClient(options);
|
|
20
|
+
api.requireAuthTo("get authentication");
|
|
21
|
+
|
|
22
|
+
const data: Authentication = await api.get(
|
|
23
|
+
`/api/v4/authentications/${authenticationId}/`,
|
|
24
|
+
{
|
|
25
|
+
customErrorHandler: (response) => {
|
|
26
|
+
if (response.status === 401) {
|
|
27
|
+
return new Error(
|
|
28
|
+
`Authentication failed. Your token may not have permission to access authentications or may be expired. (HTTP ${response.status})`,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
if (response.status === 403) {
|
|
32
|
+
return new Error(
|
|
33
|
+
`Access forbidden. Your token may not have the required scopes to get authentication ${authenticationId}. (HTTP ${response.status})`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
if (response.status === 404) {
|
|
37
|
+
return new Error(
|
|
38
|
+
`Authentication ${authenticationId} not found. It may not exist or you may not have access to it. (HTTP ${response.status})`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
// Coerce title from label if title is missing (API cleanup)
|
|
47
|
+
return {
|
|
48
|
+
...data,
|
|
49
|
+
title: data.title || (data as any).label || undefined,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { getAuthentication } from "./index";
|
|
2
|
+
import { GetAuthenticationSchema } from "./schemas";
|
|
3
|
+
|
|
4
|
+
// Function registry info - imports both function and schema
|
|
5
|
+
export const getAuthenticationInfo = {
|
|
6
|
+
name: getAuthentication.name,
|
|
7
|
+
inputSchema: GetAuthenticationSchema,
|
|
8
|
+
implementation: getAuthentication,
|
|
9
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { withOutputSchema } from "../../schema-utils";
|
|
3
|
+
import { AuthItemSchema } from "../../schemas/Auth";
|
|
4
|
+
import type { Authentication } from "../../types/domain";
|
|
5
|
+
|
|
6
|
+
// Pure Zod schema - no resolver metadata!
|
|
7
|
+
export const GetAuthenticationSchema = withOutputSchema(
|
|
8
|
+
z
|
|
9
|
+
.object({
|
|
10
|
+
authenticationId: z
|
|
11
|
+
.number()
|
|
12
|
+
.int()
|
|
13
|
+
.positive()
|
|
14
|
+
.describe("Authentication ID to retrieve"),
|
|
15
|
+
})
|
|
16
|
+
.describe("Get a specific authentication by ID"),
|
|
17
|
+
AuthItemSchema,
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
// Type inferred from schema + function config
|
|
21
|
+
export type GetAuthenticationOptions = z.infer<
|
|
22
|
+
typeof GetAuthenticationSchema
|
|
23
|
+
> & {
|
|
24
|
+
/** Base URL for Zapier API */
|
|
25
|
+
baseUrl?: string;
|
|
26
|
+
/** Authentication token */
|
|
27
|
+
token?: string;
|
|
28
|
+
/** Function to dynamically resolve authentication token */
|
|
29
|
+
getToken?: () => Promise<string | undefined>;
|
|
30
|
+
/** Optional pre-instantiated API client */
|
|
31
|
+
api?: any;
|
|
32
|
+
/** Enable debug logging */
|
|
33
|
+
debug?: boolean;
|
|
34
|
+
/** Custom fetch implementation */
|
|
35
|
+
fetch?: typeof globalThis.fetch;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// SDK function interface - ready to be mixed into main SDK interface
|
|
39
|
+
export interface GetAuthenticationSdkFunction {
|
|
40
|
+
getAuthentication: (
|
|
41
|
+
options: GetAuthenticationOptions,
|
|
42
|
+
) => Promise<Authentication>;
|
|
43
|
+
}
|
|
@@ -22,7 +22,6 @@ export async function runAction(
|
|
|
22
22
|
actionKey,
|
|
23
23
|
inputs,
|
|
24
24
|
authenticationId: providedAuthenticationId,
|
|
25
|
-
token,
|
|
26
25
|
} = options;
|
|
27
26
|
|
|
28
27
|
const api = getOrCreateApiClient(options);
|
|
@@ -43,17 +42,15 @@ export async function runAction(
|
|
|
43
42
|
);
|
|
44
43
|
}
|
|
45
44
|
|
|
46
|
-
// Execute the action using the
|
|
45
|
+
// Execute the action using the Actions API (supports all action types)
|
|
47
46
|
const startTime = Date.now();
|
|
48
|
-
const result = await
|
|
47
|
+
const result = await executeAction({
|
|
49
48
|
api,
|
|
50
49
|
appSlug: appKey,
|
|
51
50
|
actionKey: actionKey,
|
|
52
51
|
actionType: actionData.type,
|
|
53
52
|
executionOptions: { inputs: inputs || {} },
|
|
54
|
-
|
|
55
|
-
? { token: token, authentication_id: providedAuthenticationId }
|
|
56
|
-
: undefined,
|
|
53
|
+
authenticationId: providedAuthenticationId,
|
|
57
54
|
options,
|
|
58
55
|
});
|
|
59
56
|
const executionTime = Date.now() - startTime;
|
|
@@ -68,70 +65,18 @@ export async function runAction(
|
|
|
68
65
|
};
|
|
69
66
|
}
|
|
70
67
|
|
|
71
|
-
interface
|
|
72
|
-
api: any;
|
|
73
|
-
appSlug: string;
|
|
74
|
-
actionKey: string;
|
|
75
|
-
actionType: string;
|
|
76
|
-
executionOptions: { inputs: Record<string, any> };
|
|
77
|
-
auth?: { token: string; authentication_id?: number };
|
|
78
|
-
options: RunActionOptions;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
async function executeActionWithStrategy(
|
|
82
|
-
strategyOptions: ExecuteActionStrategyOptions,
|
|
83
|
-
): Promise<any> {
|
|
84
|
-
const {
|
|
85
|
-
api,
|
|
86
|
-
appSlug,
|
|
87
|
-
actionKey,
|
|
88
|
-
actionType,
|
|
89
|
-
executionOptions,
|
|
90
|
-
auth,
|
|
91
|
-
options,
|
|
92
|
-
} = strategyOptions;
|
|
93
|
-
|
|
94
|
-
// Actions API supports: read, read_bulk, write
|
|
95
|
-
// Invoke API supports: search, read, write, read_bulk, and more
|
|
96
|
-
|
|
97
|
-
const actionsApiTypes = ["read", "read_bulk", "write"];
|
|
98
|
-
const useActionsApi = actionsApiTypes.includes(actionType);
|
|
99
|
-
|
|
100
|
-
if (useActionsApi) {
|
|
101
|
-
return executeActionViaActionsApi({
|
|
102
|
-
api,
|
|
103
|
-
appSlug,
|
|
104
|
-
actionKey,
|
|
105
|
-
actionType,
|
|
106
|
-
executionOptions,
|
|
107
|
-
auth,
|
|
108
|
-
options,
|
|
109
|
-
});
|
|
110
|
-
} else {
|
|
111
|
-
return executeActionViaInvokeApi({
|
|
112
|
-
api,
|
|
113
|
-
appSlug,
|
|
114
|
-
actionKey,
|
|
115
|
-
actionType,
|
|
116
|
-
executionOptions,
|
|
117
|
-
auth,
|
|
118
|
-
options,
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
interface ExecuteActionViaActionsApiOptions {
|
|
68
|
+
interface ExecuteActionOptions {
|
|
124
69
|
api: any;
|
|
125
70
|
appSlug: string;
|
|
126
71
|
actionKey: string;
|
|
127
72
|
actionType: string;
|
|
128
73
|
executionOptions: { inputs: Record<string, any> };
|
|
129
|
-
|
|
74
|
+
authenticationId?: number;
|
|
130
75
|
options: RunActionOptions;
|
|
131
76
|
}
|
|
132
77
|
|
|
133
|
-
async function
|
|
134
|
-
|
|
78
|
+
async function executeAction(
|
|
79
|
+
actionOptions: ExecuteActionOptions,
|
|
135
80
|
): Promise<any> {
|
|
136
81
|
const {
|
|
137
82
|
api,
|
|
@@ -139,9 +84,9 @@ async function executeActionViaActionsApi(
|
|
|
139
84
|
actionKey,
|
|
140
85
|
actionType,
|
|
141
86
|
executionOptions,
|
|
142
|
-
|
|
87
|
+
authenticationId,
|
|
143
88
|
options,
|
|
144
|
-
} =
|
|
89
|
+
} = actionOptions;
|
|
145
90
|
|
|
146
91
|
// Use the standalone getApp function
|
|
147
92
|
const appData = await getApp({
|
|
@@ -158,16 +103,10 @@ async function executeActionViaActionsApi(
|
|
|
158
103
|
throw new Error("No current_implementation_id found for app");
|
|
159
104
|
}
|
|
160
105
|
|
|
161
|
-
if (!auth?.token) {
|
|
162
|
-
throw new Error(
|
|
163
|
-
"Authentication token is required. Please provide token when creating the SDK.",
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
106
|
// Step 1: POST to /actions/v1/runs to start execution
|
|
168
107
|
const runRequest = {
|
|
169
108
|
data: {
|
|
170
|
-
authentication_id:
|
|
109
|
+
authentication_id: authenticationId || 1,
|
|
171
110
|
selected_api: selectedApi,
|
|
172
111
|
action_key: actionKey,
|
|
173
112
|
action_type: actionType,
|
|
@@ -186,68 +125,4 @@ async function executeActionViaActionsApi(
|
|
|
186
125
|
});
|
|
187
126
|
}
|
|
188
127
|
|
|
189
|
-
interface ExecuteActionViaInvokeApiOptions {
|
|
190
|
-
api: any;
|
|
191
|
-
appSlug: string;
|
|
192
|
-
actionKey: string;
|
|
193
|
-
actionType: string;
|
|
194
|
-
executionOptions: { inputs: Record<string, any> };
|
|
195
|
-
auth?: { token: string; authentication_id?: number };
|
|
196
|
-
options: RunActionOptions;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
async function executeActionViaInvokeApi(
|
|
200
|
-
apiOptions: ExecuteActionViaInvokeApiOptions,
|
|
201
|
-
): Promise<any> {
|
|
202
|
-
const {
|
|
203
|
-
api,
|
|
204
|
-
appSlug,
|
|
205
|
-
actionKey,
|
|
206
|
-
actionType,
|
|
207
|
-
executionOptions,
|
|
208
|
-
auth,
|
|
209
|
-
options,
|
|
210
|
-
} = apiOptions;
|
|
211
|
-
|
|
212
|
-
// Use the standalone getApp function
|
|
213
|
-
const appData = await getApp({
|
|
214
|
-
appKey: appSlug,
|
|
215
|
-
api,
|
|
216
|
-
token: options.token,
|
|
217
|
-
baseUrl: options.baseUrl,
|
|
218
|
-
debug: options.debug,
|
|
219
|
-
fetch: options.fetch,
|
|
220
|
-
});
|
|
221
|
-
const selectedApi = appData.current_implementation_id;
|
|
222
|
-
|
|
223
|
-
if (!selectedApi) {
|
|
224
|
-
throw new Error("No current_implementation_id found for app");
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if (!auth?.token) {
|
|
228
|
-
throw new Error(
|
|
229
|
-
"Authentication token is required. Please provide token when creating the SDK.",
|
|
230
|
-
);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// Step 1: POST to /invoke/v1/invoke to start execution
|
|
234
|
-
const invokeRequest = {
|
|
235
|
-
selected_api: selectedApi,
|
|
236
|
-
action: actionKey,
|
|
237
|
-
type_of: actionType,
|
|
238
|
-
authentication_id: auth.authentication_id || 1,
|
|
239
|
-
params: executionOptions.inputs || {},
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
const invokeData = await api.post("/api/invoke/v1/invoke", invokeRequest);
|
|
243
|
-
const invocationId = invokeData.invocation_id;
|
|
244
|
-
|
|
245
|
-
// Step 2: Poll GET /invoke/v1/invoke/{invocation_id} for results
|
|
246
|
-
return await api.poll(`/api/invoke/v1/invoke/${invocationId}`, {
|
|
247
|
-
successStatus: 200,
|
|
248
|
-
pendingStatus: 202,
|
|
249
|
-
resultExtractor: (result: any) => result.results || result,
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
|
|
253
128
|
// No registry info here - moved to info.ts for proper tree-shaking
|
package/src/index.ts
CHANGED
|
@@ -6,6 +6,9 @@ export * from "./plugins/apps";
|
|
|
6
6
|
// Export schema utilities for CLI
|
|
7
7
|
export { isPositional } from "./schema-utils";
|
|
8
8
|
|
|
9
|
+
// Export auth utilities for CLI use
|
|
10
|
+
export * from "./auth";
|
|
11
|
+
|
|
9
12
|
// Export resolvers for CLI use
|
|
10
13
|
export * from "./resolvers";
|
|
11
14
|
|
|
@@ -13,6 +16,7 @@ export * from "./resolvers";
|
|
|
13
16
|
|
|
14
17
|
// Export individual functions for tree-shaking
|
|
15
18
|
export { listAuthentications } from "./functions/listAuthentications";
|
|
19
|
+
export { getAuthentication } from "./functions/getAuthentication";
|
|
16
20
|
export { findFirstAuthentication } from "./functions/findFirstAuthentication";
|
|
17
21
|
export { findUniqueAuthentication } from "./functions/findUniqueAuthentication";
|
|
18
22
|
export { listApps } from "./functions/listApps";
|