@zapier/zapier-sdk 0.0.2 → 0.0.3

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 (41) hide show
  1. package/dist/actions-sdk.d.ts +0 -4
  2. package/dist/actions-sdk.js +43 -1029
  3. package/dist/api.d.ts +62 -0
  4. package/dist/api.js +227 -0
  5. package/dist/functions/bundleCode.d.ts +18 -0
  6. package/dist/functions/bundleCode.js +91 -0
  7. package/dist/functions/generateTypes.d.ts +16 -0
  8. package/dist/functions/generateTypes.js +271 -0
  9. package/dist/functions/getAction.d.ts +16 -0
  10. package/dist/functions/getAction.js +25 -0
  11. package/dist/functions/getApp.d.ts +14 -0
  12. package/dist/functions/getApp.js +41 -0
  13. package/dist/functions/listActions.d.ts +15 -0
  14. package/dist/functions/listActions.js +127 -0
  15. package/dist/functions/listApps.d.ts +16 -0
  16. package/dist/functions/listApps.js +50 -0
  17. package/dist/functions/listAuths.d.ts +18 -0
  18. package/dist/functions/listAuths.js +118 -0
  19. package/dist/functions/listFields.d.ts +18 -0
  20. package/dist/functions/listFields.js +67 -0
  21. package/dist/functions/runAction.d.ts +18 -0
  22. package/dist/functions/runAction.js +156 -0
  23. package/dist/index.d.ts +9 -0
  24. package/dist/index.js +20 -1
  25. package/dist/output-schemas.d.ts +4 -4
  26. package/dist/schemas.d.ts +24 -24
  27. package/dist/types.d.ts +12 -0
  28. package/package.json +1 -1
  29. package/src/actions-sdk.ts +47 -1376
  30. package/src/api.ts +361 -0
  31. package/src/functions/bundleCode.ts +85 -0
  32. package/src/functions/generateTypes.ts +309 -0
  33. package/src/functions/getAction.ts +34 -0
  34. package/src/functions/getApp.ts +47 -0
  35. package/src/functions/listActions.ts +151 -0
  36. package/src/functions/listApps.ts +65 -0
  37. package/src/functions/listAuths.ts +161 -0
  38. package/src/functions/listFields.ts +95 -0
  39. package/src/functions/runAction.ts +256 -0
  40. package/src/index.ts +11 -0
  41. package/src/types.ts +13 -0
