@zapier/zapier-sdk 0.1.1 → 0.2.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 (50) hide show
  1. package/dist/api/client.js +23 -6
  2. package/dist/api/index.d.ts +4 -2
  3. package/dist/api/index.js +5 -4
  4. package/dist/api/types.d.ts +3 -1
  5. package/dist/auth.d.ts +59 -0
  6. package/dist/auth.js +261 -0
  7. package/dist/functions/getAction/info.d.ts +3 -3
  8. package/dist/functions/getAction/schemas.d.ts +3 -3
  9. package/dist/functions/getAuthentication/index.d.ts +13 -0
  10. package/dist/functions/getAuthentication/index.js +38 -0
  11. package/dist/functions/getAuthentication/info.d.ts +12 -0
  12. package/dist/functions/getAuthentication/info.js +11 -0
  13. package/dist/functions/getAuthentication/schemas.d.ts +26 -0
  14. package/dist/functions/getAuthentication/schemas.js +16 -0
  15. package/dist/functions/listActions/info.d.ts +3 -3
  16. package/dist/functions/listActions/schemas.d.ts +3 -3
  17. package/dist/functions/listAuthentications/index.js +1 -4
  18. package/dist/functions/listAuthentications/schemas.d.ts +2 -0
  19. package/dist/functions/listFields/info.d.ts +3 -3
  20. package/dist/functions/listFields/schemas.d.ts +3 -3
  21. package/dist/functions/runAction/index.js +8 -79
  22. package/dist/functions/runAction/info.d.ts +3 -3
  23. package/dist/functions/runAction/schemas.d.ts +5 -3
  24. package/dist/index.d.ts +2 -0
  25. package/dist/index.js +5 -1
  26. package/dist/sdk.js +40 -60
  27. package/dist/types/domain.d.ts +3 -0
  28. package/dist/types/events.d.ts +37 -0
  29. package/dist/types/events.js +8 -0
  30. package/dist/types/properties.d.ts +1 -1
  31. package/dist/types/properties.js +10 -1
  32. package/dist/types/sdk.d.ts +2 -1
  33. package/package.json +4 -3
  34. package/src/api/client.ts +31 -5
  35. package/src/api/index.ts +12 -5
  36. package/src/api/types.ts +11 -1
  37. package/src/auth.ts +340 -0
  38. package/src/functions/getAuthentication/index.ts +51 -0
  39. package/src/functions/getAuthentication/info.ts +9 -0
  40. package/src/functions/getAuthentication/schemas.ts +43 -0
  41. package/src/functions/listAuthentications/index.ts +1 -8
  42. package/src/functions/listAuthentications/schemas.ts +2 -0
  43. package/src/functions/runAction/index.ts +11 -141
  44. package/src/functions/runAction/schemas.ts +2 -0
  45. package/src/index.ts +4 -0
  46. package/src/sdk.ts +35 -87
  47. package/src/types/domain.ts +5 -0
  48. package/src/types/events.ts +43 -0
  49. package/src/types/properties.ts +10 -1
  50. package/src/types/sdk.ts +2 -0
@@ -22,16 +22,10 @@ export async function runAction(
22
22
  actionKey,
23
23
  inputs,
24
24
  authenticationId: providedAuthenticationId,
25
- token,
26
25
  } = options;
27
26
 
28
- if (!token && !process.env.ZAPIER_TOKEN) {
29
- throw new Error(
30
- "Authentication token is required to run actions. Please provide token in options or set ZAPIER_TOKEN environment variable.",
31
- );
32
- }
33
-
34
27
  const api = getOrCreateApiClient(options);
28
+ api.requireAuthTo("run actions");
35
29
 
36
30
  // Validate that the action exists
