@zapier/zapier-sdk 0.6.4 → 0.8.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 (59) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +55 -23
  3. package/dist/api/schemas.d.ts +114 -0
  4. package/dist/api/schemas.d.ts.map +1 -1
  5. package/dist/api/schemas.js +45 -0
  6. package/dist/api/types.d.ts +8 -3
  7. package/dist/api/types.d.ts.map +1 -1
  8. package/dist/index.cjs +173 -9
  9. package/dist/index.d.mts +516 -442
  10. package/dist/index.mjs +173 -9
  11. package/dist/plugins/findFirstAuthentication/index.test.js +3 -3
  12. package/dist/plugins/findFirstAuthentication/schemas.d.ts +3 -3
  13. package/dist/plugins/findFirstAuthentication/schemas.js +1 -1
  14. package/dist/plugins/findUniqueAuthentication/index.test.js +3 -3
  15. package/dist/plugins/findUniqueAuthentication/schemas.d.ts +3 -3
  16. package/dist/plugins/findUniqueAuthentication/schemas.js +1 -1
  17. package/dist/plugins/listAuthentications/index.d.ts.map +1 -1
  18. package/dist/plugins/listAuthentications/index.js +2 -3
  19. package/dist/plugins/listAuthentications/index.test.js +5 -5
  20. package/dist/plugins/listAuthentications/schemas.d.ts +3 -3
  21. package/dist/plugins/listAuthentications/schemas.js +1 -1
  22. package/dist/plugins/listInputFieldChoices/index.d.ts +28 -0
  23. package/dist/plugins/listInputFieldChoices/index.d.ts.map +1 -0
  24. package/dist/plugins/listInputFieldChoices/index.js +78 -0
  25. package/dist/plugins/listInputFieldChoices/index.test.d.ts +2 -0
  26. package/dist/plugins/listInputFieldChoices/index.test.d.ts.map +1 -0
  27. package/dist/plugins/listInputFieldChoices/index.test.js +537 -0
  28. package/dist/plugins/listInputFieldChoices/schemas.d.ts +61 -0
  29. package/dist/plugins/listInputFieldChoices/schemas.d.ts.map +1 -0
  30. package/dist/plugins/listInputFieldChoices/schemas.js +73 -0
  31. package/dist/plugins/listInputFields/schemas.d.ts +3 -3
  32. package/dist/plugins/runAction/schemas.d.ts +5 -5
  33. package/dist/plugins/runAction/schemas.d.ts.map +1 -1
  34. package/dist/sdk.d.ts +7 -1
  35. package/dist/sdk.d.ts.map +1 -1
  36. package/dist/sdk.js +2 -0
  37. package/dist/types/functions.d.ts +1 -1
  38. package/dist/types/functions.d.ts.map +1 -1
  39. package/dist/types/properties.d.ts +2 -2
  40. package/dist/types/properties.d.ts.map +1 -1
  41. package/dist/types/properties.js +2 -2
  42. package/package.json +1 -1
  43. package/src/api/schemas.ts +62 -0
  44. package/src/api/types.ts +17 -2
  45. package/src/plugins/findFirstAuthentication/index.test.ts +3 -3
  46. package/src/plugins/findFirstAuthentication/schemas.ts +1 -1
  47. package/src/plugins/findUniqueAuthentication/index.test.ts +3 -3
  48. package/src/plugins/findUniqueAuthentication/schemas.ts +1 -1
  49. package/src/plugins/listAuthentications/index.test.ts +5 -5
  50. package/src/plugins/listAuthentications/index.ts +2 -3
  51. package/src/plugins/listAuthentications/schemas.ts +1 -1
  52. package/src/plugins/listInputFieldChoices/index.test.ts +653 -0
  53. package/src/plugins/listInputFieldChoices/index.ts +152 -0
  54. package/src/plugins/listInputFieldChoices/schemas.ts +139 -0
  55. package/src/plugins/runAction/schemas.ts +2 -2
  56. package/src/sdk.ts +2 -0
  57. package/src/types/functions.ts +1 -1
  58. package/src/types/properties.ts +2 -2
  59. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,152 @@
