@zapier/zapier-sdk 0.13.3 → 0.13.5

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 (81) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/api/schemas.d.ts +174 -174
  3. package/dist/constants.d.ts +4 -0
  4. package/dist/constants.d.ts.map +1 -1
  5. package/dist/constants.js +4 -0
  6. package/dist/index.cjs +457 -11
  7. package/dist/index.d.mts +389 -158
  8. package/dist/index.d.ts +3 -0
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +1 -0
  11. package/dist/index.mjs +428 -15
  12. package/dist/plugins/api/index.d.ts +1 -3
  13. package/dist/plugins/api/index.d.ts.map +1 -1
  14. package/dist/plugins/eventEmission/builders.d.ts +13 -0
  15. package/dist/plugins/eventEmission/builders.d.ts.map +1 -0
  16. package/dist/plugins/eventEmission/builders.js +78 -0
  17. package/dist/plugins/eventEmission/index.d.ts +34 -0
  18. package/dist/plugins/eventEmission/index.d.ts.map +1 -0
  19. package/dist/plugins/eventEmission/index.js +216 -0
  20. package/dist/plugins/eventEmission/index.test.d.ts +5 -0
  21. package/dist/plugins/eventEmission/index.test.d.ts.map +1 -0
  22. package/dist/plugins/eventEmission/index.test.js +143 -0
  23. package/dist/plugins/eventEmission/transport.d.ts +37 -0
  24. package/dist/plugins/eventEmission/transport.d.ts.map +1 -0
  25. package/dist/plugins/eventEmission/transport.js +96 -0
  26. package/dist/plugins/eventEmission/transport.test.d.ts +5 -0
  27. package/dist/plugins/eventEmission/transport.test.d.ts.map +1 -0
  28. package/dist/plugins/eventEmission/transport.test.js +153 -0
  29. package/dist/plugins/eventEmission/types.d.ts +53 -0
  30. package/dist/plugins/eventEmission/types.d.ts.map +1 -0
  31. package/dist/plugins/eventEmission/types.js +1 -0
  32. package/dist/plugins/eventEmission/utils.d.ts +45 -0
  33. package/dist/plugins/eventEmission/utils.d.ts.map +1 -0
  34. package/dist/plugins/eventEmission/utils.js +114 -0
  35. package/dist/plugins/fetch/schemas.d.ts +4 -4
  36. package/dist/plugins/getAction/schemas.d.ts +2 -2
  37. package/dist/plugins/listActions/index.test.js +3 -1
  38. package/dist/plugins/listActions/schemas.d.ts +2 -2
  39. package/dist/plugins/listAuthentications/index.test.js +3 -1
  40. package/dist/plugins/listInputFieldChoices/schemas.d.ts +4 -4
  41. package/dist/plugins/listInputFields/schemas.d.ts +2 -2
  42. package/dist/plugins/manifest/index.d.ts +9 -1
  43. package/dist/plugins/manifest/index.d.ts.map +1 -1
  44. package/dist/plugins/manifest/index.js +19 -7
  45. package/dist/plugins/manifest/index.test.js +4 -2
  46. package/dist/plugins/request/schemas.d.ts +4 -4
  47. package/dist/plugins/runAction/schemas.d.ts +2 -2
  48. package/dist/resolvers/actionType.d.ts.map +1 -1
  49. package/dist/resolvers/actionType.js +2 -3
  50. package/dist/resolvers/authenticationId.d.ts.map +1 -1
  51. package/dist/schemas/Action.d.ts +2 -2
  52. package/dist/schemas/App.d.ts +30 -30
  53. package/dist/sdk.d.ts +3 -3
  54. package/dist/sdk.d.ts.map +1 -1
  55. package/dist/sdk.js +4 -1
  56. package/dist/types/sdk.d.ts +5 -1
  57. package/dist/types/sdk.d.ts.map +1 -1
  58. package/dist/types/telemetry-events.d.ts +76 -0
  59. package/dist/types/telemetry-events.d.ts.map +1 -0
  60. package/dist/types/telemetry-events.js +8 -0
  61. package/package.json +1 -1
  62. package/src/constants.ts +6 -0
  63. package/src/index.ts +24 -0
  64. package/src/plugins/api/index.ts +1 -5
  65. package/src/plugins/eventEmission/builders.ts +115 -0
  66. package/src/plugins/eventEmission/index.test.ts +169 -0
  67. package/src/plugins/eventEmission/index.ts +294 -0
  68. package/src/plugins/eventEmission/transport.test.ts +214 -0
  69. package/src/plugins/eventEmission/transport.ts +135 -0
  70. package/src/plugins/eventEmission/types.ts +58 -0
  71. package/src/plugins/eventEmission/utils.ts +121 -0
  72. package/src/plugins/listActions/index.test.ts +3 -1
  73. package/src/plugins/listAuthentications/index.test.ts +3 -1
  74. package/src/plugins/manifest/index.test.ts +4 -4
  75. package/src/plugins/manifest/index.ts +39 -14
  76. package/src/resolvers/actionType.ts +4 -3
  77. package/src/resolvers/authenticationId.ts +2 -1
  78. package/src/sdk.ts +5 -1
  79. package/src/types/sdk.ts +7 -1
  80. package/src/types/telemetry-events.ts +85 -0
  81. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Simple utility functions for event emission
