@rewritetoday/types 1.0.1 → 1.0.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.
package/README.md CHANGED
@@ -43,6 +43,9 @@ Types used in direct API requests.
43
43
 
44
44
  - Suffix `Data` → data returned by the API
45
45
  E.g.: `RESTGetListWebhooksData`
46
+
47
+ - Prefix `Webhook*`
48
+ Represents any data that come from a webhook.
46
49
 
47
50
  <div align="center">
48
51
 
@@ -66,6 +69,35 @@ async function fetchHooks() {
66
69
 
67
70
  <div align="center">
68
71
 
72
+ ### Typing webhook events
73
+
74
+ We are using [Bun](https://elysiajs.com/) here as example, but you can use it however you want.
75
+
76
+ </div>
77
+
78
+ ```ts
79
+ import { type WebhookEvent, WebhookEventType } from '@rewritetoday/types/v1';
80
+
81
+ const server = Bun.serve({
82
+ port: 3000,
83
+ routes: {
84
+ async '/webhooks/rewrite'(request) {
85
+ const event: WebhookEvent = await request.json();
86
+
87
+ switch (event.type) {
88
+ case WebhookEventType.MessageSent:
89
+ console.log('Hey, a new message!');
90
+
91
+ return Response.json({});
92
+ case ...
93
+ }
94
+ },
95
+ },
96
+ });
97
+ ```
98
+
99
+ <div align="center">
100
+
69
101
  ### Using Route Builders
70
102
 
71
103
  </div>
@@ -7,3 +7,4 @@ export * from './resources/templates';
7
7
  export * from './resources/webhooks';
8
8
  export * from './rest';
9
9
  export { API_BASE_URL, Routes } from './routes';
10
+ export * from './webhooks';
package/dist/v1/index.js CHANGED
@@ -7,3 +7,4 @@ export * from './resources/templates';
7
7
  export * from './resources/webhooks';
8
8
  export * from './rest';
9
9
  export { API_BASE_URL, Routes } from './routes';
10
+ export * from './webhooks';
@@ -0,0 +1,90 @@
1
+ import type { Snowflake } from './resources/globals';
2
+ import type { APIMessage, MessageStatus, MessageType } from './resources/message';
3
+ import type { APIOTPMessage } from './resources/otp';
4
+ import type { WebhookEventType } from './resources/webhooks';
5
+ export interface WebhookBase<Type extends WebhookEventType, Data extends object> {
6
+ /** Event name. See {@link WebhookEventType}. */
7
+ type: Type;
8
+ /** The data of the event. */
9
+ data: Data & {
10
+ /** The ID of the project that sent the OTP message. See {@link Snowflake}. */
11
+ projectId: Snowflake;
12
+ };
13
+ /** Webhook event identifier. See {@link Snowflake}. */
14
+ id: Snowflake;
15
+ /** Timestamp when Rewrite sent the event. */
16
+ createdAt: string;
17
+ }
18
+ /** https://docs.rewritetoday.com/en/webhooks/events/sms-otp */
19
+ export type WebhookSMSOTPEvent = WebhookBase<WebhookEventType.SMSOTP, Omit<APIMessage, 'type' | 'status'> & {
20
+ /** OTP data. */
21
+ otp: Pick<APIOTPMessage, 'prefix' | 'expiresAt'>;
22
+ /** The type of the message. Always {@link MessageType.OTP} */
23
+ type: MessageType.OTP;
24
+ /** Latest delivery status known by Rewrite. Always {@link MessageStatus.Sent} */
25
+ status: MessageStatus.Sent;
26
+ }>;
27
+ /** https://docs.rewritetoday.com/en/webhooks/events/message-sent */
28
+ export type WebhookMessageSentEvent = WebhookBase<WebhookEventType.MessageSent, Omit<APIMessage, 'type' | 'status'> & {
29
+ /** The type of the message. Always {@link MessageType.SMS} */
30
+ type: MessageType.SMS;
31
+ /** Latest delivery status known by Rewrite. Always {@link MessageStatus.Sent} */
32
+ status: MessageStatus.Sent;
33
+ }>;
34
+ /** https://docs.rewritetoday.com/en/webhooks/events/message-batch */
35
+ export type WebhookMessageBatchEvent = WebhookBase<WebhookEventType.MessageBatch, {
36
+ /** Message ID in {@link Snowflake} format. */
37
+ id: Snowflake;
38
+ /**
39
+ * The IDs of the messages in {@link Snowflake} format that were sent.
40
+ *
41
+ * @remarks Can be longer than the original number of request items when some entries were segmented into multiple SMS parts.
42
+ * @see {@link https://docs.rewritetoday.com/en/webhooks/events/message-batch}
43
+ */
44
+ ids: Snowflake[];
45
+ }>;
46
+ /** https://docs.rewritetoday.com/en/webhooks/events/message-queued */
47
+ export type WebhookMessageQueuedEvent = WebhookBase<WebhookEventType.MessageQueued, Omit<APIMessage, 'type' | 'status'> & {
48
+ /** The type of the message. Always {@link MessageType.SMS} */
49
+ type: MessageType.SMS;
50
+ /** Latest delivery status known by Rewrite. Always {@link MessageStatus.Queued} */
51
+ status: MessageStatus.Queued;
52
+ }>;
53
+ /**
54
+ * https://docs.rewritetoday.com/en/webhooks/events/message-delivered
55
+ *
56
+ * @wip
57
+ */
58
+ export type WebhookMessageDeliveredEvent = WebhookBase<WebhookEventType.MessageDelivered, never>;
59
+ /** https://docs.rewritetoday.com/en/webhooks/events/message-scheduled */
60
+ export type WebhookMessageScheduledEvent = WebhookBase<WebhookEventType.MessageScheduled, Omit<APIMessage, 'type' | 'status' | 'scheduledAt'> & {
61
+ /** Scheduled send time, when the message was delayed intentionally. */
62
+ scheduledAt: string;
63
+ /** The type of the message. Always {@link MessageType.SMS} */
64
+ type: MessageType.SMS;
65
+ /** Latest delivery status known by Rewrite. Always {@link MessageStatus.Scheduled} */
66
+ status: MessageStatus.Scheduled;
67
+ }>;
68
+ /** https://docs.rewritetoday.com/en/webhooks/events/message-failed */
69
+ export type WebhookMessageFailedEvent = WebhookBase<WebhookEventType.MessageFailed, Omit<APIMessage, 'status'> & {
70
+ /** Latest delivery status known by Rewrite. Always {@link MessageStatus.Failed} */
71
+ status: MessageStatus.Failed;
72
+ /** The error explaining why the message failed. */
73
+ error: {
74
+ /** A human-readable error code. */
75
+ code: string;
76
+ /** Internal message error. */
77
+ message: string;
78
+ };
79
+ }>;
80
+ /** https://docs.rewritetoday.com/en/webhooks/events/message-canceled */
81
+ export type WebhookMessageCanceledEvent = WebhookBase<WebhookEventType.MessageCanceled, Omit<APIMessage, 'type' | 'status' | ('scheduledAt' & {
82
+ /** Scheduled send time, when the message was delayed intentionally. */
83
+ scheduledAt: string;
84
+ /** The type of the message. Always {@link MessageType.SMS} */
85
+ type: MessageType.SMS;
86
+ /** Latest delivery status known by Rewrite. Always {@link MessageStatus.Canceled} */
87
+ status: MessageStatus.Canceled;
88
+ })>>;
89
+ /** https://docs.rewritetoday.com/en/webhooks */
90
+ export type WebhookEvent = WebhookSMSOTPEvent | WebhookMessageSentEvent | WebhookMessageBatchEvent | WebhookMessageQueuedEvent | WebhookMessageDeliveredEvent | WebhookMessageScheduledEvent | WebhookMessageFailedEvent | WebhookMessageCanceledEvent;
@@ -0,0 +1 @@
1
+ export {};
package/dist/version.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Current version of [@rewritetoday/types](https://www.npmjs.com/package/@rewritetoday/types).
3
3
  */
4
- export declare const version: "1.0.1";
4
+ export declare const version: "1.0.2";
5
5
  /**
6
6
  * Current Rewrite API version.
7
7
  */
package/dist/version.js CHANGED
@@ -2,7 +2,7 @@
2
2
  /**
3
3
  * Current version of [@rewritetoday/types](https://www.npmjs.com/package/@rewritetoday/types).
4
4
  */
5
- export const version = '1.0.1';
5
+ export const version = '1.0.2';
6
6
  /**
7
7
  * Current Rewrite API version.
8
8
  */
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.1",
2
+ "version": "1.0.2",
3
3
  "name": "@rewritetoday/types",
4
4
  "description": "Official Rewrite API typings that are always up to date",
5
5
  "type": "module",