@zapier/zapier-sdk 0.33.0 → 0.33.2

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 (128) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/index.cjs +2 -1
  3. package/dist/index.d.mts +9 -1
  4. package/dist/index.d.ts +1 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.mjs +2 -1
  7. package/dist/plugins/registry/index.d.ts.map +1 -1
  8. package/dist/plugins/registry/index.js +1 -0
  9. package/dist/types/sdk.d.ts +8 -0
  10. package/dist/types/sdk.d.ts.map +1 -1
  11. package/package.json +2 -2
  12. package/dist/api/auth.test.d.ts +0 -2
  13. package/dist/api/auth.test.d.ts.map +0 -1
  14. package/dist/api/auth.test.js +0 -220
  15. package/dist/api/client.test.d.ts +0 -2
  16. package/dist/api/client.test.d.ts.map +0 -1
  17. package/dist/api/client.test.js +0 -611
  18. package/dist/api/debug.test.d.ts +0 -2
  19. package/dist/api/debug.test.d.ts.map +0 -1
  20. package/dist/api/debug.test.js +0 -59
  21. package/dist/api/polling.test.d.ts +0 -2
  22. package/dist/api/polling.test.d.ts.map +0 -1
  23. package/dist/api/polling.test.js +0 -360
  24. package/dist/auth.test.d.ts +0 -2
  25. package/dist/auth.test.d.ts.map +0 -1
  26. package/dist/auth.test.js +0 -480
  27. package/dist/plugins/eventEmission/builders.test.d.ts +0 -2
  28. package/dist/plugins/eventEmission/builders.test.d.ts.map +0 -1
  29. package/dist/plugins/eventEmission/builders.test.js +0 -138
  30. package/dist/plugins/eventEmission/index.test.d.ts +0 -5
  31. package/dist/plugins/eventEmission/index.test.d.ts.map +0 -1
  32. package/dist/plugins/eventEmission/index.test.js +0 -712
  33. package/dist/plugins/eventEmission/transport.test.d.ts +0 -5
  34. package/dist/plugins/eventEmission/transport.test.d.ts.map +0 -1
  35. package/dist/plugins/eventEmission/transport.test.js +0 -164
  36. package/dist/plugins/fetch/index.test.d.ts +0 -2
  37. package/dist/plugins/fetch/index.test.d.ts.map +0 -1
  38. package/dist/plugins/fetch/index.test.js +0 -428
  39. package/dist/plugins/findFirstConnection/index.test.d.ts +0 -2
  40. package/dist/plugins/findFirstConnection/index.test.d.ts.map +0 -1
  41. package/dist/plugins/findFirstConnection/index.test.js +0 -177
  42. package/dist/plugins/findUniqueConnection/index.test.d.ts +0 -2
  43. package/dist/plugins/findUniqueConnection/index.test.d.ts.map +0 -1
  44. package/dist/plugins/findUniqueConnection/index.test.js +0 -159
  45. package/dist/plugins/getAction/index.test.d.ts +0 -2
  46. package/dist/plugins/getAction/index.test.d.ts.map +0 -1
  47. package/dist/plugins/getAction/index.test.js +0 -211
  48. package/dist/plugins/getApp/index.test.d.ts +0 -2
  49. package/dist/plugins/getApp/index.test.d.ts.map +0 -1
  50. package/dist/plugins/getApp/index.test.js +0 -157
  51. package/dist/plugins/getConnection/index.test.d.ts +0 -2
  52. package/dist/plugins/getConnection/index.test.d.ts.map +0 -1
  53. package/dist/plugins/getConnection/index.test.js +0 -124
  54. package/dist/plugins/getInputFieldsSchema/index.test.d.ts +0 -2
  55. package/dist/plugins/getInputFieldsSchema/index.test.d.ts.map +0 -1
  56. package/dist/plugins/getInputFieldsSchema/index.test.js +0 -291
  57. package/dist/plugins/listActions/index.test.d.ts +0 -2
  58. package/dist/plugins/listActions/index.test.d.ts.map +0 -1
  59. package/dist/plugins/listActions/index.test.js +0 -454
  60. package/dist/plugins/listApps/index.test.d.ts +0 -2
  61. package/dist/plugins/listApps/index.test.d.ts.map +0 -1
  62. package/dist/plugins/listApps/index.test.js +0 -124
  63. package/dist/plugins/listConnections/index.test.d.ts +0 -2
  64. package/dist/plugins/listConnections/index.test.d.ts.map +0 -1
  65. package/dist/plugins/listConnections/index.test.js +0 -920
  66. package/dist/plugins/listInputFieldChoices/index.test.d.ts +0 -2
  67. package/dist/plugins/listInputFieldChoices/index.test.d.ts.map +0 -1
  68. package/dist/plugins/listInputFieldChoices/index.test.js +0 -717
  69. package/dist/plugins/listInputFields/index.test.d.ts +0 -2
  70. package/dist/plugins/listInputFields/index.test.d.ts.map +0 -1
  71. package/dist/plugins/listInputFields/index.test.js +0 -359
  72. package/dist/plugins/manifest/index.test.d.ts +0 -2
  73. package/dist/plugins/manifest/index.test.d.ts.map +0 -1
  74. package/dist/plugins/manifest/index.test.js +0 -1179
  75. package/dist/plugins/request/index.test.d.ts +0 -2
  76. package/dist/plugins/request/index.test.d.ts.map +0 -1
  77. package/dist/plugins/request/index.test.js +0 -458
  78. package/dist/plugins/runAction/index.test.d.ts +0 -2
  79. package/dist/plugins/runAction/index.test.d.ts.map +0 -1
  80. package/dist/plugins/runAction/index.test.js +0 -350
  81. package/dist/resolvers/connectionId.test.d.ts +0 -2
  82. package/dist/resolvers/connectionId.test.d.ts.map +0 -1
  83. package/dist/resolvers/connectionId.test.js +0 -61
  84. package/dist/sdk.test.d.ts +0 -2
  85. package/dist/sdk.test.d.ts.map +0 -1
  86. package/dist/sdk.test.js +0 -260
  87. package/dist/types/domain.test.d.ts +0 -2
  88. package/dist/types/domain.test.d.ts.map +0 -1
  89. package/dist/types/domain.test.js +0 -39
  90. package/dist/utils/array-utils.test.d.ts +0 -2
  91. package/dist/utils/array-utils.test.d.ts.map +0 -1
  92. package/dist/utils/array-utils.test.js +0 -107
  93. package/dist/utils/batch-utils.test.d.ts +0 -2
  94. package/dist/utils/batch-utils.test.d.ts.map +0 -1
  95. package/dist/utils/batch-utils.test.js +0 -476
  96. package/dist/utils/domain-utils.test.d.ts +0 -2
  97. package/dist/utils/domain-utils.test.d.ts.map +0 -1
  98. package/dist/utils/domain-utils.test.js +0 -346
  99. package/dist/utils/file-utils.test.d.ts +0 -2
  100. package/dist/utils/file-utils.test.d.ts.map +0 -1
  101. package/dist/utils/file-utils.test.js +0 -51
  102. package/dist/utils/function-utils.test.d.ts +0 -2
  103. package/dist/utils/function-utils.test.d.ts.map +0 -1
  104. package/dist/utils/function-utils.test.js +0 -188
  105. package/dist/utils/id-utils.test.d.ts +0 -2
  106. package/dist/utils/id-utils.test.d.ts.map +0 -1
  107. package/dist/utils/id-utils.test.js +0 -22
  108. package/dist/utils/pagination-utils.test.d.ts +0 -17
  109. package/dist/utils/pagination-utils.test.d.ts.map +0 -1
  110. package/dist/utils/pagination-utils.test.js +0 -461
  111. package/dist/utils/retry-utils.test.d.ts +0 -2
  112. package/dist/utils/retry-utils.test.d.ts.map +0 -1
  113. package/dist/utils/retry-utils.test.js +0 -90
  114. package/dist/utils/string-utils.test.d.ts +0 -2
  115. package/dist/utils/string-utils.test.d.ts.map +0 -1
  116. package/dist/utils/string-utils.test.js +0 -59
  117. package/dist/utils/telemetry-context.test.d.ts +0 -2
  118. package/dist/utils/telemetry-context.test.d.ts.map +0 -1
  119. package/dist/utils/telemetry-context.test.js +0 -154
  120. package/dist/utils/telemetry-utils.test.d.ts +0 -2
  121. package/dist/utils/telemetry-utils.test.d.ts.map +0 -1
  122. package/dist/utils/telemetry-utils.test.js +0 -155
  123. package/dist/utils/url-utils.test.d.ts +0 -2
  124. package/dist/utils/url-utils.test.d.ts.map +0 -1
  125. package/dist/utils/url-utils.test.js +0 -103
  126. package/dist/utils/validation.test.d.ts +0 -2
  127. package/dist/utils/validation.test.d.ts.map +0 -1
  128. package/dist/utils/validation.test.js +0 -44
