@zapier/zapier-sdk 0.13.2 → 0.13.4

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 (86) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/api/debug.d.ts.map +1 -1
  3. package/dist/api/debug.js +36 -1
  4. package/dist/api/schemas.d.ts +174 -174
  5. package/dist/constants.d.ts +4 -0
  6. package/dist/constants.d.ts.map +1 -1
  7. package/dist/constants.js +4 -0
  8. package/dist/index.cjs +475 -9
  9. package/dist/index.d.mts +381 -157
  10. package/dist/index.d.ts +3 -0
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +1 -0
  13. package/dist/index.mjs +446 -13
  14. package/dist/plugins/api/index.d.ts +1 -3
  15. package/dist/plugins/api/index.d.ts.map +1 -1
  16. package/dist/plugins/eventEmission/builders.d.ts +13 -0
  17. package/dist/plugins/eventEmission/builders.d.ts.map +1 -0
  18. package/dist/plugins/eventEmission/builders.js +78 -0
  19. package/dist/plugins/eventEmission/index.d.ts +34 -0
  20. package/dist/plugins/eventEmission/index.d.ts.map +1 -0
  21. package/dist/plugins/eventEmission/index.js +216 -0
  22. package/dist/plugins/eventEmission/index.test.d.ts +5 -0
  23. package/dist/plugins/eventEmission/index.test.d.ts.map +1 -0
  24. package/dist/plugins/eventEmission/index.test.js +143 -0
  25. package/dist/plugins/eventEmission/transport.d.ts +37 -0
  26. package/dist/plugins/eventEmission/transport.d.ts.map +1 -0
  27. package/dist/plugins/eventEmission/transport.js +96 -0
  28. package/dist/plugins/eventEmission/transport.test.d.ts +5 -0
  29. package/dist/plugins/eventEmission/transport.test.d.ts.map +1 -0
  30. package/dist/plugins/eventEmission/transport.test.js +153 -0
  31. package/dist/plugins/eventEmission/types.d.ts +53 -0
  32. package/dist/plugins/eventEmission/types.d.ts.map +1 -0
  33. package/dist/plugins/eventEmission/types.js +1 -0
  34. package/dist/plugins/eventEmission/utils.d.ts +45 -0
  35. package/dist/plugins/eventEmission/utils.d.ts.map +1 -0
  36. package/dist/plugins/eventEmission/utils.js +114 -0
  37. package/dist/plugins/fetch/schemas.d.ts +4 -4
  38. package/dist/plugins/getAction/index.d.ts.map +1 -1
  39. package/dist/plugins/getAction/index.js +3 -2
  40. package/dist/plugins/getAction/schemas.d.ts +2 -2
  41. package/dist/plugins/listActions/schemas.d.ts +2 -2
  42. package/dist/plugins/listInputFieldChoices/schemas.d.ts +4 -4
  43. package/dist/plugins/listInputFields/index.d.ts +2 -1
  44. package/dist/plugins/listInputFields/index.d.ts.map +1 -1
  45. package/dist/plugins/listInputFields/index.js +7 -2
  46. package/dist/plugins/listInputFields/index.test.js +36 -5
  47. package/dist/plugins/listInputFields/schemas.d.ts +2 -2
  48. package/dist/plugins/request/schemas.d.ts +4 -4
  49. package/dist/plugins/runAction/index.d.ts.map +1 -1
  50. package/dist/plugins/runAction/index.js +6 -1
  51. package/dist/plugins/runAction/schemas.d.ts +2 -2
  52. package/dist/resolvers/actionType.d.ts.map +1 -1
  53. package/dist/resolvers/actionType.js +2 -3
  54. package/dist/resolvers/authenticationId.d.ts.map +1 -1
  55. package/dist/schemas/Action.d.ts +2 -2
  56. package/dist/schemas/App.d.ts +30 -30
  57. package/dist/sdk.d.ts +2 -2
  58. package/dist/sdk.d.ts.map +1 -1
  59. package/dist/sdk.js +4 -1
  60. package/dist/types/sdk.d.ts +5 -1
  61. package/dist/types/sdk.d.ts.map +1 -1
  62. package/dist/types/telemetry-events.d.ts +76 -0
  63. package/dist/types/telemetry-events.d.ts.map +1 -0
  64. package/dist/types/telemetry-events.js +8 -0
  65. package/package.json +1 -1
  66. package/src/api/debug.ts +44 -1
  67. package/src/constants.ts +6 -0
  68. package/src/index.ts +24 -0
  69. package/src/plugins/api/index.ts +1 -5
  70. package/src/plugins/eventEmission/builders.ts +115 -0
  71. package/src/plugins/eventEmission/index.test.ts +169 -0
  72. package/src/plugins/eventEmission/index.ts +294 -0
  73. package/src/plugins/eventEmission/transport.test.ts +214 -0
  74. package/src/plugins/eventEmission/transport.ts +135 -0
  75. package/src/plugins/eventEmission/types.ts +58 -0
  76. package/src/plugins/eventEmission/utils.ts +121 -0
  77. package/src/plugins/getAction/index.ts +5 -2
  78. package/src/plugins/listInputFields/index.test.ts +37 -5
  79. package/src/plugins/listInputFields/index.ts +10 -3
  80. package/src/plugins/runAction/index.ts +9 -0
  81. package/src/resolvers/actionType.ts +4 -3
  82. package/src/resolvers/authenticationId.ts +2 -1
  83. package/src/sdk.ts +5 -1
  84. package/src/types/sdk.ts +7 -1
  85. package/src/types/telemetry-events.ts +85 -0
  86. package/tsconfig.tsbuildinfo +1 -1
