@zapier/zapier-sdk 0.3.1 → 0.4.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 (162) hide show
  1. package/dist/index.cjs +1962 -0
  2. package/dist/index.d.mts +892 -0
  3. package/dist/index.d.ts +892 -20
  4. package/dist/index.mjs +1914 -0
  5. package/package.json +19 -7
  6. package/src/api/client.ts +106 -25
  7. package/src/api/polling.ts +4 -8
  8. package/src/api/types.ts +8 -1
  9. package/src/auth.ts +19 -287
  10. package/src/functions/findFirstAuthentication/schemas.ts +3 -13
  11. package/src/functions/findUniqueAuthentication/schemas.ts +3 -13
  12. package/src/functions/generateTypes/index.ts +15 -0
  13. package/src/functions/generateTypes/schemas.ts +3 -10
  14. package/src/functions/getAction/schemas.ts +3 -13
  15. package/src/functions/getApp/schemas.ts +2 -13
  16. package/src/functions/getAuthentication/index.ts +0 -1
  17. package/src/functions/getAuthentication/schemas.ts +3 -17
  18. package/src/functions/listActions/schemas.ts +3 -13
  19. package/src/functions/listApps/schemas.ts +2 -13
  20. package/src/functions/listAuthentications/index.ts +0 -1
  21. package/src/functions/listAuthentications/schemas.ts +3 -15
  22. package/src/functions/listFields/schemas.ts +3 -13
  23. package/src/functions/request/index.ts +150 -0
  24. package/src/functions/request/info.ts +11 -0
  25. package/src/functions/request/schemas.ts +53 -0
  26. package/src/functions/runAction/index.ts +0 -1
  27. package/src/functions/runAction/schemas.ts +6 -15
  28. package/src/index.ts +8 -2
  29. package/src/plugins/apps/index.ts +34 -9
  30. package/src/plugins/apps/types.ts +17 -1
  31. package/src/plugins/fetch/index.ts +50 -0
  32. package/src/plugins/fetch/types.ts +2 -0
  33. package/src/sdk.ts +19 -23
  34. package/src/types/domain.ts +16 -0
  35. package/src/types/sdk.ts +17 -2
  36. package/tsconfig.json +2 -2
  37. package/tsup.config.ts +21 -0
  38. package/dist/api/auth.d.ts +0 -8
  39. package/dist/api/auth.js +0 -29
  40. package/dist/api/client.d.ts +0 -8
  41. package/dist/api/client.js +0 -119
  42. package/dist/api/debug.d.ts +0 -12
  43. package/dist/api/debug.js +0 -50
  44. package/dist/api/index.d.ts +0 -28
  45. package/dist/api/index.js +0 -52
  46. package/dist/api/polling.d.ts +0 -17
  47. package/dist/api/polling.js +0 -34
  48. package/dist/api/types.d.ts +0 -147
  49. package/dist/api/types.js +0 -9
  50. package/dist/auth.d.ts +0 -59
  51. package/dist/auth.js +0 -261
  52. package/dist/functions/bundleCode/index.d.ts +0 -11
  53. package/dist/functions/bundleCode/index.js +0 -91
  54. package/dist/functions/bundleCode/info.d.ts +0 -27
  55. package/dist/functions/bundleCode/info.js +0 -11
  56. package/dist/functions/bundleCode/schemas.d.ts +0 -27
  57. package/dist/functions/bundleCode/schemas.js +0 -22
  58. package/dist/functions/findFirstAuthentication/index.d.ts +0 -12
  59. package/dist/functions/findFirstAuthentication/index.js +0 -21
  60. package/dist/functions/findFirstAuthentication/info.d.ts +0 -30
  61. package/dist/functions/findFirstAuthentication/info.js +0 -11
  62. package/dist/functions/findFirstAuthentication/schemas.d.ts +0 -42
  63. package/dist/functions/findFirstAuthentication/schemas.js +0 -25
  64. package/dist/functions/findUniqueAuthentication/index.d.ts +0 -13
  65. package/dist/functions/findUniqueAuthentication/index.js +0 -28
  66. package/dist/functions/findUniqueAuthentication/info.d.ts +0 -30
  67. package/dist/functions/findUniqueAuthentication/info.js +0 -11
  68. package/dist/functions/findUniqueAuthentication/schemas.d.ts +0 -42
  69. package/dist/functions/findUniqueAuthentication/schemas.js +0 -25
  70. package/dist/functions/generateTypes/index.d.ts +0 -11
  71. package/dist/functions/generateTypes/index.js +0 -305
  72. package/dist/functions/generateTypes/info.d.ts +0 -21
  73. package/dist/functions/generateTypes/info.js +0 -11
  74. package/dist/functions/generateTypes/schemas.d.ts +0 -30
  75. package/dist/functions/generateTypes/schemas.js +0 -14
  76. package/dist/functions/getAction/index.d.ts +0 -12
  77. package/dist/functions/getAction/index.js +0 -26
  78. package/dist/functions/getAction/info.d.ts +0 -18
  79. package/dist/functions/getAction/info.js +0 -11
  80. package/dist/functions/getAction/schemas.d.ts +0 -30
  81. package/dist/functions/getAction/schemas.js +0 -13
  82. package/dist/functions/getApp/index.d.ts +0 -12
  83. package/dist/functions/getApp/index.js +0 -37
  84. package/dist/functions/getApp/info.d.ts +0 -12
  85. package/dist/functions/getApp/info.js +0 -11
  86. package/dist/functions/getApp/schemas.d.ts +0 -24
  87. package/dist/functions/getApp/schemas.js +0 -11
  88. package/dist/functions/getAuthentication/index.d.ts +0 -13
  89. package/dist/functions/getAuthentication/index.js +0 -38
  90. package/dist/functions/getAuthentication/info.d.ts +0 -12
  91. package/dist/functions/getAuthentication/info.js +0 -11
  92. package/dist/functions/getAuthentication/schemas.d.ts +0 -26
  93. package/dist/functions/getAuthentication/schemas.js +0 -16
  94. package/dist/functions/listActions/index.d.ts +0 -12
  95. package/dist/functions/listActions/index.js +0 -128
  96. package/dist/functions/listActions/info.d.ts +0 -15
  97. package/dist/functions/listActions/info.js +0 -11
  98. package/dist/functions/listActions/schemas.d.ts +0 -27
  99. package/dist/functions/listActions/schemas.js +0 -14
  100. package/dist/functions/listApps/index.d.ts +0 -12
  101. package/dist/functions/listApps/index.js +0 -50
  102. package/dist/functions/listApps/info.d.ts +0 -18
  103. package/dist/functions/listApps/info.js +0 -11
  104. package/dist/functions/listApps/schemas.d.ts +0 -30
  105. package/dist/functions/listApps/schemas.js +0 -15
  106. package/dist/functions/listAuthentications/index.d.ts +0 -12
  107. package/dist/functions/listAuthentications/index.js +0 -131
  108. package/dist/functions/listAuthentications/info.d.ts +0 -30
  109. package/dist/functions/listAuthentications/info.js +0 -11
  110. package/dist/functions/listAuthentications/schemas.d.ts +0 -44
  111. package/dist/functions/listAuthentications/schemas.js +0 -25
  112. package/dist/functions/listFields/index.d.ts +0 -12
  113. package/dist/functions/listFields/index.js +0 -65
  114. package/dist/functions/listFields/info.d.ts +0 -24
  115. package/dist/functions/listFields/info.js +0 -11
  116. package/dist/functions/listFields/schemas.d.ts +0 -36
  117. package/dist/functions/listFields/schemas.js +0 -17
  118. package/dist/functions/runAction/index.d.ts +0 -12
  119. package/dist/functions/runAction/index.js +0 -86
  120. package/dist/functions/runAction/info.d.ts +0 -24
  121. package/dist/functions/runAction/info.js +0 -11
  122. package/dist/functions/runAction/schemas.d.ts +0 -38
  123. package/dist/functions/runAction/schemas.js +0 -15
  124. package/dist/index.js +0 -57
  125. package/dist/plugins/apps/index.d.ts +0 -8
  126. package/dist/plugins/apps/index.js +0 -77
  127. package/dist/plugins/apps/info.d.ts +0 -6
  128. package/dist/plugins/apps/info.js +0 -13
  129. package/dist/plugins/apps/types.d.ts +0 -21
  130. package/dist/plugins/apps/types.js +0 -2
  131. package/dist/resolvers/actionKey.d.ts +0 -8
  132. package/dist/resolvers/actionKey.js +0 -20
  133. package/dist/resolvers/actionType.d.ts +0 -8
  134. package/dist/resolvers/actionType.js +0 -21
  135. package/dist/resolvers/appKey.d.ts +0 -6
  136. package/dist/resolvers/appKey.js +0 -8
  137. package/dist/resolvers/authenticationId.d.ts +0 -8
  138. package/dist/resolvers/authenticationId.js +0 -29
  139. package/dist/resolvers/index.d.ts +0 -39
  140. package/dist/resolvers/index.js +0 -105
  141. package/dist/resolvers/inputs.d.ts +0 -7
  142. package/dist/resolvers/inputs.js +0 -15
  143. package/dist/schema-utils.d.ts +0 -44
  144. package/dist/schema-utils.js +0 -76
  145. package/dist/schemas/Action.d.ts +0 -21
  146. package/dist/schemas/Action.js +0 -31
  147. package/dist/schemas/App.d.ts +0 -19
  148. package/dist/schemas/App.js +0 -32
  149. package/dist/schemas/Auth.d.ts +0 -27
  150. package/dist/schemas/Auth.js +0 -42
  151. package/dist/schemas/Field.d.ts +0 -15
  152. package/dist/schemas/Field.js +0 -25
  153. package/dist/sdk.d.ts +0 -6
  154. package/dist/sdk.js +0 -90
  155. package/dist/types/domain.d.ts +0 -25
  156. package/dist/types/domain.js +0 -21
  157. package/dist/types/events.d.ts +0 -37
  158. package/dist/types/events.js +0 -8
  159. package/dist/types/properties.d.ts +0 -21
  160. package/dist/types/properties.js +0 -52
  161. package/dist/types/sdk.d.ts +0 -24
  162. package/dist/types/sdk.js +0 -2