@@ -1,717 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from "vitest";
2
- import { ZapierValidationError, ZapierApiError } from "../../types/errors";
3
- import { listInputFieldChoicesPlugin } from "./index";
4
- import { createSdk } from "../../sdk";
5
- import { GetActionSchema } from "../getAction/schemas";
6
- import { eventEmissionPlugin } from "../eventEmission";
7
- const mockChoices = [
8
- {
9
- key: "general",
10
- label: "General Channel",
11
- sample: "#general",
12
- value: "general",
13
- },
14
- {
15
- key: "random",
16
- label: "Random Channel",
17
- sample: "#random",
18
- value: "random",
19
- },
20
- {
21
- key: "dev",
22
- label: "Development Channel",
23
- sample: "#dev",
24
- value: "dev",
25
- },
26
- ];
27
- const mockChoicesResponse = {
28
- success: true,
29
- choices: mockChoices,
30
- };
31
- const mockPaginatedChoicesResponse = {
32
- success: true,
33
- choices: mockChoices.slice(0, 2),
34
- next_page: 1,
35
- };
36
- const mockExternalChoicesResponse = {
37
- success: true,
38
- choices: mockChoices,
39
- meta: { page: "0" },
40
- links: {
41
- next: "https://api.zapier.com/api/v4/implementations/choices/?page=1",
42
- prev: null,
43
- },
44
- };
45
- // Mock needs data for static choices
46
- const mockNeedsWithStaticChoices = [
47
- {
48
- key: "priority",
49
- label: "Priority",
50
- help_text: "Select priority",
51
- required: true,
52
- type: "string",
53
- choices: [
54
- {
55
- key: "high",
56
- sample: "high",
57
- label: "High Priority",
58
- value: "high",
59
- },
60
- {
61
- key: "medium",
62
- sample: "medium",
63
- label: "Medium Priority",
64
- value: "medium",
65
- },
66
- ],
67
- },
68
- ];
69
- const mockNeedsWithoutChoices = [
70
- {
71
- key: "channel",
72
- label: "Channel",
73
- help_text: "Select a channel",
74
- required: true,
75
- type: "string",
76
- choices: [], // Empty choices
77
- },
78
- ];
79
- const mockNeedsResponse = {
80
- success: true,
81
- needs: mockNeedsWithoutChoices,
82
- };
83
- const mockNeedsResponseWithStaticChoices = {
84
- success: true,
85
- needs: mockNeedsWithStaticChoices,
86
- };
87
- describe("listInputFieldChoices plugin", () => {
88
- let mockApiClient;
89
- let mockGetAction;
90
- let mockGetVersionedImplementationId;
91
- beforeEach(() => {
92
- vi.clearAllMocks();
93
- // Mock api.post to handle both endpoints
94
- const mockPost = vi.fn().mockImplementation((path) => {
95
- if (path === "/zapier/api/v4/implementations/needs/") {
96
- return Promise.resolve(mockNeedsResponse);
97
- }
98
- else if (path === "/zapier/api/v4/implementations/choices/") {
99
- return Promise.resolve(mockChoicesResponse);
100
- }
101
- return Promise.reject(new Error(`Unexpected endpoint: ${path}`));
102
- });
103
- mockApiClient = {
104
- post: mockPost,
105
- };
106
- mockGetAction = vi.fn().mockResolvedValue({
107
- data: { id: "core:123", key: "send_message", action_type: "read" },
108
- });
109
- mockGetVersionedImplementationId = vi
110
- .fn()
111
- .mockResolvedValue("SlackCLIAPI@1.21.1");
112
- });
113
- function createTestSdk() {
114
- // Create a mock getAction plugin
115
- const mockGetActionPlugin = () => ({
116
- getAction: mockGetAction,
117
- context: {
118
- meta: {
119
- getAction: {
120
- inputSchema: GetActionSchema,
121
- },
122
- },
123
- },
124
- });
125
- // Build SDK with proper plugin composition
126
- return createSdk({}, {}, {
127
- api: mockApiClient,
128
- meta: {},
129
- options: {},
130
- getVersionedImplementationId: mockGetVersionedImplementationId,
131
- })
132
- .addPlugin(eventEmissionPlugin)
133
- .addPlugin(mockGetActionPlugin)
134
- .addPlugin(listInputFieldChoicesPlugin);
135
- }
136
- describe("schema validation", () => {
137
- it("should throw validation error for missing inputFieldKey", async () => {
138
- const sdk = createTestSdk();
139
- try {
140
- await sdk.listInputFieldChoices({
141
- appKey: "slack",
142
- actionType: "read",
143
- actionKey: "send_message",
144
- // Missing inputFieldKey
145
- });
146
- expect.fail("Expected ZapierValidationError to be thrown");
147
- }
148
- catch (error) {
149
- expect(error).toBeInstanceOf(ZapierValidationError);
150
- }
151
- });
152
- it("should throw validation error for missing actionKey", async () => {
153
- const sdk = createTestSdk();
154
- try {
155
- await sdk.listInputFieldChoices({
156
- appKey: "slack",
157
- actionType: "read",
158
- inputFieldKey: "channel",
159
- // Missing actionKey
160
- });
161
- expect.fail("Expected ZapierValidationError to be thrown");
162
- }
163
- catch (error) {
164
- expect(error).toBeInstanceOf(ZapierValidationError);
165
- }
166
- });
167
- it("should pass validation with action method", async () => {
168
- const sdk = createTestSdk();
169
- const result = await sdk.listInputFieldChoices({
170
- appKey: "slack",
171
- actionType: "read",
172
- actionKey: "send_message",
173
- inputFieldKey: "channel",
174
- });
175
- expect(result.data).toBeDefined();
176
- });
177
- it("should pass validation with optional fields", async () => {
178
- const sdk = createTestSdk();
179
- const result = await sdk.listInputFieldChoices({
180
- appKey: "slack",
181
- actionType: "read",
182
- actionKey: "send_message",
183
- inputFieldKey: "channel",
184
- authenticationId: 456,
185
- inputs: { workspace: "my-team" },
186
- page: 0,
187
- });
188
- expect(result.data).toBeDefined();
189
- });
190
- it("should throw validation error for empty string actionKey", async () => {
191
- const sdk = createTestSdk();
192
- try {
193
- await sdk.listInputFieldChoices({
194
- appKey: "slack",
195
- actionType: "read",
196
- actionKey: "", // Empty string
197
- inputFieldKey: "channel",
198
- });
199
- expect.fail("Expected ZapierValidationError to be thrown");
200
- }
201
- catch (error) {
202
- expect(error).toBeInstanceOf(ZapierValidationError);
203
- }
204
- });
205
- it("should throw validation error for empty string inputFieldKey", async () => {
206
- const sdk = createTestSdk();
207
- try {
208
- await sdk.listInputFieldChoices({
209
- appKey: "slack",
210
- actionType: "read",
211
- actionKey: "send_message",
212
- inputFieldKey: "", // Empty string
213
- });
214
- expect.fail("Expected ZapierValidationError to be thrown");
215
- }
216
- catch (error) {
217
- expect(error).toBeInstanceOf(ZapierValidationError);
218
- }
219
- });
220
- });
221
- describe("static choices from needs endpoint", () => {
222
- it("should return static choices when field has them", async () => {
223
- // Mock api.post to return needs with static choices
224
- const mockPost = vi.fn().mockImplementation((path) => {
225
- if (path === "/zapier/api/v4/implementations/needs/") {
226
- return Promise.resolve(mockNeedsResponseWithStaticChoices);
227
- }
228
- else if (path === "/zapier/api/v4/implementations/choices/") {
229
- return Promise.resolve(mockChoicesResponse);
230
- }
231
- return Promise.reject(new Error(`Unexpected endpoint: ${path}`));
232
- });
233
- mockApiClient.post = mockPost;
234
- const sdk = createTestSdk();
235
- const result = await sdk.listInputFieldChoices({
236
- appKey: "slack",
237
- actionType: "read",
238
- actionKey: "send_message",
239
- inputFieldKey: "priority",
240
- });
241
- // Should return static choices
242
- expect(result.data).toHaveLength(2);
243
- expect(result.data[0]).toEqual({
244
- key: "high",
245
- label: "High Priority",
246
- sample: "high",
247
- value: "high",
248
- });
249
- expect(result.data[1]).toEqual({
250
- key: "medium",
251
- label: "Medium Priority",
252
- sample: "medium",
253
- value: "medium",
254
- });
255
- // Should have called needs endpoint to check for static choices
256
- expect(mockApiClient.post).toHaveBeenCalledWith("/zapier/api/v4/implementations/needs/", {
257
- selected_api: "SlackCLIAPI@1.21.1",
258
- action: "send_message",
259
- type_of: "read",
260
- params: {},
261
- });
262
- // Should not call choices endpoint since static choices were found
263
- expect(mockApiClient.post).not.toHaveBeenCalledWith("/zapier/api/v4/implementations/choices/", expect.anything());
264
- });
265
- it("should fall back to dynamic choices when field has empty choices array", async () => {
266
- // Mock api.post - needs endpoint returns field with empty choices
267
- const mockPost = vi.fn().mockImplementation((path) => {
268
- if (path === "/zapier/api/v4/implementations/needs/") {
269
- return Promise.resolve(mockNeedsResponse); // Has empty choices
270
- }
271
- else if (path === "/zapier/api/v4/implementations/choices/") {
272
- return Promise.resolve(mockChoicesResponse);
273
- }
274
- return Promise.reject(new Error(`Unexpected endpoint: ${path}`));
275
- });
276
- mockApiClient.post = mockPost;
277
- const sdk = createTestSdk();
278
- const result = await sdk.listInputFieldChoices({
279
- appKey: "slack",
280
- actionType: "read",
281
- actionKey: "send_message",
282
- inputFieldKey: "channel",
283
- });
284
- // Should first call needs endpoint
285
- expect(mockApiClient.post).toHaveBeenCalledWith("/zapier/api/v4/implementations/needs/", {
286
- selected_api: "SlackCLIAPI@1.21.1",
287
- action: "send_message",
288
- type_of: "read",
289
- params: {},
290
- });
291
- // Should then call choices endpoint for dynamic choices
292
- expect(mockApiClient.post).toHaveBeenCalledWith("/zapier/api/v4/implementations/choices/", expect.objectContaining({
293
- action_id: "core:123",
294
- input_field_id: "channel",
295
- }));
296
- // Should return dynamic choices
297
- expect(result.data).toHaveLength(3);
298
- });
299
- it("should fall back to dynamic choices when field is not found", async () => {
300
- // Mock api.post - needs endpoint returns different field
301
- const mockNeedsWithOtherField = {
302
- success: true,
303
- needs: [
304
- {
305
- key: "other_field",
306
- label: "Other Field",
307
- help_text: "Some other field",
308
- required: false,
309
- type: "string",
310
- choices: [],
311
- },
312
- ],
313
- };
314
- const mockPost = vi.fn().mockImplementation((path) => {
315
- if (path === "/zapier/api/v4/implementations/needs/") {
316
- return Promise.resolve(mockNeedsWithOtherField);
317
- }
318
- else if (path === "/zapier/api/v4/implementations/choices/") {
319
- return Promise.resolve(mockChoicesResponse);
320
- }
321
- return Promise.reject(new Error(`Unexpected endpoint: ${path}`));
322
- });
323
- mockApiClient.post = mockPost;
324
- const sdk = createTestSdk();
325
- await sdk.listInputFieldChoices({
326
- appKey: "slack",
327
- actionType: "read",
328
- actionKey: "send_message",
329
- inputFieldKey: "channel",
330
- });
331
- // Should call needs endpoint first
332
- expect(mockApiClient.post).toHaveBeenCalledWith("/zapier/api/v4/implementations/needs/", expect.anything());
333
- // Should call choices endpoint since field was not found in needs
334
- expect(mockApiClient.post).toHaveBeenCalledWith("/zapier/api/v4/implementations/choices/", expect.anything());
335
- });
336
- });
337
- describe("API integration - action method", () => {
338
- it("should call the correct API endpoint with action method", async () => {
339
- const sdk = createTestSdk();
340
- await sdk.listInputFieldChoices({
341
- appKey: "slack",
342
- actionType: "read",
343
- actionKey: "send_message",
344
- inputFieldKey: "channel",
345
- });
346
- expect(mockGetAction).toHaveBeenCalledWith(expect.objectContaining({
347
- appKey: "slack",
348
- actionType: "read",
349
- actionKey: "send_message",
350
- }));
351
- expect(mockApiClient.post).toHaveBeenCalledWith("/zapier/api/v4/implementations/choices/", {
352
- action_id: "core:123",
353
- input_field_id: "channel",
354
- page: 0,
355
- params: {},
356
- });
357
- });
358
- it("should include authentication_id when provided", async () => {
359
- const sdk = createTestSdk();
360
- await sdk.listInputFieldChoices({
361
- appKey: "slack",
362
- actionType: "read",
363
- actionKey: "send_message",
364
- inputFieldKey: "channel",
365
- authenticationId: 456,
366
- });
367
- expect(mockApiClient.post).toHaveBeenCalledWith("/zapier/api/v4/implementations/choices/", {
368
- action_id: "core:123",
369
- input_field_id: "channel",
370
- authentication_id: 456,
371
- page: 0,
372
- params: {},
373
- });
374
- });
375
- it("should exclude authentication_id when null", async () => {
376
- const sdk = createTestSdk();
377
- await sdk.listInputFieldChoices({
378
- appKey: "slack",
379
- actionType: "read",
380
- actionKey: "send_message",
381
- inputFieldKey: "channel",
382
- authenticationId: null,
383
- });
384
- expect(mockApiClient.post).toHaveBeenCalledWith("/zapier/api/v4/implementations/choices/", {
385
- action_id: "core:123",
386
- input_field_id: "channel",
387
- page: 0,
388
- params: {},
389
- // No authentication_id
390
- });
391
- });
392
- it("should include inputs when provided", async () => {
393
- const sdk = createTestSdk();
394
- const inputs = { workspace: "my-team", board: "project" };
395
- await sdk.listInputFieldChoices({
396
- appKey: "slack",
397
- actionType: "read",
398
- actionKey: "send_message",
399
- inputFieldKey: "channel",
400
- inputs,
401
- });
402
- expect(mockApiClient.post).toHaveBeenCalledWith("/zapier/api/v4/implementations/choices/", {
403
- action_id: "core:123",
404
- input_field_id: "channel",
405
- page: 0,
406
- params: inputs,
407
- });
408
- });
409
- it("should include page when provided", async () => {
410
- const sdk = createTestSdk();
411
- await sdk.listInputFieldChoices({
412
- appKey: "slack",
413
- actionType: "read",
414
- actionKey: "send_message",
415
- inputFieldKey: "channel",
416
- page: 2,
417
- });
418
- expect(mockApiClient.post).toHaveBeenCalledWith("/zapier/api/v4/implementations/choices/", {
419
- action_id: "core:123",
420
- input_field_id: "channel",
421
- page: 2,
422
- params: {},
423
- });
424
- });
425
- });
426
- describe("data transformation", () => {
427
- it("should transform choices to input field choice items correctly", async () => {
428
- const sdk = createTestSdk();
429
- const result = await sdk.listInputFieldChoices({
430
- appKey: "slack",
431
- actionType: "read",
432
- actionKey: "send_message",
433
- inputFieldKey: "channel",
434
- });
435
- expect(result.data).toHaveLength(3);
436
- // Check first choice transformation
437
- expect(result.data[0]).toEqual({
438
- key: "general",
439
- label: "General Channel",
440
- sample: "#general",
441
- value: "general",
442
- });
443
- // Check second choice transformation
444
- expect(result.data[1]).toEqual({
445
- key: "random",
446
- label: "Random Channel",
447
- sample: "#random",
448
- value: "random",
449
- });
450
- });
451
- it("should handle choices with missing fields gracefully", async () => {
452
- const sparseChoices = [
453
- { key: "item1", label: "Item 1" },
454
- { value: "item2", sample: "item2" },
455
- { label: "Item 3" },
456
- ];
457
- mockApiClient.post = vi.fn().mockResolvedValue({
458
- success: true,
459
- choices: sparseChoices,
460
- });
461
- const sdk = createTestSdk();
462
- const result = await sdk.listInputFieldChoices({
463
- appKey: "slack",
464
- actionType: "read",
465
- actionKey: "send_message",
466
- inputFieldKey: "item",
467
- });
468
- expect(result.data).toHaveLength(3);
469
- expect(result.data[0]).toEqual({
470
- key: "item1",
471
- label: "Item 1",
472
- sample: undefined,
473
- value: undefined,
474
- });
475
- expect(result.data[1]).toEqual({
476
- key: undefined,
477
- label: undefined,
478
- sample: "item2",
479
- value: "item2",
480
- });
481
- });
482
- });
483
- describe("pagination handling", () => {
484
- it("should handle next_page pagination correctly in the plugin", async () => {
485
- mockApiClient.post = vi
486
- .fn()
487
- .mockResolvedValue(mockPaginatedChoicesResponse);
488
- const sdk = createTestSdk();
489
- // Test that the plugin correctly sets nextCursor when next_page is present
490
- const listChoicesResult = sdk.listInputFieldChoices({
491
- appKey: "slack",
492
- actionType: "read",
493
- actionKey: "send_message",
494
- inputFieldKey: "channel",
495
- pageSize: 2, // Set a small page size to test pagination behavior
496
- });
497
- const pages = [];
498
- for await (const page of listChoicesResult) {
499
- pages.push(page);
500
- break; // Just get first page for testing
501
- }
502
- expect(pages).toHaveLength(1);
503
- expect(pages[0].nextCursor).toBe("1");
504
- });
505
- it("should handle external action link-based pagination correctly in the plugin", async () => {
506
- mockApiClient.post = vi
507
- .fn()
508
- .mockResolvedValue(mockExternalChoicesResponse);
509
- const sdk = createTestSdk();
510
- // Test that the plugin correctly extracts cursor from links.next
511
- const listChoicesResult = sdk.listInputFieldChoices({
512
- appKey: "slack",
513
- actionType: "read",
514
- actionKey: "send_message",
515
- inputFieldKey: "option",
516
- pageSize: 3, // Set a small page size to test pagination behavior
517
- });
518
- const pages = [];
519
- for await (const page of listChoicesResult) {
520
- pages.push(page);
521
- break; // Just get first page for testing
522
- }
523
- expect(pages).toHaveLength(1);
524
- expect(pages[0].nextCursor).toBe("1"); // Extracted from links.next URL
525
- });
526
- it("should handle no pagination", async () => {
527
- const sdk = createTestSdk();
528
- const listChoicesResult = sdk.listInputFieldChoices({
529
- appKey: "slack",
530
- actionType: "read",
531
- actionKey: "send_message",
532
- inputFieldKey: "channel",
533
- });
534
- const pages = [];
535
- for await (const page of listChoicesResult) {
536
- pages.push(page);
537
- break; // Just get first page for testing
538
- }
539
- expect(pages).toHaveLength(1);
540
- expect(pages[0].nextCursor).toBeUndefined();
541
- });
542
- it("should support async iteration over individual items", async () => {
543
- const sdk = createTestSdk();
544
- const listChoicesResult = sdk.listInputFieldChoices({
545
- appKey: "slack",
546
- actionType: "read",
547
- actionKey: "send_message",
548
- inputFieldKey: "channel",
549
- });
550
- const items = [];
551
- for await (const item of listChoicesResult.items()) {
552
- items.push(item);
553
- }
554
- expect(items).toHaveLength(3);
555
- expect(items[0].key).toBe("general");
556
- });
557
- it("should handle malformed links.next URL gracefully", async () => {
558
- const malformedExternalResponse = {
559
- success: true,
560
- choices: mockChoices,
561
- links: {
562
- next: "invalid-url-format", // Malformed URL
563
- prev: null,
564
- },
565
- };
566
- mockApiClient.post = vi.fn().mockResolvedValue(malformedExternalResponse);
567
- const sdk = createTestSdk();
568
- const listChoicesResult = sdk.listInputFieldChoices({
569
- appKey: "slack",
570
- actionType: "read",
571
- actionKey: "send_message",
572
- inputFieldKey: "option",
573
- });
574
- const pages = [];
575
- for await (const page of listChoicesResult) {
576
- pages.push(page);
577
- break;
578
- }
579
- expect(pages).toHaveLength(1);
580
- expect(pages[0].nextCursor).toBeUndefined(); // Should handle gracefully
581
- });
582
- it("should handle links.next URL without page parameter", async () => {
583
- const noPageResponse = {
584
- success: true,
585
- choices: mockChoices,
586
- links: {
587
- next: "https://api.zapier.com/api/v4/implementations/choices/", // No page param
588
- prev: null,
589
- },
590
- };
591
- mockApiClient.post = vi.fn().mockResolvedValue(noPageResponse);
592
- const sdk = createTestSdk();
593
- const listChoicesResult = sdk.listInputFieldChoices({
594
- appKey: "slack",
595
- actionType: "read",
596
- actionKey: "send_message",
597
- inputFieldKey: "option",
598
- });
599
- const pages = [];
600
- for await (const page of listChoicesResult) {
601
- pages.push(page);
602
- break;
603
- }
604
- expect(pages).toHaveLength(1);
605
- expect(pages[0].nextCursor).toBeUndefined();
606
- });
607
- });
608
- describe("error handling", () => {
609
- it("should throw ZapierApiError when API response indicates failure", async () => {
610
- mockApiClient.post = vi.fn().mockResolvedValue({
611
- success: false,
612
- errors: ["Invalid field", "Missing authentication"],
613
- });
614
- const sdk = createTestSdk();
615
- await expect(sdk.listInputFieldChoices({
616
- appKey: "slack",
617
- actionType: "read",
618
- actionKey: "send_message",
619
- inputFieldKey: "invalid_field",
620
- })).rejects.toThrow(ZapierApiError);
621
- // Error comes from needs endpoint (first call) with user-friendly message
622
- await expect(sdk.listInputFieldChoices({
623
- appKey: "slack",
624
- actionType: "read",
625
- actionKey: "send_message",
626
- inputFieldKey: "invalid_field",
627
- })).rejects.toThrow("Failed to get input fields: Invalid field, Missing authentication");
628
- });
629
- it("should handle API errors gracefully", async () => {
630
- mockApiClient.post = vi
631
- .fn()
632
- .mockRejectedValue(new Error("Network error"));
633
- const sdk = createTestSdk();
634
- await expect(sdk.listInputFieldChoices({
635
- appKey: "slack",
636
- actionType: "read",
637
- actionKey: "send_message",
638
- inputFieldKey: "channel",
639
- })).rejects.toThrow("Network error");
640
- });
641
- it("should handle empty choices array response", async () => {
642
- mockApiClient.post = vi.fn().mockResolvedValue({
643
- success: true,
644
- choices: [], // Empty choices
645
- });
646
- const sdk = createTestSdk();
647
- const result = await sdk.listInputFieldChoices({
648
- appKey: "slack",
649
- actionType: "read",
650
- actionKey: "send_message",
651
- inputFieldKey: "channel",
652
- });
653
- expect(result.data).toEqual([]);
654
- });
655
- it("should handle missing choices field in response", async () => {
656
- mockApiClient.post = vi.fn().mockResolvedValue({
657
- success: true,
658
- // Missing choices field
659
- });
660
- const sdk = createTestSdk();
661
- const result = await sdk.listInputFieldChoices({
662
- appKey: "slack",
663
- actionType: "read",
664
- actionKey: "send_message",
665
- inputFieldKey: "channel",
666
- });
667
- expect(result.data).toEqual([]);
668
- });
669
- it("should treat missing success field as failure", async () => {
670
- mockApiClient.post = vi.fn().mockResolvedValue({
671
- // Missing success field (undefined)
672
- choices: mockChoices,
673
- });
674
- const sdk = createTestSdk();
675
- await expect(sdk.listInputFieldChoices({
676
- appKey: "slack",
677
- actionType: "read",
678
- actionKey: "send_message",
679
- inputFieldKey: "channel",
680
- })).rejects.toThrow("Failed to get input fields: Unknown error");
681
- });
682
- it("should handle API response with errors but no error messages", async () => {
683
- mockApiClient.post = vi.fn().mockResolvedValue({
684
- success: false,
685
- // Missing errors field
686
- });
687
- const sdk = createTestSdk();
688
- await expect(sdk.listInputFieldChoices({
689
- appKey: "slack",
690
- actionType: "read",
691
- actionKey: "send_message",
692
- inputFieldKey: "channel",
693
- })).rejects.toThrow("Failed to get input fields: Unknown error");
694
- });
695
- it("should handle API response with empty errors array", async () => {
696
- mockApiClient.post = vi.fn().mockResolvedValue({
697
- success: false,
698
- errors: [], // Empty errors array
699
- });
700
- const sdk = createTestSdk();
701
- await expect(sdk.listInputFieldChoices({
702
- appKey: "slack",
703
- actionType: "read",
704
- actionKey: "send_message",
705
- inputFieldKey: "channel",
706
- })).rejects.toThrow("Failed to get input fields: Unknown error");
707
- });
708
- });
709
- describe("context and metadata", () => {
710
- it("should provide context with meta information", () => {
711
- const sdk = createTestSdk();
712
- const context = sdk.getContext();
713
- expect(context.meta.listInputFieldChoices).toBeDefined();
714
- expect(context.meta.listInputFieldChoices.inputSchema).toBeDefined();
715
- });
716
- });
717
- });