37
31
  const actionData = await getAction({
@@ -48,17 +42,15 @@ export async function runAction(
48
42
  );
49
43
  }
50
44
 
51
- // Execute the action using the appropriate API based on action type
45
+ // Execute the action using the Actions API (supports all action types)
52
46
  const startTime = Date.now();
53
- const result = await executeActionWithStrategy({
47
+ const result = await executeAction({
54
48
  api,
55
49
  appSlug: appKey,
56
50
  actionKey: actionKey,
57
51
  actionType: actionData.type,
58
52
  executionOptions: { inputs: inputs || {} },
59
- auth: token
60
- ? { token: token, authentication_id: providedAuthenticationId }
61
- : undefined,
53
+ authenticationId: providedAuthenticationId,
62
54
  options,
63
55
  });
64
56
  const executionTime = Date.now() - startTime;
@@ -73,70 +65,18 @@ export async function runAction(
73
65
  };
74
66
  }
75
67
 
76
- interface ExecuteActionStrategyOptions {
77
- api: any;
78
- appSlug: string;
79
- actionKey: string;
80
- actionType: string;
81
- executionOptions: { inputs: Record<string, any> };
82
- auth?: { token: string; authentication_id?: number };
83
- options: RunActionOptions;
84
- }
85
-
86
- async function executeActionWithStrategy(
87
- strategyOptions: ExecuteActionStrategyOptions,
88
- ): Promise<any> {
89
- const {
90
- api,
91
- appSlug,
92
- actionKey,
93
- actionType,
94
- executionOptions,
95
- auth,
96
- options,
97
- } = strategyOptions;
98
-
99
- // Actions API supports: read, read_bulk, write
100
- // Invoke API supports: search, read, write, read_bulk, and more
101
-
102
- const actionsApiTypes = ["read", "read_bulk", "write"];
103
- const useActionsApi = actionsApiTypes.includes(actionType);
104
-
105
- if (useActionsApi) {
106
- return executeActionViaActionsApi({
107
- api,
108
- appSlug,
109
- actionKey,
110
- actionType,
111
- executionOptions,
112
- auth,
113
- options,
114
- });
115
- } else {
116
- return executeActionViaInvokeApi({
117
- api,
118
- appSlug,
119
- actionKey,
120
- actionType,
121
- executionOptions,
122
- auth,
123
- options,
124
- });
125
- }
126
- }
127
-
128
- interface ExecuteActionViaActionsApiOptions {
68
+ interface ExecuteActionOptions {
129
69
  api: any;
130
70
  appSlug: string;
131
71
  actionKey: string;
132
72
  actionType: string;
133
73
  executionOptions: { inputs: Record<string, any> };
134
- auth?: { token: string; authentication_id?: number };
74
+ authenticationId?: number;
135
75
  options: RunActionOptions;
136
76
  }
137
77
 
138
- async function executeActionViaActionsApi(
139
- apiOptions: ExecuteActionViaActionsApiOptions,
78
+ async function executeAction(
79
+ actionOptions: ExecuteActionOptions,
140
80
  ): Promise<any> {
141
81
  const {
142
82
  api,
@@ -144,9 +84,9 @@ async function executeActionViaActionsApi(
144
84
  actionKey,
145
85
  actionType,
146
86
  executionOptions,
147
- auth,
87
+ authenticationId,
148
88
  options,
149
- } = apiOptions;
89
+ } = actionOptions;
150
90
 
151
91
  // Use the standalone getApp function
152
92
  const appData = await getApp({
@@ -163,16 +103,10 @@ async function executeActionViaActionsApi(
163
103
  throw new Error("No current_implementation_id found for app");
164
104
  }
165
105
 
166
- if (!auth?.token) {
167
- throw new Error(
168
- "Authentication token is required. Please provide token when creating the SDK.",
169
- );
170
- }
171
-
172
106
  // Step 1: POST to /actions/v1/runs to start execution
173
107
  const runRequest = {
174
108
  data: {
175
- authentication_id: auth.authentication_id || 1,
109
+ authentication_id: authenticationId || 1,
176
110
  selected_api: selectedApi,
177
111
  action_key: actionKey,
178
112
  action_type: actionType,
@@ -191,68 +125,4 @@ async function executeActionViaActionsApi(
191
125
  });
192
126
  }
193
127
 
194
- interface ExecuteActionViaInvokeApiOptions {
195
- api: any;
196
- appSlug: string;
197
- actionKey: string;
198
- actionType: string;
199
- executionOptions: { inputs: Record<string, any> };
200
- auth?: { token: string; authentication_id?: number };
201
- options: RunActionOptions;
202
- }
203
-
204
- async function executeActionViaInvokeApi(
205
- apiOptions: ExecuteActionViaInvokeApiOptions,
206
- ): Promise<any> {
207
- const {
208
- api,
209
- appSlug,
210
- actionKey,
211
- actionType,
212
- executionOptions,
213
- auth,
214
- options,
215
- } = apiOptions;
216
-
217
- // Use the standalone getApp function
218
- const appData = await getApp({
219
- appKey: appSlug,
220
- api,
221
- token: options.token,
222
- baseUrl: options.baseUrl,
223
- debug: options.debug,
224
- fetch: options.fetch,
225
- });
226
- const selectedApi = appData.current_implementation_id;
227
-
228
- if (!selectedApi) {
229
- throw new Error("No current_implementation_id found for app");
230
- }
231
-
232
- if (!auth?.token) {
233
- throw new Error(
234
- "Authentication token is required. Please provide token when creating the SDK.",
235
- );
236
- }
237
-
238
- // Step 1: POST to /invoke/v1/invoke to start execution
239
- const invokeRequest = {
240
- selected_api: selectedApi,
241
- action: actionKey,
242
- type_of: actionType,
243
- authentication_id: auth.authentication_id || 1,
244
- params: executionOptions.inputs || {},
245
- };
246
-
247
- const invokeData = await api.post("/api/invoke/v1/invoke", invokeRequest);
248
- const invocationId = invokeData.invocation_id;
249
-
250
- // Step 2: Poll GET /invoke/v1/invoke/{invocation_id} for results
251
- return await api.poll(`/api/invoke/v1/invoke/${invocationId}`, {
252
- successStatus: 200,
253
- pendingStatus: 202,
254
- resultExtractor: (result: any) => result.results || result,
255
- });
256
- }
257
-
258
128
  // No registry info here - moved to info.ts for proper tree-shaking
@@ -27,6 +27,8 @@ export type RunActionOptions = z.infer<typeof RunActionSchema> & {
27
27
  baseUrl?: string;
28
28
  /** Authentication token */
29
29
  token?: string;
30
+ /** Function to dynamically resolve authentication token */
31
+ getToken?: () => Promise<string | undefined>;
30
32
  /** Optional pre-instantiated API client */
31
33
  api?: any;
32
34
  /** Enable debug logging */
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";
package/src/sdk.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createZapierApi } from "./api";
2
2
  import { BaseSdkOptions } from "./types/domain";
3
+ import { getTokenFromEnvOrConfig } from "./auth";
3
4
  // Import function implementations
4
5
  import { listApps } from "./functions/listApps";
5
6
  import { getApp } from "./functions/getApp";
@@ -7,6 +8,7 @@ import { listActions } from "./functions/listActions";
7
8
  import { getAction } from "./functions/getAction";
8
9
  import { runAction } from "./functions/runAction";
9
10
  import { listAuthentications } from "./functions/listAuthentications";
11
+ import { getAuthentication } from "./functions/getAuthentication";
10
12
  import { findFirstAuthentication } from "./functions/findFirstAuthentication";
11
13
  import { findUniqueAuthentication } from "./functions/findUniqueAuthentication";
12
14
  import { listFields } from "./functions/listFields";
@@ -20,6 +22,7 @@ import { listActionsInfo } from "./functions/listActions/info";
20
22
  import { getActionInfo } from "./functions/getAction/info";
21
23
  import { runActionInfo } from "./functions/runAction/info";
22
24
  import { listAuthenticationsInfo } from "./functions/listAuthentications/info";
25
+ import { getAuthenticationInfo } from "./functions/getAuthentication/info";
23
26
  import { findFirstAuthenticationInfo } from "./functions/findFirstAuthentication/info";
24
27
  import { findUniqueAuthenticationInfo } from "./functions/findUniqueAuthentication/info";
25
28
  import { listFieldsInfo } from "./functions/listFields/info";
@@ -34,6 +37,7 @@ const functionRegistry = [
34
37
  getActionInfo,
35
38
  runActionInfo,
36
39
  listAuthenticationsInfo,
40
+ getAuthenticationInfo,
37
41
  findFirstAuthenticationInfo,
38
42
  findUniqueAuthenticationInfo,
39
43
  listFieldsInfo,
@@ -49,122 +53,66 @@ import { createAppsPlugin } from "./plugins/apps/index";
49
53
 
50
54
  // TODO: Add plugin registry back when needed for CLI compatibility
51
55
 
52
- // Base SDK functions interface (functions + registry, no plugins)
53
- interface BaseZapierSdkWithFunctions {
54
- __registry: Array<{
55
- name: string;
56
- inputSchema: any;
57
- implementation: Function;
58
- }>;
59
- listApps: ZapierSdk["listApps"];
60
- getApp: ZapierSdk["getApp"];
61
- listActions: ZapierSdk["listActions"];
62
- getAction: ZapierSdk["getAction"];
63
- runAction: ZapierSdk["runAction"];
64
- listAuthentications: ZapierSdk["listAuthentications"];
65
- findFirstAuthentication: ZapierSdk["findFirstAuthentication"];
66
- findUniqueAuthentication: ZapierSdk["findUniqueAuthentication"];
67
- listFields: ZapierSdk["listFields"];
68
- generateTypes: ZapierSdk["generateTypes"];
69
- bundleCode: ZapierSdk["bundleCode"];
70
- }
71
-
72
56
  // Full SDK interface with plugins applied
73
57
  export type { ZapierSdk } from "./types/sdk";
74
58
 
75
59
  export interface ZapierSdkOptions extends BaseSdkOptions {}
76
60
 
77
- function createBaseZapierSdk(
78
- options: ZapierSdkOptions = {},
79
- ): BaseZapierSdkWithFunctions {
80
- // Auto-load .env files (searches up directory tree)
81
- try {
82
- const { findUpSync } = require("find-up");
83
- const envPath = findUpSync(".env");
84
- if (envPath) {
85
- require("dotenv").config({ path: envPath, quiet: true });
86
- }
87
- } catch {
88
- // Silently fail if dotenv/find-up not available or .env not found
89
- }
90
-
61
+ export function createZapierSdk(options: ZapierSdkOptions = {}): ZapierSdk {
91
62
  const {
92
63
  fetch: customFetch = globalThis.fetch,
93
64
  baseUrl = "https://zapier.com",
94
65
  token,
66
+ getToken,
67
+ onEvent,
95
68
  debug = false,
96
69
  } = options;
97
70
 
98
- // If no token provided, try to get it from environment variable
99
- const finalToken = token || process.env.ZAPIER_TOKEN;
71
+ // If no explicit token or getToken provided, try env var then CLI config with event callbacks
72
+ const resolvedGetToken =
73
+ getToken ||
74
+ (!token
75
+ ? () => getTokenFromEnvOrConfig({ onEvent, fetch: customFetch })
76
+ : undefined);
100
77
 
101
78
  // Create the API client
102
79
  const api = createZapierApi({
103
80
  baseUrl,
104
- token: finalToken,
81
+ token,
82
+ getToken: resolvedGetToken,
105
83
  debug,
106
84
  fetch: customFetch,
107
85
  });
108
86
 
109
- // Build SDK directly - TypeScript will enforce correct implementation
110
- const sdk: BaseZapierSdkWithFunctions = {
87
+ // Create plugins directly - TypeScript will enforce correct implementation
88
+ const appsPlugin = createAppsPlugin({
89
+ api,
90
+ token,
91
+ });
92
+
93
+ // Compose final SDK - TypeScript will enforce we have all required properties
94
+ const fullSdk: ZapierSdk = {
111
95
  // Registry for CLI
112
96
  __registry: functionRegistry,
113
97
 
114
98
  // Function implementations with API config injection
115
- listApps: (options = {}) =>
116
- listApps({ ...options, api, token: finalToken }),
117
- getApp: (options) => getApp({ ...options, api, token: finalToken }),
118
- listActions: (options = {}) =>
119
- listActions({ ...options, api, token: finalToken }),
120
- getAction: (options) => getAction({ ...options, api, token: finalToken }),
121
- runAction: (options) => runAction({ ...options, api, token: finalToken }),
99
+ listApps: (options = {}) => listApps({ ...options, api }),
100
+ getApp: (options) => getApp({ ...options, api }),
101
+ listActions: (options = {}) => listActions({ ...options, api }),
102
+ getAction: (options) => getAction({ ...options, api }),
103
+ runAction: (options) => runAction({ ...options, api }),
122
104
  listAuthentications: (options = {}) =>
123
- listAuthentications({ ...options, api, token: finalToken }),
105
+ listAuthentications({ ...options, api }),
106
+ getAuthentication: (options) => getAuthentication({ ...options, api }),
124
107
  findFirstAuthentication: (options = {}) =>
125
- findFirstAuthentication({ ...options, api, token: finalToken }),
108
+ findFirstAuthentication({ ...options, api }),
126
109
  findUniqueAuthentication: (options = {}) =>
127
- findUniqueAuthentication({ ...options, api, token: finalToken }),
128
- listFields: (options) => listFields({ ...options, api, token: finalToken }),
129
- generateTypes: (options) =>
130
- generateTypes({ ...options, api, token: finalToken }),
110
+ findUniqueAuthentication({ ...options, api }),
111
+ listFields: (options) => listFields({ ...options, api }),
112
+ generateTypes: (options) => generateTypes({ ...options, api }),
131
113
  bundleCode: (options) => bundleCode(options), // No API config needed
132
- };
133
-
134
- return sdk;
135
- }
136
-
137
- export function createZapierSdk(options: ZapierSdkOptions = {}): ZapierSdk {
138
- // Create base SDK
139
- const baseSdk = createBaseZapierSdk(options);
140
114
 
141
- // Extract options needed for plugins
142
- const {
143
- fetch: customFetch = globalThis.fetch,
144
- baseUrl = "https://zapier.com",
145
- token,
146
- debug = false,
147
- } = options;
148
-
149
- const finalToken = token || process.env.ZAPIER_TOKEN;
150
-
151
- // Create the API client for plugins
152
- const api = createZapierApi({
153
- baseUrl,
154
- token: finalToken,
155
- debug,
156
- fetch: customFetch,
157
- });
158
-
159
- // Create plugins directly - TypeScript will enforce correct implementation
160
- const appsPlugin = createAppsPlugin({
161
- api,
162
- token: finalToken,
163
- });
164
-
165
- // Compose final SDK - TypeScript will enforce we have all required properties
166
- const fullSdk: ZapierSdk = {
167
- ...baseSdk,
115
+ // Add plugins
168
116
  apps: appsPlugin,
169
117
  };
170
118
 
@@ -14,6 +14,9 @@ export type {
14
14
  AuthenticationsResponse,
15
15
  } from "../api/types";
16
16
 
17
+ // Import event types
18
+ import type { EventCallback } from "./events";
19
+
17
20
  // SDK Configuration Types
18
21
  export interface ActionExecutionOptions {
19
22
  inputs?: Record<string, any>;
@@ -26,6 +29,8 @@ export interface AuthObject {
26
29
 
27
30
  export interface BaseSdkOptions {
28
31
  token?: string;
32
+ getToken?: () => Promise<string | undefined>;
33
+ onEvent?: EventCallback;
29
34
  fetch?: typeof fetch;
30
35
  baseUrl?: string;
31
36
  debug?: boolean;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * SDK Event System
3
+ *
4
+ * This module provides common event type definitions for the SDK's
5
+ * event system, including authentication, API, and loading events.
6
+ */
7
+
8
+ // Generic event system
9
+ export interface SdkEvent {
10
+ type: string;
11
+ payload?: Record<string, any>;
12
+ timestamp?: number;
13
+ }
14
+
15
+ export interface AuthEvent extends SdkEvent {
16
+ type: "auth_refreshing" | "auth_success" | "auth_error" | "auth_logout";
17
+ payload?: {
18
+ message?: string;
19
+ error?: string;
20
+ operation?: string;
21
+ };
22
+ }
23
+
24
+ export interface ApiEvent extends SdkEvent {
25
+ type: "api_request" | "api_response" | "api_error";
26
+ payload?: {
27
+ url?: string;
28
+ method?: string;
29
+ status?: number;
30
+ duration?: number;
31
+ error?: string;
32
+ };
33
+ }
34
+
35
+ export interface LoadingEvent extends SdkEvent {
36
+ type: "loading_start" | "loading_end";
37
+ payload?: {
38
+ operation?: string;
39
+ resource?: string;
40
+ };
41
+ }
42
+
43
+ export type EventCallback = (event: SdkEvent) => void;
@@ -9,7 +9,16 @@ export const AppKeyPropertySchema = withPositional(
9
9
  );
10
10
 
11
11
  export const ActionTypePropertySchema = z
12
- .enum(["read", "write", "search", "create", "update", "delete"])
12
+ .enum([
13
+ "read",
14
+ "read_bulk",
15
+ "write",
16
+ "run",
17
+ "search",
18
+ "search_or_write",
19
+ "search_and_write",
20
+ "filter",
21
+ ])
13
22
  .describe("Action type that matches the action's defined type");
14
23
 
15
24
  export const ActionKeyPropertySchema = z
package/src/types/sdk.ts CHANGED
@@ -5,6 +5,7 @@ import type { GetAppSdkFunction } from "../functions/getApp/schemas";
5
5
  import type { RunActionSdkFunction } from "../functions/runAction/schemas";
6
6
  import type { ListFieldsSdkFunction } from "../functions/listFields/schemas";
7
7
  import type { ListAuthenticationsSdkFunction } from "../functions/listAuthentications/schemas";
8
+ import type { GetAuthenticationSdkFunction } from "../functions/getAuthentication/schemas";
8
9
  import type { FindFirstAuthenticationSdkFunction } from "../functions/findFirstAuthentication/schemas";
9
10
  import type { FindUniqueAuthenticationSdkFunction } from "../functions/findUniqueAuthentication/schemas";
10
11
  import type { GenerateTypesSdkFunction } from "../functions/generateTypes/schemas";
@@ -22,6 +23,7 @@ export interface ZapierSdkFunctions
22
23
  RunActionSdkFunction,
23
24
  ListFieldsSdkFunction,
24
25
  ListAuthenticationsSdkFunction,
26
+ GetAuthenticationSdkFunction,
25
27
  FindFirstAuthenticationSdkFunction,
26
28
  FindUniqueAuthenticationSdkFunction,
27
29
  GenerateTypesSdkFunction,