@zapier/zapier-sdk 0.6.3 → 0.8.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 (56) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +73 -41
  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 +5 -1
  7. package/dist/api/types.d.ts.map +1 -1
  8. package/dist/index.cjs +2208 -2022
  9. package/dist/index.d.mts +327 -10
  10. package/dist/index.d.ts +18 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +19 -1
  13. package/dist/index.mjs +2191 -2024
  14. package/dist/plugins/findFirstAuthentication/index.test.js +3 -3
  15. package/dist/plugins/findFirstAuthentication/schemas.d.ts +3 -3
  16. package/dist/plugins/findFirstAuthentication/schemas.js +1 -1
  17. package/dist/plugins/findUniqueAuthentication/index.test.js +3 -3
  18. package/dist/plugins/findUniqueAuthentication/schemas.d.ts +3 -3
  19. package/dist/plugins/findUniqueAuthentication/schemas.js +1 -1
  20. package/dist/plugins/getProfile/index.js +1 -1
  21. package/dist/plugins/listAuthentications/index.d.ts.map +1 -1
  22. package/dist/plugins/listAuthentications/index.js +2 -3
  23. package/dist/plugins/listAuthentications/index.test.js +5 -5
  24. package/dist/plugins/listAuthentications/schemas.d.ts +3 -3
  25. package/dist/plugins/listAuthentications/schemas.js +1 -1
  26. package/dist/plugins/listInputFieldChoices/index.d.ts +28 -0
  27. package/dist/plugins/listInputFieldChoices/index.d.ts.map +1 -0
  28. package/dist/plugins/listInputFieldChoices/index.js +78 -0
  29. package/dist/plugins/listInputFieldChoices/index.test.d.ts +2 -0
  30. package/dist/plugins/listInputFieldChoices/index.test.d.ts.map +1 -0
  31. package/dist/plugins/listInputFieldChoices/index.test.js +537 -0
  32. package/dist/plugins/listInputFieldChoices/schemas.d.ts +61 -0
  33. package/dist/plugins/listInputFieldChoices/schemas.d.ts.map +1 -0
  34. package/dist/plugins/listInputFieldChoices/schemas.js +73 -0
  35. package/dist/plugins/registry/index.js +3 -3
  36. package/dist/sdk.d.ts +93 -0
  37. package/dist/sdk.d.ts.map +1 -1
  38. package/dist/sdk.js +7 -2
  39. package/package.json +1 -1
  40. package/src/api/schemas.ts +62 -0
  41. package/src/api/types.ts +14 -0
  42. package/src/index.ts +25 -1
  43. package/src/plugins/findFirstAuthentication/index.test.ts +3 -3
  44. package/src/plugins/findFirstAuthentication/schemas.ts +1 -1
  45. package/src/plugins/findUniqueAuthentication/index.test.ts +3 -3
  46. package/src/plugins/findUniqueAuthentication/schemas.ts +1 -1
  47. package/src/plugins/getProfile/index.ts +1 -1
  48. package/src/plugins/listAuthentications/index.test.ts +5 -5
  49. package/src/plugins/listAuthentications/index.ts +2 -3
  50. package/src/plugins/listAuthentications/schemas.ts +1 -1
  51. package/src/plugins/listInputFieldChoices/index.test.ts +653 -0
  52. package/src/plugins/listInputFieldChoices/index.ts +152 -0
  53. package/src/plugins/listInputFieldChoices/schemas.ts +139 -0
  54. package/src/plugins/registry/index.ts +3 -3
  55. package/src/sdk.ts +8 -1
  56. 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
+ }
@@ -27,6 +27,9 @@ export const registryPlugin: Plugin<
27
27
  string,
28
28
  { title: string; titlePlural?: string }
29
29
  > = {
30
+ account: {
31
+ title: "Account",
32
+ },
30
33
  app: {
31
34
  title: "App",
32
35
  titlePlural: "Apps",
@@ -40,9 +43,6 @@ export const registryPlugin: Plugin<
40
43
  http: {
41
44
  title: "HTTP Request",
42
45
  },
43
- user: {
44
- title: "User",
45
- },
46
46
  utility: {
47
47
  title: "Utility",
48
48
  titlePlural: "Utilities",
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";
@@ -117,7 +118,7 @@ export function createSdk<
117
118
  };
118
119
  }
119
120
 
120
- export function createZapierSdk(options: ZapierSdkOptions = {}): ZapierSdk {
121
+ export function createZapierSdkWithoutRegistry(options: ZapierSdkOptions = {}) {
121
122
  return (
122
123
  createSdk(options)
123
124
  // Provides the API client in context
@@ -132,6 +133,7 @@ export function createZapierSdk(options: ZapierSdkOptions = {}): ZapierSdk {
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)
@@ -154,7 +156,12 @@ export function createZapierSdk(options: ZapierSdkOptions = {}): ZapierSdk {
154
156
 
155
157
  // Profile
156
158
  .addPlugin(getProfilePlugin)
159
+ );
160
+ }
157
161
 
162
+ export function createZapierSdk(options: ZapierSdkOptions = {}): ZapierSdk {
163
+ return (
164
+ createZapierSdkWithoutRegistry(options)
158
165
  // Register plugins for CLI/MCP metadata
159
166
  .addPlugin(registryPlugin)
160
167
  );