1
+ import type { Plugin, GetSdkType } from "../../types/plugin";
2
+ import type { ApiClient } from "../../api";
3
+ import type {
4
+ NeedChoicesRequest,
5
+ NeedChoicesResponse,
6
+ NeedChoices,
7
+ } from "../../api/types";
8
+ import type { InputFieldChoiceItem } from "./schemas";
9
+ import {
10
+ ListInputFieldChoicesSchema,
11
+ type ListInputFieldChoicesOptions,
12
+ type ListInputFieldChoicesPage,
13
+ } from "./schemas";
14
+ import { ZapierApiError } from "../../types/errors";
15
+ import { createPaginatedFunction } from "../../utils/function-utils";
16
+ import type { GetActionPluginProvides } from "../getAction";
17
+
18
+ // Transform NeedChoices to InputFieldChoiceItem
19
+ function transformNeedChoicesToInputFieldChoiceItem(
20
+ choice: NeedChoices,
21
+ ): InputFieldChoiceItem {
22
+ return {
23
+ key: choice.key,
24
+ label: choice.label,
25
+ sample: choice.sample,
26
+ value: choice.value,
27
+ };
28
+ }
29
+
30
+ export interface ListInputFieldChoicesPluginProvides {
31
+ listInputFieldChoices: (options: ListInputFieldChoicesOptions) => Promise<{
32
+ data: InputFieldChoiceItem[];
33
+ }> &
34
+ AsyncIterable<{ data: InputFieldChoiceItem[]; nextCursor?: string }> & {
35
+ items(): AsyncIterable<InputFieldChoiceItem>;
36
+ };
37
+ context: {
38
+ meta: {
39
+ listInputFieldChoices: {
40
+ inputSchema: typeof ListInputFieldChoicesSchema;
41
+ };
42
+ };
43
+ };
44
+ }
45
+
46
+ export const listInputFieldChoicesPlugin: Plugin<
47
+ GetSdkType<GetActionPluginProvides>, // requires getAction in SDK
48
+ { api: ApiClient }, // requires api in context
49
+ ListInputFieldChoicesPluginProvides
50
+ > = ({ context, sdk }) => {
51
+ const listInputFieldChoices = createPaginatedFunction<
52
+ ListInputFieldChoicesOptions & { cursor?: string },
53
+ ListInputFieldChoicesPage,
54
+ ListInputFieldChoicesOptions
55
+ >(async function listInputFieldChoicesPage(
56
+ options: ListInputFieldChoicesOptions & { cursor?: string } & {
57
+ pageSize: number;
58
+ },
59
+ ): Promise<ListInputFieldChoicesPage> {
60
+ const { api } = context;
61
+
62
+ // Extract parameters
63
+ const {
64
+ appKey,
65
+ actionType,
66
+ actionKey,
67
+ inputFieldKey,
68
+ authenticationId,
69
+ inputs,
70
+ page,
71
+ cursor,
72
+ } = options;
73
+
74
+ // Use sdk.getAction to get the action ID
75
+ const actionResult = await sdk.getAction({ appKey, actionType, actionKey });
76
+ const actionId = actionResult.data.id;
77
+
78
+ if (!actionId) {
79
+ throw new ZapierApiError(
80
+ `Action ${actionKey} does not have an ID - cannot retrieve input field choices`,
81
+ );
82
+ }
83
+
84
+ // Build choices request using action ID from getAction
85
+ // Use cursor (from pagination) as page number if available, otherwise use explicit page or default to 0
86
+ const requestPage = cursor ? parseInt(cursor, 10) : (page ?? 0);
87
+ const choicesRequest: NeedChoicesRequest = {
88
+ action_id: actionId,
89
+ input_field_id: inputFieldKey,
90
+ page: requestPage,
91
+ params: inputs || {},
92
+ };
93
+
94
+ // Only include authentication_id if it's not null (skip authentication when null)
95
+ if (authenticationId !== null) {
96
+ choicesRequest.authentication_id = authenticationId;
97
+ }
98
+
99
+ const choicesData: NeedChoicesResponse = await api.post(
100
+ "/api/v4/implementations/choices/",
101
+ choicesRequest,
102
+ );
103
+
104
+ if (!choicesData.success) {
105
+ throw new ZapierApiError(
106
+ `Failed to get input field choices: ${
107
+ choicesData.errors?.join(", ") || "Unknown error"
108
+ }`,
109
+ );
110
+ }
111
+
112
+ // Transform NeedChoices objects to InputFieldChoiceItem objects
113
+ const choices: InputFieldChoiceItem[] = (choicesData.choices || []).map(
114
+ transformNeedChoicesToInputFieldChoiceItem,
115
+ );
116
+
117
+ // Handle pagination
118
+ let nextCursor: string | undefined;
119
+ if (choicesData.next_page !== undefined) {
120
+ nextCursor = choicesData.next_page.toString();
121
+ } else if (choicesData.links?.next) {
122
+ // Extract page from next URL for external actions
123
+ try {
124
+ const nextUrl = new URL(choicesData.links.next);
125
+ const nextPage = nextUrl.searchParams.get("page");
126
+ if (nextPage) {
127
+ nextCursor = nextPage;
128
+ }
129
+ } catch {
130
+ // Handle malformed URLs gracefully by not setting nextCursor
131
+ nextCursor = undefined;
132
+ }
133
+ }
134
+
135
+ return {
136
+ data: choices,
137
+ nextCursor,
138
+ };
139
+ }, ListInputFieldChoicesSchema);
140
+
141
+ return {
142
+ listInputFieldChoices,
143
+ context: {
144
+ meta: {
145
+ listInputFieldChoices: {
146
+ categories: ["action"],
147
+ inputSchema: ListInputFieldChoicesSchema,
148
+ },
149
+ },
150
+ },
151
+ };
152
+ };
@@ -0,0 +1,139 @@
1
+ import { z } from "zod";
2
+ import {
3
+ AuthenticationIdPropertySchema,
4
+ AppKeyPropertySchema,
5
+ ActionTypePropertySchema,
6
+ ActionKeyPropertySchema,
7
+ InputsPropertySchema,
8
+ } from "../../types/properties";
9
+ import { withFormatter, withOutputSchema } from "../../utils/schema-utils";
10
+ import type { PaginatedSdkFunction } from "../../types/functions";
11
+ import type {
12
+ ZapierConfigurationError,
13
+ ZapierApiError,
14
+ ZapierAuthenticationError,
15
+ ZapierAppNotFoundError,
16
+ ZapierValidationError,
17
+ ZapierUnknownError,
18
+ } from "../../types/errors";
19
+
20
+ // ============================================================================
21
+ // Input Field Choice Item Schema
22
+ // ============================================================================
23
+
24
+ export const InputFieldChoiceItemSchema = withFormatter(
25
+ z.object({
26
+ key: z.string().optional().describe("Unique key/value for the choice"),
27
+ label: z
28
+ .string()
29
+ .optional()
30
+ .describe("Human readable label for the choice"),
31
+ sample: z.string().optional().describe("Sample value for the choice"),
32
+ value: z
33
+ .string()
34
+ .optional()
35
+ .describe("Value to be submitted when selected"),
36
+ }),
37
+ {
38
+ format: (item) => {
39
+ const title = item.label || item.key || "Choice";
40
+ const subtitle =
41
+ item.label && item.key && item.label !== item.key
42
+ ? `(${item.key})`
43
+ : undefined;
44
+ const details: Array<{
45
+ text: string;
46
+ style: "success" | "dim" | "normal" | "accent" | "warning";
47
+ }> = [];
48
+
49
+ if (item.sample && item.sample !== item.key) {
50
+ details.push({ text: `Sample: ${item.sample}`, style: "dim" });
51
+ }
52
+ if (item.value && item.value !== item.key) {
53
+ details.push({ text: `Value: ${item.value}`, style: "normal" });
54
+ }
55
+
56
+ return {
57
+ title,
58
+ subtitle,
59
+ details,
60
+ };
61
+ },
62
+ },
63
+ );
64
+
65
+ export type InputFieldChoiceItem = z.infer<typeof InputFieldChoiceItemSchema>;
66
+
67
+ // ============================================================================
68
+ // Plugin Schema Definition
69
+ // ============================================================================
70
+
71
+ export const ListInputFieldChoicesSchema = withOutputSchema(
72
+ z
73
+ .object({
74
+ // Required action identification
75
+ appKey: AppKeyPropertySchema,
76
+ actionType: ActionTypePropertySchema,
77
+ actionKey: ActionKeyPropertySchema,
78
+
79
+ // Input field specification
80
+ inputFieldKey: z
81
+ .string()
82
+ .min(1)
83
+ .describe("Input field key to get choices for."),
84
+
85
+ // Common parameters
86
+ authenticationId: AuthenticationIdPropertySchema.nullable().optional(),
87
+ inputs: InputsPropertySchema.optional().describe(
88
+ "Current input values that may affect available choices",
89
+ ),
90
+ page: z
91
+ .number()
92
+ .int()
93
+ .min(0)
94
+ .optional()
95
+ .describe("Page number for paginated results"),
96
+
97
+ // Pagination options (SDK-level)
98
+ pageSize: z
99
+ .number()
100
+ .min(1)
101
+ .optional()
102
+ .describe("Number of choices per page"),
103
+ maxItems: z
104
+ .number()
105
+ .min(1)
106
+ .optional()
107
+ .describe("Maximum total items to return across all pages"),
108
+ })
109
+ .describe("Get the available choices for a dynamic dropdown input field"),
110
+ InputFieldChoiceItemSchema,
111
+ );
112
+
113
+ // Type inferred from schema
114
+ export type ListInputFieldChoicesOptions = z.infer<
115
+ typeof ListInputFieldChoicesSchema
116
+ >;
117
+
118
+ // Error union for this function
119
+ export type ListInputFieldChoicesError =
120
+ | ZapierConfigurationError
121
+ | ZapierApiError
122
+ | ZapierAuthenticationError
123
+ | ZapierAppNotFoundError
124
+ | ZapierValidationError
125
+ | ZapierUnknownError;
126
+
127
+ // Page result structure
128
+ export interface ListInputFieldChoicesPage {
129
+ data: InputFieldChoiceItem[];
130
+ nextCursor?: string;
131
+ }
132
+
133
+ // SDK function interface
134
+ export interface ListInputFieldChoicesSdkFunction {
135
+ listInputFieldChoices: PaginatedSdkFunction<
136
+ ListInputFieldChoicesOptions,
137
+ InputFieldChoiceItem
138
+ >;
139
+ }
@@ -36,7 +36,7 @@ export type RunActionOptions = z.infer<typeof RunActionSchema>;
36
36
 
