@cstar.help/js 0.12.1 → 0.14.0

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.
@@ -0,0 +1,440 @@
1
+ /** Every webhook event type cStar can dispatch. */
2
+ type WebhookEventType = 'article.created' | 'article.deleted' | 'article.published' | 'article.updated' | 'boss.damaged' | 'boss.defeated' | 'boss.spawned' | 'community_post.commented' | 'community_post.created' | 'community_post.deleted' | 'community_post.status_changed' | 'community_post.updated' | 'community_post.voted' | 'customer.created' | 'customer.deleted' | 'customer.updated' | 'customer_group.created' | 'customer_group.deleted' | 'customer_group.updated' | 'player.achievement_unlocked' | 'player.level_up' | 'player.quest_completed' | 'player.streak_milestone' | 'player.xp_gained' | 'social.high_five_sent' | 'social.one_up_sent' | 'survey.submitted' | 'ticket.assigned' | 'ticket.closed' | 'ticket.created' | 'ticket.deleted' | 'ticket.message_added' | 'ticket.updated';
3
+ /** Payload for `article.created` events. */
4
+ interface ArticleCreatedPayload {
5
+ article: {
6
+ id: string;
7
+ title: string;
8
+ slug: string;
9
+ is_public: boolean;
10
+ created_at: string;
11
+ };
12
+ }
13
+ /** Payload for `article.deleted` events. */
14
+ interface ArticleDeletedPayload {
15
+ id: string;
16
+ }
17
+ /** Payload for `article.published` events. */
18
+ interface ArticlePublishedPayload {
19
+ article: {
20
+ id: string;
21
+ title: string;
22
+ slug: string;
23
+ is_public: boolean;
24
+ published_at: string;
25
+ };
26
+ }
27
+ /** Payload for `article.updated` events. */
28
+ interface ArticleUpdatedPayload {
29
+ article: {
30
+ id: string;
31
+ title: string;
32
+ slug: string;
33
+ is_public: boolean;
34
+ };
35
+ }
36
+ /** Payload for `boss.damaged` events. */
37
+ interface BossDamagedPayload {
38
+ boss: {
39
+ id: string;
40
+ current_hp: number;
41
+ };
42
+ damage: number;
43
+ by: {
44
+ agent_id: string;
45
+ name: string;
46
+ };
47
+ }
48
+ /** Payload for `boss.defeated` events. */
49
+ interface BossDefeatedPayload {
50
+ boss: {
51
+ id: string;
52
+ name: string;
53
+ };
54
+ final_blow_by: {
55
+ agent_id: string;
56
+ name: string;
57
+ };
58
+ team_xp_awarded: number;
59
+ }
60
+ /** Payload for `boss.spawned` events. */
61
+ interface BossSpawnedPayload {
62
+ boss: {
63
+ id: string;
64
+ name: string;
65
+ level: number;
66
+ max_hp: number;
67
+ };
68
+ }
69
+ /** Payload for `community_post.commented` events. */
70
+ interface CommunityPostCommentedPayload {
71
+ comment: {
72
+ id: string;
73
+ post_id: string;
74
+ content: string;
75
+ author_id: string;
76
+ created_at: string;
77
+ };
78
+ }
79
+ /** Payload for `community_post.created` events. */
80
+ interface CommunityPostCreatedPayload {
81
+ post: {
82
+ id: string;
83
+ prefixed_id: string;
84
+ title: string;
85
+ status: string;
86
+ visibility: string;
87
+ topic: string;
88
+ created_at: string;
89
+ };
90
+ }
91
+ /** Payload for `community_post.deleted` events. */
92
+ interface CommunityPostDeletedPayload {
93
+ post: {
94
+ id: string;
95
+ prefixed_id?: string;
96
+ title?: string;
97
+ };
98
+ }
99
+ /** Payload for `community_post.status_changed` events. */
100
+ interface CommunityPostStatusChangedPayload {
101
+ post: {
102
+ id: string;
103
+ title: string;
104
+ status: string;
105
+ visibility: string;
106
+ };
107
+ }
108
+ /** Payload for `community_post.updated` events. */
109
+ interface CommunityPostUpdatedPayload {
110
+ post: {
111
+ id: string;
112
+ title: string;
113
+ status: string;
114
+ visibility: string;
115
+ };
116
+ }
117
+ /** Payload for `community_post.voted` events. */
118
+ interface CommunityPostVotedPayload {
119
+ post: {
120
+ id: string;
121
+ vote_count: number;
122
+ };
123
+ voter: {
124
+ id: string;
125
+ };
126
+ direction: string;
127
+ }
128
+ /** Payload for `customer.created` events. */
129
+ interface CustomerCreatedPayload {
130
+ customer: {
131
+ id: string;
132
+ name: string;
133
+ email: string;
134
+ status: string;
135
+ tags: Array<string>;
136
+ created_at: string;
137
+ };
138
+ }
139
+ /** Payload for `customer.deleted` events. */
140
+ interface CustomerDeletedPayload {
141
+ id: string;
142
+ }
143
+ /** Payload for `customer.updated` events. */
144
+ interface CustomerUpdatedPayload {
145
+ customer: {
146
+ id: string;
147
+ name: string;
148
+ email: string;
149
+ status: string;
150
+ tags: Array<string>;
151
+ };
152
+ changes: {
153
+ tags: {
154
+ from: Array<string>;
155
+ to: Array<string>;
156
+ };
157
+ };
158
+ }
159
+ /** Payload for `customer_group.created` events. */
160
+ interface CustomerGroupCreatedPayload {
161
+ group: {
162
+ id: string;
163
+ name: string;
164
+ color: string;
165
+ member_count: number;
166
+ };
167
+ }
168
+ /** Payload for `customer_group.deleted` events. */
169
+ interface CustomerGroupDeletedPayload {
170
+ customer_group: {
171
+ id: string;
172
+ };
173
+ }
174
+ /** Payload for `customer_group.updated` events. */
175
+ interface CustomerGroupUpdatedPayload {
176
+ group: {
177
+ id: string;
178
+ name: string;
179
+ color: string;
180
+ member_count: number;
181
+ };
182
+ }
183
+ /** Payload for `player.achievement_unlocked` events. */
184
+ interface PlayerAchievementUnlockedPayload {
185
+ player: {
186
+ agent_id: string;
187
+ name: string;
188
+ };
189
+ achievement: {
190
+ id: string;
191
+ name: string;
192
+ xp: number;
193
+ };
194
+ }
195
+ /** Payload for `player.level_up` events. */
196
+ interface PlayerLevelUpPayload {
197
+ player: {
198
+ agent_id: string;
199
+ name: string;
200
+ };
201
+ level: {
202
+ from: number;
203
+ to: number;
204
+ };
205
+ title_unlocked: string;
206
+ }
207
+ /** Payload for `player.quest_completed` events. */
208
+ interface PlayerQuestCompletedPayload {
209
+ player: {
210
+ agent_id: string;
211
+ name: string;
212
+ };
213
+ quest: {
214
+ id: string;
215
+ name: string;
216
+ xp: number;
217
+ };
218
+ }
219
+ /** Payload for `player.streak_milestone` events. */
220
+ interface PlayerStreakMilestonePayload {
221
+ player: {
222
+ agent_id: string;
223
+ name: string;
224
+ };
225
+ streak_days: number;
226
+ milestone: number;
227
+ }
228
+ /** Payload for `player.xp_gained` events. */
229
+ interface PlayerXpGainedPayload {
230
+ player: {
231
+ agent_id: string;
232
+ name: string;
233
+ };
234
+ amount: number;
235
+ reason: string;
236
+ ticket_id: string;
237
+ }
238
+ /** Payload for `social.high_five_sent` events. */
239
+ interface SocialHighFiveSentPayload {
240
+ from_team: {
241
+ id: string;
242
+ name: string;
243
+ };
244
+ to_team: {
245
+ id: string;
246
+ name: string;
247
+ };
248
+ message: string;
249
+ }
250
+ /** Payload for `social.one_up_sent` events. */
251
+ interface SocialOneUpSentPayload {
252
+ from: {
253
+ agent_id: string;
254
+ name: string;
255
+ };
256
+ to: {
257
+ agent_id: string;
258
+ name: string;
259
+ };
260
+ ticket_id: string;
261
+ message: string;
262
+ }
263
+ /** Payload for `survey.submitted` events. */
264
+ interface SurveySubmittedPayload {
265
+ survey: {
266
+ id: string;
267
+ ticket_id: string;
268
+ score: number;
269
+ comment: string;
270
+ submitted_at: string;
271
+ };
272
+ customer: {
273
+ id: string;
274
+ email: string;
275
+ };
276
+ }
277
+ /** Payload for `ticket.assigned` events. */
278
+ interface TicketAssignedPayload {
279
+ ticket: {
280
+ id: string;
281
+ prefixed_id: string;
282
+ };
283
+ assignee: {
284
+ id: string;
285
+ name: string;
286
+ email: string;
287
+ };
288
+ previous_assignee: null;
289
+ }
290
+ /** Payload for `ticket.closed` events. */
291
+ interface TicketClosedPayload {
292
+ ticket: {
293
+ id: string;
294
+ prefixed_id: string;
295
+ title: string;
296
+ status: string;
297
+ priority: string;
298
+ channel: string;
299
+ resolved_at: string;
300
+ };
301
+ changes?: Record<string, {
302
+ from: string;
303
+ to: string;
304
+ }>;
305
+ }
306
+ /** Payload for `ticket.created` events. */
307
+ interface TicketCreatedPayload {
308
+ ticket: {
309
+ id: string;
310
+ prefixed_id: string;
311
+ title: string;
312
+ status: string;
313
+ priority: string;
314
+ channel: string;
315
+ customer_id: string;
316
+ created_at: string;
317
+ };
318
+ customer: {
319
+ id: string;
320
+ name: string;
321
+ email: string;
322
+ };
323
+ }
324
+ /** Payload for `ticket.deleted` events. */
325
+ interface TicketDeletedPayload {
326
+ id: string;
327
+ }
328
+ /** Payload for `ticket.message_added` events. */
329
+ interface TicketMessageAddedPayload {
330
+ ticket: {
331
+ id: string;
332
+ dashboard_url: string | null;
333
+ };
334
+ message: {
335
+ id: string;
336
+ sender: string;
337
+ sender_name: string | null;
338
+ content: string;
339
+ created_at: string;
340
+ };
341
+ customer?: {
342
+ id: string;
343
+ name: string | null;
344
+ email: string | null;
345
+ };
346
+ }
347
+ /** Payload for `ticket.updated` events. */
348
+ interface TicketUpdatedPayload {
349
+ ticket: {
350
+ id: string;
351
+ prefixed_id: string;
352
+ title: string;
353
+ status: string;
354
+ priority: string;
355
+ channel: string;
356
+ };
357
+ changes?: Record<string, {
358
+ from: string;
359
+ to: string;
360
+ } | {
361
+ from: Array<string>;
362
+ to: Array<string>;
363
+ }>;
364
+ }
365
+ /** Maps each event type to its payload interface — used internally for the discriminated union. */
366
+ interface WebhookEventDataMap {
367
+ 'article.created': ArticleCreatedPayload;
368
+ 'article.deleted': ArticleDeletedPayload;
369
+ 'article.published': ArticlePublishedPayload;
370
+ 'article.updated': ArticleUpdatedPayload;
371
+ 'boss.damaged': BossDamagedPayload;
372
+ 'boss.defeated': BossDefeatedPayload;
373
+ 'boss.spawned': BossSpawnedPayload;
374
+ 'community_post.commented': CommunityPostCommentedPayload;
375
+ 'community_post.created': CommunityPostCreatedPayload;
376
+ 'community_post.deleted': CommunityPostDeletedPayload;
377
+ 'community_post.status_changed': CommunityPostStatusChangedPayload;
378
+ 'community_post.updated': CommunityPostUpdatedPayload;
379
+ 'community_post.voted': CommunityPostVotedPayload;
380
+ 'customer.created': CustomerCreatedPayload;
381
+ 'customer.deleted': CustomerDeletedPayload;
382
+ 'customer.updated': CustomerUpdatedPayload;
383
+ 'customer_group.created': CustomerGroupCreatedPayload;
384
+ 'customer_group.deleted': CustomerGroupDeletedPayload;
385
+ 'customer_group.updated': CustomerGroupUpdatedPayload;
386
+ 'player.achievement_unlocked': PlayerAchievementUnlockedPayload;
387
+ 'player.level_up': PlayerLevelUpPayload;
388
+ 'player.quest_completed': PlayerQuestCompletedPayload;
389
+ 'player.streak_milestone': PlayerStreakMilestonePayload;
390
+ 'player.xp_gained': PlayerXpGainedPayload;
391
+ 'social.high_five_sent': SocialHighFiveSentPayload;
392
+ 'social.one_up_sent': SocialOneUpSentPayload;
393
+ 'survey.submitted': SurveySubmittedPayload;
394
+ 'ticket.assigned': TicketAssignedPayload;
395
+ 'ticket.closed': TicketClosedPayload;
396
+ 'ticket.created': TicketCreatedPayload;
397
+ 'ticket.deleted': TicketDeletedPayload;
398
+ 'ticket.message_added': TicketMessageAddedPayload;
399
+ 'ticket.updated': TicketUpdatedPayload;
400
+ }
401
+ /** A webhook event delivered to your endpoint. */
402
+ interface WebhookEvent<T extends WebhookEventType = WebhookEventType> {
403
+ /** Unique event ID (idempotency key). */
404
+ id: string;
405
+ /** Event type. */
406
+ type: T;
407
+ /** When the event was created (ISO 8601). */
408
+ created_at: string;
409
+ /** Team that owns this event. */
410
+ team_id: string;
411
+ /** Event-specific payload. */
412
+ data: WebhookEventDataMap[T];
413
+ }
414
+
415
+ /** Thrown when webhook signature verification fails. */
416
+ declare class WebhookSignatureVerificationError extends Error {
417
+ constructor(message: string);
418
+ }
419
+ /**
420
+ * Verify a webhook signature and parse the event.
421
+ *
422
+ * Uses the Web Crypto API — works in browsers, Cloudflare Workers, Vercel Edge,
423
+ * Deno, Bun, and Node.js 18+. This is the recommended API for all runtimes.
424
+ *
425
+ * For a synchronous Node-only variant, import from `@cstar.help/js/webhook/node`.
426
+ *
427
+ * @param rawBody - Raw request body as a string (NOT parsed JSON)
428
+ * @param signature - Value of the `X-Signature` header
429
+ * @param secret - Your webhook endpoint's secret (`whsec_*`)
430
+ * @returns Parsed and verified webhook event
431
+ * @throws {WebhookSignatureVerificationError} If verification fails
432
+ */
433
+ declare function constructEventAsync(rawBody: string, signature: string, secret: string): Promise<WebhookEvent>;
434
+ /**
435
+ * Verify an HMAC-SHA256 signature using the Web Crypto API.
436
+ * Constant-time comparison to prevent timing attacks.
437
+ */
438
+ declare function verifySignatureAsync(rawBody: string, signature: string, secret: string): Promise<boolean>;
439
+
440
+ export { type WebhookEvent, WebhookSignatureVerificationError, constructEventAsync, verifySignatureAsync };
@@ -1,10 +1,3 @@
1
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
- }) : x)(function(x) {
4
- if (typeof require !== "undefined") return require.apply(this, arguments);
5
- throw Error('Dynamic require of "' + x + '" is not supported');
6
- });
7
-
8
1
  // src/webhook/index.ts
