@rewritetoday/types 1.0.1 → 1.0.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.
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,130 @@
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 { 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;
91
+ /**
92
+ * Checks whether the event is a {@link WebhookEventType.SMSOTP} or not
93
+ * @param event The event data received from Rewrite
94
+ */
95
+ export declare function isWebhookSMSOTPEvent(event: WebhookEvent): event is WebhookSMSOTPEvent;
96
+ /**
97
+ * Checks whether the event is a {@link WebhookEventType.MessageSent} or not
98
+ * @param event The event data received from Rewrite
99
+ */
100
+ export declare function isWebhookMessageSentEvent(event: WebhookEvent): event is WebhookMessageSentEvent;
101
+ /**
102
+ * Checks whether the event is a {@link WebhookEventType.MessageBatch} or not
103
+ * @param event The event data received from Rewrite
104
+ */
105
+ export declare function isWebhookMessageBatchEvent(event: WebhookEvent): event is WebhookMessageBatchEvent;
106
+ /**
107
+ * Checks whether the event is a {@link WebhookEventType.MessageQueued} or not
108
+ * @param event The event data received from Rewrite
109
+ */
110
+ export declare function isWebhookMessageQueuedEvent(event: WebhookEvent): event is WebhookMessageQueuedEvent;
111
+ /**
112
+ * Checks whether the event is a {@link WebhookEventType.MessageDelivered} or not
113
+ * @param event The event data received from Rewrite
114
+ */
115
+ export declare function isWebhookMessageDeliveredEvent(event: WebhookEvent): event is WebhookMessageDeliveredEvent;
116
+ /**
117
+ * Checks whether the event is a {@link WebhookEventType.MessageScheduled} or not
118
+ * @param event The event data received from Rewrite
119
+ */
120
+ export declare function isWebhookMessageScheduledEvent(event: WebhookEvent): event is WebhookMessageScheduledEvent;
121
+ /**
122
+ * Checks whether the event is a {@link WebhookEventType.MessageFailed} or not
123
+ * @param event The event data received from Rewrite
124
+ */
125
+ export declare function isWebhookMessageFailedEvent(event: WebhookEvent): event is WebhookMessageFailedEvent;
126
+ /**
127
+ * Checks whether the event is a {@link WebhookEventType.MessageCanceled} or not
128
+ * @param event The event data received from Rewrite
129
+ */
130
+ export declare function isWebhookMessageCanceledEvent(event: WebhookEvent): event is WebhookMessageCanceledEvent;
@@ -0,0 +1,57 @@
1
+ import { WebhookEventType } from './resources/webhooks';
2
+ /**
3
+ * Checks whether the event is a {@link WebhookEventType.SMSOTP} or not
4
+ * @param event The event data received from Rewrite
5
+ */
6
+ export function isWebhookSMSOTPEvent(event) {
7
+ return event.type === WebhookEventType.SMSOTP;
8
+ }
9
+ /**
10
+ * Checks whether the event is a {@link WebhookEventType.MessageSent} or not
11
+ * @param event The event data received from Rewrite
12
+ */
13
+ export function isWebhookMessageSentEvent(event) {
14
+ return event.type === WebhookEventType.MessageSent;
15
+ }
16
+ /**
17
+ * Checks whether the event is a {@link WebhookEventType.MessageBatch} or not
18
+ * @param event The event data received from Rewrite
19
+ */
20
+ export function isWebhookMessageBatchEvent(event) {
21
+ return event.type === WebhookEventType.MessageBatch;
22
+ }
23
+ /**
24
+ * Checks whether the event is a {@link WebhookEventType.MessageQueued} or not
25
+ * @param event The event data received from Rewrite
26
+ */
27
+ export function isWebhookMessageQueuedEvent(event) {
28
+ return event.type === WebhookEventType.MessageQueued;
29
+ }
30
+ /**
31
+ * Checks whether the event is a {@link WebhookEventType.MessageDelivered} or not
32
+ * @param event The event data received from Rewrite
33
+ */
34
+ export function isWebhookMessageDeliveredEvent(event) {
35
+ return event.type === WebhookEventType.MessageDelivered;
36
+ }
37
+ /**
38
+ * Checks whether the event is a {@link WebhookEventType.MessageScheduled} or not
39
+ * @param event The event data received from Rewrite
40
+ */
41
+ export function isWebhookMessageScheduledEvent(event) {
42
+ return event.type === WebhookEventType.MessageScheduled;
43
+ }
44
+ /**
45
+ * Checks whether the event is a {@link WebhookEventType.MessageFailed} or not
46
+ * @param event The event data received from Rewrite
47
+ */
48
+ export function isWebhookMessageFailedEvent(event) {
49
+ return event.type === WebhookEventType.MessageFailed;
50
+ }
51
+ /**
52
+ * Checks whether the event is a {@link WebhookEventType.MessageCanceled} or not
53
+ * @param event The event data received from Rewrite
54
+ */
55
+ export function isWebhookMessageCanceledEvent(event) {
56
+ return event.type === WebhookEventType.MessageCanceled;
57
+ }
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.3";
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.3';
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.3",
3
3
  "name": "@rewritetoday/types",
4
4
  "description": "Official Rewrite API typings that are always up to date",
5
5
  "type": "module",