@trimlynk/sdk 0.0.0-stage → 1.1.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 (172) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +68 -2
  3. package/dist/cjs/cli/commands/analytics.d.ts +3 -0
  4. package/dist/cjs/cli/commands/analytics.js +80 -0
  5. package/dist/cjs/cli/commands/auth.d.ts +4 -0
  6. package/dist/cjs/cli/commands/auth.js +122 -0
  7. package/dist/cjs/cli/commands/campaigns.d.ts +6 -0
  8. package/dist/cjs/cli/commands/campaigns.js +229 -0
  9. package/dist/cjs/cli/commands/config.d.ts +3 -0
  10. package/dist/cjs/cli/commands/config.js +51 -0
  11. package/dist/cjs/cli/commands/conversions.d.ts +4 -0
  12. package/dist/cjs/cli/commands/conversions.js +160 -0
  13. package/dist/cjs/cli/commands/links.d.ts +6 -0
  14. package/dist/cjs/cli/commands/links.js +270 -0
  15. package/dist/cjs/cli/commands/webhooks.d.ts +10 -0
  16. package/dist/cjs/cli/commands/webhooks.js +510 -0
  17. package/dist/cjs/cli/configStore.d.ts +10 -0
  18. package/dist/cjs/cli/configStore.js +63 -0
  19. package/dist/cjs/cli/credentials.d.ts +23 -0
  20. package/dist/cjs/cli/credentials.js +235 -0
  21. package/dist/cjs/cli/exitCodes.d.ts +9 -0
  22. package/dist/cjs/cli/exitCodes.js +12 -0
  23. package/dist/cjs/cli/formatting.d.ts +19 -0
  24. package/dist/cjs/cli/formatting.js +110 -0
  25. package/dist/cjs/cli/index.d.ts +3 -0
  26. package/dist/cjs/cli/index.js +278 -0
  27. package/dist/cjs/cli/types.d.ts +13 -0
  28. package/dist/cjs/cli/types.js +2 -0
  29. package/dist/cjs/client.d.ts +89 -0
  30. package/dist/cjs/client.js +126 -0
  31. package/dist/cjs/config.d.ts +49 -0
  32. package/dist/cjs/config.js +67 -0
  33. package/dist/cjs/errors.d.ts +60 -0
  34. package/dist/cjs/errors.js +149 -0
  35. package/dist/cjs/events.d.ts +50 -0
  36. package/dist/cjs/events.js +153 -0
  37. package/dist/cjs/http.d.ts +30 -0
  38. package/dist/cjs/http.js +148 -0
  39. package/dist/cjs/index.d.ts +28 -0
  40. package/dist/cjs/index.js +58 -0
  41. package/dist/cjs/package.json +3 -0
  42. package/dist/cjs/pagination.d.ts +54 -0
  43. package/dist/cjs/pagination.js +111 -0
  44. package/dist/cjs/resources/analytics.d.ts +35 -0
  45. package/dist/cjs/resources/analytics.js +74 -0
  46. package/dist/cjs/resources/campaigns.d.ts +65 -0
  47. package/dist/cjs/resources/campaigns.js +139 -0
  48. package/dist/cjs/resources/conversions.d.ts +37 -0
  49. package/dist/cjs/resources/conversions.js +82 -0
  50. package/dist/cjs/resources/links.d.ts +95 -0
  51. package/dist/cjs/resources/links.js +199 -0
  52. package/dist/cjs/resources/webhooks.d.ts +66 -0
  53. package/dist/cjs/resources/webhooks.js +178 -0
  54. package/dist/cjs/security.d.ts +53 -0
  55. package/dist/cjs/security.js +156 -0
  56. package/dist/cjs/types.d.ts +149 -0
  57. package/dist/cjs/types.js +347 -0
  58. package/dist/cjs/utm.d.ts +36 -0
  59. package/dist/cjs/utm.js +44 -0
  60. package/dist/esm/cli/commands/analytics.d.ts +4 -0
  61. package/dist/esm/cli/commands/analytics.d.ts.map +1 -0
  62. package/dist/esm/cli/commands/analytics.js +77 -0
  63. package/dist/esm/cli/commands/analytics.js.map +1 -0
  64. package/dist/esm/cli/commands/auth.d.ts +5 -0
  65. package/dist/esm/cli/commands/auth.d.ts.map +1 -0
  66. package/dist/esm/cli/commands/auth.js +118 -0
  67. package/dist/esm/cli/commands/auth.js.map +1 -0
  68. package/dist/esm/cli/commands/campaigns.d.ts +7 -0
  69. package/dist/esm/cli/commands/campaigns.d.ts.map +1 -0
  70. package/dist/esm/cli/commands/campaigns.js +223 -0
  71. package/dist/esm/cli/commands/campaigns.js.map +1 -0
  72. package/dist/esm/cli/commands/config.d.ts +4 -0
  73. package/dist/esm/cli/commands/config.d.ts.map +1 -0
  74. package/dist/esm/cli/commands/config.js +48 -0
  75. package/dist/esm/cli/commands/config.js.map +1 -0
  76. package/dist/esm/cli/commands/conversions.d.ts +5 -0
  77. package/dist/esm/cli/commands/conversions.d.ts.map +1 -0
  78. package/dist/esm/cli/commands/conversions.js +156 -0
  79. package/dist/esm/cli/commands/conversions.js.map +1 -0
  80. package/dist/esm/cli/commands/links.d.ts +7 -0
  81. package/dist/esm/cli/commands/links.d.ts.map +1 -0
  82. package/dist/esm/cli/commands/links.js +264 -0
  83. package/dist/esm/cli/commands/links.js.map +1 -0
  84. package/dist/esm/cli/commands/webhooks.d.ts +11 -0
  85. package/dist/esm/cli/commands/webhooks.d.ts.map +1 -0
  86. package/dist/esm/cli/commands/webhooks.js +500 -0
  87. package/dist/esm/cli/commands/webhooks.js.map +1 -0
  88. package/dist/esm/cli/configStore.d.ts +11 -0
  89. package/dist/esm/cli/configStore.d.ts.map +1 -0
  90. package/dist/esm/cli/configStore.js +54 -0
  91. package/dist/esm/cli/configStore.js.map +1 -0
  92. package/dist/esm/cli/credentials.d.ts +24 -0
  93. package/dist/esm/cli/credentials.d.ts.map +1 -0
  94. package/dist/esm/cli/credentials.js +226 -0
  95. package/dist/esm/cli/credentials.js.map +1 -0
  96. package/dist/esm/cli/exitCodes.d.ts +10 -0
  97. package/dist/esm/cli/exitCodes.d.ts.map +1 -0
  98. package/dist/esm/cli/exitCodes.js +10 -0
  99. package/dist/esm/cli/exitCodes.js.map +1 -0
  100. package/dist/esm/cli/formatting.d.ts +20 -0
  101. package/dist/esm/cli/formatting.d.ts.map +1 -0
  102. package/dist/esm/cli/formatting.js +95 -0
  103. package/dist/esm/cli/formatting.js.map +1 -0
  104. package/dist/esm/cli/index.d.ts +4 -0
  105. package/dist/esm/cli/index.d.ts.map +1 -0
  106. package/dist/esm/cli/index.js +276 -0
  107. package/dist/esm/cli/index.js.map +1 -0
  108. package/dist/esm/cli/types.d.ts +14 -0
  109. package/dist/esm/cli/types.d.ts.map +1 -0
  110. package/dist/esm/cli/types.js +2 -0
  111. package/dist/esm/cli/types.js.map +1 -0
  112. package/dist/esm/client.d.ts +90 -0
  113. package/dist/esm/client.d.ts.map +1 -0
  114. package/dist/esm/client.js +123 -0
  115. package/dist/esm/client.js.map +1 -0
  116. package/dist/esm/config.d.ts +50 -0
  117. package/dist/esm/config.d.ts.map +1 -0
  118. package/dist/esm/config.js +64 -0
  119. package/dist/esm/config.js.map +1 -0
  120. package/dist/esm/errors.d.ts +61 -0
  121. package/dist/esm/errors.d.ts.map +1 -0
  122. package/dist/esm/errors.js +130 -0
  123. package/dist/esm/errors.js.map +1 -0
  124. package/dist/esm/events.d.ts +51 -0
  125. package/dist/esm/events.d.ts.map +1 -0
  126. package/dist/esm/events.js +149 -0
  127. package/dist/esm/events.js.map +1 -0
  128. package/dist/esm/http.d.ts +31 -0
  129. package/dist/esm/http.d.ts.map +1 -0
  130. package/dist/esm/http.js +145 -0
  131. package/dist/esm/http.js.map +1 -0
  132. package/dist/esm/index.d.ts +29 -0
  133. package/dist/esm/index.d.ts.map +1 -0
  134. package/dist/esm/index.js +16 -0
  135. package/dist/esm/index.js.map +1 -0
  136. package/dist/esm/pagination.d.ts +55 -0
  137. package/dist/esm/pagination.d.ts.map +1 -0
  138. package/dist/esm/pagination.js +106 -0
  139. package/dist/esm/pagination.js.map +1 -0
  140. package/dist/esm/resources/analytics.d.ts +36 -0
  141. package/dist/esm/resources/analytics.d.ts.map +1 -0
  142. package/dist/esm/resources/analytics.js +71 -0
  143. package/dist/esm/resources/analytics.js.map +1 -0
  144. package/dist/esm/resources/campaigns.d.ts +66 -0
  145. package/dist/esm/resources/campaigns.d.ts.map +1 -0
  146. package/dist/esm/resources/campaigns.js +136 -0
  147. package/dist/esm/resources/campaigns.js.map +1 -0
  148. package/dist/esm/resources/conversions.d.ts +38 -0
  149. package/dist/esm/resources/conversions.d.ts.map +1 -0
  150. package/dist/esm/resources/conversions.js +79 -0
  151. package/dist/esm/resources/conversions.js.map +1 -0
  152. package/dist/esm/resources/links.d.ts +96 -0
  153. package/dist/esm/resources/links.d.ts.map +1 -0
  154. package/dist/esm/resources/links.js +196 -0
  155. package/dist/esm/resources/links.js.map +1 -0
  156. package/dist/esm/resources/webhooks.d.ts +67 -0
  157. package/dist/esm/resources/webhooks.d.ts.map +1 -0
  158. package/dist/esm/resources/webhooks.js +175 -0
  159. package/dist/esm/resources/webhooks.js.map +1 -0
  160. package/dist/esm/security.d.ts +54 -0
  161. package/dist/esm/security.d.ts.map +1 -0
  162. package/dist/esm/security.js +153 -0
  163. package/dist/esm/security.js.map +1 -0
  164. package/dist/esm/types.d.ts +150 -0
  165. package/dist/esm/types.d.ts.map +1 -0
  166. package/dist/esm/types.js +335 -0
  167. package/dist/esm/types.js.map +1 -0
  168. package/dist/esm/utm.d.ts +37 -0
  169. package/dist/esm/utm.d.ts.map +1 -0
  170. package/dist/esm/utm.js +42 -0
  171. package/dist/esm/utm.js.map +1 -0
  172. package/package.json +91 -4
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,89 @@
1
+ import { inspect } from "node:util";
2
+ import { TrimLynkConfig, TrimLynkConfigOptions } from "./config.js";
3
+ import { WebhookEvent } from "./events.js";
4
+ import { HttpClient } from "./http.js";
5
+ import { AnalyticsResource } from "./resources/analytics.js";
6
+ import { CampaignsResource } from "./resources/campaigns.js";
7
+ import { ConversionsResource } from "./resources/conversions.js";
8
+ import { LinksResource } from "./resources/links.js";
9
+ import { ConstructWebhookEventOptions, WebhooksResource } from "./resources/webhooks.js";
10
+ import { VerifyWebhookSignatureOptions } from "./security.js";
11
+ /**
12
+ * Official Node.js & TypeScript client for the TrimLynk Developer API v1.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { TrimLynk } from "@trimlynk/sdk";
17
+ *
18
+ * const client = new TrimLynk({ apiKey: process.env.TRIMLYNK_API_KEY });
19
+ * const link = await client.links.create({ originalUrl: "https://example.com" });
20
+ * console.log(link.shortUrl);
21
+ * ```
22
+ */
23
+ export declare class TrimLynk {
24
+ /** Resolved SDK configuration (with masked API key in logs). */
25
+ readonly config: TrimLynkConfig;
26
+ private readonly _http;
27
+ /** Manage shortened URLs, custom slugs, UTM tagging, and QR codes. */
28
+ readonly links: LinksResource;
29
+ /** Query aggregate workspace, per-link, and conversion funnel analytics. */
30
+ readonly analytics: AnalyticsResource;
31
+ /** Record and list attributed conversion events. */
32
+ readonly conversions: ConversionsResource;
33
+ /** Create and manage marketing campaigns. */
34
+ readonly campaigns: CampaignsResource;
35
+ /** Register webhooks, inspect deliveries, and verify incoming webhook payloads. */
36
+ readonly webhooks: WebhooksResource;
37
+ /**
38
+ * Initialize a new TrimLynk API client.
39
+ *
40
+ * @param options - Client configuration options. Falls back to `TRIMLYNK_API_KEY` and `TRIMLYNK_BASE_URL`.
41
+ * @throws {Error} If no API key is provided via `options.apiKey` or `TRIMLYNK_API_KEY`.
42
+ */
43
+ constructor(options?: TrimLynkConfigOptions);
44
+ /**
45
+ * Access the underlying {@link HttpClient} for low-level or custom API requests.
46
+ *
47
+ * @returns The internal `HttpClient` instance.
48
+ */
49
+ get http(): HttpClient;
50
+ /**
51
+ * Verify an incoming TrimLynk webhook HMAC-SHA256 signature.
52
+ *
53
+ * @param options - Verification options including raw payload, signature header, secret, and optional tolerance window.
54
+ * @returns `true` if the signature is authentic and within the replay tolerance window; `false` otherwise.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * const isValid = TrimLynk.verifyWebhookSignature({
59
+ * rawPayload: req.rawBody,
60
+ * signatureHeader: req.headers["x-trimlynk-signature"],
61
+ * secret: process.env.TRIMLYNK_WEBHOOK_SECRET!,
62
+ * });
63
+ * ```
64
+ */
65
+ static verifyWebhookSignature(options: VerifyWebhookSignatureOptions): boolean;
66
+ /**
67
+ * Verify an incoming webhook HMAC-SHA256 signature and parse the payload into a typed {@link WebhookEvent}.
68
+ *
69
+ * @param options - Verification and payload options.
70
+ * @returns A verified {@link WebhookEvent} instance.
71
+ * @throws {InvalidSignatureHeaderError} If the signature header, timestamp, or secret is missing or malformed.
72
+ * @throws {SignatureExpiredError} If the timestamp exceeds the replay tolerance window.
73
+ * @throws {SignatureMismatchError} If the HMAC-SHA256 digest does not match.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * const event = TrimLynk.constructWebhookEvent({
78
+ * rawPayload: req.rawBody,
79
+ * signatureHeader: req.headers["x-trimlynk-signature"],
80
+ * secret: process.env.TRIMLYNK_WEBHOOK_SECRET!,
81
+ * });
82
+ * if (event.isConversion) {
83
+ * console.log(`Order #${event.orderId}: $${event.revenue}`);
84
+ * }
85
+ * ```
86
+ */
87
+ static constructWebhookEvent(options: ConstructWebhookEventOptions): WebhookEvent;
88
+ [inspect.custom](): string;
89
+ }
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TrimLynk = void 0;
4
+ const node_util_1 = require("node:util");
5
+ const config_js_1 = require("./config.js");
6
+ const events_js_1 = require("./events.js");
7
+ const http_js_1 = require("./http.js");
8
+ const analytics_js_1 = require("./resources/analytics.js");
9
+ const campaigns_js_1 = require("./resources/campaigns.js");
10
+ const conversions_js_1 = require("./resources/conversions.js");
11
+ const links_js_1 = require("./resources/links.js");
12
+ const webhooks_js_1 = require("./resources/webhooks.js");
13
+ const security_js_1 = require("./security.js");
14
+ /**
15
+ * Official Node.js & TypeScript client for the TrimLynk Developer API v1.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { TrimLynk } from "@trimlynk/sdk";
20
+ *
21
+ * const client = new TrimLynk({ apiKey: process.env.TRIMLYNK_API_KEY });
22
+ * const link = await client.links.create({ originalUrl: "https://example.com" });
23
+ * console.log(link.shortUrl);
24
+ * ```
25
+ */
26
+ class TrimLynk {
27
+ /** Resolved SDK configuration (with masked API key in logs). */
28
+ config;
29
+ _http;
30
+ /** Manage shortened URLs, custom slugs, UTM tagging, and QR codes. */
31
+ links;
32
+ /** Query aggregate workspace, per-link, and conversion funnel analytics. */
33
+ analytics;
34
+ /** Record and list attributed conversion events. */
35
+ conversions;
36
+ /** Create and manage marketing campaigns. */
37
+ campaigns;
38
+ /** Register webhooks, inspect deliveries, and verify incoming webhook payloads. */
39
+ webhooks;
40
+ /**
41
+ * Initialize a new TrimLynk API client.
42
+ *
43
+ * @param options - Client configuration options. Falls back to `TRIMLYNK_API_KEY` and `TRIMLYNK_BASE_URL`.
44
+ * @throws {Error} If no API key is provided via `options.apiKey` or `TRIMLYNK_API_KEY`.
45
+ */
46
+ constructor(options = {}) {
47
+ this.config = config_js_1.TrimLynkConfig.create(options);
48
+ this._http = new http_js_1.HttpClient(this.config);
49
+ this.links = new links_js_1.LinksResource(this._http);
50
+ this.analytics = new analytics_js_1.AnalyticsResource(this._http);
51
+ this.conversions = new conversions_js_1.ConversionsResource(this._http);
52
+ this.campaigns = new campaigns_js_1.CampaignsResource(this._http);
53
+ this.webhooks = new webhooks_js_1.WebhooksResource(this._http);
54
+ }
55
+ /**
56
+ * Access the underlying {@link HttpClient} for low-level or custom API requests.
57
+ *
58
+ * @returns The internal `HttpClient` instance.
59
+ */
60
+ get http() {
61
+ return this._http;
62
+ }
63
+ /**
64
+ * Verify an incoming TrimLynk webhook HMAC-SHA256 signature.
65
+ *
66
+ * @param options - Verification options including raw payload, signature header, secret, and optional tolerance window.
67
+ * @returns `true` if the signature is authentic and within the replay tolerance window; `false` otherwise.
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * const isValid = TrimLynk.verifyWebhookSignature({
72
+ * rawPayload: req.rawBody,
73
+ * signatureHeader: req.headers["x-trimlynk-signature"],
74
+ * secret: process.env.TRIMLYNK_WEBHOOK_SECRET!,
75
+ * });
76
+ * ```
77
+ */
78
+ static verifyWebhookSignature(options) {
79
+ return (0, security_js_1.verifyWebhookSignature)(options);
80
+ }
81
+ /**
82
+ * Verify an incoming webhook HMAC-SHA256 signature and parse the payload into a typed {@link WebhookEvent}.
83
+ *
84
+ * @param options - Verification and payload options.
85
+ * @returns A verified {@link WebhookEvent} instance.
86
+ * @throws {InvalidSignatureHeaderError} If the signature header, timestamp, or secret is missing or malformed.
87
+ * @throws {SignatureExpiredError} If the timestamp exceeds the replay tolerance window.
88
+ * @throws {SignatureMismatchError} If the HMAC-SHA256 digest does not match.
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * const event = TrimLynk.constructWebhookEvent({
93
+ * rawPayload: req.rawBody,
94
+ * signatureHeader: req.headers["x-trimlynk-signature"],
95
+ * secret: process.env.TRIMLYNK_WEBHOOK_SECRET!,
96
+ * });
97
+ * if (event.isConversion) {
98
+ * console.log(`Order #${event.orderId}: $${event.revenue}`);
99
+ * }
100
+ * ```
101
+ */
102
+ static constructWebhookEvent(options) {
103
+ if (options.payload !== undefined && options.rawPayload !== undefined) {
104
+ throw new Error("Cannot specify both 'payload' and 'rawPayload'. Pass only one.");
105
+ }
106
+ const target = options.rawPayload ?? options.payload;
107
+ if (target === undefined || target === null) {
108
+ throw new Error("rawPayload (or payload) is required to construct a webhook event.");
109
+ }
110
+ const rawToValidate = typeof target === "object" && !Buffer.isBuffer(target) && !(target instanceof Uint8Array)
111
+ ? JSON.stringify(target)
112
+ : target;
113
+ (0, security_js_1.validateWebhookSignature)({
114
+ rawPayload: rawToValidate,
115
+ signatureHeader: options.signatureHeader,
116
+ timestamp: options.timestamp,
117
+ secret: options.secret,
118
+ toleranceSeconds: options.toleranceSeconds,
119
+ });
120
+ return (0, events_js_1.parseWebhookEvent)(target);
121
+ }
122
+ [node_util_1.inspect.custom]() {
123
+ return `TrimLynk(config=${(0, node_util_1.inspect)(this.config)})`;
124
+ }
125
+ }
126
+ exports.TrimLynk = TrimLynk;
@@ -0,0 +1,49 @@
1
+ import { inspect } from "node:util";
2
+ export declare const DEFAULT_BASE_URL = "https://api.trimlynk.com";
3
+ export declare const DEFAULT_TIMEOUT_MS = 30000;
4
+ export declare const DEFAULT_API_VERSION = "1";
5
+ export declare const SDK_VERSION = "1.1.2";
6
+ export declare const USER_AGENT = "trimlynk-node/1.1.2";
7
+ /** Target TrimLynk API environment. */
8
+ export type TrimLynkEnvironment = "live" | "test";
9
+ /** Standard Fetch API function signature (allows custom fetch injection for testing or edge runtimes). */
10
+ export type FetchLike = typeof globalThis.fetch;
11
+ /**
12
+ * Configuration options accepted by {@link TrimLynk} and {@link TrimLynkConfig}.
13
+ */
14
+ export interface TrimLynkConfigOptions {
15
+ /** TrimLynk API key (`tl_live_...` or `tl_test_...`). Falls back to `TRIMLYNK_API_KEY`. */
16
+ apiKey?: string | undefined;
17
+ /** Optional API base URL override (default: `"https://api.trimlynk.com"`). Falls back to `TRIMLYNK_BASE_URL`. */
18
+ baseUrl?: string | undefined;
19
+ /** Target API environment (`"live"` or `"test"`, default: `"live"`). */
20
+ environment?: TrimLynkEnvironment | string | undefined;
21
+ /** Request timeout in milliseconds (default: `30000`). */
22
+ timeout?: number | undefined;
23
+ /** Developer API version header value (default: `"1"`). */
24
+ apiVersion?: string | undefined;
25
+ /** Maximum automatic retries for HTTP 429 rate limits (default: `0`). */
26
+ maxRetries?: number | undefined;
27
+ /** Custom `User-Agent` header override. */
28
+ userAgent?: string | undefined;
29
+ /** Optional custom `fetch` implementation (defaults to `globalThis.fetch`). */
30
+ fetch?: FetchLike | undefined;
31
+ }
32
+ /**
33
+ * Holds configuration parameters for TrimLynk client network communications.
34
+ */
35
+ export declare class TrimLynkConfig {
36
+ readonly apiKey: string;
37
+ readonly baseUrl: string;
38
+ readonly environment: TrimLynkEnvironment;
39
+ readonly timeout: number;
40
+ readonly apiVersion: string;
41
+ readonly maxRetries: number;
42
+ readonly userAgent: string;
43
+ readonly fetch: FetchLike;
44
+ constructor(options?: TrimLynkConfigOptions);
45
+ static create(options?: TrimLynkConfigOptions): TrimLynkConfig;
46
+ get maskedApiKey(): string;
47
+ toJSON(): Record<string, unknown>;
48
+ [inspect.custom](): string;
49
+ }
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TrimLynkConfig = exports.USER_AGENT = exports.SDK_VERSION = exports.DEFAULT_API_VERSION = exports.DEFAULT_TIMEOUT_MS = exports.DEFAULT_BASE_URL = void 0;
4
+ const node_util_1 = require("node:util");
5
+ exports.DEFAULT_BASE_URL = "https://api.trimlynk.com";
6
+ exports.DEFAULT_TIMEOUT_MS = 30_000;
7
+ exports.DEFAULT_API_VERSION = "1";
8
+ exports.SDK_VERSION = "1.1.2";
9
+ exports.USER_AGENT = `trimlynk-node/${exports.SDK_VERSION}`;
10
+ /**
11
+ * Holds configuration parameters for TrimLynk client network communications.
12
+ */
13
+ class TrimLynkConfig {
14
+ apiKey;
15
+ baseUrl;
16
+ environment;
17
+ timeout;
18
+ apiVersion;
19
+ maxRetries;
20
+ userAgent;
21
+ fetch;
22
+ constructor(options = {}) {
23
+ const rawKey = options.apiKey ?? process.env["TRIMLYNK_API_KEY"] ?? "";
24
+ const trimmedKey = typeof rawKey === "string" ? rawKey.trim() : "";
25
+ if (!trimmedKey) {
26
+ throw new Error("TrimLynk API key is required. Pass apiKey to new TrimLynk() or set TRIMLYNK_API_KEY in the environment.");
27
+ }
28
+ this.apiKey = trimmedKey;
29
+ const rawUrl = options.baseUrl ?? process.env["TRIMLYNK_BASE_URL"] ?? exports.DEFAULT_BASE_URL;
30
+ this.baseUrl = (rawUrl.trim() || exports.DEFAULT_BASE_URL).replace(/\/+$/, "");
31
+ this.environment = options.environment === "test" ? "test" : "live";
32
+ const timeout = options.timeout ?? exports.DEFAULT_TIMEOUT_MS;
33
+ this.timeout = typeof timeout === "number" && timeout > 0 ? timeout : exports.DEFAULT_TIMEOUT_MS;
34
+ this.apiVersion = options.apiVersion?.trim() || exports.DEFAULT_API_VERSION;
35
+ const retries = options.maxRetries ?? 0;
36
+ this.maxRetries = typeof retries === "number" && retries > 0 ? Math.floor(retries) : 0;
37
+ this.userAgent = options.userAgent?.trim() || exports.USER_AGENT;
38
+ const resolvedFetch = options.fetch ?? globalThis.fetch;
39
+ if (typeof resolvedFetch !== "function") {
40
+ throw new Error("A global fetch implementation (Node >= 22) or custom fetch option is required.");
41
+ }
42
+ this.fetch = resolvedFetch;
43
+ }
44
+ static create(options = {}) {
45
+ return new TrimLynkConfig(options);
46
+ }
47
+ get maskedApiKey() {
48
+ if (this.apiKey.length > 12) {
49
+ return `${this.apiKey.slice(0, 7)}...${this.apiKey.slice(-4)}`;
50
+ }
51
+ return "***";
52
+ }
53
+ toJSON() {
54
+ return {
55
+ baseUrl: this.baseUrl,
56
+ environment: this.environment,
57
+ timeout: this.timeout,
58
+ apiVersion: this.apiVersion,
59
+ maxRetries: this.maxRetries,
60
+ apiKey: this.maskedApiKey,
61
+ };
62
+ }
63
+ [node_util_1.inspect.custom]() {
64
+ return `TrimLynkConfig(baseUrl=${JSON.stringify(this.baseUrl)}, environment=${JSON.stringify(this.environment)}, timeout=${this.timeout}, apiKey=${JSON.stringify(this.maskedApiKey)})`;
65
+ }
66
+ }
67
+ exports.TrimLynkConfig = TrimLynkConfig;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Exception taxonomy for the TrimLynk Node.js SDK.
3
+ */
4
+ export declare class TrimLynkError extends Error {
5
+ readonly message: string;
6
+ constructor(message: string);
7
+ }
8
+ export declare class NetworkError extends TrimLynkError {
9
+ readonly originalError: unknown;
10
+ constructor(message: string, originalError?: unknown);
11
+ }
12
+ export declare class TimeoutError extends NetworkError {
13
+ }
14
+ export interface APIErrorOptions {
15
+ code?: string | undefined;
16
+ statusCode?: number | undefined;
17
+ requestId?: string | undefined;
18
+ details?: Record<string, unknown> | undefined;
19
+ }
20
+ export declare class APIError extends TrimLynkError {
21
+ readonly code: string;
22
+ readonly statusCode: number;
23
+ readonly requestId: string | undefined;
24
+ readonly details: Record<string, unknown>;
25
+ constructor(message: string, options?: APIErrorOptions);
26
+ toString(): string;
27
+ }
28
+ export declare class InvalidRequestError extends APIError {
29
+ }
30
+ export declare class AuthenticationError extends APIError {
31
+ }
32
+ export declare class PermissionError extends APIError {
33
+ }
34
+ export declare class NotFoundError extends APIError {
35
+ }
36
+ export declare class ConflictError extends APIError {
37
+ }
38
+ export declare class IdempotencyConflictError extends ConflictError {
39
+ }
40
+ export interface RateLimitErrorOptions extends APIErrorOptions {
41
+ retryAfter?: number | undefined;
42
+ }
43
+ export declare class RateLimitError extends APIError {
44
+ readonly retryAfter: number | undefined;
45
+ constructor(message: string, options?: RateLimitErrorOptions);
46
+ }
47
+ export declare class InternalServerError extends APIError {
48
+ }
49
+ export declare class SignatureVerificationError extends TrimLynkError {
50
+ }
51
+ export declare class SignatureExpiredError extends SignatureVerificationError {
52
+ }
53
+ export declare class SignatureMismatchError extends SignatureVerificationError {
54
+ }
55
+ export declare class InvalidSignatureHeaderError extends SignatureVerificationError {
56
+ }
57
+ /**
58
+ * Instantiate the appropriate typed APIError subclass based on HTTP status code and payload envelope.
59
+ */
60
+ export declare function parseApiError(statusCode: number, payload: Record<string, unknown> | null | undefined, requestId?: string, retryAfter?: number): APIError;
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ /**
3
+ * Exception taxonomy for the TrimLynk Node.js SDK.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.InvalidSignatureHeaderError = exports.SignatureMismatchError = exports.SignatureExpiredError = exports.SignatureVerificationError = exports.InternalServerError = exports.RateLimitError = exports.IdempotencyConflictError = exports.ConflictError = exports.NotFoundError = exports.PermissionError = exports.AuthenticationError = exports.InvalidRequestError = exports.APIError = exports.TimeoutError = exports.NetworkError = exports.TrimLynkError = void 0;
7
+ exports.parseApiError = parseApiError;
8
+ class TrimLynkError extends Error {
9
+ message;
10
+ constructor(message) {
11
+ super(message);
12
+ this.name = new.target.name;
13
+ this.message = message;
14
+ Object.setPrototypeOf(this, new.target.prototype);
15
+ }
16
+ }
17
+ exports.TrimLynkError = TrimLynkError;
18
+ class NetworkError extends TrimLynkError {
19
+ originalError;
20
+ constructor(message, originalError) {
21
+ super(message);
22
+ this.originalError = originalError;
23
+ }
24
+ }
25
+ exports.NetworkError = NetworkError;
26
+ class TimeoutError extends NetworkError {
27
+ }
28
+ exports.TimeoutError = TimeoutError;
29
+ class APIError extends TrimLynkError {
30
+ code;
31
+ statusCode;
32
+ requestId;
33
+ details;
34
+ constructor(message, options = {}) {
35
+ super(message);
36
+ this.code = options.code ?? "api_error";
37
+ this.statusCode = options.statusCode ?? 500;
38
+ this.requestId = options.requestId;
39
+ this.details = options.details ?? {};
40
+ }
41
+ toString() {
42
+ const reqPart = this.requestId ? ` [request_id=${this.requestId}]` : "";
43
+ return `[${this.statusCode} ${this.code}] ${this.message}${reqPart}`;
44
+ }
45
+ }
46
+ exports.APIError = APIError;
47
+ class InvalidRequestError extends APIError {
48
+ }
49
+ exports.InvalidRequestError = InvalidRequestError;
50
+ class AuthenticationError extends APIError {
51
+ }
52
+ exports.AuthenticationError = AuthenticationError;
53
+ class PermissionError extends APIError {
54
+ }
55
+ exports.PermissionError = PermissionError;
56
+ class NotFoundError extends APIError {
57
+ }
58
+ exports.NotFoundError = NotFoundError;
59
+ class ConflictError extends APIError {
60
+ }
61
+ exports.ConflictError = ConflictError;
62
+ class IdempotencyConflictError extends ConflictError {
63
+ }
64
+ exports.IdempotencyConflictError = IdempotencyConflictError;
65
+ class RateLimitError extends APIError {
66
+ retryAfter;
67
+ constructor(message, options = {}) {
68
+ super(message, {
69
+ code: options.code ?? "rate_limit_exceeded",
70
+ statusCode: options.statusCode ?? 429,
71
+ requestId: options.requestId,
72
+ details: options.details,
73
+ });
74
+ this.retryAfter = options.retryAfter;
75
+ }
76
+ }
77
+ exports.RateLimitError = RateLimitError;
78
+ class InternalServerError extends APIError {
79
+ }
80
+ exports.InternalServerError = InternalServerError;
81
+ class SignatureVerificationError extends TrimLynkError {
82
+ }
83
+ exports.SignatureVerificationError = SignatureVerificationError;
84
+ class SignatureExpiredError extends SignatureVerificationError {
85
+ }
86
+ exports.SignatureExpiredError = SignatureExpiredError;
87
+ class SignatureMismatchError extends SignatureVerificationError {
88
+ }
89
+ exports.SignatureMismatchError = SignatureMismatchError;
90
+ class InvalidSignatureHeaderError extends SignatureVerificationError {
91
+ }
92
+ exports.InvalidSignatureHeaderError = InvalidSignatureHeaderError;
93
+ /**
94
+ * Instantiate the appropriate typed APIError subclass based on HTTP status code and payload envelope.
95
+ */
96
+ function parseApiError(statusCode, payload, requestId, retryAfter) {
97
+ const safePayload = payload ?? {};
98
+ const errObj = safePayload["error"];
99
+ let code = "api_error";
100
+ let message = `HTTP ${statusCode} Error`;
101
+ let details = {};
102
+ if (errObj && typeof errObj === "object" && !Array.isArray(errObj)) {
103
+ const errRecord = errObj;
104
+ if (typeof errRecord["code"] === "string" && errRecord["code"]) {
105
+ code = errRecord["code"];
106
+ }
107
+ if (typeof errRecord["message"] === "string" && errRecord["message"]) {
108
+ message = errRecord["message"];
109
+ }
110
+ if (errRecord["details"] && typeof errRecord["details"] === "object" && !Array.isArray(errRecord["details"])) {
111
+ details = errRecord["details"];
112
+ }
113
+ }
114
+ else if (errObj) {
115
+ message = String(errObj);
116
+ }
117
+ else if (typeof safePayload["message"] === "string" && safePayload["message"]) {
118
+ message = safePayload["message"];
119
+ }
120
+ const resolvedRequestId = typeof safePayload["requestId"] === "string" && safePayload["requestId"]
121
+ ? safePayload["requestId"]
122
+ : requestId;
123
+ const opts = {
124
+ code,
125
+ statusCode,
126
+ requestId: resolvedRequestId,
127
+ details,
128
+ };
129
+ if (statusCode === 400)
130
+ return new InvalidRequestError(message, opts);
131
+ if (statusCode === 401)
132
+ return new AuthenticationError(message, opts);
133
+ if (statusCode === 403)
134
+ return new PermissionError(message, opts);
135
+ if (statusCode === 404)
136
+ return new NotFoundError(message, opts);
137
+ if (statusCode === 409) {
138
+ if (code === "idempotency_conflict") {
139
+ return new IdempotencyConflictError(message, opts);
140
+ }
141
+ return new ConflictError(message, opts);
142
+ }
143
+ if (statusCode === 429) {
144
+ return new RateLimitError(message, { ...opts, retryAfter });
145
+ }
146
+ if (statusCode >= 500)
147
+ return new InternalServerError(message, opts);
148
+ return new APIError(message, opts);
149
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Typed event models and parsers for incoming TrimLynk webhooks.
3
+ */
4
+ export interface WebhookEventInit {
5
+ event: string;
6
+ timestamp?: string;
7
+ data?: Record<string, unknown>;
8
+ id?: string | undefined;
9
+ raw?: Record<string, unknown>;
10
+ }
11
+ export declare class WebhookEvent {
12
+ readonly event: string;
13
+ readonly timestamp: string;
14
+ readonly data: Record<string, unknown>;
15
+ readonly id: string | undefined;
16
+ readonly raw: Record<string, unknown>;
17
+ constructor(init: WebhookEventInit);
18
+ get isClick(): boolean;
19
+ get isConversion(): boolean;
20
+ get isLink(): boolean;
21
+ get urlId(): string | undefined;
22
+ get linkId(): string | undefined;
23
+ get shortCode(): string | undefined;
24
+ get shortUrl(): string | undefined;
25
+ get eventName(): string | undefined;
26
+ get eventValue(): number | undefined;
27
+ get revenue(): number | undefined;
28
+ get currency(): string;
29
+ get orderId(): string | undefined;
30
+ get customerId(): string | undefined;
31
+ get customerEmail(): string | undefined;
32
+ get channel(): string | undefined;
33
+ static fromDict(payload: Record<string, unknown>): WebhookEvent;
34
+ }
35
+ /**
36
+ * Parse raw webhook bytes, JSON string, or plain object into a typed {@link WebhookEvent}.
37
+ *
38
+ * @param payload - Raw JSON string, `Buffer`/`Uint8Array`, or already-parsed object.
39
+ * @returns A typed {@link WebhookEvent} instance with convenience getters (`isConversion`, `revenue`, `orderId`, etc.).
40
+ * @throws {Error} If the payload is not valid JSON or not a JSON object.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const event = parseWebhookEvent(rawBody);
45
+ * if (event.isConversion) {
46
+ * console.log(`Order #${event.orderId}: $${event.revenue}`);
47
+ * }
48
+ * ```
49
+ */
50
+ export declare function parseWebhookEvent(payload: string | Buffer | Uint8Array | Record<string, unknown>): WebhookEvent;