@@ -2,13 +2,11 @@ import {
2
2
  ActionExecutionOptions,
3
3
  ActionExecutionResult,
4
4
  } from "../../types/domain";
5
- import { type ApiClient } from "../../api";
6
- import { runAction } from "../../functions/runAction";
7
5
  import type { ActionProxy } from "./types";
6
+ import type { ZapierSdk } from "../../types/sdk";
8
7
 
9
8
  export interface AppsPluginOptions {
10
- api: ApiClient;
11
- token?: string;
9
+ sdk: ZapierSdk;
12
10
  }
13
11
 
14
12
  interface AppFactoryOptions {
@@ -25,7 +23,7 @@ function createActionFunction(
25
23
  return async (
26
24
  actionOptions: ActionExecutionOptions = {},
27
25
  ): Promise<ActionExecutionResult> => {
28
- const { api, token } = options;
26
+ const { sdk } = options;
29
27
  const { inputs, authenticationId: providedAuthenticationId } =
30
28
  actionOptions;
31
29
 
@@ -38,15 +36,13 @@ function createActionFunction(
38
36
  );
39
37
  }
40
38
 
41
- // Call runAction with the resolved parameters
42
- return runAction({
39
+ // Call sdk.runAction with the resolved parameters
40
+ return sdk.runAction({
43
41
  appKey,
44
42
  actionType: actionType as any,
45
43
  actionKey,
46
44
  inputs,
47
45
  authenticationId,
48
- api: api,
49
- token,
50
46
  });
51
47
  };
52
48
  }
@@ -57,6 +53,35 @@ function createActionTypeProxy(
57
53
  options: AppsPluginOptions,
58
54
  pinnedAuthId?: number,
59
55
  ) {
56
+ // Special handling for "fetch" action type
57
+ if (actionType === "fetch") {
58
+ return async (
59
+ url: string | URL,
60
+ init?: RequestInit & {
61
+ authenticationId?: number;
62
+ callbackUrl?: string;
63
+ authenticationTemplate?: string;
64
+ },
65
+ ) => {
66
+ const { sdk } = options;
67
+
68
+ // Use pinned auth ID first, then provided auth ID
69
+ const authenticationId = pinnedAuthId || init?.authenticationId;
70
+
71
+ if (!authenticationId) {
72
+ throw new Error(
73
+ `Authentication ID is required for fetch. Either use the factory pattern: sdk.apps.${appKey}({ authenticationId }).fetch(...) or provide authenticationId in the fetch call.`,
74
+ );
75
+ }
76
+
77
+ // Call sdk.fetch with the resolved authenticationId
78
+ return sdk.fetch(url, {
79
+ ...init,
80
+ authenticationId,
81
+ });
82
+ };
83
+ }
84
+
60
85
  return new Proxy(
61
86
  {},
62
87
  {
@@ -7,12 +7,28 @@ interface AppFactoryOptions {
7
7
  authenticationId: number;
8
8
  }
9
9
 
10
- interface ActionTypeProxy {
10
+ // Base action type proxy for regular actions
11
+ interface BaseActionTypeProxy {
11
12
  [action: string]: (
12
13
  options?: ActionExecutionOptions,
13
14
  ) => Promise<ActionExecutionResult>;
14
15
  }
15
16
 
17
+ // Special fetch function type
18
+ interface FetchActionType {
19
+ fetch: (
20
+ url: string | URL,
21
+ init?: RequestInit & {
22
+ authenticationId?: number;
23
+ callbackUrl?: string;
24
+ authenticationTemplate?: string;
25
+ },
26
+ ) => Promise<Response>;
27
+ }
28
+
29
+ // Combined action type proxy
30
+ type ActionTypeProxy = BaseActionTypeProxy & Partial<FetchActionType>;
31
+
16
32
  interface AppProxy {
17
33
  [type: string]: ActionTypeProxy;
18
34
  }
@@ -0,0 +1,50 @@
1
+ import type { ZapierSdk } from "../../types/sdk";
2
+
3
+ export interface FetchPluginOptions {
4
+ sdk: ZapierSdk;
5
+ }
6
+
7
+ /**
8
+ * Creates a fetch function with standard fetch signature that delegates to sdk.request
9
+ */
10
+ export function createFetchPlugin(options: FetchPluginOptions) {
11
+ const { sdk } = options;
12
+
13
+ return async function fetch(
14
+ url: string | URL,
15
+ init?: RequestInit & {
16
+ authenticationId?: number;
17
+ callbackUrl?: string;
18
+ authenticationTemplate?: string;
19
+ },
20
+ ): Promise<Response> {
21
+ const {
22
+ authenticationId,
23
+ callbackUrl,
24
+ authenticationTemplate,
25
+ ...fetchInit
26
+ } = init || {};
27
+
28
+ return sdk.request({
29
+ url: url.toString(),
30
+ method: fetchInit.method as any,
31
+ body: fetchInit.body as any,
32
+ headers: fetchInit.headers as any,
33
+ authenticationId,
34
+ callbackUrl,
35
+ authenticationTemplate,
36
+ });
37
+ };
38
+ }
39
+
40
+ // Export types for TypeScript
41
+ export interface FetchPluginSdkExtension {
42
+ fetch: (
43
+ url: string | URL,
44
+ init?: RequestInit & {
45
+ authenticationId?: number;
46
+ callbackUrl?: string;
47
+ authenticationTemplate?: string;
48
+ },
49
+ ) => Promise<Response>;
50
+ }
@@ -0,0 +1,2 @@
1
+ // Re-export types from index for consistency
2
+ export type { FetchPluginSdkExtension } from "./index";
package/src/sdk.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { createZapierApi } from "./api";
2
2
  import { BaseSdkOptions } from "./types/domain";
3
- import { getTokenFromEnvOrConfig } from "./auth";
4
3
  // Import function implementations
5
4
  import { listApps } from "./functions/listApps";
6
5
  import { getApp } from "./functions/getApp";
@@ -14,6 +13,7 @@ import { findUniqueAuthentication } from "./functions/findUniqueAuthentication";
14
13
  import { listFields } from "./functions/listFields";
15
14
  import { generateTypes } from "./functions/generateTypes";
16
15
  import { bundleCode } from "./functions/bundleCode";
16
+ import { request } from "./functions/request";
17
17
 
18
18
  // Import function registry info objects (only for CLI registry)
19
19
  import { listAppsInfo } from "./functions/listApps/info";
@@ -28,6 +28,7 @@ import { findUniqueAuthenticationInfo } from "./functions/findUniqueAuthenticati
28
28
  import { listFieldsInfo } from "./functions/listFields/info";
29
29
  import { generateTypesInfo } from "./functions/generateTypes/info";
30
30
  import { bundleCodeInfo } from "./functions/bundleCode/info";
31
+ import { requestInfo } from "./functions/request/info";
31
32
 
32
33
  // Function registry as array - uses names from function info objects
33
34
  const functionRegistry = [
@@ -43,13 +44,15 @@ const functionRegistry = [
43
44
  listFieldsInfo,
44
45
  generateTypesInfo,
45
46
  bundleCodeInfo,
47
+ requestInfo,
46
48
  ];
47
49
 
48
- // Import the properly typed SDK interface
49
- import type { ZapierSdk } from "./types/sdk";
50
+ // Import the properly typed SDK interfaces
51
+ import type { BaseZapierSdk, ZapierSdk } from "./types/sdk";
50
52
 
51
53
  // Import plugin functions
52
54
  import { createAppsPlugin } from "./plugins/apps/index";
55
+ import { createFetchPlugin } from "./plugins/fetch/index";
53
56
 
54
57
  // TODO: Add plugin registry back when needed for CLI compatibility
55
58
 
@@ -68,30 +71,18 @@ export function createZapierSdk(options: ZapierSdkOptions = {}): ZapierSdk {
68
71
  debug = false,
69
72
  } = options;
70
73
 
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
-
78
- // Create the API client
74
+ // Create the API client - it will handle token resolution internally
79
75
  const api = createZapierApi({
80
76
  baseUrl,
81
77
  token,
82
- getToken: resolvedGetToken,
78
+ getToken,
83
79
  debug,
84
80
  fetch: customFetch,
81
+ onEvent,
85
82
  });
86
83
 
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 = {
84
+ // Create base SDK with core functions only - TypeScript will enforce completeness
85
+ const baseSdk: BaseZapierSdk = {
95
86
  // Registry for CLI
96
87
  __registry: functionRegistry,
97
88
 
@@ -111,10 +102,15 @@ export function createZapierSdk(options: ZapierSdkOptions = {}): ZapierSdk {
111
102
  listFields: (options) => listFields({ ...options, api }),
112
103
  generateTypes: (options) => generateTypes({ ...options, api }),
113
104
  bundleCode: (options) => bundleCode(options), // No API config needed
114
-
115
- // Add plugins
116
- apps: appsPlugin,
105
+ request: (options) => request({ ...options, api }),
117
106
  };
118
107
 
108
+ // Mutate baseSdk to add plugins so they can access each other
109
+ const fullSdk = baseSdk as ZapierSdk;
110
+
111
+ // Add plugins to the SDK - they now have access to the full SDK including other plugins
112
+ fullSdk.apps = createAppsPlugin({ sdk: fullSdk });
113
+ fullSdk.fetch = createFetchPlugin({ sdk: fullSdk });
114
+
119
115
  return fullSdk;
120
116
  }
@@ -36,6 +36,22 @@ export interface BaseSdkOptions {
36
36
  debug?: boolean;
37
37
  }
38
38
 
39
+ // Common options shared across all function types
40
+ export interface FunctionOptions {
41
+ /** Base URL for Zapier API */
42
+ baseUrl?: string;
43
+ /** Authentication token */
44
+ token?: string;
45
+ /** Function to dynamically resolve authentication token */
46
+ getToken?: () => Promise<string | undefined>;
47
+ /** Optional pre-instantiated API client */
48
+ api?: any;
49
+ /** Enable debug logging */
50
+ debug?: boolean;
51
+ /** Custom fetch implementation */
52
+ fetch?: typeof globalThis.fetch;
53
+ }
54
+
39
55
  // Error Classes
40
56
  export class ZapierSdkError extends Error {
41
57
  constructor(
package/src/types/sdk.ts CHANGED
@@ -11,9 +11,11 @@ import type { FindUniqueAuthenticationSdkFunction } from "../functions/findUniqu
11
11
  import type { GenerateTypesSdkFunction } from "../functions/generateTypes/schemas";
12
12
  import type { ListAppsSdkFunction } from "../functions/listApps/schemas";
13
13
  import type { BundleCodeSdkFunction } from "../functions/bundleCode/schemas";
14
+ import type { RelayRequestSdkFunction } from "../functions/request/schemas";
14
15
 
15
16
  // Plugin interfaces
16
17
  import type { AppsPluginSdkExtension } from "../plugins/apps/types";
18
+ import type { FetchPluginSdkExtension } from "../plugins/fetch/types";
17
19
 
18
20
  // Compose SDK functions from individual function interfaces
19
21
  export interface ZapierSdkFunctions
@@ -28,15 +30,28 @@ export interface ZapierSdkFunctions
28
30
  FindUniqueAuthenticationSdkFunction,
29
31
  GenerateTypesSdkFunction,
30
32
  ListAppsSdkFunction,
31
- BundleCodeSdkFunction {
33
+ BundleCodeSdkFunction,
34
+ RelayRequestSdkFunction {
32
35
  // All functions now properly typed!
33
36
  }
34
37
 
35
38
  // Compose SDK plugins from individual plugin interfaces
36
- export interface ZapierSdkPlugins extends AppsPluginSdkExtension {
39
+ export interface ZapierSdkPlugins
40
+ extends AppsPluginSdkExtension,
41
+ FetchPluginSdkExtension {
37
42
  // All plugins now properly typed!
38
43
  }
39
44
 
45
+ // Base SDK interface with core functions only (no plugins)
46
+ export interface BaseZapierSdk extends ZapierSdkFunctions {
47
+ // Special property for CLI to access schemas
48
+ __registry: Array<{
49
+ name: string;
50
+ inputSchema: any;
51
+ implementation: Function;
52
+ }>;
53
+ }
54
+
40
55
  // Main SDK interface with functions and plugins
41
56
  export interface ZapierSdk extends ZapierSdkFunctions, ZapierSdkPlugins {
42
57
  // Special property for CLI to access schemas
package/tsconfig.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "target": "ES2020",
4
- "module": "CommonJS",
4
+ "module": "ES2020",
5
5
  "declaration": true,
6
6
  "outDir": "./dist",
7
7
  "rootDir": "./src",
@@ -11,7 +11,7 @@
11
11
  "esModuleInterop": true,
12
12
  "skipLibCheck": true,
13
13
  "forceConsistentCasingInFileNames": true,
14
- "moduleResolution": "node"
14
+ "moduleResolution": "bundler"
15
15
  },
16
16
  "include": ["src/**/*"],
17
17
  "exclude": ["dist", "node_modules"]
package/tsup.config.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/index.ts"],
5
+ format: ["cjs", "esm"],
6
+ dts: true,
7
+ clean: true,
8
+ splitting: false,
9
+ sourcemap: false,
10
+ // Preserve tree shaking by maintaining individual module exports
11
+ treeshake: true,
12
+ // Output to separate directories for each format
13
+ outDir: "dist",
14
+ outExtension({ format }) {
15
+ return {
16
+ js: format === "cjs" ? ".cjs" : ".mjs",
17
+ };
18
+ },
19
+ // Make the optional CLI login package external so it doesn't cause build issues
20
+ external: ["@zapier/zapier-sdk-cli-login"],
21
+ });
@@ -1,8 +0,0 @@
1
- /**
2
- * Authentication Utilities
3
- *
4
- * This module provides utilities for handling authentication tokens,
5
- * including JWT detection and authorization header generation.
6
- */
7
- export declare function isJwt(token: string): boolean;
8
- export declare function getAuthorizationHeader(token: string): string;
package/dist/api/auth.js DELETED
@@ -1,29 +0,0 @@
1
- "use strict";
2
- /**
3
- * Authentication Utilities
4
- *
5
- * This module provides utilities for handling authentication tokens,
6
- * including JWT detection and authorization header generation.
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.isJwt = isJwt;
10
- exports.getAuthorizationHeader = getAuthorizationHeader;
11
- function isJwt(token) {
12
- // JWT tokens have exactly 3 parts separated by dots
13
- const parts = token.split(".");
14
- if (parts.length !== 3) {
15
- return false;
16
- }
17
- // Each part should be base64url encoded (no padding)
18
- // Basic validation - each part should be non-empty and contain valid base64url characters
19
- const base64UrlPattern = /^[A-Za-z0-9_-]+$/;
20
- return parts.every((part) => part.length > 0 && base64UrlPattern.test(part));
21
- }
22
- function getAuthorizationHeader(token) {
23
- // Check if token is a JWT (has 3 parts separated by dots)
24
- if (isJwt(token)) {
25
- return `JWT ${token}`;
26
- }
27
- // Default to Bearer for other token types
28
- return `Bearer ${token}`;
29
- }
@@ -1,8 +0,0 @@
1
- /**
2
- * API Client Implementation
3
- *
4
- * This module contains the core API client implementation, including
5
- * HTTP method handlers, response processing, and client factory functions.
6
- */
7
- import type { ApiClient, ApiClientOptions } from "./types";
8
- export declare function createZapierApi(options: ApiClientOptions): ApiClient;
@@ -1,119 +0,0 @@
1
- "use strict";
2
- /**
3
- * API Client Implementation
4
- *
5
- * This module contains the core API client implementation, including
6
- * HTTP method handlers, response processing, and client factory functions.
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.createZapierApi = createZapierApi;
10
- const auth_1 = require("./auth");
11
- const debug_1 = require("./debug");
12
- const polling_1 = require("./polling");
13
- const auth_2 = require("../auth");
14
- function createZapierApi(options) {
15
- const { baseUrl, token, getToken, debug = false, fetch: originalFetch = globalThis.fetch, } = options;
16
- const debugLog = (0, debug_1.createDebugLogger)(debug);
17
- const fetch = (0, debug_1.createDebugFetch)({ originalFetch, debugLog });
18
- // Helper to build full URLs
19
- function buildUrl(path, searchParams) {
20
- const url = new URL(path, baseUrl);
21
- if (searchParams) {
22
- Object.entries(searchParams).forEach(([key, value]) => {
23
- url.searchParams.set(key, value);
24
- });
25
- }
26
- return url.toString();
27
- }
28
- // Helper to build headers
29
- async function buildHeaders(options = {}) {
30
- const headers = {
31
- ...options.headers,
32
- };
33
- // Add auth header if token provided and not explicitly disabled
34
- if (options.authRequired !== false) {
35
- let resolvedToken = token;
36
- // Token resolution precedence: explicit token > getToken() > env var
37
- if (!resolvedToken && getToken) {
38
- resolvedToken = await getToken();
39
- }
40
- if (!resolvedToken) {
41
- resolvedToken = (0, auth_2.getTokenFromEnv)();
42
- }
43
- if (resolvedToken) {
44
- headers.Authorization = (0, auth_1.getAuthorizationHeader)(resolvedToken);
45
- }
46
- }
47
- return headers;
48
- }
49
- // Helper to handle responses
50
- async function handleResponse(response, customErrorHandler) {
51
- if (!response.ok) {
52
- // Check for custom error handling first
53
- if (customErrorHandler) {
54
- const customError = customErrorHandler(response);
55
- if (customError) {
56
- throw customError;
57
- }
58
- }
59
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
60
- }
61
- // Try to parse JSON, fall back to text if that fails
62
- try {
63
- return await response.json();
64
- }
65
- catch {
66
- return await response.text();
67
- }
68
- }
69
- // Helper to perform HTTP requests with JSON handling
70
- async function fetchJson(method, path, data, options = {}) {
71
- const url = buildUrl(path, options.searchParams);
72
- const headers = await buildHeaders(options);
73
- // Add Content-Type for JSON requests with body data
74
- if (data && typeof data === "object") {
75
- headers["Content-Type"] = "application/json";
76
- }
77
- const response = await fetch(url, {
78
- method,
79
- headers,
80
- body: data ? JSON.stringify(data) : undefined,
81
- });
82
- return handleResponse(response, options.customErrorHandler);
83
- }
84
- return {
85
- async get(path, options = {}) {
86
- return fetchJson("GET", path, undefined, options);
87
- },
88
- async post(path, data, options = {}) {
89
- return fetchJson("POST", path, data, options);
90
- },
91
- async put(path, data, options = {}) {
92
- return fetchJson("PUT", path, data, options);
93
- },
94
- async delete(path, options = {}) {
95
- return fetchJson("DELETE", path, undefined, options);
96
- },
97
- async poll(path, options = {}) {
98
- const url = buildUrl(path, options.searchParams);
99
- const headers = await buildHeaders(options);
100
- return (0, polling_1.pollUntilComplete)({
101
- fetch,
102
- url,
103
- headers,
104
- maxAttempts: options.maxAttempts,
105
- initialDelay: options.initialDelay,
106
- maxDelay: options.maxDelay,
107
- successStatus: options.successStatus,
108
- pendingStatus: options.pendingStatus,
109
- resultExtractor: options.resultExtractor,
110
- });
111
- },
112
- requireAuthTo(operation) {
113
- // Check if any authentication method is available
114
- if (!token && !getToken && !(0, auth_2.getTokenFromEnv)()) {
115
- throw new Error(`Authentication token is required to ${operation}. Please provide token in options or set ZAPIER_TOKEN environment variable.`);
116
- }
117
- },
118
- };
119
- }
@@ -1,12 +0,0 @@
1
- /**
2
- * Debug Logging Utilities
3
- *
4
- * This module provides debug logging capabilities for API requests and responses,
5
- * including request timing and detailed logging of HTTP interactions.
6
- */
7
- import type { DebugLogger } from "./types";
8
- export declare function createDebugLogger(enabled: boolean): DebugLogger;
9
- export declare function createDebugFetch(options: {
10
- originalFetch: typeof globalThis.fetch;
11
- debugLog: DebugLogger;
12
- }): (input: RequestInfo | URL, options?: RequestInit) => Promise<Response>;
package/dist/api/debug.js DELETED
@@ -1,50 +0,0 @@
1
- "use strict";
2
- /**
3
- * Debug Logging Utilities
4
- *
5
- * This module provides debug logging capabilities for API requests and responses,
6
- * including request timing and detailed logging of HTTP interactions.
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.createDebugLogger = createDebugLogger;
10
- exports.createDebugFetch = createDebugFetch;
11
- function createDebugLogger(enabled) {
12
- if (!enabled) {
13
- return () => { }; // No-op function when debug is disabled
14
- }
15
- return (message, data) => {
16
- console.log(`[Zapier SDK] ${message}`, data || "");
17
- };
18
- }
19
- function createDebugFetch(options) {
20
- const { originalFetch, debugLog } = options;
21
- return async (input, options) => {
22
- const startTime = Date.now();
23
- // Convert input to URL string for logging
24
- const url = typeof input === "string" ? input : input.toString();
25
- const method = options?.method || "GET";
26
- debugLog(`→ ${method} ${url}`, {
27
- headers: options?.headers,
28
- body: options?.body ? JSON.parse(options.body) : undefined,
29
- });
30
- try {
31
- const response = await originalFetch(input, options);
32
- const duration = Date.now() - startTime;
33
- debugLog(`← ${response.status} ${response.statusText} (${duration}ms)`, {
34
- url,
35
- method,
36
- status: response.status,
37
- });
38
- return response;
39
- }
40
- catch (error) {
41
- const duration = Date.now() - startTime;
42
- debugLog(`✖ Request failed (${duration}ms)`, {
43
- url,
44
- method,
45
- error: error instanceof Error ? error.message : error,
46
- });
47
- throw error;
48
- }
49
- };
50
- }
@@ -1,28 +0,0 @@
1
- /**
2
- * Zapier API Client Module
3
- *
4
- * This module provides a centralized API layer for all HTTP interactions
5
- * with Zapier's various APIs. It handles authentication, error handling,
6
- * polling, and provides consistent patterns across all services.
7
- */
8
- export type { ApiClient, ApiClientOptions, RequestOptions, PollOptions, DebugLogger, Integration, Action, Trigger, Field, Choice, ActionExecutionResult, ActionField, ActionFieldChoice, NeedsRequest, NeedsResponse, Authentication, AuthenticationsResponse, } from "./types";
9
- import type { ApiClient } from "./types";
10
- export { isJwt, getAuthorizationHeader } from "./auth";
11
- export { createDebugLogger, createDebugFetch } from "./debug";
12
- export { pollUntilComplete } from "./polling";
13
- export { createZapierApi } from "./client";
14
- export declare function generateRequestId(): string;
15
- /**
16
- * Utility function to get or create an API client for standalone functions
17
- *
18
- * @param config - Configuration that may include an existing API client
19
- * @returns ApiClient instance
20
- */
21
- export declare function getOrCreateApiClient(config: {
22
- baseUrl?: string;
23
- token?: string;
24
- getToken?: () => Promise<string | undefined>;
25
- api?: ApiClient;
26
- debug?: boolean;
27
- fetch?: typeof globalThis.fetch;
28
- }): ApiClient;
package/dist/api/index.js DELETED
@@ -1,52 +0,0 @@
1
- "use strict";
2
- /**
3
- * Zapier API Client Module
4
- *
5
- * This module provides a centralized API layer for all HTTP interactions
6
- * with Zapier's various APIs. It handles authentication, error handling,
7
- * polling, and provides consistent patterns across all services.
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.createZapierApi = exports.pollUntilComplete = exports.createDebugFetch = exports.createDebugLogger = exports.getAuthorizationHeader = exports.isJwt = void 0;
11
- exports.generateRequestId = generateRequestId;
12
- exports.getOrCreateApiClient = getOrCreateApiClient;
13
- // Re-export authentication utilities
14
- var auth_1 = require("./auth");
15
- Object.defineProperty(exports, "isJwt", { enumerable: true, get: function () { return auth_1.isJwt; } });
16
- Object.defineProperty(exports, "getAuthorizationHeader", { enumerable: true, get: function () { return auth_1.getAuthorizationHeader; } });
17
- // Re-export debug utilities
18
- var debug_1 = require("./debug");
19
- Object.defineProperty(exports, "createDebugLogger", { enumerable: true, get: function () { return debug_1.createDebugLogger; } });
20
- Object.defineProperty(exports, "createDebugFetch", { enumerable: true, get: function () { return debug_1.createDebugFetch; } });
21
- // Re-export polling utilities
22
- var polling_1 = require("./polling");
23
- Object.defineProperty(exports, "pollUntilComplete", { enumerable: true, get: function () { return polling_1.pollUntilComplete; } });
24
- // Re-export the main client factory
25
- var client_1 = require("./client");
26
- Object.defineProperty(exports, "createZapierApi", { enumerable: true, get: function () { return client_1.createZapierApi; } });
27
- // Import for local use
28
- const client_2 = require("./client");
29
- // Utility Functions
30
- function generateRequestId() {
31
- return Math.random().toString(36).substring(2) + Date.now().toString(36);
32
- }
33
- /**
34
- * Utility function to get or create an API client for standalone functions
35
- *
36
- * @param config - Configuration that may include an existing API client
37
- * @returns ApiClient instance
38
- */
39
- function getOrCreateApiClient(config) {
40
- const { baseUrl = "https://zapier.com", token, getToken, api: providedApi, debug = false, fetch: customFetch, } = config;
41
- // Use provided API client or create a new one
42
- if (providedApi) {
43
- return providedApi;
44
- }
45
- return (0, client_2.createZapierApi)({
46
- baseUrl,
47
- token,
48
- getToken,
49
- debug,
50
- fetch: customFetch,
51
- });
52
- }