@ryusenkei/mail 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 +35 -2
- package/dist/contract.d.ts +6 -0
- package/dist/contract.js +22 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +15 -0
- package/dist/testing.d.ts +5 -0
- package/dist/testing.js +16 -0
- package/package.json +51 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 RYUSENKEI
|
|
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,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ryusenkei/mail
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The port for sending email: the `Mailer` interface, its error codes, an in-memory implementation, and a contract test. No runtime dependencies besides `@ryusenkei/core`. Implementations for a provider live in the `adapter-*` packages.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @ryusenkei/mail
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Interface
|
|
12
|
+
|
|
13
|
+
`send({ from, to, replyTo?, subject, text, html? })` returns the provider message ID.
|
|
14
|
+
|
|
15
|
+
- Business failures come back as `Result` with these codes: `invalid_input` (malformed address, or a line break in a header field, which blocks header injection), `rejected`
|
|
16
|
+
- Infrastructure failures are thrown as `TransientError` (may succeed on retry) or `ConfigError` (a missing setting), from `@ryusenkei/core`
|
|
17
|
+
|
|
18
|
+
`validateMail(mail)` is the shared input check. Every implementation runs it before sending.
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
const sent = await services.mailer.send({ from, to: [owner], subject, text });
|
|
22
|
+
if (!sent.ok) log('warn', 'mail_rejected', { error: sent.error });
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Testing
|
|
26
|
+
|
|
27
|
+
| Entry | Contents |
|
|
28
|
+
| --- | --- |
|
|
29
|
+
| `@ryusenkei/mail/testing` | `memoryMailer()`: keeps sent mail in `outbox`. For tests and local development; keep it out of production code |
|
|
30
|
+
| `@ryusenkei/mail/contract` | `mailerContract(name, make, mail)`: the behavior every implementation must have, as Vitest tests. Run it against each adapter |
|
|
31
|
+
|
|
32
|
+
The contract imports `vitest`, so it is a separate entry from the in-memory implementation, which local development can use without Vitest.
|
|
33
|
+
|
|
34
|
+
## License
|
|
35
|
+
|
|
36
|
+
MIT
|
package/dist/contract.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
/**
|
|
3
|
+
* 実装が守る契約の試験(Vitest)
|
|
4
|
+
* @param mail 実装が送れる正しいメール(送信元は実装で使える値)
|
|
5
|
+
*/
|
|
6
|
+
export function mailerContract(name, make, mail) {
|
|
7
|
+
describe(`${name}: Mailer の契約`, () => {
|
|
8
|
+
it('正しいメールを送る', async () => {
|
|
9
|
+
expect((await make().send(mail)).ok).toBe(true);
|
|
10
|
+
});
|
|
11
|
+
it('ヘッダーに改行を含むメールは送らずに invalid_input', async () => {
|
|
12
|
+
const mailer = make();
|
|
13
|
+
expect(await mailer.send({ ...mail, subject: 'a\r\nBcc: x@example.com' })).toEqual({ ok: false, error: 'invalid_input' });
|
|
14
|
+
expect(await mailer.send({ ...mail, to: [{ email: 'x@example.com', name: 'a\nb' }] })).toEqual({ ok: false, error: 'invalid_input' });
|
|
15
|
+
});
|
|
16
|
+
it('宛先の形式の誤り・宛先なしは invalid_input', async () => {
|
|
17
|
+
const mailer = make();
|
|
18
|
+
expect(await mailer.send({ ...mail, to: [{ email: 'not-an-address' }] })).toEqual({ ok: false, error: 'invalid_input' });
|
|
19
|
+
expect(await mailer.send({ ...mail, to: [] })).toEqual({ ok: false, error: 'invalid_input' });
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Result } from '@ryusenkei/core';
|
|
2
|
+
export type MailAddress = {
|
|
3
|
+
email: string;
|
|
4
|
+
name?: string;
|
|
5
|
+
};
|
|
6
|
+
export type Mail = {
|
|
7
|
+
from: MailAddress;
|
|
8
|
+
to: MailAddress[];
|
|
9
|
+
replyTo?: MailAddress;
|
|
10
|
+
subject: string;
|
|
11
|
+
text: string;
|
|
12
|
+
html?: string;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* メール送信の業務の失敗
|
|
16
|
+
* - 基盤の失敗(送信の API の障害)は `TransientError`、送信元の未認証・binding の不足は `ConfigError` を投げる
|
|
17
|
+
*/
|
|
18
|
+
export type MailError = 'invalid_input' | 'rejected';
|
|
19
|
+
/** メールの送信 */
|
|
20
|
+
export interface Mailer {
|
|
21
|
+
send(mail: Mail): Promise<Result<{
|
|
22
|
+
id: string | undefined;
|
|
23
|
+
}, MailError>>;
|
|
24
|
+
}
|
|
25
|
+
/** 送信の前の入力の検査(実装は送る前にこれを通す) */
|
|
26
|
+
export declare function validateMail(mail: Mail): Result<void, 'invalid_input'>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { err, ok } from '@ryusenkei/core';
|
|
2
|
+
// `RFC 5322` の厳密な検査ではなく、明らかな誤りと改行・空白の混入だけを弾く
|
|
3
|
+
const EMAIL = /^[^\s@<>()",;:]+@[^\s@<>()",;:]+\.[^\s@<>()",;:]+$/;
|
|
4
|
+
const HEADER_BREAK = /[\r\n\u0000]/;
|
|
5
|
+
/** 送信の前の入力の検査(実装は送る前にこれを通す) */
|
|
6
|
+
export function validateMail(mail) {
|
|
7
|
+
const addresses = [mail.from, ...mail.to, ...(mail.replyTo ? [mail.replyTo] : [])];
|
|
8
|
+
if (!mail.to.length)
|
|
9
|
+
return err('invalid_input');
|
|
10
|
+
if (addresses.some((a) => !EMAIL.test(a.email) || (a.name !== undefined && HEADER_BREAK.test(a.name))))
|
|
11
|
+
return err('invalid_input');
|
|
12
|
+
if (HEADER_BREAK.test(mail.subject))
|
|
13
|
+
return err('invalid_input');
|
|
14
|
+
return ok();
|
|
15
|
+
}
|
package/dist/testing.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ok } from '@ryusenkei/core';
|
|
2
|
+
import { validateMail } from "./index.js";
|
|
3
|
+
/** 送ったメールを `outbox` に溜めるメモリの実装(テストと dev 用) */
|
|
4
|
+
export function memoryMailer() {
|
|
5
|
+
const outbox = [];
|
|
6
|
+
return {
|
|
7
|
+
outbox,
|
|
8
|
+
async send(mail) {
|
|
9
|
+
const valid = validateMail(mail);
|
|
10
|
+
if (!valid.ok)
|
|
11
|
+
return valid;
|
|
12
|
+
outbox.push(mail);
|
|
13
|
+
return ok({ id: `memory-${outbox.length}` });
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ryusenkei/mail",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "メール送信の差し込み口(型、エラーコード、入力の検査、メモリの実装、契約テスト)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "RYUSENKEI",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/ryusenkei-inc/toolkit.git",
|
|
10
|
+
"directory": "packages/runtime/mail"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/ryusenkei-inc/toolkit/tree/main/packages/runtime/mail",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./testing": {
|
|
22
|
+
"types": "./dist/testing.d.ts",
|
|
23
|
+
"default": "./dist/testing.js"
|
|
24
|
+
},
|
|
25
|
+
"./contract": {
|
|
26
|
+
"types": "./dist/contract.d.ts",
|
|
27
|
+
"default": "./dist/contract.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@ryusenkei/core": "^0.1.0"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"vitest": ">=3"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"vitest": {
|
|
44
|
+
"optional": true
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"typescript": "^5.9.3"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsc -p tsconfig.build.json",
|
|
52
|
+
"typecheck": "tsc --noEmit"
|
|
53
|
+
}
|
|
6
54
|
}
|