@@ -7,4 +7,8 @@
7
7
  * Maximum number of items that can be requested per page across all paginated functions
8
8
  */
9
9
  export declare const MAX_PAGE_LIMIT = 10000;
10
+ /**
11
+ * Default telemetry endpoint for the Tracking API
12
+ */
13
+ export declare const TRACKING_API_ENDPOINT = "https://zapier.com/api/v4/tracking/event/";
10
14
  //# sourceMappingURL=constants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,eAAO,MAAM,cAAc,QAAQ,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,eAAO,MAAM,cAAc,QAAQ,CAAC;AAEpC;;GAEG;AACH,eAAO,MAAM,qBAAqB,8CACW,CAAC"}
package/dist/constants.js CHANGED
@@ -7,3 +7,7 @@
7
7
  * Maximum number of items that can be requested per page across all paginated functions
8
8
  */
9
9
  export const MAX_PAGE_LIMIT = 10000;
10
+ /**
11
+ * Default telemetry endpoint for the Tracking API
12
+ */
13
+ export const TRACKING_API_ENDPOINT = "https://zapier.com/api/v4/tracking/event/";
package/dist/index.cjs CHANGED
@@ -2,6 +2,27 @@
2
2
 
3
3
  var zod = require('zod');
4
4
  var promises = require('timers/promises');
5
+ var os = require('os');
6
+
7
+ function _interopNamespace(e) {
8
+ if (e && e.__esModule) return e;
9
+ var n = Object.create(null);
10
+ if (e) {
11
+ Object.keys(e).forEach(function (k) {
12
+ if (k !== 'default') {
13
+ var d = Object.getOwnPropertyDescriptor(e, k);
14
+ Object.defineProperty(n, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: function () { return e[k]; }
17
+ });
18
+ }
19
+ });
20
+ }
21
+ n.default = e;
22
+ return Object.freeze(n);
23
+ }
24
+
25
+ var os__namespace = /*#__PURE__*/_interopNamespace(os);
5
26
 
6
27
  // src/types/properties.ts