3
+ * These are pure functions that can be used to populate common event fields
4
+ */
5
+
6
+ import * as os from "os";
7
+
8
+ /**
9
+ * Generate a unique event ID
10
+ */
11
+ export function generateEventId(): string {
12
+ return crypto.randomUUID();
13
+ }
14
+
15
+ /**
16
+ * Get current timestamp in milliseconds since epoch
17
+ */
18
+ export function getCurrentTimestamp(): number {
19
+ return Date.now();
20
+ }
21
+
22
+ /**
23
+ * Get release ID (git SHA) - in production this would come from build process
24
+ */
25
+ export function getReleaseId(): string {
26
+ return process?.env?.SDK_RELEASE_ID || "development";
27
+ }
28
+
29
+ /**
30
+ * Get operating system information
31
+ */
32
+ export function getOsInfo(): {
33
+ platform: string | null;
34
+ release: string | null;
35
+ architecture: string | null;
36
+ } {
37
+ try {
38
+ return {
39
+ platform: os.platform() || null,
40
+ release: os.release() || null,
41
+ architecture: os.arch() || null,
42
+ };
43
+ } catch {
44
+ // Browser environment - os module not available
45
+ return {
46
+ platform: null,
47
+ release: null,
48
+ architecture: null,
49
+ };
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Get platform versions (Node.js, npm, etc.)
55
+ */
56
+ export function getPlatformVersions(): Record<string, string | null> {
57
+ const versions: Record<string, string | null> = {};
58
+ if (typeof process?.versions === "object") {
59
+ for (const [key, value] of Object.entries(process.versions)) {
60
+ versions[key] = value || null;
61
+ }
62
+ }
63
+ return versions;
64
+ }
65
+
66
+ /**
67
+ * Check if running in CI environment
68
+ */
69
+ export function isCi(): boolean {
70
+ return !!(
71
+ process?.env?.CI ||
72
+ process?.env?.CONTINUOUS_INTEGRATION ||
73
+ process?.env?.GITHUB_ACTIONS ||
74
+ process?.env?.JENKINS_URL ||
75
+ process?.env?.GITLAB_CI ||
76
+ process?.env?.CIRCLECI ||
77
+ process?.env?.TRAVIS ||
78
+ process?.env?.BUILDKITE ||
79
+ process?.env?.DRONE ||
80
+ process?.env?.BITBUCKET_PIPELINES_UUID
81
+ );
82
+ }
83
+
84
+ /**
85
+ * Get CI platform name if running in CI
86
+ */
87
+ export function getCiPlatform(): string | null {
88
+ if (process?.env?.GITHUB_ACTIONS) return "github-actions";
89
+ if (process?.env?.JENKINS_URL) return "jenkins";
90
+ if (process?.env?.GITLAB_CI) return "gitlab-ci";
91
+ if (process?.env?.CIRCLECI) return "circleci";
92
+ if (process?.env?.TRAVIS) return "travis";
93
+ if (process?.env?.BUILDKITE) return "buildkite";
94
+ if (process?.env?.DRONE) return "drone";
95
+ if (process?.env?.BITBUCKET_PIPELINES_UUID) return "bitbucket-pipelines";
96
+ if (process?.env?.CI || process?.env?.CONTINUOUS_INTEGRATION)
97
+ return "unknown-ci";
98
+ return null;
99
+ }
100
+
101
+ /**
102
+ * Get memory usage in bytes
103
+ */
104
+ export function getMemoryUsage(): number | null {
105
+ if (process?.memoryUsage) {
106
+ const usage = process.memoryUsage();
107
+ return usage.rss || null; // Resident Set Size
108
+ }
109
+ return null;
110
+ }
111
+
112
+ /**
113
+ * Get CPU time in milliseconds
114
+ */
115
+ export function getCpuTime(): number | null {
116
+ if (process?.cpuUsage) {
117
+ const usage = process.cpuUsage();
118
+ return Math.round((usage.user + usage.system) / 1000); // Convert to milliseconds
119
+ }
120
+ return null;
121
+ }
@@ -85,7 +85,9 @@ describe("listActions plugin", () => {
85
85
  manifest: null,
86
86
  getVersionedImplementationId: mockGetVersionedImplementationId,
87
87
  resolveAppKeys: mockResolveAppKeys,
88
- updateManifestEntry: vi.fn().mockResolvedValue(["test-key", {}]),
88
+ updateManifestEntry: vi
89
+ .fn()
90
+ .mockResolvedValue(["test-key", {}, { apps: {} }]),
89
91
  },
90
92
  }))
