@tansy/email-postmark 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +12 -2
- package/index.d.ts +2 -0
- package/index.js +73 -0
- package/package.json +26 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mohamed Salah
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
<!-- Generated by scripts/package-metadata.mjs. Change that script, not this file. -->
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# @tansy/email-postmark
|
|
4
|
+
|
|
5
|
+
Postmark for @tansy/email.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tansy/email-postmark
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Add it to `tansy.config.ts`. See [the email plugin documentation](https://github.com/mhmdsalahsebai/tansy/blob/main/docs/plugins/email.md) for what it adds and the options it takes.
|
|
12
|
+
|
|
13
|
+
A first-party plugin for [tansy](https://github.com/mhmdsalahsebai/tansy), a self-hosted, runtime-schema CMS for Next.js. MIT licensed.
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { callProvider, defineEmailProvider, idOf, mailbox, str, ProviderError } from "@tansy/email/provider";
|
|
2
|
+
const token = (settings) => str(settings.postmarkServerToken);
|
|
3
|
+
const STREAM = /^[a-z0-9-]{1,30}$/;
|
|
4
|
+
/** Mail to many goes through the broadcast stream, as Postmark requires. Everything else through the transactional one. */
|
|
5
|
+
function streamOf(email, settings) {
|
|
6
|
+
const [chosen, fallback] = email.bulk ? [str(settings.postmarkBroadcastStream), "broadcast"] : [str(settings.postmarkMessageStream), "outbound"];
|
|
7
|
+
return STREAM.test(chosen) ? chosen : fallback;
|
|
8
|
+
}
|
|
9
|
+
const payload = (email, settings) => ({
|
|
10
|
+
From: mailbox(email.from), To: email.to, Subject: email.subject, TextBody: email.text, HtmlBody: email.html,
|
|
11
|
+
MessageStream: streamOf(email, settings),
|
|
12
|
+
...(email.replyTo ? { ReplyTo: email.replyTo } : {}),
|
|
13
|
+
...(Object.keys(email.headers).length ? { Headers: Object.entries(email.headers).map(([Name, Value]) => ({ Name, Value })) } : {}),
|
|
14
|
+
});
|
|
15
|
+
/** Postmark answers a batch with a JSON array, which callProvider only returns as an object. */
|
|
16
|
+
const batchFetch = (fetcher) => async (input, init) => {
|
|
17
|
+
const response = await fetcher(input, init);
|
|
18
|
+
if (!response.ok)
|
|
19
|
+
return response;
|
|
20
|
+
const items = await response.json().catch(() => []);
|
|
21
|
+
return new Response(JSON.stringify({ items }), { status: response.status, headers: { "content-type": "application/json" } });
|
|
22
|
+
};
|
|
23
|
+
/** Postmark, through its REST API. */
|
|
24
|
+
export const postmark = (fetcher = fetch) => defineEmailProvider({
|
|
25
|
+
id: "postmark",
|
|
26
|
+
label: "Postmark",
|
|
27
|
+
// Its settings in other interface languages. English is each setting's own label and description.
|
|
28
|
+
messages: {
|
|
29
|
+
ar: {
|
|
30
|
+
settings: {
|
|
31
|
+
postmarkServerToken: {
|
|
32
|
+
label: "رمز API لخادم Postmark",
|
|
33
|
+
description: "من خادمك في Postmark ← API Tokens. أضف المرسل تحت Sender Signatures أولًا."
|
|
34
|
+
},
|
|
35
|
+
postmarkMessageStream: {
|
|
36
|
+
label: "مسار رسائل Postmark",
|
|
37
|
+
description: "روابط الدخول والإيصالات رسائل معاملات، فيناسبها المسار الافتراضي outbound."
|
|
38
|
+
},
|
|
39
|
+
postmarkBroadcastStream: {
|
|
40
|
+
label: "مسار البث في Postmark",
|
|
41
|
+
description: "البريد المرسل إلى كثيرين دفعة واحدة، مثل النشرة البريدية. يشترط Postmark أن يمر عبر مسار بث."
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
settings: () => [
|
|
47
|
+
{ name: "postmarkServerToken", type: "secret", env: "POSTMARK_SERVER_TOKEN", label: "Postmark server API token", description: "From your server in Postmark → API Tokens. Add your sender under Sender Signatures first." },
|
|
48
|
+
{ name: "postmarkMessageStream", type: "text", default: "outbound", maxLength: 30, label: "Postmark message stream", description: "Sign-in links and receipts are transactional, so the default outbound stream suits them." },
|
|
49
|
+
{ name: "postmarkBroadcastStream", type: "text", default: "broadcast", maxLength: 30, label: "Postmark broadcast stream", description: "Mail sent to many people at once, such as a newsletter. Postmark requires it to go through a broadcast stream." },
|
|
50
|
+
],
|
|
51
|
+
ready: (settings) => Boolean(token(settings)),
|
|
52
|
+
async send(email, settings) {
|
|
53
|
+
const answer = await callProvider("Postmark", "https://api.postmarkapp.com/email", {
|
|
54
|
+
method: "POST",
|
|
55
|
+
headers: { "x-postmark-server-token": token(settings), "content-type": "application/json", accept: "application/json" },
|
|
56
|
+
body: JSON.stringify(payload(email, settings)),
|
|
57
|
+
}, fetcher);
|
|
58
|
+
return { id: idOf(answer.MessageID) };
|
|
59
|
+
},
|
|
60
|
+
// Up to 500 in one call, each answered on its own: ErrorCode 0 is sent.
|
|
61
|
+
async sendBatch(emails, settings) {
|
|
62
|
+
const answer = await callProvider("Postmark", "https://api.postmarkapp.com/email/batch", {
|
|
63
|
+
method: "POST",
|
|
64
|
+
headers: { "x-postmark-server-token": token(settings), "content-type": "application/json", accept: "application/json" },
|
|
65
|
+
body: JSON.stringify(emails.map((email) => payload(email, settings))),
|
|
66
|
+
}, batchFetch(fetcher));
|
|
67
|
+
const items = Array.isArray(answer.items) ? answer.items : [];
|
|
68
|
+
return emails.map((_, index) => {
|
|
69
|
+
const item = items[index];
|
|
70
|
+
return item && item.ErrorCode === 0 ? { id: idOf(item.MessageID) } : new ProviderError("Postmark", 422);
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tansy/email-postmark",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Postmark for @tansy/email.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"tansy",
|
|
7
|
+
"tansy-email-provider"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Mohamed Salah <mohamedsalahsebai@gmail.com>",
|
|
11
|
+
"homepage": "https://github.com/mhmdsalahsebai/tansy",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/mhmdsalahsebai/tansy.git",
|
|
15
|
+
"directory": "plugins/email-postmark"
|
|
16
|
+
},
|
|
17
|
+
"bugs": "https://github.com/mhmdsalahsebai/tansy/issues",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./index.d.ts",
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@tansy/email": "^0.1.0",
|
|
26
|
+
"tansy": "^0.1.0"
|
|
27
|
+
}
|
|
28
|
+
}
|