9
2
  var WebhookSignatureVerificationError = class extends Error {
10
3
  constructor(message) {
@@ -12,14 +5,6 @@ var WebhookSignatureVerificationError = class extends Error {
12
5
  this.name = "WebhookSignatureVerificationError";
13
6
  }
14
7
  };
15
- function constructEvent(rawBody, signature, secret) {
16
- if (!verifySignature(rawBody, signature, secret)) {
17
- throw new WebhookSignatureVerificationError(
18
- "Webhook signature verification failed. Ensure you are using the raw request body and the correct endpoint secret."
19
- );
20
- }
21
- return JSON.parse(rawBody);
22
- }
23
8
  async function constructEventAsync(rawBody, signature, secret) {
24
9
  const valid = await verifySignatureAsync(rawBody, signature, secret);
25
10
  if (!valid) {
@@ -29,12 +14,6 @@ async function constructEventAsync(rawBody, signature, secret) {
29
14
  }
30
15
  return JSON.parse(rawBody);
31
16
  }
32
- function verifySignature(rawBody, signature, secret) {
33
- const crypto2 = __require("crypto");
34
- const expected = "sha256=" + crypto2.createHmac("sha256", secret).update(rawBody).digest("hex");
35
- if (signature.length !== expected.length) return false;
36
- return crypto2.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
37
- }
38
17
  async function verifySignatureAsync(rawBody, signature, secret) {
39
18
  const encoder = new TextEncoder();
40
19
  const key = await crypto.subtle.importKey(
@@ -55,8 +34,6 @@ async function verifySignatureAsync(rawBody, signature, secret) {
55
34
  }
56
35
  export {
57
36
  WebhookSignatureVerificationError,
58
- constructEvent,
59
37
  constructEventAsync,
60
- verifySignature,
61
38
  verifySignatureAsync
62
39
  };
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/webhook/node.ts
21
+ var node_exports = {};
22
+ __export(node_exports, {
23
+ WebhookSignatureVerificationError: () => WebhookSignatureVerificationError,
24
+ constructEvent: () => constructEvent,
25
+ verifySignature: () => verifySignature
26
+ });
27
+ module.exports = __toCommonJS(node_exports);
28
+ var import_node_crypto = require("crypto");
29
+
30
+ // src/webhook/index.ts
31
+ var WebhookSignatureVerificationError = class extends Error {
32
+ constructor(message) {
33
+ super(message);
34
+ this.name = "WebhookSignatureVerificationError";
35
+ }
36
+ };
37
+
38
+ // src/webhook/node.ts
39
+ function constructEvent(rawBody, signature, secret) {
40
+ if (!verifySignature(rawBody, signature, secret)) {
41
+ throw new WebhookSignatureVerificationError(
42
+ "Webhook signature verification failed. Ensure you are using the raw request body and the correct endpoint secret."
43
+ );
44
+ }
45
+ return JSON.parse(rawBody);
46
+ }
47
+ function verifySignature(rawBody, signature, secret) {
48
+ const expected = "sha256=" + (0, import_node_crypto.createHmac)("sha256", secret).update(rawBody).digest("hex");
49
+ if (signature.length !== expected.length) return false;
50
+ return (0, import_node_crypto.timingSafeEqual)(Buffer.from(signature), Buffer.from(expected));
51
+ }
52
+ // Annotate the CommonJS export names for ESM import in node:
53
+ 0 && (module.exports = {
54
+ WebhookSignatureVerificationError,
55
+ constructEvent,
56
+ verifySignature
57
+ });