@rewritetoday/types 1.0.0 → 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';
package/dist/v1/rest.d.ts CHANGED
@@ -103,7 +103,10 @@ export interface RESTGetListTemplatesQueryParams extends RESTCursorOptions {
103
103
  /** `POST https://api.rewritetoday.com/v1/templates`. */
104
104
  export type RESTPostCreateTemplateData = APIResponse<Pick<APITemplate, 'id' | 'createdAt'>>;
105
105
  /** `POST https://api.rewritetoday.com/v1/templates`. */
106
- export type RESTPostCreateTemplateBody = Pick<APITemplate, 'name' | 'i18n' | 'content' | 'variables' | 'description'>;
106
+ export interface RESTPostCreateTemplateBody extends Pick<APITemplate, 'name' | 'content' | 'variables' | 'description'> {
107
+ /** Locale-specific overrides available for the template. */
108
+ i18n?: Partial<Record<CountryCode, string>>;
109
+ }
107
110
  /** `PATCH https://api.rewritetoday.com/v1/templates/:id`. */
108
111
  export type RESTPatchUpdateTemplateData = APIResponse<null>;
109
112
  /** `PATCH https://api.rewritetoday.com/v1/templates/:id`. */
@@ -159,7 +162,7 @@ export type RESTPostSendMessageBody = {
159
162
  /** Destination number in E.164 format. */
160
163
  to: string;
161
164
  /** Optional metadata stored with the message. */
162
- tags: APIMessageTag[];
165
+ tags?: APIMessageTag[];
163
166
  /** When provided, Rewrite schedules the message for later delivery. */
164
167
  scheduledAt?: string;
165
168
  /** Optional segmentation rules for long SMS bodies. */
@@ -1,4 +1,4 @@
1
- import type { RESTCursorOptions } from './rest';
1
+ import type { RESTGetListMessagesQueryParams, RESTGetListTemplatesQueryParams, RESTGetListWebhookLogsQueryParams, RESTGetListWebhooksQueryParams } from './rest';
2
2
  export declare const API_BASE_URL = "https://api.rewritetoday.com";
3
3
  export declare const Routes: {
4
4
  otp: {
@@ -13,7 +13,7 @@ export declare const Routes: {
13
13
  /** `POST https://api.rewritetoday.com/v1/messages/batch`. */
14
14
  batch(): string;
15
15
  /** `GET https://api.rewritetoday.com/v1/messages`. */
16
- list(): string;
16
+ list(options?: RESTGetListMessagesQueryParams): string;
17
17
  /** `GET https://api.rewritetoday.com/v1/messages/:id`. */
18
18
  get(id: string): string;
19
19
  /** `POST https://api.rewritetoday.com/v1/messages/:id/cancel`. */
@@ -21,7 +21,7 @@ export declare const Routes: {
21
21
  };
22
22
  webhooks: {
23
23
  /** `GET https://api.rewritetoday.com/v1/webhooks`. */
24
- list(options?: RESTCursorOptions): string;
24
+ list(options?: RESTGetListWebhooksQueryParams): string;
25
25
  /** `POST https://api.rewritetoday.com/v1/webhooks`. */
26
26
  create(): string;
27
27
  /** `PATCH https://api.rewritetoday.com/v1/webhooks/:id`. */
@@ -31,11 +31,11 @@ export declare const Routes: {
31
31
  /** `GET https://api.rewritetoday.com/v1/webhooks/:id`. */
32
32
  get(id: string): string;
33
33
  /** `GET https://api.rewritetoday.com/v1/webhooks/:id/logs`. */
34
- logs(id: string): string;
34
+ logs(id: string, options?: RESTGetListWebhookLogsQueryParams): string;
35
35
  };
36
36
  templates: {
37
37
  /** `GET https://api.rewritetoday.com/v1/templates`. */
38
- list(options?: RESTCursorOptions): string;
38
+ list(options?: RESTGetListTemplatesQueryParams): string;
39
39
  /** `POST https://api.rewritetoday.com/v1/templates`. */
40
40
  create(): string;
41
41
  /** `PATCH https://api.rewritetoday.com/v1/templates/:id`. */
package/dist/v1/routes.js CHANGED
@@ -21,8 +21,8 @@ export const Routes = {
21
21
  return '/messages/batch';
22
22
  },
23
23
  /** `GET https://api.rewritetoday.com/v1/messages`. */
24
- list() {
25
- return '/messages';
24
+ list(options) {
25
+ return `/messages?${createCursorQuery(options)}`;
26
26
  },
27
27
  /** `GET https://api.rewritetoday.com/v1/messages/:id`. */
28
28
  get(id) {
@@ -55,8 +55,8 @@ export const Routes = {
55
55
  return `/webhooks/${id}`;
56
56
  },
57
57
  /** `GET https://api.rewritetoday.com/v1/webhooks/:id/logs`. */
58
- logs(id) {
59
- return `/webhooks/${id}/logs`;
58
+ logs(id, options) {
59
+ return `/webhooks/${id}/logs?${createCursorQuery(options)}`;
60
60
  },
61
61
  },
62
62
  templates: {
@@ -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.0";
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.0';
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.0",
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",