Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.

Commit 7953857

Browse files
committed
feat: add new resources
1 parent cf734f7 commit 7953857

15 files changed

Lines changed: 234 additions & 264 deletions

File tree

README.md

Lines changed: 13 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
# Rewrite Node SDK
44

5-
[`@rewritejs/sdk`](https://www.npmjs.com/package/@rewritejs/sdk), the official Node.js/TypeScript SDK for the Rewrite API.
5+
[`@rewritetoday/sdk`](https://www.npmjs.com/package/@rewritetoday/sdk), the official Node.js/TypeScript SDK for the Rewrite API.
66

7-
It wraps authentication, typed REST calls, and resource helpers on top of [`@rewritejs/rest`](https://www.npmjs.com/package/@rewritejs/rest) and [`@rewritejs/types`](https://www.npmjs.com/package/@rewritejs/types).
7+
It wraps authentication, typed REST calls, and resource helpers on top of [`@rewritetoday/rest`](https://www.npmjs.com/package/@rewritetoday/rest) and [`@rewritetoday/types`](https://www.npmjs.com/package/@rewritetoday/types).
88

99
<img src="https://cdn.rewritetoday.com/assets/banners/node-sdk.png" width="100%" alt="Rewrite Banner"/>
1010

@@ -15,13 +15,13 @@ Use your preferred package manager:
1515
</div>
1616

1717
```bash
18-
bun add @rewritejs/sdk
18+
bun add @rewritetoday/sdk
1919
# Or
20-
npm install @rewritejs/sdk
20+
npm install @rewritetoday/sdk
2121
# Or
22-
pnpm add @rewritejs/sdk
22+
pnpm add @rewritetoday/sdk
2323
# Or
24-
yarn add @rewritejs/sdk
24+
yarn add @rewritetoday/sdk
2525
```
2626

2727
<div align="center">
@@ -31,13 +31,13 @@ yarn add @rewritejs/sdk
3131
</div>
3232

3333
```ts
34-
import { Rewrite } from '@rewritejs/sdk';
34+
import { Rewrite } from '@rewritetoday/sdk';
3535

3636
const rewrite = new Rewrite(process.env.REWRITE_API_KEY!);
3737

38-
const project = await rewrite.projects.get('123456789012345678');
38+
const { data } = await rewrite.webhooks.get('123456789012345678');
3939

40-
console.log({ project });
40+
console.log({ data });
4141
```
4242

4343
<div align="center">
@@ -49,10 +49,10 @@ You can pass the API key directly or use the full options object.
4949
</div>
5050

5151
```ts
52-
import { Rewrite } from '@rewritejs/sdk';
52+
import { Rewrite } from '@rewritetoday/sdk';
5353

5454
const advanced = new Rewrite({
55-
secret: 'rw_live_xxx',
55+
secret: 'rw_xxx',
5656
rest: {
5757
timeout: 10_000,
5858
headers: {
@@ -68,98 +68,14 @@ const advanced = new Rewrite({
6868

6969
<div align="center">
7070

71-
### Projects
72-
73-
</div>
74-
75-
```ts
76-
const project = await rewrite.projects.create({ name: 'AbacatePay Notifications' });
77-
78-
console.log({ project });
79-
```
80-
81-
<div align="center">
82-
83-
### Templates
84-
85-
</div>
86-
87-
```ts
88-
const project = '123456789012345678';
89-
90-
const created = await rewrite.templates.create({
91-
project,
92-
name: 'welcome_sms',
93-
content: 'Hey {{name}}, welcome to {{company}}!',
94-
variables: [
95-
{ name: 'name', fallback: 'customer' },
96-
{ name: 'company', fallback: 'Rewrite' },
97-
],
98-
});
99-
100-
const templates = await rewrite.templates.list(project, { limit: 20 });
101-
102-
console.log({ templates });
103-
```
104-
105-
<div align="center">
106-
107-
### Webhooks
108-
109-
</div>
110-
111-
```ts
112-
import { WebhookEventType, WebhookStatus } from '@rewritejs/types';
113-
114-
const project = '123456789012345678';
115-
116-
const hook = await rewrite.webhooks.create({
117-
project,
118-
name: 'delivery-events',
119-
endpoint: 'https://example.com/rewrite/webhooks',
120-
events: [WebhookEventType.SMSDelivered, WebhookEventType.SMSFailed],
121-
});
122-
123-
await rewrite.webhooks.update(hook.data.id, {
124-
project,
125-
status: WebhookStatus.Inactive,
126-
});
127-
128-
const hooks = await rewrite.webhooks.list(project, { limit: 10 });
129-
130-
console.log({ hooks });
131-
```
132-
133-
<div align="center">
134-
135-
### API Keys
136-
137-
</div>
138-
139-
```ts
140-
import { APIKeyScope } from '@rewritejs/types';
141-
142-
const project = '123456789012345678';
143-
144-
const key = await rewrite.apiKeys.create({
145-
project,
146-
name: 'backend-prod',
147-
scopes: [APIKeyScope.ReadProject, APIKeyScope.ReadTemplates],
148-
});
149-
150-
console.log({ key });
151-
```
152-
153-
<div align="center">
154-
15571
## Error Handling
15672

157-
Requests run through `@rewritejs/rest`. HTTP failures can throw `HTTPError`.
73+
Requests run through `@rewritetoday/rest`. HTTP failures can throw `HTTPError`.
15874

15975
</div>
16076

16177
```ts
162-
import { HTTPError } from '@rewritejs/rest';
78+
import { HTTPError } from '@rewritetoday/rest';
16379

16480
try {
16581
await rewrite.projects.get('invalid_id');
@@ -172,8 +88,6 @@ try {
17288

17389
<div align="center">
17490

175-
---
176-
17791
Made with 🤍 by the Rewrite team. <br/>
17892
SMS the way it should be.
17993

bun.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { REST } from '@rewritejs/rest';
1+
import { REST } from '@rewritetoday/rest';
22
import { APIKeys } from './resources/apiKeys';
3-
import { Projects } from './resources/projects';
3+
import { Logs } from './resources/logs';
4+
import { Messages } from './resources/message';
5+
import { OTP } from './resources/otp';
46
import { Templates } from './resources/templates';
57
import { Webhooks } from './resources/webhooks';
68
import type { RewriteOptions } from './types';
@@ -15,18 +17,24 @@ export class Rewrite {
1517
/** Resolved API secret used for authentication. */
1618
private readonly secret: string;
1719

18-
/** API keys resource client. */
19-
public readonly apiKeys: APIKeys;
20+
/** OTP resource client. */
21+
public readonly otp: OTP;
2022

21-
/** Projects resource client. */
22-
public readonly projects: Projects;
23+
/** Webhook logs resource client. */
24+
public readonly logs: Logs;
2325

24-
/** Templates resource client. */
25-
public readonly templates: Templates;
26+
/** API key resource client. */
27+
public readonly apiKeys: APIKeys;
28+
29+
/** Messages resource client. */
30+
public readonly messages: Messages;
2631

27-
/** Webhooks resource client. */
32+
/** Webhook resource client. */
2833
public readonly webhooks: Webhooks;
2934

35+
/** Template resource client. */
36+
public readonly templates: Templates;
37+
3038
/**
3139
* Creates a new Rewrite client instance.
3240
*/
@@ -44,9 +52,11 @@ export class Rewrite {
4452
auth: this.secret,
4553
});
4654

55+
this.otp = new OTP(this.rest);
56+
this.logs = new Logs(this.rest);
4757
this.apiKeys = new APIKeys(this.rest);
48-
this.projects = new Projects(this.rest);
49-
this.templates = new Templates(this.rest);
5058
this.webhooks = new Webhooks(this.rest);
59+
this.messages = new Messages(this.rest);
60+
this.templates = new Templates(this.rest);
5161
}
5262
}

src/resources/apiKeys.ts

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,16 @@
1-
import {
2-
type RESTGetListAPIKeysData,
3-
type RESTGetListAPIKeysQueryParams,
4-
type RESTPostCreateAPIKeyBody,
5-
type RESTPostCreateAPIKeyData,
6-
Routes,
7-
} from '@rewritejs/types';
1+
import { type RESTDeleteAPIKeyData, Routes } from '@rewritetoday/types';
82
import { Base } from './base';
93

104
/**
115
* API key resource operations.
126
*/
137
export class APIKeys extends Base {
14-
/**
15-
* Creates an API key for a project.
16-
*/
17-
public async create(options: RESTPostCreateAPIKeyBody & { project: string }) {
18-
const data = await this.rest.post<RESTPostCreateAPIKeyData>(
19-
Routes.apiKeys.create(options.project),
20-
options,
21-
);
22-
23-
return data;
24-
}
25-
268
/**
279
* Deletes an API key by id.
2810
*/
29-
public async delete(id: string, project: string) {
30-
await this.rest.delete(Routes.apiKeys.delete(project, id));
31-
}
32-
33-
/**
34-
* Lists API keys for a project.
35-
*/
36-
public async list(project: string, query?: RESTGetListAPIKeysQueryParams) {
37-
const data = await this.rest.get<RESTGetListAPIKeysData>(
38-
Routes.apiKeys.list(project, query),
11+
public async delete(id: string) {
12+
return await this.rest.delete<RESTDeleteAPIKeyData>(
13+
Routes.apiKeys.delete(id),
3914
);
40-
41-
return data;
4215
}
4316
}

src/resources/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { REST } from '@rewritejs/rest';
1+
import type { REST } from '@rewritetoday/rest';
22

33
/**
44
* Shared base resource with access to the REST client.

src/resources/logs.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
type RESTGetListWebhookLogsData,
3+
type RESTGetListWebhookLogsQueryParams,
4+
type RESTGetWebhookLogData,
5+
Routes,
6+
type Snowflake,
7+
} from '@rewritetoday/types';
8+
import { Base } from './base';
9+
10+
/**
11+
* Webhook logs resource operations.
12+
*/
13+
export class Logs extends Base {
14+
/**
15+
* Deletes an API key by id.
16+
*/
17+
public async list(
18+
id: Snowflake,
19+
options?: RESTGetListWebhookLogsQueryParams,
20+
) {
21+
return await this.rest.get<RESTGetListWebhookLogsData>(
22+
Routes.webhooks.logs(id, options),
23+
);
24+
}
25+
26+
/**
27+
* Fetch for a single webhook log.
28+
*/
29+
public async get(id: Snowflake) {
30+
return await this.rest.get<RESTGetWebhookLogData>(Routes.logs.get(id));
31+
}
32+
}

0 commit comments

Comments
 (0)