@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.
Files changed (45) hide show
  1. package/dist/api/client.js +3 -2
  2. package/dist/api/types.d.ts +1 -1
  3. package/dist/auth.d.ts +59 -0
  4. package/dist/auth.js +261 -0
  5. package/dist/functions/getAction/info.d.ts +3 -3
  6. package/dist/functions/getAction/schemas.d.ts +3 -3
  7. package/dist/functions/getAuthentication/index.d.ts +13 -0
  8. package/dist/functions/getAuthentication/index.js +38 -0
  9. package/dist/functions/getAuthentication/info.d.ts +12 -0
  10. package/dist/functions/getAuthentication/info.js +11 -0
  11. package/dist/functions/getAuthentication/schemas.d.ts +26 -0
  12. package/dist/functions/getAuthentication/schemas.js +16 -0
  13. package/dist/functions/listActions/info.d.ts +3 -3
  14. package/dist/functions/listActions/schemas.d.ts +3 -3
  15. package/dist/functions/listFields/info.d.ts +3 -3
  16. package/dist/functions/listFields/schemas.d.ts +3 -3
  17. package/dist/functions/runAction/index.js +7 -76
  18. package/dist/functions/runAction/info.d.ts +3 -3
  19. package/dist/functions/runAction/schemas.d.ts +3 -3
  20. package/dist/index.d.ts +2 -0
  21. package/dist/index.js +5 -1
  22. package/dist/sdk.js +31 -39
  23. package/dist/types/domain.d.ts +2 -0
  24. package/dist/types/events.d.ts +37 -0
  25. package/dist/types/events.js +8 -0
  26. package/dist/types/properties.d.ts +1 -1
  27. package/dist/types/properties.js +10 -1
  28. package/dist/types/sdk.d.ts +2 -1
  29. package/package.json +4 -1
  30. package/src/api/client.ts +3 -2
  31. package/src/api/types.ts +9 -1
  32. package/src/auth.ts +340 -0
  33. package/src/functions/getAuthentication/index.ts +51 -0
  34. package/src/functions/getAuthentication/info.ts +9 -0
  35. package/src/functions/getAuthentication/schemas.ts +43 -0
  36. package/src/functions/runAction/index.ts +10 -135
  37. package/src/index.ts +4 -0
  38. package/src/sdk.ts +24 -61
  39. package/src/types/domain.ts +4 -0
  40. package/src/types/events.ts +43 -0
  41. package/src/types/properties.ts +10 -1
  42. package/src/types/sdk.ts +2 -0
  43. package/dist/utils/getTokenFromConfig.d.ts +0 -7
  44. package/dist/utils/getTokenFromConfig.js +0 -29
  45. package/src/utils/getTokenFromConfig.ts +0 -28
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,53 +53,45 @@ 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 {
61
+ export function createZapierSdk(options: ZapierSdkOptions = {}): ZapierSdk {
80
62
  const {
81
63
  fetch: customFetch = globalThis.fetch,
82
64
  baseUrl = "https://zapier.com",
83
65
  token,
84
66
  getToken,
67
+ onEvent,
85
68
  debug = false,
86
69
  } = options;
87
70
 
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);
77
+
88
78
  // Create the API client
89
79
  const api = createZapierApi({
90
80
  baseUrl,
91
81
  token,
92
- getToken,
82
+ getToken: resolvedGetToken,
93
83
  debug,
94
84
  fetch: customFetch,
95
85
  });
96
86
 
97
- // Build SDK directly - TypeScript will enforce correct implementation
98
- 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 = {
99
95
  // Registry for CLI
100
96
  __registry: functionRegistry,
101
97
 
@@ -107,6 +103,7 @@ function createBaseZapierSdk(
107
103
  runAction: (options) => runAction({ ...options, api }),
108
104
  listAuthentications: (options = {}) =>
109
105
  listAuthentications({ ...options, api }),
106
+ getAuthentication: (options) => getAuthentication({ ...options, api }),
110
107
  findFirstAuthentication: (options = {}) =>
111
108
  findFirstAuthentication({ ...options, api }),
112
109
  findUniqueAuthentication: (options = {}) =>
@@ -114,42 +111,8 @@ function createBaseZapierSdk(
114
111
  listFields: (options) => listFields({ ...options, api }),
115
112
  generateTypes: (options) => generateTypes({ ...options, api }),
116
113
  bundleCode: (options) => bundleCode(options), // No API config needed
117
- };
118
-
119
- return sdk;
120
- }
121
-
122
- export function createZapierSdk(options: ZapierSdkOptions = {}): ZapierSdk {
123
- // Create base SDK
124
- const baseSdk = createBaseZapierSdk(options);
125
-
126
- // Extract options needed for plugins
127
- const {
128
- fetch: customFetch = globalThis.fetch,
129
- baseUrl = "https://zapier.com",
130
- token,
131
- getToken,
132
- debug = false,
133
- } = options;
134
114
 
135
- // Create the API client for plugins
136
- const api = createZapierApi({
137
- baseUrl,
138
- token,
139
- getToken,
140
- debug,
141
- fetch: customFetch,
142
- });
143
-
144
- // Create plugins directly - TypeScript will enforce correct implementation
145
- const appsPlugin = createAppsPlugin({
146
- api,
147
- token,
148
- });
149
-
150
- // Compose final SDK - TypeScript will enforce we have all required properties
151
- const fullSdk: ZapierSdk = {
152
- ...baseSdk,
115
+ // Add plugins
153
116
  apps: appsPlugin,
154
117
  };
155
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>;
@@ -27,6 +30,7 @@ export interface AuthObject {
27
30
  export interface BaseSdkOptions {
28
31
  token?: string;
29
32
  getToken?: () => Promise<string | undefined>;
33
+ onEvent?: EventCallback;
30
34
  fetch?: typeof fetch;
31
35
  baseUrl?: string;
32
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,
@@ -1,7 +0,0 @@
1
- /**
2
- * Attempts to read the JWT token from the CLI config file.
3
- * This is a synchronous function that only returns valid, non-expired tokens.
4
- * For token refresh logic, the CLI should handle that separately.
5
- * Returns undefined if config file doesn't exist, token is not available, or token is expired.
6
- */
7
- export declare function getTokenFromConfig(): string | undefined;
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getTokenFromConfig = getTokenFromConfig;
4
- /**
5
- * Attempts to read the JWT token from the CLI config file.
6
- * This is a synchronous function that only returns valid, non-expired tokens.
7
- * For token refresh logic, the CLI should handle that separately.
8
- * Returns undefined if config file doesn't exist, token is not available, or token is expired.
9
- */
10
- function getTokenFromConfig() {
11
- try {
12
- // Dynamically import conf to avoid dependency issues if not installed
13
- const Conf = require("conf");
14
- const config = new Conf({ projectName: "zapier-sdk-cli" });
15
- // Get the stored JWT token
16
- const jwt = config.get("login_jwt");
17
- const expiresAt = config.get("login_expires_at");
18
- // Check if token exists and is not expired (with 30s buffer)
19
- if (jwt && expiresAt && expiresAt > Date.now() + 30 * 1000) {
20
- return jwt;
21
- }
22
- // Token is missing or expired - return undefined so SDK will require explicit auth
23
- return undefined;
24
- }
25
- catch {
26
- // Config package not available or other error - silently fail
27
- return undefined;
28
- }
29
- }
@@ -1,28 +0,0 @@
1
- /**
2
- * Attempts to read the JWT token from the CLI config file.
3
- * This is a synchronous function that only returns valid, non-expired tokens.
4
- * For token refresh logic, the CLI should handle that separately.
5
- * Returns undefined if config file doesn't exist, token is not available, or token is expired.
6
- */
7
- export function getTokenFromConfig(): string | undefined {
8
- try {
9
- // Dynamically import conf to avoid dependency issues if not installed
10
- const Conf = require("conf");
11
- const config = new Conf({ projectName: "zapier-sdk-cli" });
12
-
13
- // Get the stored JWT token
14
- const jwt = config.get("login_jwt") as string | undefined;
15
- const expiresAt = config.get("login_expires_at") as number | undefined;
16
-
17
- // Check if token exists and is not expired (with 30s buffer)
18
- if (jwt && expiresAt && expiresAt > Date.now() + 30 * 1000) {
19
- return jwt;
20
- }
21
-
22
- // Token is missing or expired - return undefined so SDK will require explicit auth
23
- return undefined;
24
- } catch {
25
- // Config package not available or other error - silently fail
26
- return undefined;
27
- }
28
- }