@sendery/sdk 0.1.0 → 0.1.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/CHANGELOG.md +13 -0
- package/README.md +23 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +17 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.3
|
|
4
|
+
|
|
5
|
+
- See the GitHub release notes for this version.
|
|
6
|
+
|
|
7
|
+
## 0.1.2
|
|
8
|
+
|
|
9
|
+
- See the GitHub release notes for this version.
|
|
10
|
+
|
|
11
|
+
## 0.1.1
|
|
12
|
+
|
|
13
|
+
- Attach up to 10 files (5 MB total) to an email.
|
|
14
|
+
|
|
15
|
+
|
|
3
16
|
## 0.1.0
|
|
4
17
|
|
|
5
18
|
- Send published templates and retrieve delivery status from JavaScript or TypeScript on the server.
|
package/README.md
CHANGED
|
@@ -42,6 +42,29 @@ const receipt = await sendery.send({
|
|
|
42
42
|
console.log(receipt.id);
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
+
## Attachments
|
|
46
|
+
|
|
47
|
+
Use `attachment()` to create an attachment from file bytes. The helper handles base64 encoding.
|
|
48
|
+
|
|
49
|
+
Send up to 10 files totaling 5 MB. See the [attachment reference](https://sendery.co/en/docs/send-email#section-5) for supported formats and limits.
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
import { Sendery, attachment } from '@sendery/sdk';
|
|
53
|
+
import { readFile } from 'node:fs/promises';
|
|
54
|
+
|
|
55
|
+
const sendery = new Sendery(process.env.SENDERY_API_KEY);
|
|
56
|
+
const file = await readFile('/path/document.pdf');
|
|
57
|
+
|
|
58
|
+
await sendery.prepare({
|
|
59
|
+
to: 'alex@example.com',
|
|
60
|
+
template: 'welcome',
|
|
61
|
+
data: { name: 'Alex', action_url: 'https://example.com/start' },
|
|
62
|
+
attachments: [attachment('document.pdf', file, 'application/pdf')],
|
|
63
|
+
}, 'welcome-attachment-123').retry().send();
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
In Next.js and Nuxt, use this in server-side routes or actions.
|
|
67
|
+
|
|
45
68
|
## Retrieve an email
|
|
46
69
|
|
|
47
70
|
Use the returned ID to [check delivery status](https://sendery.co/en/docs/get-email).
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
export type Variables = Record<string, string | number | boolean | Record<string, string | number | boolean>[]>;
|
|
2
|
+
export interface Attachment {
|
|
3
|
+
filename: string;
|
|
4
|
+
/** Standard base64 file content, without a data URL prefix. */
|
|
5
|
+
content: string;
|
|
6
|
+
content_type?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function attachment(filename: string, bytes: Uint8Array, contentType?: string): Attachment;
|
|
2
9
|
export interface SendEmailInput {
|
|
3
10
|
to: string;
|
|
4
11
|
template: string;
|
|
5
12
|
data: Variables;
|
|
6
13
|
locale?: string;
|
|
14
|
+
attachments?: Attachment[];
|
|
7
15
|
}
|
|
8
16
|
export interface SendReceipt {
|
|
9
17
|
id: string;
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
export function attachment(filename, bytes, contentType = "application/octet-stream") {
|
|
2
|
+
if (!bytes.length || bytes.length > 5 * 1024 * 1024)
|
|
3
|
+
throw new Error("Attachments must contain 1 to 5,242,880 bytes.");
|
|
4
|
+
let binary = "";
|
|
5
|
+
for (let i = 0; i < bytes.length; i += 8192)
|
|
6
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + 8192));
|
|
7
|
+
return { filename, content: btoa(binary), content_type: contentType };
|
|
8
|
+
}
|
|
1
9
|
export class SenderyError extends Error {
|
|
2
10
|
status;
|
|
3
11
|
code;
|
|
@@ -24,6 +32,14 @@ export class PendingEmail {
|
|
|
24
32
|
constructor(client, input, key) {
|
|
25
33
|
if (!/^[a-zA-Z0-9_.:-]{1,128}$/.test(key))
|
|
26
34
|
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.");
|
|
27
43
|
this.client = client;
|
|
28
44
|
this.idempotencyKey = key;
|
|
29
45
|
this.body = JSON.stringify({
|
|
@@ -31,6 +47,7 @@ export class PendingEmail {
|
|
|
31
47
|
template: input.template,
|
|
32
48
|
data: input.data,
|
|
33
49
|
...(input.locale ? { locale: input.locale } : {}),
|
|
50
|
+
...(files.length ? { attachments: files } : {}),
|
|
34
51
|
});
|
|
35
52
|
}
|
|
36
53
|
retry(retries = 3) {
|