@@ -0,0 +1,16 @@
1
+ import type { Action, FunctionConfig } from "../types";
2
+ export interface GetActionOptions extends FunctionConfig {
3
+ app: string;
4
+ action: string;
5
+ type: string;
6
+ }
7
+ /**
8
+ * Get a specific action by app, action key, and type
9
+ *
10
+ * This function can be used standalone without instantiating a full SDK,
11
+ * which enables better tree-shaking in applications that only need this functionality.
12
+ *
13
+ * @param options - App key, action key, type, and API configuration options
14
+ * @returns Promise<Action>
15
+ */
16
+ export declare function getAction(options: GetActionOptions): Promise<Action>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAction = getAction;
4
+ const listActions_1 = require("./listActions");
5
+ /**
6
+ * Get a specific action by app, action key, and type
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 - App key, action key, type, and API configuration options
12
+ * @returns Promise<Action>
13
+ */
14
+ async function getAction(options) {
15
+ const { app, action: actionKey, type } = options;
16
+ const actions = await (0, listActions_1.listActions)({
17
+ ...options,
18
+ appKey: app,
19
+ });
20
+ const action = actions.find((a) => a.key === actionKey && a.type === type);
21
+ if (!action) {
22
+ throw new Error(`Action not found: ${actionKey} with type ${type}`);
23
+ }
24
+ return action;
25
+ }
@@ -0,0 +1,14 @@
1
+ import type { Integration, FunctionConfig } from "../types";
2
+ export interface GetAppOptions extends FunctionConfig {
3
+ key: string;
4
+ }
5
+ /**
6
+ * Get a specific app by key
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 - App key and API configuration options
12
+ * @returns Promise<Integration>
13
+ */
14
+ export declare function getApp(options: GetAppOptions): Promise<Integration>;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getApp = getApp;
4
+ const api_1 = require("../api");
5
+ /**
6
+ * Get a specific app by key
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 - App key and API configuration options
12
+ * @returns Promise<Integration>
13
+ */
14
+ async function getApp(options) {
15
+ const api = (0, api_1.getOrCreateApiClient)(options);
16
+ const { key } = options;
17
+ const app = await api.get(`/api/v4/apps/${key}/`, {
18
+ customErrorHandler: (response) => {
19
+ if (response.status === 404) {
20
+ const AppNotFoundError = class extends Error {
21
+ constructor(appKey) {
22
+ super(`App not found: ${appKey}`);
23
+ this.name = "AppNotFoundError";
24
+ }
25
+ };
26
+ return new AppNotFoundError(key);
27
+ }
28
+ return undefined;
29
+ },
30
+ });
31
+ return {
32
+ key: app.slug,
33
+ name: app.name,
34
+ description: app.description,
35
+ version: "1.0.0",
36
+ category: app.category?.name,
37
+ actions: [],
38
+ triggers: [],
39
+ current_implementation_id: app.current_implementation_id,
40
+ };
41
+ }
@@ -0,0 +1,15 @@
1
+ import type { Action, FunctionConfig } from "../types";
2
+ export interface ListActionsOptions extends FunctionConfig {
3
+ appKey?: string;
4
+ type?: string;
5
+ }
6
+ /**
7
+ * List available actions with optional filtering
8
+ *
9
+ * This function can be used standalone without instantiating a full SDK,
10
+ * which enables better tree-shaking in applications that only need this functionality.
11
+ *
12
+ * @param options - Filtering and API configuration options
13
+ * @returns Promise<Action[]> with pagination metadata
14
+ */
15
+ export declare function listActions(options?: ListActionsOptions): Promise<Action[]>;
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.listActions = listActions;
4
+ const api_1 = require("../api");
5
+ const getApp_1 = require("./getApp");
6
+ /**
7
+ * List available actions with optional filtering
8
+ *
9
+ * This function can be used standalone without instantiating a full SDK,
10
+ * which enables better tree-shaking in applications that only need this functionality.
11
+ *
12
+ * @param options - Filtering and API configuration options
13
+ * @returns Promise<Action[]> with pagination metadata
14
+ */
15
+ async function listActions(options = {}) {
16
+ const api = (0, api_1.getOrCreateApiClient)(options);
17
+ // If filtering by appKey, use optimized approach with selected_apis parameter
18
+ if (options.appKey) {
19
+ try {
20
+ // Use the standalone getApp function
21
+ const appData = await (0, getApp_1.getApp)({
22
+ key: options.appKey,
23
+ api,
24
+ token: options.token,
25
+ baseUrl: options.baseUrl,
26
+ debug: options.debug,
27
+ fetch: options.fetch,
28
+ });
29
+ // Extract implementation name from current_implementation_id (e.g., "SlackCLIAPI@1.20.0" -> "SlackCLIAPI")
30
+ const implementationId = appData.current_implementation_id?.split("@")[0];
31
+ if (!implementationId) {
32
+ throw new Error("No current_implementation_id found for app");
33
+ }
34
+ const searchParams = {
35
+ global: "true",
36
+ public_only: "true",
37
+ selected_apis: implementationId,
38
+ };
39
+ const data = await api.get("/api/v4/implementations/", {
40
+ searchParams,
41
+ });
42
+ const actions = [];
43
+ // Transform implementations to actions
44
+ for (const implementation of data.results || []) {
45
+ if (implementation.actions) {
46
+ for (const action of implementation.actions) {
47
+ const transformedAction = {
48
+ key: action.key,
49
+ name: action.name || action.label,
50
+ description: action.description || "",
51
+ appKey: implementation.slug || "",
52
+ type: action.type || action.type_of || "read",
53
+ inputFields: [], // Would need additional API call for detailed fields
54
+ outputFields: [],
55
+ };
56
+ // Apply type filter if provided
57
+ if (options?.type && transformedAction.type !== options.type) {
58
+ continue;
59
+ }
60
+ actions.push(transformedAction);
61
+ }
62
+ }
63
+ }
64
+ // Add pagination metadata for app-specific queries
65
+ if (actions.length > 0) {
66
+ actions.__pagination = {
67
+ count: data.count,
68
+ hasNext: !!data.next,
69
+ hasPrevious: !!data.previous,
70
+ nextUrl: data.next,
71
+ previousUrl: data.previous,
72
+ };
73
+ }
74
+ return actions;
75
+ }
76
+ catch (error) {
77
+ // If it's an AppNotFoundError, don't try fallback - just re-throw
78
+ if (error instanceof Error && error.name === "AppNotFoundError") {
79
+ throw error;
80
+ }
81
+ // Fallback to original approach if optimized approach fails
82
+ console.warn("Optimized app lookup failed, falling back to full scan:", error);
83
+ }
84
+ }
85
+ // Original approach for general queries or when optimization fails
86
+ const searchParams = {
87
+ global: "true",
88
+ public_only: "true",
89
+ };
90
+ const data = await api.get("/api/v4/implementations/", { searchParams });
91
+ const actions = [];
92
+ // Transform implementations to actions
93
+ for (const implementation of data.results || []) {
94
+ if (implementation.actions) {
95
+ for (const action of implementation.actions) {
96
+ const transformedAction = {
97
+ key: action.key,
98
+ name: action.name || action.label,
99
+ description: action.description || "",
100
+ appKey: implementation.slug || "",
101
+ type: action.type || action.type_of || "read",
102
+ inputFields: [], // Would need additional API call for detailed fields
103
+ outputFields: [],
104
+ };
105
+ // Apply filters
106
+ if (options?.appKey && transformedAction.appKey !== options.appKey) {
107
+ continue;
108
+ }
109
+ if (options?.type && transformedAction.type !== options.type) {
110
+ continue;
111
+ }
112
+ actions.push(transformedAction);
113
+ }
114
+ }
115
+ }
116
+ // Add pagination metadata for general queries
117
+ if (actions.length > 0) {
118
+ actions.__pagination = {
119
+ count: data.count,
120
+ hasNext: !!data.next,
121
+ hasPrevious: !!data.previous,
122
+ nextUrl: data.next,
123
+ previousUrl: data.previous,
124
+ };
125
+ }
126
+ return actions;
127
+ }
@@ -0,0 +1,16 @@
1
+ import type { Integration, FunctionConfig } from "../types";
2
+ export interface ListAppsOptions extends FunctionConfig {
3
+ category?: string;
4
+ limit?: number;
5
+ offset?: number;
6
+ }
7
+ /**
8
+ * List available apps with optional filtering
9
+ *
10
+ * This function can be used standalone without instantiating a full SDK,
11
+ * which enables better tree-shaking in applications that only need this functionality.
12
+ *
13
+ * @param options - Filtering, pagination, and API configuration options
14
+ * @returns Promise<Integration[]> with pagination metadata
15
+ */
16
+ export declare function listApps(options?: ListAppsOptions): Promise<Integration[]>;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.listApps = listApps;
4
+ const api_1 = require("../api");
5
+ /**
6
+ * List available apps with optional filtering
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 - Filtering, pagination, and API configuration options
12
+ * @returns Promise<Integration[]> with pagination metadata
13
+ */
14
+ async function listApps(options = {}) {
15
+ const api = (0, api_1.getOrCreateApiClient)(options);
16
+ // Build search parameters
17
+ const searchParams = {};
18
+ if (options.category) {
19
+ searchParams.category = options.category;
20
+ }
21
+ if (options.limit) {
22
+ searchParams.limit = options.limit.toString();
23
+ }
24
+ if (options.offset) {
25
+ searchParams.offset = options.offset.toString();
26
+ }
27
+ const data = await api.get("/api/v4/apps/", { searchParams });
28
+ // Transform API response to our Integration interface
29
+ const apps = data.results?.map((app) => ({
30
+ key: app.slug,
31
+ name: app.name,
32
+ description: app.description,
33
+ version: "1.0.0", // API doesn't provide version
34
+ category: app.category?.name,
35
+ actions: [], // Will be populated separately
36
+ triggers: [],
37
+ current_implementation_id: app.current_implementation_id,
38
+ })) || [];
39
+ // Add pagination metadata to the response
40
+ if (apps.length > 0) {
41
+ apps.__pagination = {
42
+ count: data.count,
43
+ hasNext: !!data.next,
44
+ hasPrevious: !!data.previous,
45
+ nextUrl: data.next,
46
+ previousUrl: data.previous,
47
+ };
48
+ }
49
+ return apps;
50
+ }
@@ -0,0 +1,18 @@
1
+ import type { Authentication, FunctionConfig } from "../types";
2
+ export interface ListAuthsOptions extends FunctionConfig {
3
+ appKey?: string;
4
+ account_id?: string;
5
+ owner?: string;
6
+ limit?: number;
7
+ offset?: number;
8
+ }
9
+ /**
10
+ * List available authentications with optional filtering
11
+ *
12
+ * This function can be used standalone without instantiating a full SDK,
13
+ * which enables better tree-shaking in applications that only need this functionality.
14
+ *
15
+ * @param options - Filtering, pagination, and API configuration options
16
+ * @returns Promise<Authentication[]> with pagination metadata
17
+ */
18
+ export declare function listAuths(options?: ListAuthsOptions): Promise<Authentication[]>;
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.listAuths = listAuths;
4
+ const api_1 = require("../api");
5
+ const getApp_1 = require("./getApp");
6
+ /**
7
+ * List available authentications with optional filtering
8
+ *
9
+ * This function can be used standalone without instantiating a full SDK,
10
+ * which enables better tree-shaking in applications that only need this functionality.
11
+ *
12
+ * @param options - Filtering, pagination, and API configuration options
13
+ * @returns Promise<Authentication[]> with pagination metadata
14
+ */
15
+ async function listAuths(options = {}) {
16
+ const { token } = options;
17
+ if (!token && !process.env.ZAPIER_TOKEN) {
18
+ throw new Error("Authentication token is required to list authentications. Please provide token in options or set ZAPIER_TOKEN environment variable.");
19
+ }
20
+ const api = (0, api_1.getOrCreateApiClient)(options);
21
+ // Local function to handle the actual API fetching
22
+ const listAuthsInternal = async (options = {}) => {
23
+ // Build search parameters
24
+ const searchParams = {};
25
+ // Handle appKey filtering by getting the selected_api first
26
+ if (options.appKey) {
27
+ try {
28
+ // Use the standalone getApp function
29
+ const app = await (0, getApp_1.getApp)({
30
+ key: options.appKey,
31
+ api,
32
+ token: options.token,
33
+ baseUrl: options.baseUrl,
34
+ debug: options.debug,
35
+ fetch: options.fetch,
36
+ });
37
+ const selectedApi = app.current_implementation_id;
38
+ if (selectedApi) {
39
+ // Use versionless_selected_api to find auths across all app versions
40
+ // Extract the base name without the version (e.g., "SlackCLIAPI" from "SlackCLIAPI@1.21.1")
41
+ const versionlessApi = selectedApi.split("@")[0];
42
+ searchParams.versionless_selected_api = versionlessApi;
43
+ }
44
+ }
45
+ catch (error) {
46
+ // If it's an AppNotFoundError, re-throw it
47
+ if (error instanceof Error && error.name === "AppNotFoundError") {
48
+ throw error;
49
+ }
50
+ // For other errors, continue without app filtering
51
+ console.warn(`Warning: Could not filter by app ${options.appKey}:`, error instanceof Error ? error.message : "Unknown error");
52
+ }
53
+ }
54
+ // Add other query parameters if provided
55
+ if (options.account_id) {
56
+ searchParams.account_id = options.account_id;
57
+ }
58
+ if (options.owner) {
59
+ searchParams.owner = options.owner;
60
+ }
61
+ if (options.limit) {
62
+ searchParams.limit = options.limit.toString();
63
+ }
64
+ if (options.offset) {
65
+ searchParams.offset = options.offset.toString();
66
+ }
67
+ const data = await api.get("/api/v4/authentications/", {
68
+ searchParams,
69
+ customErrorHandler: (response) => {
70
+ if (response.status === 401) {
71
+ return new Error(`Authentication failed. Your token may not have permission to access authentications or may be expired. (HTTP ${response.status})`);
72
+ }
73
+ if (response.status === 403) {
74
+ return new Error(`Access forbidden. Your token may not have the required scopes to list authentications. (HTTP ${response.status})`);
75
+ }
76
+ return undefined;
77
+ },
78
+ });
79
+ // Transform API response
80
+ const auths = data.results || [];
81
+ // Add pagination metadata to the response
82
+ if (auths.length > 0) {
83
+ auths.__pagination = {
84
+ count: data.count,
85
+ hasNext: !!data.next,
86
+ hasPrevious: !!data.previous,
87
+ nextUrl: data.next,
88
+ previousUrl: data.previous,
89
+ };
90
+ }
91
+ return auths;
92
+ };
93
+ // If a limit is provided and no specific owner filter, prioritize owned auths
94
+ if (options.limit && options.owner === undefined) {
95
+ // First get owned auths
96
+ const ownedAuths = await listAuthsInternal({
97
+ ...options,
98
+ owner: "me",
99
+ });
100
+ // If we have enough owned auths, just slice and return
101
+ if (ownedAuths.length >= options.limit) {
102
+ return ownedAuths.slice(0, options.limit);
103
+ }
104
+ // Get all auths up to the original limit to fill remaining slots
105
+ const allAuths = await listAuthsInternal({
106
+ ...options,
107
+ owner: undefined,
108
+ });
109
+ // Filter out auths the user already owns to avoid duplicates
110
+ const ownedAuthIds = new Set(ownedAuths.map((auth) => auth.id));
111
+ const additionalAuths = allAuths.filter((auth) => !ownedAuthIds.has(auth.id));
112
+ // Combine and slice to the requested limit
113
+ const combined = [...ownedAuths, ...additionalAuths];
114
+ return combined.slice(0, options.limit);
115
+ }
116
+ // Standard implementation for non-prioritized requests
117
+ return listAuthsInternal(options);
118
+ }
@@ -0,0 +1,18 @@
1
+ import type { ActionField, FunctionConfig } from "../types";
2
+ export interface ListFieldsOptions extends FunctionConfig {
3
+ app: string;
4
+ action: string;
5
+ type: string;
6
+ authId?: number;
7
+ params?: Record<string, any>;
8
+ }
9
+ /**
10
+ * List available fields for an action
11
+ *
12
+ * This function can be used standalone without instantiating a full SDK,
13
+ * which enables better tree-shaking in applications that only need this functionality.
14
+ *
15
+ * @param options - Action details, auth ID, params, and API configuration options
16
+ * @returns Promise<ActionField[]>
17
+ */
18
+ export declare function listFields(options: ListFieldsOptions): Promise<ActionField[]>;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.listFields = listFields;
4
+ const api_1 = require("../api");
5
+ const getApp_1 = require("./getApp");
6
+ /**
7
+ * List available fields for an action
8
+ *
9
+ * This function can be used standalone without instantiating a full SDK,
10
+ * which enables better tree-shaking in applications that only need this functionality.
11
+ *
12
+ * @param options - Action details, auth ID, params, and API configuration options
13
+ * @returns Promise<ActionField[]>
14
+ */
15
+ async function listFields(options) {
16
+ const api = (0, api_1.getOrCreateApiClient)(options);
17
+ // Map consistent parameter names to internal API names
18
+ const { app, action, type, authId, params } = options;
19
+ const appKey = app;
20
+ const actionKey = action;
21
+ const actionType = type;
22
+ // Use the standalone getApp function
23
+ const appData = await (0, getApp_1.getApp)({
24
+ key: appKey,
25
+ api,
26
+ token: options.token,
27
+ baseUrl: options.baseUrl,
28
+ debug: options.debug,
29
+ fetch: options.fetch,
30
+ });
31
+ const selectedApi = appData.current_implementation_id;
32
+ if (!selectedApi) {
33
+ throw new Error("No current_implementation_id found for app");
34
+ }
35
+ // Build needs request
36
+ const needsRequest = {
37
+ selected_api: selectedApi,
38
+ action: actionKey,
39
+ type_of: actionType,
40
+ authentication_id: authId,
41
+ params: params || {},
42
+ };
43
+ const needsData = await api.post("/api/v4/implementations/needs/", needsRequest);
44
+ if (!needsData.success) {
45
+ throw new Error(`Failed to get action fields: ${needsData.errors?.join(", ") || "Unknown error"}`);
46
+ }
47
+ // Transform API response to our ActionField interface
48
+ return (needsData.needs || []).map((need) => ({
49
+ key: need.key,
50
+ label: need.label,
51
+ required: need.required || false,
52
+ type: need.type,
53
+ helpText: need.help_text,
54
+ helpTextHtml: need.help_text_html,
55
+ choices: need.choices?.map((choice) => ({
56
+ value: choice.value,
57
+ label: choice.label,
58
+ })),
59
+ default: need.default,
60
+ placeholder: need.placeholder,
61
+ computed: need.computed,
62
+ customField: need.custom_field,
63
+ dependsOn: need.depends_on,
64
+ format: need.format,
65
+ inputFormat: need.input_format,
66
+ }));
67
+ }
@@ -0,0 +1,18 @@
1
+ import type { ActionExecutionResult, FunctionConfig } from "../types";
2
+ export interface RunActionOptions extends FunctionConfig {
3
+ app: string;
4
+ type: string;
5
+ action: string;
6
+ inputs?: Record<string, any>;
7
+ authId?: number;
8
+ }
9
+ /**
10
+ * Execute an action
11
+ *
12
+ * This function can be used standalone without instantiating a full SDK,
13
+ * which enables better tree-shaking in applications that only need this functionality.
14
+ *
15
+ * @param options - Action execution parameters and API configuration options
16
+ * @returns Promise<ActionExecutionResult>
17
+ */
18
+ export declare function runAction(options: RunActionOptions): Promise<ActionExecutionResult>;