@sendery/sdk 0.1.4 → 0.1.6
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/CHANGELOG.md +10 -0
- package/README.md +16 -0
- package/dist/index.d.ts +78 -6
- package/dist/index.js +72 -29
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.6
|
|
4
|
+
|
|
5
|
+
- See the GitHub release notes for this version.
|
|
6
|
+
|
|
7
|
+
## 0.1.5
|
|
8
|
+
|
|
9
|
+
- Register and list event definitions with readable data fields and recipient roles for the visual workflow builder.
|
|
10
|
+
|
|
11
|
+
- Add application events, stable-key retries, event status lookup, and object-data deletion. Framework integrations use the project client.
|
|
12
|
+
|
|
3
13
|
## 0.1.4
|
|
4
14
|
|
|
5
15
|
- Optionally pin sends to a published template version.
|
package/README.md
CHANGED
|
@@ -132,3 +132,19 @@ Learn how to [retry emails without duplicate sends](https://sendery.co/en/docs/i
|
|
|
132
132
|
## License
|
|
133
133
|
|
|
134
134
|
[MIT](LICENSE).
|
|
135
|
+
|
|
136
|
+
## Events and workflows
|
|
137
|
+
|
|
138
|
+
Send events such as account signups or orders from your app. Your team can use them to automate emails in Sendery, choosing the content, recipients, and timing. Available on all plans.
|
|
139
|
+
|
|
140
|
+
Start with [Connecting your application](https://sendery.co/en/docs/connect-events) to define the data Sendery can expect, then [send events](https://sendery.co/en/docs/send-events) as they happen.
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
const receipt = await sendery.event({
|
|
144
|
+
name: "account.created",
|
|
145
|
+
object: { type: "account", id: "842" },
|
|
146
|
+
data: { email: "alex@example.com", name: "Alex" },
|
|
147
|
+
}).retry().send();
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Retries reuse the same event data and idempotency key. To retry across jobs or process restarts, supply and persist your own key. A successful response confirms that Sendery accepted the event. [Learn what you can automate with workflows](https://sendery.co/en/docs/build-workflow).
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
+
export interface EventField {
|
|
2
|
+
label: string;
|
|
3
|
+
path: string;
|
|
4
|
+
type: "text" | "number" | "boolean" | "email" | "url" | "date" | "rows";
|
|
5
|
+
required?: boolean;
|
|
6
|
+
/** Use synthetic examples, never customer information. */
|
|
7
|
+
example?: string | number | boolean | Record<string, string | number | boolean>[];
|
|
8
|
+
}
|
|
9
|
+
export interface EventDefinition {
|
|
10
|
+
label: string;
|
|
11
|
+
object: {
|
|
12
|
+
type: string;
|
|
13
|
+
label: string;
|
|
14
|
+
};
|
|
15
|
+
fields: Record<string, EventField>;
|
|
16
|
+
recipients: Record<string, {
|
|
17
|
+
label: string;
|
|
18
|
+
email: string;
|
|
19
|
+
name?: string;
|
|
20
|
+
locale?: string;
|
|
21
|
+
}>;
|
|
22
|
+
}
|
|
23
|
+
export interface EventType extends EventDefinition {
|
|
24
|
+
name: string;
|
|
25
|
+
verified: boolean;
|
|
26
|
+
last_received_at: string | null;
|
|
27
|
+
}
|
|
28
|
+
export interface EventTypeReceipt {
|
|
29
|
+
name: string;
|
|
30
|
+
verified: boolean;
|
|
31
|
+
}
|
|
1
32
|
export type Variables = Record<string, string | number | boolean | Record<string, string | number | boolean>[]>;
|
|
2
33
|
export interface Attachment {
|
|
3
34
|
filename: string;
|
|
@@ -29,15 +60,50 @@ export declare class SenderyError extends Error {
|
|
|
29
60
|
constructor(status: number, code?: string, errors?: Record<string, string[]>, retryAfter?: number);
|
|
30
61
|
get retryable(): boolean;
|
|
31
62
|
}
|
|
32
|
-
export
|
|
63
|
+
export interface EventInput {
|
|
64
|
+
name: string;
|
|
65
|
+
object: {
|
|
66
|
+
type: string;
|
|
67
|
+
id: string;
|
|
68
|
+
};
|
|
69
|
+
correlation_id?: string;
|
|
70
|
+
data: Record<string, unknown>;
|
|
71
|
+
}
|
|
72
|
+
export interface EventReceipt extends SendReceipt {
|
|
73
|
+
name?: string;
|
|
74
|
+
received_at?: string;
|
|
75
|
+
matched_workflows?: number;
|
|
76
|
+
environment?: "sandbox" | "live";
|
|
77
|
+
runs?: {
|
|
78
|
+
id: string;
|
|
79
|
+
workflow_id: number;
|
|
80
|
+
status: string;
|
|
81
|
+
due_at: string | null;
|
|
82
|
+
error_code: string | null;
|
|
83
|
+
actions: {
|
|
84
|
+
position: number;
|
|
85
|
+
type: string;
|
|
86
|
+
status: string;
|
|
87
|
+
email_id: string | null;
|
|
88
|
+
}[];
|
|
89
|
+
}[];
|
|
90
|
+
}
|
|
91
|
+
declare class PendingRequest<T extends SendReceipt> {
|
|
33
92
|
readonly idempotencyKey: string;
|
|
34
|
-
|
|
35
|
-
private readonly client;
|
|
93
|
+
protected body: string;
|
|
36
94
|
private retries;
|
|
95
|
+
private client;
|
|
96
|
+
private endpoint;
|
|
97
|
+
constructor(client: Sendery, input: unknown, key: string, endpoint: string);
|
|
98
|
+
retry(retries?: number): this;
|
|
99
|
+
send(): Promise<T>;
|
|
100
|
+
}
|
|
101
|
+
export declare class PendingEvent extends PendingRequest<EventReceipt> {
|
|
102
|
+
constructor(client: Sendery, input: EventInput, key: string);
|
|
103
|
+
}
|
|
104
|
+
export declare class PendingEmail extends PendingRequest<SendReceipt> {
|
|
37
105
|
constructor(client: Sendery, input: SendEmailInput, key: string);
|
|
38
106
|
version(version: number): this;
|
|
39
|
-
retry(retries?: number): this;
|
|
40
|
-
send(): Promise<SendReceipt>;
|
|
41
107
|
}
|
|
42
108
|
export declare class Sendery {
|
|
43
109
|
private apiKey;
|
|
@@ -45,6 +111,12 @@ export declare class Sendery {
|
|
|
45
111
|
constructor(apiKey: string, baseUrl?: string);
|
|
46
112
|
prepare(input: SendEmailInput, idempotencyKey?: string): PendingEmail;
|
|
47
113
|
send(input: SendEmailInput, idempotencyKey?: string): Promise<SendReceipt>;
|
|
114
|
+
defineEvent(name: string, definition: EventDefinition): Promise<EventTypeReceipt>;
|
|
115
|
+
getEventTypes(): Promise<EventType[]>;
|
|
116
|
+
event(input: EventInput, idempotencyKey?: string): PendingEvent;
|
|
117
|
+
getEvent(id: string): Promise<EventReceipt>;
|
|
118
|
+
forgetObject(type: string, id: string): Promise<SendReceipt>;
|
|
48
119
|
get(id: string): Promise<SendReceipt>;
|
|
49
|
-
request(method: string, path: string, body?: string, key?: string): Promise<
|
|
120
|
+
request<T = SendReceipt>(method: string, path: string, body?: string, key?: string): Promise<T>;
|
|
50
121
|
}
|
|
122
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -24,39 +24,19 @@ export class SenderyError extends Error {
|
|
|
24
24
|
(this.status === 429 && this.code === "rate_limited"));
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
class PendingRequest {
|
|
28
28
|
idempotencyKey;
|
|
29
29
|
body;
|
|
30
|
-
client;
|
|
31
30
|
retries = 0;
|
|
32
|
-
|
|
31
|
+
client;
|
|
32
|
+
endpoint;
|
|
33
|
+
constructor(client, input, key, endpoint) {
|
|
33
34
|
if (!/^[a-zA-Z0-9_.:-]{1,128}$/.test(key))
|
|
34
35
|
throw new Error("Invalid idempotency key.");
|
|
35
|
-
const files = input.attachments ?? [];
|
|
36
|
-
const size = files.reduce((sum, file) => {
|
|
37
|
-
if (!file.content.length || file.content.length > 6990508 || (file.content.length % 4 !== 0 || /[^A-Za-z0-9+/]/.test(file.content.replace(/={1,2}$/, ""))))
|
|
38
|
-
throw new Error("Provide standard base64 attachment content.");
|
|
39
|
-
return sum + (file.content.length / 4) * 3 - (file.content.endsWith("==") ? 2 : file.content.endsWith("=") ? 1 : 0);
|
|
40
|
-
}, 0);
|
|
41
|
-
if (files.length > 10 || size > 5 * 1024 * 1024)
|
|
42
|
-
throw new Error("Use at most 10 attachments, up to 5 MB combined.");
|
|
43
|
-
this.client = client;
|
|
44
36
|
this.idempotencyKey = key;
|
|
45
|
-
this.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
data: input.data,
|
|
49
|
-
...(input.locale ? { locale: input.locale } : {}),
|
|
50
|
-
...(files.length ? { attachments: files } : {}),
|
|
51
|
-
});
|
|
52
|
-
if (input.version !== undefined)
|
|
53
|
-
this.version(input.version);
|
|
54
|
-
}
|
|
55
|
-
version(version) {
|
|
56
|
-
if (!Number.isSafeInteger(version) || version < 1)
|
|
57
|
-
throw new Error("Version must be a positive integer.");
|
|
58
|
-
this.body = JSON.stringify({ ...JSON.parse(this.body), version });
|
|
59
|
-
return this;
|
|
37
|
+
this.client = client;
|
|
38
|
+
this.endpoint = endpoint;
|
|
39
|
+
this.body = JSON.stringify(input);
|
|
60
40
|
}
|
|
61
41
|
retry(retries = 3) {
|
|
62
42
|
if (!Number.isInteger(retries) || retries < 0 || retries > 5)
|
|
@@ -67,7 +47,7 @@ export class PendingEmail {
|
|
|
67
47
|
async send() {
|
|
68
48
|
for (let attempt = 0;; attempt++) {
|
|
69
49
|
try {
|
|
70
|
-
return await this.client.request("POST",
|
|
50
|
+
return await this.client.request("POST", this.endpoint, this.body, this.idempotencyKey);
|
|
71
51
|
}
|
|
72
52
|
catch (error) {
|
|
73
53
|
if (!(error instanceof SenderyError) ||
|
|
@@ -83,6 +63,47 @@ export class PendingEmail {
|
|
|
83
63
|
}
|
|
84
64
|
}
|
|
85
65
|
}
|
|
66
|
+
export class PendingEvent extends PendingRequest {
|
|
67
|
+
constructor(client, input, key) {
|
|
68
|
+
super(client, input, key, "/api/v1/events");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
export class PendingEmail extends PendingRequest {
|
|
72
|
+
constructor(client, input, key) {
|
|
73
|
+
const files = input.attachments ?? [];
|
|
74
|
+
const size = files.reduce((sum, file) => {
|
|
75
|
+
if (!file.content.length ||
|
|
76
|
+
file.content.length > 6990508 ||
|
|
77
|
+
file.content.length % 4 !== 0 ||
|
|
78
|
+
/[^A-Za-z0-9+/]/.test(file.content.replace(/={1,2}$/, "")))
|
|
79
|
+
throw new Error("Provide standard base64 attachment content.");
|
|
80
|
+
return (sum +
|
|
81
|
+
(file.content.length / 4) * 3 -
|
|
82
|
+
(file.content.endsWith("==")
|
|
83
|
+
? 2
|
|
84
|
+
: file.content.endsWith("=")
|
|
85
|
+
? 1
|
|
86
|
+
: 0));
|
|
87
|
+
}, 0);
|
|
88
|
+
if (files.length > 10 || size > 5 * 1024 * 1024)
|
|
89
|
+
throw new Error("Use at most 10 attachments, up to 5 MB combined.");
|
|
90
|
+
super(client, {
|
|
91
|
+
to: input.to,
|
|
92
|
+
template: input.template,
|
|
93
|
+
data: input.data,
|
|
94
|
+
...(input.locale ? { locale: input.locale } : {}),
|
|
95
|
+
...(files.length ? { attachments: files } : {}),
|
|
96
|
+
}, key, "/api/v1/emails");
|
|
97
|
+
if (input.version !== undefined)
|
|
98
|
+
this.version(input.version);
|
|
99
|
+
}
|
|
100
|
+
version(version) {
|
|
101
|
+
if (!Number.isSafeInteger(version) || version < 1)
|
|
102
|
+
throw new Error("Version must be a positive integer.");
|
|
103
|
+
this.body = JSON.stringify({ ...JSON.parse(this.body), version });
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
86
107
|
export class Sendery {
|
|
87
108
|
apiKey;
|
|
88
109
|
baseUrl;
|
|
@@ -108,6 +129,21 @@ export class Sendery {
|
|
|
108
129
|
send(input, idempotencyKey) {
|
|
109
130
|
return this.prepare(input, idempotencyKey).send();
|
|
110
131
|
}
|
|
132
|
+
defineEvent(name, definition) {
|
|
133
|
+
return this.request("PUT", `/api/v1/event-types/${encodeURIComponent(name)}`, JSON.stringify(definition));
|
|
134
|
+
}
|
|
135
|
+
async getEventTypes() {
|
|
136
|
+
return (await this.request("GET", "/api/v1/event-types")).events;
|
|
137
|
+
}
|
|
138
|
+
event(input, idempotencyKey = crypto.randomUUID()) {
|
|
139
|
+
return new PendingEvent(this, input, idempotencyKey);
|
|
140
|
+
}
|
|
141
|
+
getEvent(id) {
|
|
142
|
+
return this.request("GET", `/api/v1/events/${encodeURIComponent(id)}`);
|
|
143
|
+
}
|
|
144
|
+
forgetObject(type, id) {
|
|
145
|
+
return this.request("DELETE", "/api/v1/event-objects", JSON.stringify({ type, id }));
|
|
146
|
+
}
|
|
111
147
|
get(id) {
|
|
112
148
|
return this.request("GET", `/api/v1/emails/${encodeURIComponent(id)}`);
|
|
113
149
|
}
|
|
@@ -145,7 +181,14 @@ export class Sendery {
|
|
|
145
181
|
? Math.max(0, seconds)
|
|
146
182
|
: undefined);
|
|
147
183
|
}
|
|
148
|
-
|
|
184
|
+
const valid = path === "/api/v1/event-types"
|
|
185
|
+
? Array.isArray(data.events)
|
|
186
|
+
: path.startsWith("/api/v1/event-types/")
|
|
187
|
+
? typeof data.name === "string" &&
|
|
188
|
+
typeof data.verified === "boolean"
|
|
189
|
+
: typeof data.id === "string" &&
|
|
190
|
+
typeof data.status === "string";
|
|
191
|
+
if (!valid)
|
|
149
192
|
throw new SenderyError(0, "invalid_response");
|
|
150
193
|
return data;
|
|
151
194
|
}
|