37
37
  // Page result structure (for consistency with other paginated functions)
38
38
  export interface RunActionPage {
39
- data: any[];
39
+ data: unknown[];
40
40
  nextCursor?: string;
41
41
  }
42
42
 
@@ -49,5 +49,5 @@ export type RunActionError =
49
49
 
50
50
  // SDK function interface - ready to be mixed into main SDK interface
51
51
  export interface RunActionSdkFunction {
52
- runAction: PaginatedSdkFunction<RunActionOptions, any>;
52
+ runAction: PaginatedSdkFunction<RunActionOptions, unknown>;
53
53
  }
package/src/sdk.ts CHANGED
@@ -29,6 +29,7 @@ import { getAuthenticationPlugin } from "./plugins/getAuthentication";
29
29
  import { findFirstAuthenticationPlugin } from "./plugins/findFirstAuthentication";
30
30
  import { findUniqueAuthenticationPlugin } from "./plugins/findUniqueAuthentication";
31
31
  import { listInputFieldsPlugin } from "./plugins/listInputFields";
32
+ import { listInputFieldChoicesPlugin } from "./plugins/listInputFieldChoices";
32
33
  import { requestPlugin } from "./plugins/request";
33
34
  import { manifestPlugin } from "./plugins/manifest";
34
35
  import { lockVersionPlugin } from "./plugins/lockVersion";
