@trimlynk/sdk 0.0.0-stage → 1.1.3

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 +387 -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 +89 -4
@@ -0,0 +1,510 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cmdWebhooksCreate = cmdWebhooksCreate;
4
+ exports.cmdWebhooksList = cmdWebhooksList;
5
+ exports.cmdWebhooksGet = cmdWebhooksGet;
6
+ exports.cmdWebhooksUpdate = cmdWebhooksUpdate;
7
+ exports.cmdWebhooksDelete = cmdWebhooksDelete;
8
+ exports.cmdWebhooksTest = cmdWebhooksTest;
9
+ exports.cmdWebhooksDeliveries = cmdWebhooksDeliveries;
10
+ exports.cmdWebhooksRetry = cmdWebhooksRetry;
11
+ exports.cmdWebhooksListen = cmdWebhooksListen;
12
+ const node_util_1 = require("node:util");
13
+ const exitCodes_js_1 = require("../exitCodes.js");
14
+ const formatting_js_1 = require("../formatting.js");
15
+ function splitEvents(values) {
16
+ if (!values || values.length === 0)
17
+ return [];
18
+ const out = [];
19
+ for (const ev of values) {
20
+ for (const part of ev.split(",")) {
21
+ const t = part.trim();
22
+ if (t)
23
+ out.push(t);
24
+ }
25
+ }
26
+ return out;
27
+ }
28
+ async function cmdWebhooksCreate(argv, opts, getClient) {
29
+ const { values } = (0, node_util_1.parseArgs)({
30
+ args: argv,
31
+ allowPositionals: false,
32
+ options: {
33
+ url: { type: "string" },
34
+ event: { type: "string", multiple: true },
35
+ description: { type: "string" },
36
+ secret: { type: "string" },
37
+ "idempotency-key": { type: "string" },
38
+ },
39
+ });
40
+ if (!values.url) {
41
+ (0, formatting_js_1.printError)("--url is required.");
42
+ return exitCodes_js_1.EXIT_INVALID_ARGS;
43
+ }
44
+ let events = splitEvents(values.event);
45
+ if (events.length === 0) {
46
+ events = ["link.created", "link.updated", "click.created", "conversion.created"];
47
+ }
48
+ const client = getClient();
49
+ const webhook = await client.webhooks.create({
50
+ url: values.url,
51
+ events,
52
+ description: values.description,
53
+ secret: values.secret,
54
+ idempotencyKey: values["idempotency-key"],
55
+ });
56
+ if (opts.quiet) {
57
+ process.stdout.write(`${webhook.id}\n`);
58
+ }
59
+ else if (opts.json) {
60
+ (0, formatting_js_1.outputJson)(webhook);
61
+ }
62
+ else {
63
+ process.stdout.write(`✓ Webhook registered: ${webhook.id}\n\n`);
64
+ (0, formatting_js_1.printDetails)([
65
+ ["ID:", webhook.id],
66
+ ["Destination URL:", webhook.url],
67
+ ["Subscribed Events:", webhook.events.join(", ")],
68
+ ["Description:", webhook.description ?? "(none)"],
69
+ ["Signing Secret:", webhook.secret ?? "(hidden)"],
70
+ ["Request ID:", webhook.requestId],
71
+ ]);
72
+ if (webhook.secret) {
73
+ process.stdout.write("\n⚠️ Save the signing secret now. It will not be shown again.\n");
74
+ }
75
+ }
76
+ return exitCodes_js_1.EXIT_SUCCESS;
77
+ }
78
+ async function cmdWebhooksList(argv, opts, getClient) {
79
+ const { values } = (0, node_util_1.parseArgs)({
80
+ args: argv,
81
+ allowPositionals: false,
82
+ options: {
83
+ page: { type: "string" },
84
+ limit: { type: "string" },
85
+ all: { type: "boolean", default: false },
86
+ },
87
+ });
88
+ const pageNum = values.page ? Number.parseInt(values.page, 10) : 1;
89
+ const limitNum = values.limit ? Number.parseInt(values.limit, 10) : 50;
90
+ const client = getClient();
91
+ if (values.all) {
92
+ const items = await client.webhooks.iter({ limit: limitNum }).toArray();
93
+ if (opts.quiet) {
94
+ for (const item of items)
95
+ process.stdout.write(`${item.id}\n`);
96
+ }
97
+ else if (opts.json) {
98
+ (0, formatting_js_1.outputJson)(items);
99
+ }
100
+ else {
101
+ const rows = items.map((w) => [
102
+ w.id,
103
+ w.url,
104
+ w.events.join(", "),
105
+ w.isActive ? "yes" : "no",
106
+ ]);
107
+ (0, formatting_js_1.printTable)(["ID", "DESTINATION URL", "EVENTS", "ACTIVE"], rows);
108
+ process.stdout.write(`\nTotal: ${items.length} webhooks (streamed)\n`);
109
+ }
110
+ return exitCodes_js_1.EXIT_SUCCESS;
111
+ }
112
+ const page = await client.webhooks.list({ page: pageNum, limit: limitNum });
113
+ if (opts.quiet) {
114
+ for (const item of page)
115
+ process.stdout.write(`${item.id}\n`);
116
+ }
117
+ else if (opts.json) {
118
+ (0, formatting_js_1.outputJson)(page);
119
+ }
120
+ else {
121
+ const rows = page.data.map((w) => [
122
+ w.id,
123
+ w.url,
124
+ w.events.join(", "),
125
+ w.isActive ? "yes" : "no",
126
+ ]);
127
+ (0, formatting_js_1.printTable)(["ID", "DESTINATION URL", "EVENTS", "ACTIVE"], rows);
128
+ const p = page.pagination;
129
+ const start = p.total > 0 ? (p.page - 1) * p.limit + 1 : 0;
130
+ const end = Math.min(p.page * p.limit, p.total);
131
+ process.stdout.write(`\nShowing ${start}–${end} of ${p.total} (Page ${p.page} of ${p.totalPages})\n`);
132
+ }
133
+ return exitCodes_js_1.EXIT_SUCCESS;
134
+ }
135
+ async function cmdWebhooksGet(argv, opts, getClient) {
136
+ const webhookId = argv[0];
137
+ if (!webhookId) {
138
+ (0, formatting_js_1.printError)("Missing required <webhook_id> argument.");
139
+ return exitCodes_js_1.EXIT_INVALID_ARGS;
140
+ }
141
+ const client = getClient();
142
+ const whk = await client.webhooks.get(webhookId);
143
+ if (opts.quiet) {
144
+ process.stdout.write(`${whk.id}\n`);
145
+ }
146
+ else if (opts.json) {
147
+ (0, formatting_js_1.outputJson)(whk);
148
+ }
149
+ else {
150
+ (0, formatting_js_1.printDetails)([
151
+ ["ID:", whk.id],
152
+ ["Destination URL:", whk.url],
153
+ ["Subscribed Events:", whk.events.join(", ")],
154
+ ["Active Status:", whk.isActive ? "Active" : "Inactive"],
155
+ ["Description:", whk.description ?? "(none)"],
156
+ ["Created At:", whk.createdAt ?? "(none)"],
157
+ ["Request ID:", whk.requestId],
158
+ ]);
159
+ }
160
+ return exitCodes_js_1.EXIT_SUCCESS;
161
+ }
162
+ async function cmdWebhooksUpdate(argv, opts, getClient) {
163
+ const { values, positionals } = (0, node_util_1.parseArgs)({
164
+ args: argv,
165
+ allowPositionals: true,
166
+ options: {
167
+ url: { type: "string" },
168
+ event: { type: "string", multiple: true },
169
+ description: { type: "string" },
170
+ active: { type: "boolean" },
171
+ inactive: { type: "boolean" },
172
+ },
173
+ });
174
+ const webhookId = positionals[0];
175
+ if (!webhookId) {
176
+ (0, formatting_js_1.printError)("Missing required <webhook_id> argument.");
177
+ return exitCodes_js_1.EXIT_INVALID_ARGS;
178
+ }
179
+ let isActive;
180
+ if (values.active)
181
+ isActive = true;
182
+ if (values.inactive)
183
+ isActive = false;
184
+ const events = values.event ? splitEvents(values.event) : undefined;
185
+ const client = getClient();
186
+ const whk = await client.webhooks.update(webhookId, {
187
+ url: values.url,
188
+ events,
189
+ description: values.description,
190
+ isActive,
191
+ });
192
+ if (opts.quiet) {
193
+ process.stdout.write(`${whk.id}\n`);
194
+ }
195
+ else if (opts.json) {
196
+ (0, formatting_js_1.outputJson)(whk);
197
+ }
198
+ else {
199
+ process.stdout.write(`✓ Webhook updated: ${whk.id}\n\n`);
200
+ (0, formatting_js_1.printDetails)([
201
+ ["ID:", whk.id],
202
+ ["Destination URL:", whk.url],
203
+ ["Subscribed Events:", whk.events.join(", ")],
204
+ ["Active Status:", whk.isActive ? "Active" : "Inactive"],
205
+ ["Description:", whk.description ?? "(none)"],
206
+ ["Request ID:", whk.requestId],
207
+ ]);
208
+ }
209
+ return exitCodes_js_1.EXIT_SUCCESS;
210
+ }
211
+ async function cmdWebhooksDelete(argv, opts, getClient) {
212
+ const { values, positionals } = (0, node_util_1.parseArgs)({
213
+ args: argv,
214
+ allowPositionals: true,
215
+ options: {
216
+ yes: { type: "boolean", short: "y", default: false },
217
+ },
218
+ });
219
+ const webhookId = positionals[0];
220
+ if (!webhookId) {
221
+ (0, formatting_js_1.printError)("Missing required <webhook_id> argument.");
222
+ return exitCodes_js_1.EXIT_INVALID_ARGS;
223
+ }
224
+ if (!values.yes) {
225
+ const confirmed = await (0, formatting_js_1.confirmAction)(`Are you sure you want to delete webhook ${webhookId}?`);
226
+ if (!confirmed) {
227
+ process.stdout.write("Operation cancelled.\n");
228
+ return exitCodes_js_1.EXIT_SUCCESS;
229
+ }
230
+ }
231
+ const client = getClient();
232
+ const res = await client.webhooks.delete(webhookId);
233
+ if (opts.quiet) {
234
+ process.stdout.write(`${webhookId}\n`);
235
+ }
236
+ else if (opts.json) {
237
+ (0, formatting_js_1.outputJson)(res);
238
+ }
239
+ else {
240
+ process.stdout.write(`✓ Webhook deleted: ${webhookId}\n`);
241
+ }
242
+ return exitCodes_js_1.EXIT_SUCCESS;
243
+ }
244
+ async function cmdWebhooksTest(argv, opts, getClient) {
245
+ const webhookId = argv[0];
246
+ if (!webhookId) {
247
+ (0, formatting_js_1.printError)("Missing required <webhook_id> argument.");
248
+ return exitCodes_js_1.EXIT_INVALID_ARGS;
249
+ }
250
+ const client = getClient();
251
+ const res = await client.webhooks.test(webhookId);
252
+ if (opts.quiet) {
253
+ process.stdout.write(`${res.success ? "success" : "failed"}\n`);
254
+ }
255
+ else if (opts.json) {
256
+ (0, formatting_js_1.outputJson)(res);
257
+ }
258
+ else {
259
+ const label = res.success ? "SUCCESS" : "FAILED";
260
+ process.stdout.write(`[${label}] Test ping dispatched to webhook ${webhookId}: ${res.message}\n`);
261
+ }
262
+ return exitCodes_js_1.EXIT_SUCCESS;
263
+ }
264
+ async function cmdWebhooksDeliveries(argv, opts, getClient) {
265
+ const { values, positionals } = (0, node_util_1.parseArgs)({
266
+ args: argv,
267
+ allowPositionals: true,
268
+ options: {
269
+ page: { type: "string" },
270
+ limit: { type: "string" },
271
+ all: { type: "boolean", default: false },
272
+ },
273
+ });
274
+ const webhookId = positionals[0];
275
+ if (!webhookId) {
276
+ (0, formatting_js_1.printError)("Missing required <webhook_id> argument.");
277
+ return exitCodes_js_1.EXIT_INVALID_ARGS;
278
+ }
279
+ const pageNum = values.page ? Number.parseInt(values.page, 10) : 1;
280
+ const limitNum = values.limit ? Number.parseInt(values.limit, 10) : 50;
281
+ const client = getClient();
282
+ if (values.all) {
283
+ const items = await client.webhooks.iterDeliveries(webhookId, { limit: limitNum }).toArray();
284
+ if (opts.quiet) {
285
+ for (const item of items)
286
+ process.stdout.write(`${item.id}\n`);
287
+ }
288
+ else if (opts.json) {
289
+ (0, formatting_js_1.outputJson)(items);
290
+ }
291
+ else {
292
+ const rows = items.map((d) => [
293
+ d.id,
294
+ d.event,
295
+ d.statusCode ?? "-",
296
+ d.success ? "SUCCESS" : "FAILED",
297
+ `${d.durationMs ?? 0}ms`,
298
+ ]);
299
+ (0, formatting_js_1.printTable)(["ID", "EVENT", "STATUS CODE", "OUTCOME", "DURATION"], rows);
300
+ process.stdout.write(`\nTotal: ${items.length} deliveries (streamed)\n`);
301
+ }
302
+ return exitCodes_js_1.EXIT_SUCCESS;
303
+ }
304
+ const page = await client.webhooks.deliveries(webhookId, { page: pageNum, limit: limitNum });
305
+ if (opts.quiet) {
306
+ for (const item of page)
307
+ process.stdout.write(`${item.id}\n`);
308
+ }
309
+ else if (opts.json) {
310
+ (0, formatting_js_1.outputJson)(page);
311
+ }
312
+ else {
313
+ const rows = page.data.map((d) => [
314
+ d.id,
315
+ d.event,
316
+ d.statusCode ?? "-",
317
+ d.success ? "SUCCESS" : "FAILED",
318
+ `${d.durationMs ?? 0}ms`,
319
+ ]);
320
+ (0, formatting_js_1.printTable)(["ID", "EVENT", "STATUS CODE", "OUTCOME", "DURATION"], rows);
321
+ const p = page.pagination;
322
+ const start = p.total > 0 ? (p.page - 1) * p.limit + 1 : 0;
323
+ const end = Math.min(p.page * p.limit, p.total);
324
+ process.stdout.write(`\nShowing ${start}–${end} of ${p.total} (Page ${p.page} of ${p.totalPages})\n`);
325
+ }
326
+ return exitCodes_js_1.EXIT_SUCCESS;
327
+ }
328
+ async function cmdWebhooksRetry(argv, opts, getClient) {
329
+ const webhookId = argv[0];
330
+ const deliveryId = argv[1];
331
+ if (!webhookId || !deliveryId) {
332
+ (0, formatting_js_1.printError)("Usage: trimlynk webhooks retry <webhook_id> <delivery_id>");
333
+ return exitCodes_js_1.EXIT_INVALID_ARGS;
334
+ }
335
+ const client = getClient();
336
+ const res = await client.webhooks.retry(webhookId, deliveryId);
337
+ if (opts.quiet) {
338
+ process.stdout.write(`${deliveryId}\n`);
339
+ }
340
+ else if (opts.json) {
341
+ (0, formatting_js_1.outputJson)(res);
342
+ }
343
+ else {
344
+ process.stdout.write(`✓ Webhook delivery ${deliveryId} queued for retry\n`);
345
+ }
346
+ return exitCodes_js_1.EXIT_SUCCESS;
347
+ }
348
+ async function discoverTunnel(customTunnelUrl) {
349
+ if (customTunnelUrl) {
350
+ return { publicUrl: customTunnelUrl.replace(/\/+$/, ""), source: "CLI argument" };
351
+ }
352
+ for (const port of [4040, 4041]) {
353
+ try {
354
+ const resp = await fetch(`http://127.0.0.1:${port}/api/tunnels`, {
355
+ headers: { Accept: "application/json" },
356
+ signal: AbortSignal.timeout(1200),
357
+ });
358
+ if (!resp.ok)
359
+ continue;
360
+ const data = (await resp.json());
361
+ for (const t of data.tunnels ?? []) {
362
+ const pub = String(t.public_url ?? "");
363
+ if (pub.startsWith("https://") || pub.startsWith("http://")) {
364
+ return {
365
+ publicUrl: pub.replace(/\/+$/, ""),
366
+ localAddr: t.config?.addr,
367
+ source: `ngrok agent (port ${port})`,
368
+ };
369
+ }
370
+ }
371
+ }
372
+ catch {
373
+ continue;
374
+ }
375
+ }
376
+ return null;
377
+ }
378
+ async function cmdWebhooksListen(argv, opts, getClient) {
379
+ const { values } = (0, node_util_1.parseArgs)({
380
+ args: argv,
381
+ allowPositionals: false,
382
+ options: {
383
+ "forward-to": { type: "string", short: "f" },
384
+ "tunnel-url": { type: "string", short: "t" },
385
+ path: { type: "string", default: "/api/webhooks" },
386
+ event: { type: "string", short: "e", multiple: true },
387
+ "poll-interval": { type: "string", default: "1.0" },
388
+ "max-events": { type: "string" },
389
+ once: { type: "boolean", default: false },
390
+ },
391
+ });
392
+ const rawPath = values.path || "/api/webhooks";
393
+ const path = rawPath.startsWith("/") ? rawPath : `/${rawPath}`;
394
+ const tunnelInfo = await discoverTunnel(values["tunnel-url"]);
395
+ if (!tunnelInfo) {
396
+ (0, formatting_js_1.printError)("Could not detect an active public tunnel.\n" +
397
+ "Either start ngrok (e.g. 'ngrok http 8000') or pass an explicit tunnel URL:\n" +
398
+ " trimlynk webhooks listen --tunnel-url https://your-domain.ngrok-free.dev");
399
+ return exitCodes_js_1.EXIT_INVALID_ARGS;
400
+ }
401
+ const webhookUrl = `${tunnelInfo.publicUrl}${path}`;
402
+ let forwardTo = values["forward-to"];
403
+ if (!forwardTo && tunnelInfo.localAddr) {
404
+ forwardTo = `${tunnelInfo.localAddr.replace(/\/+$/, "")}${path}`;
405
+ }
406
+ let events = splitEvents(values.event);
407
+ if (events.length === 0) {
408
+ events = ["link.created", "link.updated", "click.created", "conversion.created"];
409
+ }
410
+ const client = getClient();
411
+ const ts = new Date().toISOString().replace("T", " ").slice(0, 19);
412
+ const webhook = await client.webhooks.create({
413
+ url: webhookUrl,
414
+ events,
415
+ description: `[CLI Listen] Session started at ${ts}`,
416
+ });
417
+ if (opts.quiet) {
418
+ process.stdout.write(`${webhook.secret ?? webhook.id}\n`);
419
+ }
420
+ else if (opts.json) {
421
+ (0, formatting_js_1.outputJson)({
422
+ status: "listening",
423
+ webhook_id: webhook.id,
424
+ url: webhook.url,
425
+ secret: webhook.secret,
426
+ events: webhook.events,
427
+ forward_to: forwardTo,
428
+ });
429
+ }
430
+ else {
431
+ process.stdout.write(`${"=".repeat(68)}\n`);
432
+ process.stdout.write("⚡ TrimLynk Webhook Listener Active\n");
433
+ process.stdout.write(`${"=".repeat(68)}\n`);
434
+ (0, formatting_js_1.printDetails)([
435
+ ["Webhook ID:", webhook.id],
436
+ ["Tunnel URL:", webhook.url],
437
+ ["Detected via:", tunnelInfo.source],
438
+ ["Forwarding to:", forwardTo ?? "(direct to tunnel destination)"],
439
+ ["Signing Secret:", webhook.secret ?? "(none)"],
440
+ ["Subscribed:", webhook.events.join(", ")],
441
+ ]);
442
+ process.stdout.write("\n> Ready! Watching for incoming webhook dispatches...\n");
443
+ process.stdout.write(`> Press Ctrl+C to stop listening and unregister webhook.\n${"-".repeat(68)}\n`);
444
+ }
445
+ const seenIds = new Set();
446
+ const pollMs = Math.max(500, Number.parseFloat(values["poll-interval"] || "1.0") * 1000);
447
+ const maxEvents = values.once
448
+ ? 1
449
+ : values["max-events"] !== undefined
450
+ ? Number.parseInt(values["max-events"], 10)
451
+ : undefined;
452
+ let eventsReceived = 0;
453
+ let stopped = false;
454
+ const onSigInt = () => {
455
+ stopped = true;
456
+ };
457
+ process.once("SIGINT", onSigInt);
458
+ try {
459
+ while (!stopped) {
460
+ try {
461
+ const page = await client.webhooks.deliveries(webhook.id, { page: 1, limit: 20 });
462
+ for (const delivery of [...page.data].reverse()) {
463
+ if (!seenIds.has(delivery.id)) {
464
+ seenIds.add(delivery.id);
465
+ eventsReceived += 1;
466
+ const statusTag = delivery.success
467
+ ? `[${delivery.statusCode ?? 200}]`
468
+ : `[${delivery.statusCode ?? "ERR"}]`;
469
+ const durStr = `(${delivery.durationMs ?? 0}ms)`;
470
+ const nowStr = new Date().toISOString().replace("T", " ").slice(0, 19);
471
+ if (opts.json) {
472
+ (0, formatting_js_1.outputJson)(delivery);
473
+ }
474
+ else if (opts.quiet) {
475
+ process.stdout.write(`${delivery.event} ${delivery.id}\n`);
476
+ }
477
+ else {
478
+ const outcome = delivery.success ? "SUCCESS" : "FAILED";
479
+ process.stdout.write(`${nowStr} ${statusTag.padEnd(7)} ${delivery.event.padEnd(22)} -> ${outcome} ${durStr}\n`);
480
+ }
481
+ if (maxEvents !== undefined && eventsReceived >= maxEvents) {
482
+ stopped = true;
483
+ break;
484
+ }
485
+ }
486
+ }
487
+ }
488
+ catch {
489
+ // Ignore transient poll errors
490
+ }
491
+ if (stopped || (maxEvents !== undefined && eventsReceived >= maxEvents)) {
492
+ break;
493
+ }
494
+ await new Promise((resolve) => setTimeout(resolve, pollMs));
495
+ }
496
+ }
497
+ finally {
498
+ process.removeListener("SIGINT", onSigInt);
499
+ try {
500
+ await client.webhooks.delete(webhook.id);
501
+ if (!opts.quiet && !opts.json) {
502
+ process.stdout.write(`\n✓ Cleaned up temporary webhook ${webhook.id}. Bye!\n`);
503
+ }
504
+ }
505
+ catch {
506
+ // Ignore cleanup failures
507
+ }
508
+ }
509
+ return exitCodes_js_1.EXIT_SUCCESS;
510
+ }
@@ -0,0 +1,10 @@
1
+ export declare const CONFIG_FILE_NAME = "config.json";
2
+ export declare const DEFAULT_ENVIRONMENT = "live";
3
+ export declare const DEFAULT_BASE_URL = "https://api.trimlynk.com";
4
+ export declare function getConfigFile(homeOverride?: string): string;
5
+ export declare function readConfig(homeOverride?: string): Record<string, unknown>;
6
+ export declare function writeConfig(config: Record<string, unknown>, homeOverride?: string): void;
7
+ export declare function getConfigValue(key: string, homeOverride?: string): string | null;
8
+ export declare function setConfigValue(key: string, value: string, homeOverride?: string): void;
9
+ export declare function resolveEnvironment(cliArg?: string | null, homeOverride?: string): string;
10
+ export declare function resolveBaseUrl(cliArg?: string | null, homeOverride?: string): string;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_BASE_URL = exports.DEFAULT_ENVIRONMENT = exports.CONFIG_FILE_NAME = void 0;
4
+ exports.getConfigFile = getConfigFile;
5
+ exports.readConfig = readConfig;
6
+ exports.writeConfig = writeConfig;
7
+ exports.getConfigValue = getConfigValue;
8
+ exports.setConfigValue = setConfigValue;
9
+ exports.resolveEnvironment = resolveEnvironment;
10
+ exports.resolveBaseUrl = resolveBaseUrl;
11
+ const node_fs_1 = require("node:fs");
12
+ const node_path_1 = require("node:path");
13
+ const credentials_js_1 = require("./credentials.js");
14
+ exports.CONFIG_FILE_NAME = "config.json";
15
+ exports.DEFAULT_ENVIRONMENT = "live";
16
+ exports.DEFAULT_BASE_URL = "https://api.trimlynk.com";
17
+ function getConfigFile(homeOverride) {
18
+ return (0, node_path_1.join)((0, credentials_js_1.getConfigDir)(homeOverride), exports.CONFIG_FILE_NAME);
19
+ }
20
+ function readConfig(homeOverride) {
21
+ const file = getConfigFile(homeOverride);
22
+ if (!(0, node_fs_1.existsSync)(file))
23
+ return {};
24
+ try {
25
+ const parsed = JSON.parse((0, node_fs_1.readFileSync)(file, "utf8"));
26
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed)
27
+ ? parsed
28
+ : {};
29
+ }
30
+ catch {
31
+ return {};
32
+ }
33
+ }
34
+ function writeConfig(config, homeOverride) {
35
+ const file = getConfigFile(homeOverride);
36
+ (0, node_fs_1.writeFileSync)(file, JSON.stringify(config, null, 2), "utf8");
37
+ }
38
+ function getConfigValue(key, homeOverride) {
39
+ const config = readConfig(homeOverride);
40
+ const val = config[key];
41
+ return val !== undefined && val !== null ? String(val) : null;
42
+ }
43
+ function setConfigValue(key, value, homeOverride) {
44
+ const config = readConfig(homeOverride);
45
+ config[key] = value;
46
+ writeConfig(config, homeOverride);
47
+ }
48
+ function resolveEnvironment(cliArg, homeOverride) {
49
+ if (cliArg && cliArg.trim())
50
+ return cliArg.trim();
51
+ const envVar = (process.env["TRIMLYNK_ENVIRONMENT"] ?? "").trim();
52
+ if (envVar)
53
+ return envVar;
54
+ return getConfigValue("environment", homeOverride) ?? exports.DEFAULT_ENVIRONMENT;
55
+ }
56
+ function resolveBaseUrl(cliArg, homeOverride) {
57
+ if (cliArg && cliArg.trim())
58
+ return cliArg.trim();
59
+ const envVar = (process.env["TRIMLYNK_BASE_URL"] ?? "").trim();
60
+ if (envVar)
61
+ return envVar;
62
+ return getConfigValue("base_url", homeOverride) ?? exports.DEFAULT_BASE_URL;
63
+ }
@@ -0,0 +1,23 @@
1
+ export declare const KEYRING_SERVICE_NAME = "trimlynk";
2
+ export declare const KEYRING_USERNAME = "api_key";
3
+ export declare const CONFIG_DIR_NAME = ".trimlynk";
4
+ export declare const CREDENTIALS_FILE_NAME = "credentials.json";
5
+ export interface KeychainAdapter {
6
+ getPassword(service: string, account: string): string | null;
7
+ setPassword(service: string, account: string, secret: string): boolean;
8
+ deletePassword(service: string, account: string): boolean;
9
+ }
10
+ /**
11
+ * Zero-dependency OS keychain adapter using native OS tools:
12
+ * - macOS: `security` (Keychain)
13
+ * - Linux: `secret-tool` (libsecret / FreeDesktop Secret Service)
14
+ * - Windows: PowerShell DPAPI protected credential blob
15
+ */
16
+ export declare const defaultKeychainAdapter: KeychainAdapter;
17
+ export declare function getConfigDir(homeOverride?: string): string;
18
+ export declare function getCredentialsFile(homeOverride?: string): string;
19
+ export declare function maskApiKey(apiKey?: string | null): string;
20
+ export declare function resolveApiKey(cliArg?: string | null, keychain?: KeychainAdapter, homeOverride?: string): string | null;
21
+ export declare function getCredentialSource(cliArg?: string | null, keychain?: KeychainAdapter, homeOverride?: string): string;
22
+ export declare function saveApiKey(apiKey: string, keychain?: KeychainAdapter, homeOverride?: string): void;
23
+ export declare function deleteApiKey(keychain?: KeychainAdapter, homeOverride?: string): boolean;