@rewritejs/sdk 1.0.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.
- package/README.md +178 -0
- package/dist/client.d.ts +27 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +313 -0
- package/dist/resources/apiKeys.d.ts +21 -0
- package/dist/resources/base.d.ts +12 -0
- package/dist/resources/projects.d.ts +23 -0
- package/dist/resources/templates.d.ts +31 -0
- package/dist/resources/webhooks.d.ts +31 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/version.d.ts +4 -0
- package/package.json +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# Rewrite Node SDK
|
|
4
|
+
|
|
5
|
+
[`@rewritejs/sdk`](https://www.npmjs.com/package/@rewritejs/sdk), the official Node.js/TypeScript SDK for the Rewrite API.
|
|
6
|
+
|
|
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).
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Use your preferred package manager:
|
|
12
|
+
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
bun add @rewritejs/sdk
|
|
17
|
+
# Or
|
|
18
|
+
npm install @rewritejs/sdk
|
|
19
|
+
# Or
|
|
20
|
+
pnpm add @rewritejs/sdk
|
|
21
|
+
# Or
|
|
22
|
+
yarn add @rewritejs/sdk
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
<div align="center">
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
</div>
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { Rewrite } from '@rewritejs/sdk';
|
|
33
|
+
|
|
34
|
+
const rewrite = new Rewrite(process.env.REWRITE_API_KEY!);
|
|
35
|
+
|
|
36
|
+
const project = await rewrite.projects.get('123456789012345678');
|
|
37
|
+
|
|
38
|
+
console.log({ project });
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
<div align="center">
|
|
42
|
+
|
|
43
|
+
## Create The Client
|
|
44
|
+
|
|
45
|
+
You can pass the API key directly or use the full options object.
|
|
46
|
+
|
|
47
|
+
</div>
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { Rewrite } from '@rewritejs/sdk';
|
|
51
|
+
|
|
52
|
+
const advanced = new Rewrite({
|
|
53
|
+
secret: 'rw_live_xxx',
|
|
54
|
+
rest: {
|
|
55
|
+
timeout: 10_000,
|
|
56
|
+
headers: {
|
|
57
|
+
'x-trace-id': 'my-service',
|
|
58
|
+
},
|
|
59
|
+
retry: {
|
|
60
|
+
max: 3,
|
|
61
|
+
delay: (attempt) => attempt * 250,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
<div align="center">
|
|
68
|
+
|
|
69
|
+
### Projects
|
|
70
|
+
|
|
71
|
+
</div>
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const project = await rewrite.projects.create({ name: 'AbacatePay Notifications' });
|
|
75
|
+
|
|
76
|
+
console.log({ project });
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
<div align="center">
|
|
80
|
+
|
|
81
|
+
### Templates
|
|
82
|
+
|
|
83
|
+
</div>
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
const project = '123456789012345678';
|
|
87
|
+
|
|
88
|
+
const created = await rewrite.templates.create({
|
|
89
|
+
project,
|
|
90
|
+
name: 'welcome_sms',
|
|
91
|
+
content: 'Hey {{name}}, welcome to {{company}}!',
|
|
92
|
+
variables: [
|
|
93
|
+
{ name: 'name', fallback: 'customer' },
|
|
94
|
+
{ name: 'company', fallback: 'Rewrite' },
|
|
95
|
+
],
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const templates = await rewrite.templates.list(project, { limit: 20 });
|
|
99
|
+
|
|
100
|
+
console.log({ templates });
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
<div align="center">
|
|
104
|
+
|
|
105
|
+
### Webhooks
|
|
106
|
+
|
|
107
|
+
</div>
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { WebhookEventType, WebhookStatus } from '@rewritejs/types';
|
|
111
|
+
|
|
112
|
+
const project = '123456789012345678';
|
|
113
|
+
|
|
114
|
+
const hook = await rewrite.webhooks.create({
|
|
115
|
+
project,
|
|
116
|
+
name: 'delivery-events',
|
|
117
|
+
endpoint: 'https://example.com/rewrite/webhooks',
|
|
118
|
+
events: [WebhookEventType.SMSDelivered, WebhookEventType.SMSFailed],
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
await rewrite.webhooks.update(hook.data.id, {
|
|
122
|
+
project,
|
|
123
|
+
status: WebhookStatus.Inactive,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const hooks = await rewrite.webhooks.list(project, { limit: 10 });
|
|
127
|
+
|
|
128
|
+
console.log({ hooks });
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
<div align="center">
|
|
132
|
+
|
|
133
|
+
### API Keys
|
|
134
|
+
|
|
135
|
+
</div>
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { APIKeyScope } from '@rewritejs/types';
|
|
139
|
+
|
|
140
|
+
const project = '123456789012345678';
|
|
141
|
+
|
|
142
|
+
const key = await rewrite.apiKeys.create({
|
|
143
|
+
project,
|
|
144
|
+
name: 'backend-prod',
|
|
145
|
+
scopes: [APIKeyScope.ReadProject, APIKeyScope.ReadTemplates],
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
console.log({ key });
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
<div align="center">
|
|
152
|
+
|
|
153
|
+
## Error Handling
|
|
154
|
+
|
|
155
|
+
Requests run through `@rewritejs/rest`. HTTP failures can throw `HTTPError`.
|
|
156
|
+
|
|
157
|
+
</div>
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
import { HTTPError } from '@rewritejs/rest';
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
await rewrite.projects.get('invalid_id');
|
|
164
|
+
} catch (error) {
|
|
165
|
+
if (error instanceof HTTPError) {
|
|
166
|
+
console.error('HTTP Error:', error.status, error.method, error.url);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
<div align="center">
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
Made with 🤍 by the Rewrite team. <br/>
|
|
176
|
+
SMS the way it should be.
|
|
177
|
+
|
|
178
|
+
</div>
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { REST } from '@rewritejs/rest';
|
|
2
|
+
import { APIKeys } from './resources/apiKeys';
|
|
3
|
+
import { Projects } from './resources/projects';
|
|
4
|
+
import { Templates } from './resources/templates';
|
|
5
|
+
import { Webhooks } from './resources/webhooks';
|
|
6
|
+
import type { RewriteOptions } from './types';
|
|
7
|
+
/**
|
|
8
|
+
* Main SDK client for the Rewrite API.
|
|
9
|
+
*/
|
|
10
|
+
export declare class Rewrite {
|
|
11
|
+
/** Low-level REST client instance. */
|
|
12
|
+
readonly rest: REST;
|
|
13
|
+
/** Resolved API secret used for authentication. */
|
|
14
|
+
private readonly secret;
|
|
15
|
+
/** API keys resource client. */
|
|
16
|
+
readonly apiKeys: APIKeys;
|
|
17
|
+
/** Projects resource client. */
|
|
18
|
+
readonly projects: Projects;
|
|
19
|
+
/** Templates resource client. */
|
|
20
|
+
readonly templates: Templates;
|
|
21
|
+
/** Webhooks resource client. */
|
|
22
|
+
readonly webhooks: Webhooks;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new Rewrite client instance.
|
|
25
|
+
*/
|
|
26
|
+
constructor(options: RewriteOptions | string);
|
|
27
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// node_modules/@rewritejs/rest/dist/errors.js
|
|
2
|
+
class HTTPError extends Error {
|
|
3
|
+
message;
|
|
4
|
+
status;
|
|
5
|
+
url;
|
|
6
|
+
method;
|
|
7
|
+
constructor(message, status, url, method) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.message = message;
|
|
10
|
+
this.status = status;
|
|
11
|
+
this.url = url;
|
|
12
|
+
this.method = method;
|
|
13
|
+
this.name = `HTTPError(${status})`;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// node_modules/@rewritejs/rest/dist/utils.js
|
|
18
|
+
var createURL = (route, query, baseURL = "https://api.userewrite.me") => {
|
|
19
|
+
const url = `${baseURL}/v1${route}`;
|
|
20
|
+
return query ? `${url}?${new URLSearchParams(query)}` : url;
|
|
21
|
+
};
|
|
22
|
+
var FIVE_SECONDS_IN_MS = 5000;
|
|
23
|
+
var RETRYABLE_STATUS = [408, 425, 429, 500, 502, 503, 504];
|
|
24
|
+
var isRetryableStatus = (status) => RETRYABLE_STATUS.includes(status);
|
|
25
|
+
var BASE_DELAY_MS = 300;
|
|
26
|
+
var MAX_DELAY_MS = 1e4;
|
|
27
|
+
var backoff = (attempt) => {
|
|
28
|
+
const exp = Math.min(MAX_DELAY_MS, BASE_DELAY_MS * 2 ** attempt);
|
|
29
|
+
const jitter = Math.random() * exp * 0.3;
|
|
30
|
+
return Math.floor(exp + jitter);
|
|
31
|
+
};
|
|
32
|
+
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
33
|
+
|
|
34
|
+
// node_modules/@rewritejs/rest/dist/client.js
|
|
35
|
+
class REST {
|
|
36
|
+
options;
|
|
37
|
+
headers = { "Content-Type": "application/json" };
|
|
38
|
+
constructor(options) {
|
|
39
|
+
this.options = typeof options === "string" ? { auth: options } : options;
|
|
40
|
+
this.headers = {
|
|
41
|
+
...this.options.headers,
|
|
42
|
+
Authorization: `Bearer ${this.options.auth}`
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
setAuth(authorization) {
|
|
46
|
+
this.options.auth = authorization;
|
|
47
|
+
this.headers.Authorization = `Bearer ${authorization}`;
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
get(route, options) {
|
|
51
|
+
return this.fetch(route, { ...options, method: "GET" });
|
|
52
|
+
}
|
|
53
|
+
post(route, data, options) {
|
|
54
|
+
return this.fetch(route, { data, ...options, method: "POST" });
|
|
55
|
+
}
|
|
56
|
+
delete(route, options) {
|
|
57
|
+
return this.fetch(route, { ...options, method: "DELETE" });
|
|
58
|
+
}
|
|
59
|
+
put(route, options) {
|
|
60
|
+
return this.fetch(route, { ...options, method: "PUT" });
|
|
61
|
+
}
|
|
62
|
+
patch(route, data, options) {
|
|
63
|
+
return this.fetch(route, { data, ...options, method: "PATCH" });
|
|
64
|
+
}
|
|
65
|
+
async fetch(route, options, attempt = 0) {
|
|
66
|
+
const response = await this.createRequest(route, options);
|
|
67
|
+
if (!response.ok)
|
|
68
|
+
return this.handleError({
|
|
69
|
+
route,
|
|
70
|
+
attempt,
|
|
71
|
+
options,
|
|
72
|
+
response,
|
|
73
|
+
method: options.method
|
|
74
|
+
});
|
|
75
|
+
const { data } = await response.json();
|
|
76
|
+
return data;
|
|
77
|
+
}
|
|
78
|
+
async handleError({ route, method, options, attempt, response }) {
|
|
79
|
+
if (!isRetryableStatus(response.status)) {
|
|
80
|
+
const { error } = await response.json();
|
|
81
|
+
throw new HTTPError(error, response.status, response.url, method);
|
|
82
|
+
}
|
|
83
|
+
const { retry } = this.options;
|
|
84
|
+
if (attempt >= (retry?.max ?? 3))
|
|
85
|
+
throw new HTTPError("Max retries reached", response.status, response.url, method);
|
|
86
|
+
if (retry?.onRetry)
|
|
87
|
+
await retry.onRetry({ route, attempt, options, response, method });
|
|
88
|
+
const delay = (retry?.delay ?? backoff)(attempt);
|
|
89
|
+
await sleep(delay);
|
|
90
|
+
return this.fetch(route, options, attempt + 1);
|
|
91
|
+
}
|
|
92
|
+
createRequest(route, options) {
|
|
93
|
+
const timeout = options.timeout ?? this.options.timeout ?? FIVE_SECONDS_IN_MS;
|
|
94
|
+
const url = createURL(route, options.query, this.options.baseURL);
|
|
95
|
+
return fetch(url, {
|
|
96
|
+
method: options.method,
|
|
97
|
+
signal: AbortSignal.timeout(timeout),
|
|
98
|
+
headers: { ...this.headers, ...options.headers },
|
|
99
|
+
body: "data" in options ? JSON.stringify(options.data) : null
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// node_modules/@rewritejs/types/dist/v1/resources/apiKeys.js
|
|
104
|
+
var APIKeyScope;
|
|
105
|
+
(function(APIKeyScope2) {
|
|
106
|
+
APIKeyScope2["Wildcard"] = "*";
|
|
107
|
+
APIKeyScope2["ReadProject"] = "project:read";
|
|
108
|
+
APIKeyScope2["WriteProject"] = "project:write";
|
|
109
|
+
APIKeyScope2["ReadAPIKeys"] = "project:api_key:read";
|
|
110
|
+
APIKeyScope2["WriteTemplate"] = "project:template:write";
|
|
111
|
+
APIKeyScope2["ReadTemplates"] = "project:template:read";
|
|
112
|
+
APIKeyScope2["ReadPayments"] = "project:payment:read";
|
|
113
|
+
APIKeyScope2["ReadWebhooks"] = "project:webhook:read";
|
|
114
|
+
APIKeyScope2["WriteWebhooks"] = "project:webhook:write";
|
|
115
|
+
})(APIKeyScope || (APIKeyScope = {}));
|
|
116
|
+
// node_modules/@rewritejs/types/dist/v1/resources/webhooks.js
|
|
117
|
+
var WebhookEventType;
|
|
118
|
+
(function(WebhookEventType2) {
|
|
119
|
+
WebhookEventType2["SMSQueued"] = "sms.queued";
|
|
120
|
+
WebhookEventType2["SMSDelivered"] = "sms.delivered";
|
|
121
|
+
WebhookEventType2["SMSScheduled"] = "sms.scheduled";
|
|
122
|
+
WebhookEventType2["SMSFailed"] = "sms.failed";
|
|
123
|
+
WebhookEventType2["SMSCanceled"] = "sms.canceled";
|
|
124
|
+
})(WebhookEventType || (WebhookEventType = {}));
|
|
125
|
+
var WebhookStatus;
|
|
126
|
+
(function(WebhookStatus2) {
|
|
127
|
+
WebhookStatus2["Active"] = "ACTIVE";
|
|
128
|
+
WebhookStatus2["Inactive"] = "INACTIVE";
|
|
129
|
+
})(WebhookStatus || (WebhookStatus = {}));
|
|
130
|
+
// node_modules/@rewritejs/types/dist/v1/utils.js
|
|
131
|
+
var createCursorQuery = ({ limit = 15, ...options } = {}) => {
|
|
132
|
+
return new URLSearchParams({ limit: limit.toString(), ...options });
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// node_modules/@rewritejs/types/dist/v1/routes.js
|
|
136
|
+
var Routes = {
|
|
137
|
+
webhooks: {
|
|
138
|
+
list(id, options) {
|
|
139
|
+
return `/projects/${id}/webhooks?${createCursorQuery(options)}`;
|
|
140
|
+
},
|
|
141
|
+
create(id) {
|
|
142
|
+
return `/projects/${id}/webhooks`;
|
|
143
|
+
},
|
|
144
|
+
update(id, webhookId) {
|
|
145
|
+
return `/projects/${id}/webhooks/${webhookId}`;
|
|
146
|
+
},
|
|
147
|
+
delete(id, webhookId) {
|
|
148
|
+
return `/projects/${id}/webhooks/${webhookId}`;
|
|
149
|
+
},
|
|
150
|
+
get(id, webhookId) {
|
|
151
|
+
return `/projects/${id}/webhooks/${webhookId}`;
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
templates: {
|
|
155
|
+
list(id, options) {
|
|
156
|
+
return `/projects/${id}/templates?${createCursorQuery(options)}`;
|
|
157
|
+
},
|
|
158
|
+
create(id) {
|
|
159
|
+
return `/projects/${id}/templates`;
|
|
160
|
+
},
|
|
161
|
+
update(id, templateId) {
|
|
162
|
+
return `/projects/${id}/templates/${templateId}`;
|
|
163
|
+
},
|
|
164
|
+
delete(id, templateId) {
|
|
165
|
+
return `/projects/${id}/templates/${templateId}`;
|
|
166
|
+
},
|
|
167
|
+
get(id, templateId) {
|
|
168
|
+
return `/projects/${id}/templates/${templateId}`;
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
apiKeys: {
|
|
172
|
+
list(id, options) {
|
|
173
|
+
return `/projects/${id}/api-keys?${createCursorQuery(options)}`;
|
|
174
|
+
},
|
|
175
|
+
create(id) {
|
|
176
|
+
return `/projects/${id}/api-keys`;
|
|
177
|
+
},
|
|
178
|
+
delete(id, apiKeyId) {
|
|
179
|
+
return `/projects/${id}/api-keys/${apiKeyId}`;
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
projects: {
|
|
183
|
+
create() {
|
|
184
|
+
return `/projects`;
|
|
185
|
+
},
|
|
186
|
+
update(id) {
|
|
187
|
+
return `/projects/${id}`;
|
|
188
|
+
},
|
|
189
|
+
delete(id) {
|
|
190
|
+
return `/projects/${id}`;
|
|
191
|
+
},
|
|
192
|
+
get(id) {
|
|
193
|
+
return `/projects/${id}`;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
// src/resources/base.ts
|
|
198
|
+
class Base {
|
|
199
|
+
rest;
|
|
200
|
+
constructor(rest2) {
|
|
201
|
+
this.rest = rest2;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/resources/apiKeys.ts
|
|
206
|
+
class APIKeys extends Base {
|
|
207
|
+
async create(options) {
|
|
208
|
+
const data = await this.rest.post(Routes.apiKeys.create(options.project), options);
|
|
209
|
+
return data;
|
|
210
|
+
}
|
|
211
|
+
async delete(id, project) {
|
|
212
|
+
await this.rest.delete(Routes.apiKeys.delete(project, id));
|
|
213
|
+
}
|
|
214
|
+
async list(project, query) {
|
|
215
|
+
const data = await this.rest.get(Routes.apiKeys.list(project, query));
|
|
216
|
+
return data;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// src/resources/projects.ts
|
|
221
|
+
class Projects extends Base {
|
|
222
|
+
async create(options) {
|
|
223
|
+
const data = await this.rest.post(Routes.projects.create(), options);
|
|
224
|
+
return data;
|
|
225
|
+
}
|
|
226
|
+
async update(id, options) {
|
|
227
|
+
const data = await this.rest.patch(Routes.projects.update(id), options);
|
|
228
|
+
return data;
|
|
229
|
+
}
|
|
230
|
+
async delete(id) {
|
|
231
|
+
await this.rest.delete(Routes.projects.delete(id));
|
|
232
|
+
}
|
|
233
|
+
async get(id) {
|
|
234
|
+
const data = await this.rest.get(Routes.projects.get(id));
|
|
235
|
+
return data;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// src/resources/templates.ts
|
|
240
|
+
class Templates extends Base {
|
|
241
|
+
async create(options) {
|
|
242
|
+
const data = await this.rest.post(Routes.templates.create(options.project), options);
|
|
243
|
+
return data;
|
|
244
|
+
}
|
|
245
|
+
async update(id, options) {
|
|
246
|
+
const data = await this.rest.patch(Routes.templates.update(options.project, id), options);
|
|
247
|
+
return data;
|
|
248
|
+
}
|
|
249
|
+
async delete(id, project) {
|
|
250
|
+
await this.rest.delete(Routes.templates.delete(project, id));
|
|
251
|
+
}
|
|
252
|
+
async list(project, query) {
|
|
253
|
+
const data = await this.rest.get(Routes.templates.list(project, query));
|
|
254
|
+
return data;
|
|
255
|
+
}
|
|
256
|
+
async get(id, project) {
|
|
257
|
+
const data = await this.rest.get(Routes.templates.get(project, id));
|
|
258
|
+
return data;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// src/resources/webhooks.ts
|
|
263
|
+
class Webhooks extends Base {
|
|
264
|
+
async create(options) {
|
|
265
|
+
const data = await this.rest.post(Routes.webhooks.create(options.project), options);
|
|
266
|
+
return data;
|
|
267
|
+
}
|
|
268
|
+
async update(id, options) {
|
|
269
|
+
const data = await this.rest.patch(Routes.webhooks.update(options.project, id), options);
|
|
270
|
+
return data;
|
|
271
|
+
}
|
|
272
|
+
async delete(id, project) {
|
|
273
|
+
await this.rest.delete(Routes.webhooks.delete(project, id));
|
|
274
|
+
}
|
|
275
|
+
async list(project, query) {
|
|
276
|
+
const data = await this.rest.get(Routes.webhooks.list(project, query));
|
|
277
|
+
return data;
|
|
278
|
+
}
|
|
279
|
+
async get(id, project) {
|
|
280
|
+
const data = await this.rest.get(Routes.webhooks.get(project, id));
|
|
281
|
+
return data;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// src/client.ts
|
|
286
|
+
class Rewrite {
|
|
287
|
+
rest;
|
|
288
|
+
secret;
|
|
289
|
+
apiKeys;
|
|
290
|
+
projects;
|
|
291
|
+
templates;
|
|
292
|
+
webhooks;
|
|
293
|
+
constructor(options) {
|
|
294
|
+
const resolved = typeof options === "string" ? { secret: options } : options;
|
|
295
|
+
if (typeof resolved.secret !== "string")
|
|
296
|
+
throw new Error("Expected a string for the secret");
|
|
297
|
+
this.secret = resolved.secret;
|
|
298
|
+
this.rest = new REST({
|
|
299
|
+
...resolved.rest,
|
|
300
|
+
auth: this.secret
|
|
301
|
+
});
|
|
302
|
+
this.apiKeys = new APIKeys(this.rest);
|
|
303
|
+
this.projects = new Projects(this.rest);
|
|
304
|
+
this.templates = new Templates(this.rest);
|
|
305
|
+
this.webhooks = new Webhooks(this.rest);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
// src/version.ts
|
|
309
|
+
var version = "1.0.0";
|
|
310
|
+
export {
|
|
311
|
+
version,
|
|
312
|
+
Rewrite
|
|
313
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type RESTGetListAPIKeysData, type RESTGetListAPIKeysQueryParams, type RESTPostCreateAPIKeyBody, type RESTPostCreateAPIKeyData } from '@rewritejs/types';
|
|
2
|
+
import { Base } from './base';
|
|
3
|
+
/**
|
|
4
|
+
* API key resource operations.
|
|
5
|
+
*/
|
|
6
|
+
export declare class APIKeys extends Base {
|
|
7
|
+
/**
|
|
8
|
+
* Creates an API key for a project.
|
|
9
|
+
*/
|
|
10
|
+
create(options: RESTPostCreateAPIKeyBody & {
|
|
11
|
+
project: string;
|
|
12
|
+
}): Promise<RESTPostCreateAPIKeyData>;
|
|
13
|
+
/**
|
|
14
|
+
* Deletes an API key by id.
|
|
15
|
+
*/
|
|
16
|
+
delete(id: string, project: string): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Lists API keys for a project.
|
|
19
|
+
*/
|
|
20
|
+
list(project: string, query?: RESTGetListAPIKeysQueryParams): Promise<RESTGetListAPIKeysData>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { REST } from '@rewritejs/rest';
|
|
2
|
+
/**
|
|
3
|
+
* Shared base resource with access to the REST client.
|
|
4
|
+
*/
|
|
5
|
+
export declare abstract class Base {
|
|
6
|
+
/** REST client used by resource operations. */
|
|
7
|
+
readonly rest: REST;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a base resource wrapper.
|
|
10
|
+
*/
|
|
11
|
+
constructor(rest: REST);
|
|
12
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type RESTGetProjectData, type RESTPatchUpdateProjectBody, type RESTPatchUpdateProjectData, type RESTPostCreateProjectBody, type RESTPostCreateProjectData } from '@rewritejs/types';
|
|
2
|
+
import { Base } from './base';
|
|
3
|
+
/**
|
|
4
|
+
* Project resource operations.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Projects extends Base {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new project.
|
|
9
|
+
*/
|
|
10
|
+
create(options: RESTPostCreateProjectBody): Promise<RESTPostCreateProjectData>;
|
|
11
|
+
/**
|
|
12
|
+
* Updates a project by id.
|
|
13
|
+
*/
|
|
14
|
+
update(id: string, options: RESTPatchUpdateProjectBody): Promise<RESTPatchUpdateProjectData>;
|
|
15
|
+
/**
|
|
16
|
+
* Deletes a project by id.
|
|
17
|
+
*/
|
|
18
|
+
delete(id: string): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Fetches a project by id.
|
|
21
|
+
*/
|
|
22
|
+
get(id: string): Promise<RESTGetProjectData>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type RESTGetListTemplatesData, type RESTGetListTemplatesQueryParams, type RESTGetTemplateData, type RESTPostCreateTemplateBody, type RESTPostCreateTemplateData } from '@rewritejs/types';
|
|
2
|
+
import { Base } from './base';
|
|
3
|
+
/**
|
|
4
|
+
* Template resource operations.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Templates extends Base {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a template for a project.
|
|
9
|
+
*/
|
|
10
|
+
create(options: RESTPostCreateTemplateBody & {
|
|
11
|
+
project: string;
|
|
12
|
+
}): Promise<RESTPostCreateTemplateData>;
|
|
13
|
+
/**
|
|
14
|
+
* Updates a template by id.
|
|
15
|
+
*/
|
|
16
|
+
update(id: string, options: RESTPostCreateTemplateBody & {
|
|
17
|
+
project: string;
|
|
18
|
+
}): Promise<RESTPostCreateTemplateData>;
|
|
19
|
+
/**
|
|
20
|
+
* Deletes a template by id.
|
|
21
|
+
*/
|
|
22
|
+
delete(id: string, project: string): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Lists templates for a project.
|
|
25
|
+
*/
|
|
26
|
+
list(project: string, query?: RESTGetListTemplatesQueryParams): Promise<RESTGetListTemplatesData>;
|
|
27
|
+
/**
|
|
28
|
+
* Fetches a template by id.
|
|
29
|
+
*/
|
|
30
|
+
get(id: string, project: string): Promise<RESTGetTemplateData>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type RESTGetListWebhooksData, type RESTGetListWebhooksQueryParams, type RESTGetWebhookData, type RESTPatchUpdateWebhookBody, type RESTPatchUpdateWebhookData, type RESTPostCreateWebhookBody, type RESTPostCreateWebhookData } from '@rewritejs/types';
|
|
2
|
+
import { Base } from './base';
|
|
3
|
+
/**
|
|
4
|
+
* Webhook resource operations.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Webhooks extends Base {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a webhook for a project.
|
|
9
|
+
*/
|
|
10
|
+
create(options: RESTPostCreateWebhookBody & {
|
|
11
|
+
project: string;
|
|
12
|
+
}): Promise<RESTPostCreateWebhookData>;
|
|
13
|
+
/**
|
|
14
|
+
* Updates a webhook by id.
|
|
15
|
+
*/
|
|
16
|
+
update(id: string, options: RESTPatchUpdateWebhookBody & {
|
|
17
|
+
project: string;
|
|
18
|
+
}): Promise<RESTPatchUpdateWebhookData>;
|
|
19
|
+
/**
|
|
20
|
+
* Deletes a webhook by id.
|
|
21
|
+
*/
|
|
22
|
+
delete(id: string, project: string): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Lists webhooks for a project.
|
|
25
|
+
*/
|
|
26
|
+
list(project: string, query?: RESTGetListWebhooksQueryParams): Promise<RESTGetListWebhooksData>;
|
|
27
|
+
/**
|
|
28
|
+
* Fetches a webhook by id.
|
|
29
|
+
*/
|
|
30
|
+
get(id: string, project: string): Promise<RESTGetWebhookData>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RESTOptions } from '@rewritejs/rest';
|
|
2
|
+
/**
|
|
3
|
+
* Options used to initialize the Rewrite client.
|
|
4
|
+
*/
|
|
5
|
+
export interface RewriteOptions {
|
|
6
|
+
/** API secret used for authentication. */
|
|
7
|
+
secret: string;
|
|
8
|
+
/** Extra REST client options, excluding auth. */
|
|
9
|
+
rest?: Omit<RESTOptions, 'auth'>;
|
|
10
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rewritejs/sdk",
|
|
3
|
+
"module": "src/index.ts",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"private": false,
|
|
6
|
+
"version": "1.0.0",
|
|
7
|
+
"devDependencies": {
|
|
8
|
+
"@biomejs/biome": "^2.4.2",
|
|
9
|
+
"@types/bun": "latest"
|
|
10
|
+
},
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"typescript": "^5"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@rewritejs/rest": "^1.0.1",
|
|
16
|
+
"@rewritejs/types": "^1.0.0"
|
|
17
|
+
},
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"maintainers": [
|
|
24
|
+
{
|
|
25
|
+
"name": "Almeida",
|
|
26
|
+
"url": "https://github.com/almeidazs"
|
|
27
|
+
}
|
|
28
|
+
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"test": "bun test",
|
|
31
|
+
"biome:check": "biome check --write .",
|
|
32
|
+
"build": "rm -rf dist && bun run scripts/version.ts && bun biome:check && bun build src/index.ts --outdir dist --target node && bun run types",
|
|
33
|
+
"types": "bunx tsc --emitDeclarationOnly",
|
|
34
|
+
"prepublishOnly": "bun run build"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=18",
|
|
38
|
+
"bun": ">=1.0.0"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/rewritetoday/node.git"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"description": "An ergonomic SDK to integrate with Rewrite in your workflow"
|
|
48
|
+
}
|