91
93
  .addPlugin(listAppsPlugin)
@@ -69,7 +69,9 @@ describe("listAuthentications plugin", () => {
69
69
  manifest: null,
70
70
  getVersionedImplementationId: mockGetVersionedImplementationId,
71
71
  resolveAppKeys: mockResolveAppKeys,
72
- updateManifestEntry: vi.fn().mockResolvedValue(["test-key", {}]),
72
+ updateManifestEntry: vi
73
+ .fn()
74
+ .mockResolvedValue(["test-key", {}, { apps: {} }]),
73
75
  },
74
76
  });
75
77
 
@@ -491,10 +491,10 @@ describe("manifestPlugin", () => {
491
491
  const context = sdk.getContext();
492
492
 
493
493
  // This should now find the existing "SlackCLIAPI" entry
494
- const [manifestKey] = await context.updateManifestEntry(
495
- "slack", // Input is slug
496
- { implementationName: "SlackCLIAPI", version: "1.30.0" },
497
- );
494
+ const [manifestKey] = await context.updateManifestEntry({
495
+ appKey: "slack", // Input is slug
496
+ entry: { implementationName: "SlackCLIAPI", version: "1.30.0" },
497
+ });
498
498
 
499
499
  // Should return the existing key, not create a new one
500
500
  expect(manifestKey).toBe("SlackCLIAPI");
@@ -27,15 +27,21 @@ import { toArrayFromAsync } from "../../utils/array-utils";
27
27
 
28
28
  export type ManifestPluginOptions = z.infer<typeof ManifestPluginOptionsSchema>;
29
29
 
30
+ export interface UpdateManifestEntryOptions {
31
+ appKey: string;
32
+ entry: ManifestEntry;
33
+ configPath?: string;
34
+ skipWrite?: boolean;
35
+ manifest?: Manifest;
36
+ }
37
+
30
38
  export interface ManifestPluginProvides {
31
39
  context: {
32
40
  getVersionedImplementationId: GetVersionedImplementationId;
33
41
  resolveAppKeys: ResolveAppKeys;
34
42
  updateManifestEntry: (
35
- appKey: string,
36
- entry: ManifestEntry,
37
- configPath?: string,
38
- ) => Promise<[string, ManifestEntry]>;
43
+ options: UpdateManifestEntryOptions,
44
+ ) => Promise<[string, ManifestEntry, Manifest]>;
39
45
  };
40
46
  }
41
47
 
@@ -319,6 +325,7 @@ async function resolveAppKeys({
319
325
  }
320
326
 
321
327
  export { DEFAULT_CONFIG_PATH } from "./schemas";
328
+ export type { ManifestEntry, Manifest } from "./schemas";
322
329
 
323
330
  export const manifestPlugin: Plugin<
324
331
  {}, // no SDK dependencies
@@ -365,11 +372,19 @@ export const manifestPlugin: Plugin<
365
372
  };
366
373
 
367
374
  const updateManifestEntry = async (
368
- appKey: string,
369
- entry: ManifestEntry,
370
- configPath: string = DEFAULT_CONFIG_PATH,
371
- ): Promise<[string, ManifestEntry]> => {
372
- const manifest = (await readManifestFromFile(configPath)) || { apps: {} };
375
+ options: UpdateManifestEntryOptions,
376
+ ): Promise<[string, ManifestEntry, Manifest]> => {
377
+ const {
378
+ appKey,
379
+ entry,
380
+ configPath = DEFAULT_CONFIG_PATH,
381
+ skipWrite = false,
382
+ manifest: inputManifest,
383
+ } = options;
384
+
385
+ // Use provided manifest or read from file
386
+ const manifest = inputManifest ||
387
+ (await readManifestFromFile(configPath)) || { apps: {} };
373
388
 
374
389
  // Try to find existing entry by direct key first
375
390
  let existingEntry = findManifestEntry({
@@ -412,13 +427,23 @@ export const manifestPlugin: Plugin<
412
427
  });
413
428
  }
414
429
 
415
- manifest.apps[manifestKey] = entry;
416
- await writeManifestToFile(manifest, configPath);
430
+ const updatedManifest = {
431
+ ...manifest,
432
+ apps: {
433
+ ...manifest.apps,
434
+ [manifestKey]: entry,
435
+ },
436
+ };
437
+
438
+ // Conditionally write to file
439
+ if (!skipWrite) {
440
+ await writeManifestToFile(updatedManifest, configPath);
417
441
 
418
- // Clear the cached manifest so it gets reloaded with the new data
419
- resolvedManifest = undefined;
442
+ // Clear the cached manifest so it gets reloaded with the new data
443
+ resolvedManifest = undefined;
444
+ }
420
445
 
421
- return [manifestKey, entry];
446
+ return [manifestKey, entry, updatedManifest];
422
447
  };