7
28
  function withFormatter(schema, formatMeta) {
@@ -34,6 +55,7 @@ function isPositional(schema) {
34
55
 
35
56
  // src/constants.ts
36
57
  var MAX_PAGE_LIMIT = 1e4;
58
+ var TRACKING_API_ENDPOINT = "https://zapier.com/api/v4/tracking/event/";
37
59
 
38
60
  // src/types/properties.ts
39
61
  var AppKeyPropertySchema = withPositional(
@@ -370,7 +392,7 @@ var FetchInitSchema = zod.z.object({
370
392
  // src/plugins/fetch/index.ts
371
393
  var fetchPlugin = ({ sdk }) => {
372
394
  return {
373
- fetch: async function fetch(url, init) {
395
+ fetch: async function fetch2(url, init) {
374
396
  const {
375
397
  authenticationId,
376
398
  callbackUrl,
@@ -1409,9 +1431,10 @@ var actionTypeResolver = {
1409
1431
  const actionsResponse = await sdk.listActions({
1410
1432
  appKey: resolvedParams.appKey
1411
1433
  });
1412
- const types = [
1413
- ...new Set(actionsResponse.data.map((action) => action.action_type))
1414
- ];
1434
+ const actionTypes = actionsResponse.data.map(
1435
+ (action) => action.action_type
1436
+ );
1437
+ const types = [...new Set(actionTypes)];
1415
1438
  return types.map((type) => ({ key: type, name: type }));
1416
1439
  },
1417
1440
  prompt: (types) => ({
@@ -1888,7 +1911,7 @@ function transformNeedsToFields(needs) {
1888
1911
  }
1889
1912
  return rootFields;
1890
1913
  }
1891
- var listInputFieldsPlugin = ({ context }) => {
1914
+ var listInputFieldsPlugin = ({ sdk, context }) => {
1892
1915
  const listInputFields = createPaginatedFunction(
1893
1916
  async function listInputFieldsPage(options) {
1894
1917
  const { api, getVersionedImplementationId } = context;
@@ -1900,9 +1923,14 @@ var listInputFieldsPlugin = ({ context }) => {
1900
1923
  { configType: "current_implementation_id" }
1901
1924
  );
1902
1925
  }
1926
+ const { data: action } = await sdk.getAction({
1927
+ appKey,
1928
+ actionType,
1929
+ actionKey
1930
+ });
1903
1931
  const needsRequest = {
1904
1932
  selected_api: selectedApi,
1905
- action: actionKey,
1933
+ action: action.key,
1906
1934
  type_of: actionType,
1907
1935
  params: inputs || {}
1908
1936
  };
@@ -2143,7 +2171,7 @@ var getActionPlugin = ({ sdk }) => {
2143
2171
  const { actionKey, actionType, appKey } = options;
2144
2172
  const actionsResult = await sdk.listActions({ appKey });
2145
2173
  for (const action of actionsResult.data) {
2146
- if (action.key === actionKey && action.action_type === actionType) {
2174
+ if ((action.key === actionKey || action.id === actionKey) && action.action_type === actionType) {
2147
2175
  return { data: action };
2148
2176
  }
2149
2177
  }
@@ -2348,6 +2376,7 @@ async function executeAction(actionOptions) {
2348
2376
  api,
2349
2377
  context,
2350
2378
  appKey,
2379
+ actionId,
2351
2380
  actionKey,
2352
2381
  actionType,
2353
2382
  executionOptions,
@@ -2362,6 +2391,7 @@ async function executeAction(actionOptions) {
2362
2391
  }
2363
2392
  const runRequestData = {
2364
2393
  selected_api: selectedApi,
2394
+ action_id: actionId,
2365
2395
  action_key: actionKey,
2366
2396
  action_type: actionType,
2367
2397
  inputs: executionOptions.inputs || {}
@@ -2403,10 +2433,14 @@ var runActionPlugin = ({ sdk, context }) => {
2403
2433
  `Action type mismatch: expected ${actionType}, got ${actionData.data.action_type}`
2404
2434
  );
2405
2435
  }
2436
+ const actionId = actionData.data.id;
2406
2437
  const result = await executeAction({
2407
2438
  api,
2408
2439
  context,
2409
2440
  appKey,
2441
+ // Some actions require the action ID to run them, but technically the ID is not guaranteed to be available when
2442
+ // we retrieve actions (probably legacy reasons), so we just pass along all the things!
2443
+ actionId,
2410
2444
  actionKey,
2411
2445
  actionType,
2412
2446
  executionOptions: { inputs },
@@ -2951,13 +2985,37 @@ function getAuthorizationHeader(token) {
2951
2985
  }
2952
2986
 
2953
2987
  // src/api/debug.ts
2988
+ var utilModule = null;
2989
+ var utilPromise = null;
2954
2990
  function createDebugLogger(enabled) {
2955
2991
  if (!enabled) {
2956
2992
  return () => {
2957
2993
  };
2958
2994
  }
2995
+ if (!utilPromise) {
2996
+ utilPromise = import('util').then((util) => {
2997
+ utilModule = util;
2998
+ return util;
2999
+ }).catch(() => {
3000
+ utilModule = null;
3001
+ return null;
3002
+ });
3003
+ }
2959
3004
  return (message, data) => {
2960
- console.log(`[Zapier SDK] ${message}`, data || "");
3005
+ if (data === void 0 || data === "") {
3006
+ console.log(`[Zapier SDK] ${message}`);
3007
+ return;
3008
+ }
3009
+ if (utilModule) {
3010
+ const formatted = utilModule.inspect(data, {
3011
+ colors: true,
3012
+ depth: null,
3013
+ breakLength: 80
3014
+ });
3015
+ console.log(`[Zapier SDK] ${message}`, formatted);
3016
+ } else {
3017
+ console.log(`[Zapier SDK] ${message}`, data);
3018
+ }
2961
3019
  };
2962
3020
  }
2963
3021
  function censorHeaders(headers) {
@@ -3776,6 +3834,401 @@ var listInputFieldChoicesPlugin = ({ context, sdk }) => {
3776
3834
  };
3777
3835
  };
3778
3836
 
3837
+ // src/plugins/eventEmission/transport.ts
3838
+ var DEFAULT_RETRY_ATTEMPTS = 2;
3839
+ var DEFAULT_RETRY_DELAY_MS = 300;
3840
+ var HttpTransport = class {
3841
+ constructor(config) {
3842
+ this.config = config;
3843
+ }
3844
+ async emit(subject, event) {
3845
+ try {
3846
+ await this.emitWithRetry(
3847
+ subject,
3848
+ event,
3849
+ this.config.retryAttempts || DEFAULT_RETRY_ATTEMPTS
3850
+ );
3851
+ } catch {
3852
+ }
3853
+ }
3854
+ async emitWithRetry(subject, event, attemptsLeft) {
3855
+ try {
3856
+ const payload = {
3857
+ subject,
3858
+ properties: event
3859
+ };
3860
+ const response = await fetch(this.config.endpoint, {
3861
+ method: "POST",
3862
+ headers: {
3863
+ "Content-Type": "application/json",
3864
+ ...this.config.headers
3865
+ },
3866
+ body: JSON.stringify(payload)
3867
+ });
3868
+ if (!response.ok && attemptsLeft > 1) {
3869
+ await this.delay(this.config.retryDelayMs || DEFAULT_RETRY_DELAY_MS);
3870
+ return this.emitWithRetry(subject, event, attemptsLeft - 1);
3871
+ }
3872
+ } catch (error) {
3873
+ if (attemptsLeft > 1) {
3874
+ await this.delay(this.config.retryDelayMs || DEFAULT_RETRY_DELAY_MS);
3875
+ return this.emitWithRetry(subject, event, attemptsLeft - 1);
3876
+ }
3877
+ throw error;
3878
+ }
3879
+ }
3880
+ async delay(ms) {
3881
+ return new Promise((resolve2) => setTimeout(resolve2, ms));
3882
+ }
3883
+ };
3884
+ var ConsoleTransport = class {
3885
+ async emit(subject, event) {
3886
+ try {
3887
+ console.log(
3888
+ "[SDK Telemetry]",
3889
+ JSON.stringify({ subject, properties: event }, null, 2)
3890
+ );
3891
+ } catch {
3892
+ }
3893
+ }
3894
+ };
3895
+ var NoopTransport = class {
3896
+ async emit(_subject, _event) {
3897
+ }
3898
+ };
3899
+ function createTransport(config) {
3900
+ try {
3901
+ switch (config.type) {
3902
+ case "http":
3903
+ if (!config.endpoint) {
3904
+ throw new Error("HTTP transport requires endpoint");
3905
+ }
3906
+ return new HttpTransport({
3907
+ endpoint: config.endpoint,
3908
+ headers: config.headers,
3909
+ retryAttempts: config.retryAttempts,
3910
+ retryDelayMs: config.retryDelayMs
3911
+ });
3912
+ case "console":
3913
+ return new ConsoleTransport();
3914
+ case "noop":
3915
+ default:
3916
+ return new NoopTransport();
3917
+ }
3918
+ } catch {
3919
+ return new NoopTransport();
3920
+ }
3921
+ }
3922
+ function generateEventId() {
3923
+ return crypto.randomUUID();
3924
+ }
3925
+ function getCurrentTimestamp() {
3926
+ return Date.now();
3927
+ }
3928
+ function getReleaseId() {
3929
+ return process?.env?.SDK_RELEASE_ID || "development";
3930
+ }
3931
+ function getOsInfo() {
3932
+ try {
3933
+ return {
3934
+ platform: os__namespace.platform() || null,
3935
+ release: os__namespace.release() || null,
3936
+ architecture: os__namespace.arch() || null
3937
+ };
3938
+ } catch {
3939
+ return {
3940
+ platform: null,
3941
+ release: null,
3942
+ architecture: null
3943
+ };
3944
+ }
3945
+ }
3946
+ function getPlatformVersions() {
3947
+ const versions = {};
3948
+ if (typeof process?.versions === "object") {
3949
+ for (const [key, value] of Object.entries(process.versions)) {
3950
+ versions[key] = value || null;
3951
+ }
3952
+ }
3953
+ return versions;
3954
+ }
3955
+ function isCi() {
3956
+ return !!(process?.env?.CI || process?.env?.CONTINUOUS_INTEGRATION || process?.env?.GITHUB_ACTIONS || process?.env?.JENKINS_URL || process?.env?.GITLAB_CI || process?.env?.CIRCLECI || process?.env?.TRAVIS || process?.env?.BUILDKITE || process?.env?.DRONE || process?.env?.BITBUCKET_PIPELINES_UUID);
3957
+ }
3958
+ function getCiPlatform() {
3959
+ if (process?.env?.GITHUB_ACTIONS) return "github-actions";
3960
+ if (process?.env?.JENKINS_URL) return "jenkins";
3961
+ if (process?.env?.GITLAB_CI) return "gitlab-ci";
3962
+ if (process?.env?.CIRCLECI) return "circleci";
3963
+ if (process?.env?.TRAVIS) return "travis";
3964
+ if (process?.env?.BUILDKITE) return "buildkite";
3965
+ if (process?.env?.DRONE) return "drone";
3966
+ if (process?.env?.BITBUCKET_PIPELINES_UUID) return "bitbucket-pipelines";
3967
+ if (process?.env?.CI || process?.env?.CONTINUOUS_INTEGRATION)
3968
+ return "unknown-ci";
3969
+ return null;
3970
+ }
3971
+ function getMemoryUsage() {
3972
+ if (process?.memoryUsage) {
3973
+ const usage = process.memoryUsage();
3974
+ return usage.rss || null;
3975
+ }
3976
+ return null;
3977
+ }
3978
+ function getCpuTime() {
3979
+ if (process?.cpuUsage) {
3980
+ const usage = process.cpuUsage();
3981
+ return Math.round((usage.user + usage.system) / 1e3);
3982
+ }
3983
+ return null;
3984
+ }
3985
+
3986
+ // package.json
3987
+ var package_default = {
3988
+ version: "0.13.4"};
3989
+
3990
+ // src/plugins/eventEmission/builders.ts
3991
+ function createBaseEvent(context = {}) {
3992
+ return {
3993
+ event_id: generateEventId(),
3994
+ timestamp_ms: getCurrentTimestamp(),
3995
+ release_id: getReleaseId(),
3996
+ customuser_id: context.customuser_id,
3997
+ account_id: context.account_id,
3998
+ identity_id: context.identity_id,
3999
+ visitor_id: context.visitor_id,
4000
+ correlation_id: context.correlation_id
4001
+ };
4002
+ }
4003
+ function buildErrorEvent(data, context = {}) {
4004
+ return {
4005
+ ...createBaseEvent(context),
4006
+ zap_id: context.zap_id,
4007
+ node_id: context.node_id,
4008
+ selected_api: context.selected_api,
4009
+ app_id: context.app_id,
4010
+ app_version_id: context.app_version_id,
4011
+ environment: context.environment,
4012
+ sdk_version: package_default.version,
4013
+ ...data
4014
+ };
4015
+ }
4016
+ function buildApplicationLifecycleEvent(data, context = {}) {
4017
+ const osInfo = getOsInfo();
4018
+ const platformVersions = getPlatformVersions();
4019
+ return {
4020
+ ...createBaseEvent(context),
4021
+ selected_api: context.selected_api,
4022
+ app_id: context.app_id,
4023
+ app_version_id: context.app_version_id,
4024
+ sdk_version: package_default.version,
4025
+ cli_version: null,
4026
+ memory_usage_bytes: data.memory_usage_bytes ?? getMemoryUsage(),
4027
+ peak_memory_usage_bytes: data.peak_memory_usage_bytes ?? getMemoryUsage(),
4028
+ cpu_time_ms: data.cpu_time_ms ?? getCpuTime(),
4029
+ os_platform: osInfo.platform,
4030
+ os_release: osInfo.release,
4031
+ os_architecture: osInfo.architecture,
4032
+ platform_versions: platformVersions,
4033
+ environment: context.environment ?? (process.env.NODE_ENV || null),
4034
+ is_ci_environment: isCi(),
4035
+ ci_platform: getCiPlatform(),
4036
+ session_id: null,
4037
+ metadata: null,
4038
+ process_argv: process.argv || null,
4039
+ ...data
4040
+ };
4041
+ }
4042
+ function buildErrorEventWithContext(data, context = {}) {
4043
+ const executionTime = data.execution_start_time ? Date.now() - data.execution_start_time : null;
4044
+ return {
4045
+ ...createBaseEvent(context),
4046
+ zap_id: context.zap_id,
4047
+ node_id: context.node_id,
4048
+ selected_api: context.selected_api,
4049
+ app_id: context.app_id,
4050
+ app_version_id: context.app_version_id,
4051
+ environment: context.environment ?? (process.env.NODE_ENV || null),
4052
+ sdk_version: package_default.version,
4053
+ execution_time_before_error_ms: executionTime,
4054
+ ...data
4055
+ };
4056
+ }
4057
+
4058
+ // src/plugins/eventEmission/index.ts
4059
+ var APPLICATION_LIFECYCLE_EVENT_SUBJECT = "platform.sdk.ApplicationLifecycleEvent";
4060
+ var ERROR_OCCURRED_EVENT_SUBJECT = "platform.sdk.ErrorOccurredEvent";
4061
+ async function silentEmit(transport, subject, event) {
4062
+ try {
4063
+ transport.emit(subject, event).catch(() => {
4064
+ });
4065
+ } catch {
4066
+ }
4067
+ }
4068
+ function getTransportConfig() {
4069
+ const envTransport = process?.env?.ZAPIER_SDK_TELEMETRY_TRANSPORT;
4070
+ if (envTransport === "noop" || envTransport === "disabled") {
4071
+ return { type: "noop" };
4072
+ }
4073
+ if (envTransport === "console") {
4074
+ return { type: "console" };
4075
+ }
4076
+ const endpoint = process?.env?.ZAPIER_SDK_TELEMETRY_ENDPOINT || TRACKING_API_ENDPOINT;
4077
+ return {
4078
+ type: "http",
4079
+ endpoint
4080
+ };
4081
+ }
4082
+ var eventEmissionPlugin = ({ context }) => {
4083
+ const defaultTransport = getTransportConfig();
4084
+ const config = {
4085
+ enabled: context.options.eventEmission?.enabled ?? true,
4086
+ transport: (
4087
+ // If env var is set, use it (defaultTransport will be from env)
4088
+ process?.env?.ZAPIER_SDK_TELEMETRY_TRANSPORT ? defaultTransport : (
4089
+ // Otherwise, use option transport or default
4090
+ context.options.eventEmission?.transport ?? defaultTransport
4091
+ )
4092
+ )
4093
+ };
4094
+ const startupTime = Date.now();
4095
+ let shutdownStartTime = null;
4096
+ if (!config.enabled) {
4097
+ return {
4098
+ context: {
4099
+ eventEmission: {
4100
+ transport: createTransport({ type: "noop" }),
4101
+ config,
4102
+ emit: () => {
4103
+ },
4104
+ createBaseEvent: () => ({
4105
+ event_id: generateEventId(),
4106
+ timestamp_ms: getCurrentTimestamp(),
4107
+ release_id: getReleaseId(),
4108
+ customuser_id: null,
4109
+ account_id: null,
4110
+ identity_id: null,
4111
+ visitor_id: null,
4112
+ correlation_id: null
4113
+ })
4114
+ }
4115
+ }
4116
+ };
4117
+ }
4118
+ let transport;
4119
+ try {
4120
+ transport = createTransport(config.transport || { type: "noop" });
4121
+ } catch {
4122
+ transport = createTransport({ type: "noop" });
4123
+ }
4124
+ const createBaseEventHelper = () => ({
4125
+ event_id: generateEventId(),
4126
+ timestamp_ms: getCurrentTimestamp(),
4127
+ release_id: getReleaseId(),
4128
+ customuser_id: null,
4129
+ account_id: null,
4130
+ identity_id: null,
4131
+ visitor_id: null,
4132
+ correlation_id: null
4133
+ });
4134
+ if (config.enabled) {
4135
+ const startupEvent = buildApplicationLifecycleEvent({
4136
+ lifecycle_event_type: "startup"
4137
+ });
4138
+ silentEmit(transport, APPLICATION_LIFECYCLE_EVENT_SUBJECT, startupEvent);
4139
+ if (typeof process?.on === "function") {
4140
+ process.on("exit", (code) => {
4141
+ const uptime = Date.now() - startupTime;
4142
+ const shutdownDuration = shutdownStartTime ? Date.now() - shutdownStartTime : null;
4143
+ const exitEvent = buildApplicationLifecycleEvent({
4144
+ lifecycle_event_type: "exit",
4145
+ exit_code: code,
4146
+ uptime_ms: uptime,
4147
+ is_graceful_shutdown: code === 0,
4148
+ shutdown_duration_ms: shutdownDuration
4149
+ });
4150
+ silentEmit(transport, APPLICATION_LIFECYCLE_EVENT_SUBJECT, exitEvent);
4151
+ });
4152
+ process.on("uncaughtException", async (error) => {
4153
+ const errorEvent = buildErrorEventWithContext({
4154
+ error_message: error.message || "Unknown error",
4155
+ error_type: "UncaughtException",
4156
+ error_stack_trace: error.stack || null,
4157
+ error_severity: "critical",
4158
+ is_user_facing: false,
4159
+ is_recoverable: false,
4160
+ execution_start_time: startupTime
4161
+ });
4162
+ try {
4163
+ await Promise.race([
4164
+ transport.emit(ERROR_OCCURRED_EVENT_SUBJECT, errorEvent),
4165
+ new Promise((resolve2) => setTimeout(resolve2, 300))
4166
+ ]);
4167
+ } catch {
4168
+ }
4169
+ });
4170
+ process.on(
4171
+ "unhandledRejection",
4172
+ async (reason, promise) => {
4173
+ const errorMessage = reason instanceof Error ? reason.message : typeof reason === "string" ? reason : "Unhandled promise rejection";
4174
+ const errorStack = reason instanceof Error ? reason.stack : null;
4175
+ const errorEvent = buildErrorEventWithContext({
4176
+ error_message: errorMessage,
4177
+ error_type: "UnhandledRejection",
4178
+ error_stack_trace: errorStack,
4179
+ error_severity: "critical",
4180
+ is_user_facing: false,
4181
+ is_recoverable: false,
4182
+ execution_start_time: startupTime,
4183
+ error_metadata: {
4184
+ promise: String(promise)
4185
+ }
4186
+ });
4187
+ try {
4188
+ await Promise.race([
4189
+ transport.emit(ERROR_OCCURRED_EVENT_SUBJECT, errorEvent),
4190
+ new Promise((resolve2) => setTimeout(resolve2, 300))
4191
+ ]);
4192
+ } catch {
4193
+ }
4194
+ }
4195
+ );
4196
+ const handleSignal = async (signal) => {
4197
+ shutdownStartTime = Date.now();
4198
+ const uptime = Date.now() - startupTime;
4199
+ const signalEvent = buildApplicationLifecycleEvent({
4200
+ lifecycle_event_type: "signal_termination",
4201
+ signal_name: signal,
4202
+ uptime_ms: uptime,
4203
+ is_graceful_shutdown: true
4204
+ });
4205
+ try {
4206
+ await Promise.race([
4207
+ transport.emit(APPLICATION_LIFECYCLE_EVENT_SUBJECT, signalEvent),
4208
+ new Promise((resolve2) => setTimeout(resolve2, 300))
4209
+ ]);
4210
+ } catch {
4211
+ }
4212
+ };
4213
+ ["SIGINT", "SIGTERM"].forEach((signal) => {
4214
+ process.on(signal, () => handleSignal(signal));
4215
+ });
4216
+ }
4217
+ }
4218
+ return {
4219
+ context: {
4220
+ eventEmission: {
4221
+ transport,
4222
+ config,
4223
+ emit: (subject, event) => {
4224
+ silentEmit(transport, subject, event);
4225
+ },
4226
+ createBaseEvent: createBaseEventHelper
4227
+ }
4228
+ }
4229
+ };
4230
+ };
4231
+
3779
4232
  // src/sdk.ts
3780
4233
  function createSdk(options = {}, initialSdk = {}, initialContext = { meta: {} }) {
3781
4234
  return {
@@ -3825,7 +4278,7 @@ function createSdk(options = {}, initialSdk = {}, initialContext = { meta: {} })
3825
4278
  };
3826
4279
  }
3827
4280
  function createZapierSdkWithoutRegistry(options = {}) {
3828
- return createSdk(options).addPlugin(apiPlugin).addPlugin(manifestPlugin).addPlugin(listAppsPlugin).addPlugin(getAppPlugin).addPlugin(listActionsPlugin).addPlugin(getActionPlugin).addPlugin(listInputFieldsPlugin).addPlugin(listInputFieldChoicesPlugin).addPlugin(runActionPlugin).addPlugin(listAuthenticationsPlugin).addPlugin(getAuthenticationPlugin).addPlugin(findFirstAuthenticationPlugin).addPlugin(findUniqueAuthenticationPlugin).addPlugin(requestPlugin).addPlugin(fetchPlugin).addPlugin(appsPlugin).addPlugin(getProfilePlugin);
4281
+ return createSdk(options).addPlugin(eventEmissionPlugin).addPlugin(apiPlugin).addPlugin(manifestPlugin).addPlugin(listAppsPlugin).addPlugin(getAppPlugin).addPlugin(listActionsPlugin).addPlugin(getActionPlugin).addPlugin(listInputFieldsPlugin).addPlugin(listInputFieldChoicesPlugin).addPlugin(runActionPlugin).addPlugin(listAuthenticationsPlugin).addPlugin(getAuthenticationPlugin).addPlugin(findFirstAuthenticationPlugin).addPlugin(findUniqueAuthenticationPlugin).addPlugin(requestPlugin).addPlugin(fetchPlugin).addPlugin(appsPlugin).addPlugin(getProfilePlugin);
3829
4282
  }
3830
4283
  function createZapierSdk(options = {}) {
3831
4284
  return createZapierSdkWithoutRegistry(options).addPlugin(registryPlugin);
@@ -3863,6 +4316,10 @@ exports.appKeyResolver = appKeyResolver;
3863
4316
  exports.appsPlugin = appsPlugin;
3864
4317
  exports.authenticationIdGenericResolver = authenticationIdGenericResolver;
3865
4318
  exports.authenticationIdResolver = authenticationIdResolver;
4319
+ exports.buildApplicationLifecycleEvent = buildApplicationLifecycleEvent;
4320
+ exports.buildErrorEvent = buildErrorEvent;
4321
+ exports.buildErrorEventWithContext = buildErrorEventWithContext;
4322
+ exports.createBaseEvent = createBaseEvent;
3866
4323
  exports.createFunction = createFunction;
3867
4324
  exports.createSdk = createSdk;
3868
4325
  exports.createZapierSdk = createZapierSdk;
@@ -3872,17 +4329,26 @@ exports.findFirstAuthenticationPlugin = findFirstAuthenticationPlugin;
3872
4329
  exports.findManifestEntry = findManifestEntry;
3873
4330
  exports.findUniqueAuthenticationPlugin = findUniqueAuthenticationPlugin;
3874
4331
  exports.formatErrorMessage = formatErrorMessage;
4332
+ exports.generateEventId = generateEventId;
3875
4333
  exports.getActionPlugin = getActionPlugin;
3876
4334
  exports.getAppPlugin = getAppPlugin;
3877
4335
  exports.getAuthenticationPlugin = getAuthenticationPlugin;
4336
+ exports.getCiPlatform = getCiPlatform;
4337
+ exports.getCpuTime = getCpuTime;
4338
+ exports.getCurrentTimestamp = getCurrentTimestamp;
4339
+ exports.getMemoryUsage = getMemoryUsage;
4340
+ exports.getOsInfo = getOsInfo;
4341
+ exports.getPlatformVersions = getPlatformVersions;
3878
4342
  exports.getPreferredManifestEntryKey = getPreferredManifestEntryKey;
3879
4343
  exports.getProfilePlugin = getProfilePlugin;
4344
+ exports.getReleaseId = getReleaseId;
3880
4345
  exports.getTokenFromCliLogin = getTokenFromCliLogin;
3881
4346
  exports.getTokenFromEnv = getTokenFromEnv;
3882
4347
  exports.getTokenFromEnvOrConfig = getTokenFromEnvOrConfig;
3883
4348
  exports.inputFieldKeyResolver = inputFieldKeyResolver;
3884
4349
  exports.inputsAllOptionalResolver = inputsAllOptionalResolver;
3885
4350
  exports.inputsResolver = inputsResolver;
4351
+ exports.isCi = isCi;
3886
4352
  exports.isPositional = isPositional;
3887
4353
  exports.listActionsPlugin = listActionsPlugin;
3888
4354
  exports.listAppsPlugin = listAppsPlugin;