@@ -132,6 +133,7 @@ export function createZapierSdkWithoutRegistry(options: ZapierSdkOptions = {}) {
132
133
  .addPlugin(listActionsPlugin)
133
134
  .addPlugin(getActionPlugin)
134
135
  .addPlugin(listInputFieldsPlugin)
136
+ .addPlugin(listInputFieldChoicesPlugin)
135
137
 
136
138
  // Run action
137
139
  .addPlugin(runActionPlugin)
@@ -11,7 +11,7 @@ export interface FunctionOptions {
11
11
  /** Function to dynamically resolve authentication token */
12
12
  getToken?: () => Promise<string | undefined>;
13
13
  /** Optional pre-instantiated API client */
14
- api?: any;
14
+ api?: import("../api/types").ApiClient;
15
15
  /** Enable debug logging */
16
16
  debug?: boolean;
17
17
  /** Custom fetch implementation */
@@ -33,7 +33,7 @@ export const AuthenticationIdPropertySchema = z
33
33
  .describe("Authentication ID to use for this action");
34
34
 
35
35
  export const InputsPropertySchema = z
36
- .record(z.any())
36
+ .record(z.unknown())
37
37
  .describe("Input parameters for the action");
38
38
 
39
39
  export const LimitPropertySchema = z
@@ -59,7 +59,7 @@ export const DebugPropertySchema = z
59
59
  .describe("Enable debug logging");
60
60
 
61
61
  export const ParamsPropertySchema = z
62
- .record(z.any())
62
+ .record(z.unknown())
63
63
  .describe("Additional parameters");
64
64
 
65
65
  // Inferred TypeScript types for the properties