423
448
 
424
449
  return {
@@ -16,9 +16,10 @@ export const actionTypeResolver: DynamicResolver<
16
16
  appKey: resolvedParams.appKey,
17
17
  });
18
18
 
19
- const types = [
20
- ...new Set(actionsResponse.data.map((action) => action.action_type)),
21
- ];
19
+ const actionTypes = actionsResponse.data.map(
20
+ (action) => action.action_type,
21
+ );
22
+ const types: string[] = [...new Set(actionTypes)];
22
23
  return types.map((type) => ({ key: type, name: type }));
23
24
  },
24
25
  prompt: (types) => ({
@@ -23,7 +23,8 @@ export const authenticationIdResolver: AuthenticationIdResolver = {
23
23
 
24
24
  // Filter out myAuths from allAuths
25
25
  const otherAuths = allAuths.data.filter(
26
- (auth) => !myAuths.data.some((myAuth) => myAuth.id === auth.id),
26
+ (auth: { id: number }) =>
27
+ !myAuths.data.some((myAuth: { id: number }) => myAuth.id === auth.id),
27
28
  );
28
29
  return [...myAuths.data, ...otherAuths];
29
30
  },
package/src/sdk.ts CHANGED
@@ -33,6 +33,7 @@ import { listInputFieldsPlugin } from "./plugins/listInputFields";
33
33
  import { listInputFieldChoicesPlugin } from "./plugins/listInputFieldChoices";
34
34
  import { requestPlugin } from "./plugins/request";
35
35
  import { manifestPlugin } from "./plugins/manifest";
36
+ import { eventEmissionPlugin } from "./plugins/eventEmission";
36
37
 
37
38
  // Full SDK interface with plugins applied
38
39
  // Note: ZapierSdk is now defined as ReturnType<typeof createZapierSdk> at the bottom of this file
@@ -130,10 +131,13 @@ export function createSdk<
130
131
  export function createZapierSdkWithoutRegistry(options: ZapierSdkOptions = {}) {
131
132
  return (
132
133
  createSdk(options)
134
+ // Event emission (must be first to be available to other plugins)
135
+ .addPlugin(eventEmissionPlugin)
136
+
133
137
  // Provides the API client in context
134
138
  .addPlugin(apiPlugin)
135
139
 
136
- // Manifest plugin (provides version locking context)
140
+ // Manifest plugin (provides version locking context) - must come after apiPlugin
137
141
  .addPlugin(manifestPlugin)
138
142
 
139
143
  // Apps/actions/fields
package/src/types/sdk.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  */
4
4
 
5
5
  import type { EventCallback } from "./events";
6
+ import type { EventEmissionConfig } from "../plugins/eventEmission";
6
7
 
7
8
  // SDK Configuration Types
8
9
  export interface BaseSdkOptions {
@@ -21,6 +22,7 @@ export interface BaseSdkOptions {
21
22
  };
22
23
  };
23
24
  };
25
+ eventEmission?: EventEmissionConfig;
24
26
  }
25
27
 
26
28
  // SDK interface composed from individual function interfaces
@@ -33,6 +35,8 @@ import type { RelayRequestSdkFunction } from "../plugins/request/schemas";
33
35
  import type { z } from "zod";
34
36
  import type { RegistryPluginProvides } from "../plugins/registry";
35
37
  import type { GetProfilePluginProvides } from "../plugins/getProfile";
38
+ import type { EventEmissionProvides } from "../plugins/eventEmission";
39
+ import type { ApiPluginProvides } from "../plugins/api";
36
40
  import type { AppsPluginProvides, ZapierSdkApps } from "../plugins/apps";
37
41
  import type { ActionProxy } from "../plugins/apps/schemas";
38
42
  import type { FetchPluginProvides } from "../plugins/fetch";
@@ -104,7 +108,9 @@ export interface ZapierSdk
104
108
  ListInputFieldsPluginProvides &
105
109
  ListInputFieldChoicesPluginProvides &
106
110
  RequestPluginProvides &
107
- GetProfilePluginProvides
111
+ GetProfilePluginProvides &
112
+ EventEmissionProvides &
113
+ ApiPluginProvides
108
114
  > {
109
115
  // Override apps property to intersect ActionProxy with our typed apps
110
116
  apps: ActionProxy & ZapierSdkApps;
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Event emission types matching Avro schemas for SDK telemetry
3
+ *
4
+ * These interfaces correspond to the Avro schemas:
5
+ * - application_lifecycle_event.avsc
6
+ * - error_occurred_event.avsc
7
+ */
8
+
9
+ // Common fields present in all events
10
+ export interface BaseEvent {
11
+ event_id: string;
12
+ timestamp_ms: number;
13
+ release_id: string;
14
+ customuser_id?: number | null;
15
+ account_id?: number | null;
16
+ identity_id?: number | null;
17
+ visitor_id?: string | null;
18
+ correlation_id?: string | null;
19
+ }
20
+
21
+ // Error Event - tracks SDK errors and exceptions
22
+ export interface ErrorOccurredEvent extends BaseEvent {
23
+ zap_id?: number | null;
24
+ node_id?: number | null;
25
+ selected_api?: string | null;
26
+ app_id?: number | null;
27
+ app_version_id?: number | null;
28
+ error_message: string;
29
+ error_type?: string | null;
30
+ error_status_code?: number | null;
31
+ error_stack_trace?: string | null;
32
+ error_source_method?: string | null;
33
+ error_source_file?: string | null;
34
+ error_line_number?: number | null;
35
+ operation_type?: string | null;
36
+ operation_key?: string | null;
37
+ error_severity?: string | null;
38
+ is_user_facing: boolean;
39
+ is_recoverable?: boolean | null;
40
+ sdk_version?: string | null;
41
+ error_metadata?: Record<string, string | null> | null;
42
+ parent_error_id?: string | null;
43
+ error_occurred_timestamp_ms?: number | null;
44
+ error_code?: string | null;
45
+ recovery_attempted?: boolean | null;
46
+ recovery_action?: string | null;
47
+ recovery_successful?: boolean | null;
48
+ environment?: string | null;
49
+ execution_time_before_error_ms?: number | null;
50
+ }
51
+
52
+ // Application Lifecycle Event - tracks SDK application lifecycle transitions
53
+ export interface ApplicationLifecycleEvent extends BaseEvent {
54
+ lifecycle_event_type: "startup" | "exit" | "signal_termination";
55
+ selected_api?: string | null;
56
+ app_id?: number | null;
57
+ app_version_id?: number | null;
58
+ sdk_version?: string | null;
59
+ cli_version?: string | null;
60
+ exit_code?: number | null;
61
+ signal_name?: string | null;
62
+ uptime_ms?: number | null;
63
+ memory_usage_bytes?: number | null;
64
+ peak_memory_usage_bytes?: number | null;
65
+ cpu_time_ms?: number | null;
66
+ os_platform?: string | null;
67
+ os_release?: string | null;
68
+ os_architecture?: string | null;
69
+ platform_versions?: Record<string, string | null> | null;
70
+ environment?: string | null;
71
+ is_ci_environment?: boolean | null;
72
+ ci_platform?: string | null;
73
+ session_id?: string | null;
74
+ metadata?: Record<string, string | null> | null;
75
+ process_argv?: (string | null)[] | null;
76
+ is_graceful_shutdown?: boolean | null;
77
+ shutdown_duration_ms?: number | null;
78
+ active_requests_count?: number | null;
79
+ }
80
+
81
+ // Union type for all telemetry events
82
+ export type TelemetryEvent = ErrorOccurredEvent | ApplicationLifecycleEvent;
83
+
84
+ // Event type discriminator
85
+ export type TelemetryEventType = "error_occurred" | "application_lifecycle";