@zigordev/notifications-events 0.0.0-stage → 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 +101 -2
- package/dist/events.d.ts +84 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +75 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/topics.d.ts +6 -0
- package/dist/topics.d.ts.map +1 -0
- package/dist/topics.js +23 -0
- package/dist/topics.js.map +1 -0
- package/events.json +173 -0
- package/package.json +42 -3
package/README.md
CHANGED
|
@@ -1,3 +1,102 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zigordev/notifications-events
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The events of the notifications service: the request an app sends to have an email delivered, the two outcomes the service sends back, and the names of the Kafka topics they travel on. The service and every app that sends email use this one package, so they always agree on what a request holds.
|
|
4
|
+
|
|
5
|
+
## What it is
|
|
6
|
+
|
|
7
|
+
| Export | What it is |
|
|
8
|
+
| -------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
|
|
9
|
+
| `emailRequested` | The event `email.requested`, version 1: the email to deliver, for `addToOutbox` of `@zigordev/events` |
|
|
10
|
+
| `emailSent`, `emailFailed` | The events `email.sent` and `email.failed`, version 1: what happened to a request |
|
|
11
|
+
| `outcomeEvents` | Both outcomes, for the `events` of an app's consumer |
|
|
12
|
+
| `requestTopicOf(app)`, `outcomeTopicOf(app)` | The topics of an app: `notifications.requests.<app>` and `notifications.outcomes.<app>` |
|
|
13
|
+
| `emailRequestSchema`, `mailboxSchema` | The Zod schemas of a request and of an address, for a check before the request is built |
|
|
14
|
+
| `failureReasons` | Why an email can fail: `rejected`, `attempts_exhausted`, `retry_limit_reached` and `sender_forbidden` |
|
|
15
|
+
| `maxRequestBytes` | 800,000: the largest request, as JSON |
|
|
16
|
+
| `EmailRequest`, `EmailOutcomeEvent`, … | The types of the data and of the events |
|
|
17
|
+
|
|
18
|
+
The package also ships `events.json`: the contract of the three events as JSON Schema, with the names of their topics, for a reader that is not written in TypeScript.
|
|
19
|
+
|
|
20
|
+
An event is defined with `defineEvent` of [`@zigordev/events`](https://github.com/zigordev/toolkit/blob/main/packages/events/README.md): a name, a version and a Zod schema, written once and used by the sender and the reader.
|
|
21
|
+
|
|
22
|
+
## Why we have it
|
|
23
|
+
|
|
24
|
+
Without one shared package, each app would describe the request in its own code, and the first field that one of them misspells would only show when the service set the message aside. With it, a request that breaks a rule fails in the app's own `addToOutbox`, inside its own transaction, before anything is saved. toolkit's [record 0042](https://github.com/zigordev/toolkit/blob/main/docs/adr/0042-notifications-takes-requests-and-gives-outcomes-over-kafka.md) put the schemas in a package published from this repo, and the repo's [record 0002](../../docs/adr/0002-requests-and-outcomes-travel-on-topics-named-after-the-service.md) holds the topics and the fields.
|
|
25
|
+
|
|
26
|
+
## How it works here
|
|
27
|
+
|
|
28
|
+
```mermaid
|
|
29
|
+
flowchart LR
|
|
30
|
+
app["An app: addToOutbox with emailRequested"] --> requests["notifications.requests.app"]
|
|
31
|
+
requests --> service["The service reads it with the same schema"]
|
|
32
|
+
service --> outcomes["notifications.outcomes.app: emailSent or emailFailed"]
|
|
33
|
+
outcomes --> reader["The app's consumer, with outcomeEvents"]
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- **The request is strict.** An unknown field, a missing body, a subject with a line break, more than 10 tags, or a request over 800,000 bytes fails at once. Kafka's default limit for one message is 1,048,588 bytes.
|
|
37
|
+
- **The outcomes are lenient.** A reader on version 1 lets an unknown field through, so the service can add a field to an outcome without breaking any app. A field that changes or goes gets a new version.
|
|
38
|
+
- **The request's id is its key.** `addToOutbox` gives each request a random id. The service accepts a request once, however often Kafka brings it, and its outcome names that id.
|
|
39
|
+
- **The topics carry the sender.** Only the app may write its request topic, as `platform`'s access rules say, so the service knows the producer from the topic.
|
|
40
|
+
- **The contract is committed.** `events.json` holds each event's type, version and JSON Schema. A test fails while it differs from the code, so a change to an event shows in review as a change of the contract. Another test keeps reading requests and outcomes that version 1 allowed, so tightening a rule of version 1 fails too.
|
|
41
|
+
- **It is published by the pipeline.** On a merge to `main`, the Release job of toolkit tags the next version and publishes the package to npm, through npm's trust in GitHub's proof of the workflow, with no stored token. The service's app is never published. toolkit's [packages page](https://github.com/zigordev/toolkit/blob/main/docs/handbook/packages.md) explains the release.
|
|
42
|
+
|
|
43
|
+
## How to use it
|
|
44
|
+
|
|
45
|
+
Install it beside `@zigordev/events`:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pnpm add @zigordev/notifications-events @zigordev/events zod
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Send a request in the transaction of the change it belongs to, and keep its id:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { addToOutbox } from '@zigordev/events';
|
|
55
|
+
import { emailRequested, requestTopicOf } from '@zigordev/notifications-events';
|
|
56
|
+
|
|
57
|
+
const request = await addToOutbox(tx, {
|
|
58
|
+
topic: requestTopicOf('playground-api'),
|
|
59
|
+
event: emailRequested,
|
|
60
|
+
data: {
|
|
61
|
+
to: { email: 'ana@example.com', name: 'Ana' },
|
|
62
|
+
subject: 'Confirm your email address',
|
|
63
|
+
html: '<p>…</p>',
|
|
64
|
+
text: '…',
|
|
65
|
+
tags: { template: 'verify-email', locale: 'en' },
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Read the outcomes with a consumer of the app's own:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { defineConsumer } from '@zigordev/events';
|
|
74
|
+
import { outcomeEvents, outcomeTopicOf } from '@zigordev/notifications-events';
|
|
75
|
+
|
|
76
|
+
defineConsumer({
|
|
77
|
+
group: 'playground-api.email-outcomes',
|
|
78
|
+
topics: [outcomeTopicOf('playground-api')],
|
|
79
|
+
events: outcomeEvents,
|
|
80
|
+
handle: async (event, { tx }) => {
|
|
81
|
+
if (event.type === 'email.failed') {
|
|
82
|
+
await markVerificationFailed(tx, event.data.requestId, event.data.reason);
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The repo's [user manual](../../docs/user-manual.md) lists every field, every reason and what an app needs before its first email.
|
|
89
|
+
|
|
90
|
+
## How to check that it works
|
|
91
|
+
|
|
92
|
+
- `pnpm exec vitest run --project notifications-events` runs the package's own tests: every rule of the request, the outcomes, the topic names, a request read back through the envelope of `@zigordev/events`, and the committed contract.
|
|
93
|
+
- After a change to an event, `pnpm --dir packages/notifications-events run contract` writes `events.json` again. Read the difference before committing it: a change that a reader of the same version would not understand needs a new version of the event instead.
|
|
94
|
+
- The service's tests send real requests built with this package through a real Kafka, and read the outcomes with it, so the package and the service cannot drift apart.
|
|
95
|
+
|
|
96
|
+
| What you see | What it means, and what to do |
|
|
97
|
+
| ---------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
|
|
98
|
+
| `addToOutbox` throws a `ZodError` | The request breaks a rule. The error's issues name each field |
|
|
99
|
+
| `An app is named in lower case words joined by dashes` | `requestTopicOf` was given a name that is not an app's Kafka user |
|
|
100
|
+
| No outcome comes, and the service logs `events.message_rejected` | The service could not read the request: the app sends an event or a version that the service does not know yet |
|
|
101
|
+
| An outcome `email.failed` with `sender_forbidden` | The request's `from` is not among the app's senders in the service's `producers.json` |
|
|
102
|
+
| The contract test fails | An event changed. Write `events.json` again, and check that the change keeps version 1 readable |
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type DataOf, type EventOf, type EventsOf } from '@zigordev/events';
|
|
2
|
+
import * as z from 'zod';
|
|
3
|
+
export declare const maxRequestBytes = 800000;
|
|
4
|
+
export declare const maxTags = 10;
|
|
5
|
+
export declare const mailboxSchema: z.ZodObject<{
|
|
6
|
+
email: z.ZodEmail;
|
|
7
|
+
name: z.ZodOptional<z.ZodString>;
|
|
8
|
+
}, z.core.$strict>;
|
|
9
|
+
export type Mailbox = z.output<typeof mailboxSchema>;
|
|
10
|
+
export declare const emailRequestSchema: z.ZodObject<{
|
|
11
|
+
from: z.ZodOptional<z.ZodObject<{
|
|
12
|
+
email: z.ZodEmail;
|
|
13
|
+
name: z.ZodOptional<z.ZodString>;
|
|
14
|
+
}, z.core.$strict>>;
|
|
15
|
+
to: z.ZodObject<{
|
|
16
|
+
email: z.ZodEmail;
|
|
17
|
+
name: z.ZodOptional<z.ZodString>;
|
|
18
|
+
}, z.core.$strict>;
|
|
19
|
+
replyTo: z.ZodOptional<z.ZodObject<{
|
|
20
|
+
email: z.ZodEmail;
|
|
21
|
+
name: z.ZodOptional<z.ZodString>;
|
|
22
|
+
}, z.core.$strict>>;
|
|
23
|
+
subject: z.ZodString;
|
|
24
|
+
html: z.ZodOptional<z.ZodString>;
|
|
25
|
+
text: z.ZodOptional<z.ZodString>;
|
|
26
|
+
tags: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
27
|
+
}, z.core.$strict>;
|
|
28
|
+
export declare const emailRequested: import("@zigordev/events").EventDefinition<"email.requested", z.ZodObject<{
|
|
29
|
+
from: z.ZodOptional<z.ZodObject<{
|
|
30
|
+
email: z.ZodEmail;
|
|
31
|
+
name: z.ZodOptional<z.ZodString>;
|
|
32
|
+
}, z.core.$strict>>;
|
|
33
|
+
to: z.ZodObject<{
|
|
34
|
+
email: z.ZodEmail;
|
|
35
|
+
name: z.ZodOptional<z.ZodString>;
|
|
36
|
+
}, z.core.$strict>;
|
|
37
|
+
replyTo: z.ZodOptional<z.ZodObject<{
|
|
38
|
+
email: z.ZodEmail;
|
|
39
|
+
name: z.ZodOptional<z.ZodString>;
|
|
40
|
+
}, z.core.$strict>>;
|
|
41
|
+
subject: z.ZodString;
|
|
42
|
+
html: z.ZodOptional<z.ZodString>;
|
|
43
|
+
text: z.ZodOptional<z.ZodString>;
|
|
44
|
+
tags: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
45
|
+
}, z.core.$strict>>;
|
|
46
|
+
export declare const failureReasons: readonly ['rejected', 'attempts_exhausted', 'retry_limit_reached', 'sender_forbidden'];
|
|
47
|
+
export type FailureReason = (typeof failureReasons)[number];
|
|
48
|
+
export declare const emailSent: import("@zigordev/events").EventDefinition<"email.sent", z.ZodObject<{
|
|
49
|
+
requestId: z.ZodUUID;
|
|
50
|
+
sentAt: z.ZodISODateTime;
|
|
51
|
+
attempts: z.ZodNumber;
|
|
52
|
+
}, z.core.$strip>>;
|
|
53
|
+
export declare const emailFailed: import("@zigordev/events").EventDefinition<"email.failed", z.ZodObject<{
|
|
54
|
+
requestId: z.ZodUUID;
|
|
55
|
+
failedAt: z.ZodISODateTime;
|
|
56
|
+
attempts: z.ZodNumber;
|
|
57
|
+
reason: z.ZodEnum<{
|
|
58
|
+
attempts_exhausted: "attempts_exhausted";
|
|
59
|
+
rejected: "rejected";
|
|
60
|
+
retry_limit_reached: "retry_limit_reached";
|
|
61
|
+
sender_forbidden: "sender_forbidden";
|
|
62
|
+
}>;
|
|
63
|
+
smtpCode: z.ZodNullable<z.ZodNumber>;
|
|
64
|
+
}, z.core.$strip>>;
|
|
65
|
+
export declare const outcomeEvents: readonly [import("@zigordev/events").EventDefinition<"email.sent", z.ZodObject<{
|
|
66
|
+
requestId: z.ZodUUID;
|
|
67
|
+
sentAt: z.ZodISODateTime;
|
|
68
|
+
attempts: z.ZodNumber;
|
|
69
|
+
}, z.core.$strip>>, import("@zigordev/events").EventDefinition<"email.failed", z.ZodObject<{
|
|
70
|
+
requestId: z.ZodUUID;
|
|
71
|
+
failedAt: z.ZodISODateTime;
|
|
72
|
+
attempts: z.ZodNumber;
|
|
73
|
+
reason: z.ZodEnum<{
|
|
74
|
+
attempts_exhausted: "attempts_exhausted";
|
|
75
|
+
rejected: "rejected";
|
|
76
|
+
retry_limit_reached: "retry_limit_reached";
|
|
77
|
+
sender_forbidden: "sender_forbidden";
|
|
78
|
+
}>;
|
|
79
|
+
smtpCode: z.ZodNullable<z.ZodNumber>;
|
|
80
|
+
}, z.core.$strip>>];
|
|
81
|
+
export type EmailRequest = DataOf<typeof emailRequested>;
|
|
82
|
+
export type EmailRequestedEvent = EventOf<typeof emailRequested>;
|
|
83
|
+
export type EmailOutcomeEvent = EventsOf<typeof outcomeEvents>;
|
|
84
|
+
//# sourceMappingURL=events.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAe,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACzF,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAMzB,eAAO,MAAM,eAAe,SAAU,CAAC;AACvC,eAAO,MAAM,OAAO,KAAK,CAAC;AAE1B,eAAO,MAAM,aAAa;;;kBASxB,CAAC;AAEH,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,aAAa,CAAC,CAAC;AAErD,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;;kBAwB3B,CAAC;AAEL,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;mBAIzB,CAAC;AAEH,eAAO,MAAM,cAAc,YACzB,UAAU,EACV,oBAAoB,EACpB,qBAAqB,EACrB,kBAAkB,CACV,CAAC;AAEX,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AAE5D,eAAO,MAAM,SAAS;;;;kBAQpB,CAAC;AAEH,eAAO,MAAM,WAAW;;;;;;;;;;;kBAUtB,CAAC;AAEH,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;mBAAoC,CAAC;AAE/D,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,OAAO,cAAc,CAAC,CAAC;AACzD,MAAM,MAAM,mBAAmB,GAAG,OAAO,CAAC,OAAO,cAAc,CAAC,CAAC;AACjE,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC,OAAO,aAAa,CAAC,CAAC"}
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { defineEvent } from '@zigordev/events';
|
|
2
|
+
import * as z from 'zod';
|
|
3
|
+
const noControlCharacters = /^[^\p{Cc}]*$/u;
|
|
4
|
+
const tagKey = /^[a-z][a-z0-9_.-]{0,63}$/;
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
export const maxRequestBytes = 800_000;
|
|
7
|
+
export const maxTags = 10;
|
|
8
|
+
export const mailboxSchema = z.strictObject({
|
|
9
|
+
email: z.email().max(254),
|
|
10
|
+
name: z
|
|
11
|
+
.string()
|
|
12
|
+
.trim()
|
|
13
|
+
.min(1)
|
|
14
|
+
.max(200)
|
|
15
|
+
.regex(noControlCharacters, 'must not contain control characters')
|
|
16
|
+
.optional(),
|
|
17
|
+
});
|
|
18
|
+
export const emailRequestSchema = z
|
|
19
|
+
.strictObject({
|
|
20
|
+
from: mailboxSchema.optional(),
|
|
21
|
+
to: mailboxSchema,
|
|
22
|
+
replyTo: mailboxSchema.optional(),
|
|
23
|
+
subject: z
|
|
24
|
+
.string()
|
|
25
|
+
.trim()
|
|
26
|
+
.min(1)
|
|
27
|
+
.max(998)
|
|
28
|
+
.regex(noControlCharacters, 'must not contain control characters'),
|
|
29
|
+
html: z.string().min(1).max(maxRequestBytes).optional(),
|
|
30
|
+
text: z.string().min(1).max(maxRequestBytes).optional(),
|
|
31
|
+
tags: z
|
|
32
|
+
.record(z.string().regex(tagKey, 'must be a lowercase key'), z.string().max(256))
|
|
33
|
+
.refine((tags) => Object.keys(tags).length <= maxTags, `must hold at most ${maxTags} tags`)
|
|
34
|
+
.default({}),
|
|
35
|
+
})
|
|
36
|
+
.refine((request) => request.html !== undefined || request.text !== undefined, {
|
|
37
|
+
message: 'give html, text or both',
|
|
38
|
+
path: ['text'],
|
|
39
|
+
})
|
|
40
|
+
.refine((request) => encoder.encode(JSON.stringify(request)).byteLength <= maxRequestBytes, {
|
|
41
|
+
message: `must be at most ${maxRequestBytes} bytes as JSON`,
|
|
42
|
+
});
|
|
43
|
+
export const emailRequested = defineEvent({
|
|
44
|
+
type: 'email.requested',
|
|
45
|
+
version: 1,
|
|
46
|
+
schema: emailRequestSchema,
|
|
47
|
+
});
|
|
48
|
+
export const failureReasons = [
|
|
49
|
+
'rejected',
|
|
50
|
+
'attempts_exhausted',
|
|
51
|
+
'retry_limit_reached',
|
|
52
|
+
'sender_forbidden',
|
|
53
|
+
];
|
|
54
|
+
export const emailSent = defineEvent({
|
|
55
|
+
type: 'email.sent',
|
|
56
|
+
version: 1,
|
|
57
|
+
schema: z.object({
|
|
58
|
+
requestId: z.uuid(),
|
|
59
|
+
sentAt: z.iso.datetime({ offset: true }),
|
|
60
|
+
attempts: z.number().int().min(1),
|
|
61
|
+
}),
|
|
62
|
+
});
|
|
63
|
+
export const emailFailed = defineEvent({
|
|
64
|
+
type: 'email.failed',
|
|
65
|
+
version: 1,
|
|
66
|
+
schema: z.object({
|
|
67
|
+
requestId: z.uuid(),
|
|
68
|
+
failedAt: z.iso.datetime({ offset: true }),
|
|
69
|
+
attempts: z.number().int().min(0),
|
|
70
|
+
reason: z.enum(failureReasons),
|
|
71
|
+
smtpCode: z.number().int().min(200).max(599).nullable(),
|
|
72
|
+
}),
|
|
73
|
+
});
|
|
74
|
+
export const outcomeEvents = [emailSent, emailFailed];
|
|
75
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,WAAW,EAA+B,MAAM,kBAAkB,CAAC;AACzF,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB,MAAM,mBAAmB,GAAG,eAAe,CAAC;AAC5C,MAAM,MAAM,GAAG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C;AAEH,MAAM,CAAC,MAAM,WAAW,GAAG,WAAW,CAAC;IACrC,IAAI,EAAE,cAAc;IACpB,OAAO,EAAE,CAAC;IACV,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC;QACf,SAAS,EAAE,CAAC,CAAC,IAAI,EAAE;QACnB,QAAQ,EAAE,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;QAC1C,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QACjC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,cAAc,CAAC;QAC9B,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE;KACxD,CAAC;CACH,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,SAAS,EAAE,WAAW,CAAU,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { EmailOutcomeEvent, EmailRequest, EmailRequestedEvent, FailureReason, Mailbox, } from './events.ts';
|
|
2
|
+
export { emailFailed, emailRequested, emailRequestSchema, emailSent, failureReasons, mailboxSchema, maxRequestBytes, maxTags, outcomeEvents, } from './events.ts';
|
|
3
|
+
export { appOfRequestTopic, outcomeTopicOf, outcomeTopicPrefix, requestTopicOf, requestTopicPrefix, } from './topics.ts';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,iBAAiB,EACjB,YAAY,EACZ,mBAAmB,EACnB,aAAa,EACb,OAAO,GACR,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,WAAW,EACX,cAAc,EACd,kBAAkB,EAClB,SAAS,EACT,cAAc,EACd,aAAa,EACb,eAAe,EACf,OAAO,EACP,aAAa,GACd,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,kBAAkB,EAClB,cAAc,EACd,kBAAkB,GACnB,MAAM,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { emailFailed, emailRequested, emailRequestSchema, emailSent, failureReasons, mailboxSchema, maxRequestBytes, maxTags, outcomeEvents, } from './events.js';
|
|
2
|
+
export { appOfRequestTopic, outcomeTopicOf, outcomeTopicPrefix, requestTopicOf, requestTopicPrefix, } from './topics.js';
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,WAAW,EACX,cAAc,EACd,kBAAkB,EAClB,SAAS,EACT,cAAc,EACd,aAAa,EACb,eAAe,EACf,OAAO,EACP,aAAa,GACd,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,kBAAkB,EAClB,cAAc,EACd,kBAAkB,GACnB,MAAM,aAAa,CAAC"}
|
package/dist/topics.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const requestTopicPrefix = "notifications.requests.";
|
|
2
|
+
export declare const outcomeTopicPrefix = "notifications.outcomes.";
|
|
3
|
+
export declare function requestTopicOf(app: string): string;
|
|
4
|
+
export declare function outcomeTopicOf(app: string): string;
|
|
5
|
+
export declare function appOfRequestTopic(topic: string): string | undefined;
|
|
6
|
+
//# sourceMappingURL=topics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"topics.d.ts","sourceRoot":"","sources":["../src/topics.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,kBAAkB,4BAA4B,CAAC;AAC5D,eAAO,MAAM,kBAAkB,4BAA4B,CAAC;AAW5D,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAMnE"}
|
package/dist/topics.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const appName = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
2
|
+
export const requestTopicPrefix = 'notifications.requests.';
|
|
3
|
+
export const outcomeTopicPrefix = 'notifications.outcomes.';
|
|
4
|
+
function checked(app) {
|
|
5
|
+
if (!appName.test(app)) {
|
|
6
|
+
throw new Error(`An app is named in lower case words joined by dashes, such as playground-api, not ${app}`);
|
|
7
|
+
}
|
|
8
|
+
return app;
|
|
9
|
+
}
|
|
10
|
+
export function requestTopicOf(app) {
|
|
11
|
+
return `${requestTopicPrefix}${checked(app)}`;
|
|
12
|
+
}
|
|
13
|
+
export function outcomeTopicOf(app) {
|
|
14
|
+
return `${outcomeTopicPrefix}${checked(app)}`;
|
|
15
|
+
}
|
|
16
|
+
export function appOfRequestTopic(topic) {
|
|
17
|
+
if (!topic.startsWith(requestTopicPrefix)) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
const app = topic.slice(requestTopicPrefix.length);
|
|
21
|
+
return appName.test(app) ? app : undefined;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=topics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"topics.js","sourceRoot":"","sources":["../src/topics.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,GAAG,iCAAiC,CAAC;AAElD,MAAM,CAAC,MAAM,kBAAkB,GAAG,yBAAyB,CAAC;AAC5D,MAAM,CAAC,MAAM,kBAAkB,GAAG,yBAAyB,CAAC;AAE5D,SAAS,OAAO,CAAC,GAAW;IAC1B,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CACb,qFAAqF,GAAG,EAAE,CAC3F,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,GAAW;IACxC,OAAO,GAAG,kBAAkB,GAAG,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;AAChD,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,GAAW;IACxC,OAAO,GAAG,kBAAkB,GAAG,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;AAChD,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,KAAa;IAC7C,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,kBAAkB,CAAC,EAAE,CAAC;QAC1C,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC;IACnD,OAAO,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC;AAC7C,CAAC"}
|
package/events.json
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
{
|
|
2
|
+
"topics": {
|
|
3
|
+
"requests": "notifications.requests.<app>",
|
|
4
|
+
"outcomes": "notifications.outcomes.<app>"
|
|
5
|
+
},
|
|
6
|
+
"events": [
|
|
7
|
+
{
|
|
8
|
+
"type": "email.requested",
|
|
9
|
+
"version": 1,
|
|
10
|
+
"schema": {
|
|
11
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
12
|
+
"type": "object",
|
|
13
|
+
"properties": {
|
|
14
|
+
"from": {
|
|
15
|
+
"type": "object",
|
|
16
|
+
"properties": {
|
|
17
|
+
"email": {
|
|
18
|
+
"type": "string",
|
|
19
|
+
"maxLength": 254,
|
|
20
|
+
"format": "email",
|
|
21
|
+
"pattern": "^(?:[A-Za-z0-9_'+\\-]+\\.)*[A-Za-z0-9_'+\\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$"
|
|
22
|
+
},
|
|
23
|
+
"name": {
|
|
24
|
+
"type": "string",
|
|
25
|
+
"minLength": 1,
|
|
26
|
+
"maxLength": 200,
|
|
27
|
+
"pattern": "^[^\\p{Cc}]*$"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"required": ["email"],
|
|
31
|
+
"additionalProperties": false
|
|
32
|
+
},
|
|
33
|
+
"to": {
|
|
34
|
+
"type": "object",
|
|
35
|
+
"properties": {
|
|
36
|
+
"email": {
|
|
37
|
+
"type": "string",
|
|
38
|
+
"maxLength": 254,
|
|
39
|
+
"format": "email",
|
|
40
|
+
"pattern": "^(?:[A-Za-z0-9_'+\\-]+\\.)*[A-Za-z0-9_'+\\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$"
|
|
41
|
+
},
|
|
42
|
+
"name": {
|
|
43
|
+
"type": "string",
|
|
44
|
+
"minLength": 1,
|
|
45
|
+
"maxLength": 200,
|
|
46
|
+
"pattern": "^[^\\p{Cc}]*$"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"required": ["email"],
|
|
50
|
+
"additionalProperties": false
|
|
51
|
+
},
|
|
52
|
+
"replyTo": {
|
|
53
|
+
"type": "object",
|
|
54
|
+
"properties": {
|
|
55
|
+
"email": {
|
|
56
|
+
"type": "string",
|
|
57
|
+
"maxLength": 254,
|
|
58
|
+
"format": "email",
|
|
59
|
+
"pattern": "^(?:[A-Za-z0-9_'+\\-]+\\.)*[A-Za-z0-9_'+\\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$"
|
|
60
|
+
},
|
|
61
|
+
"name": {
|
|
62
|
+
"type": "string",
|
|
63
|
+
"minLength": 1,
|
|
64
|
+
"maxLength": 200,
|
|
65
|
+
"pattern": "^[^\\p{Cc}]*$"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"required": ["email"],
|
|
69
|
+
"additionalProperties": false
|
|
70
|
+
},
|
|
71
|
+
"subject": {
|
|
72
|
+
"type": "string",
|
|
73
|
+
"minLength": 1,
|
|
74
|
+
"maxLength": 998,
|
|
75
|
+
"pattern": "^[^\\p{Cc}]*$"
|
|
76
|
+
},
|
|
77
|
+
"html": {
|
|
78
|
+
"type": "string",
|
|
79
|
+
"minLength": 1,
|
|
80
|
+
"maxLength": 800000
|
|
81
|
+
},
|
|
82
|
+
"text": {
|
|
83
|
+
"type": "string",
|
|
84
|
+
"minLength": 1,
|
|
85
|
+
"maxLength": 800000
|
|
86
|
+
},
|
|
87
|
+
"tags": {
|
|
88
|
+
"default": {},
|
|
89
|
+
"type": "object",
|
|
90
|
+
"propertyNames": {
|
|
91
|
+
"type": "string",
|
|
92
|
+
"pattern": "^[a-z][a-z0-9_.-]{0,63}$"
|
|
93
|
+
},
|
|
94
|
+
"additionalProperties": {
|
|
95
|
+
"type": "string",
|
|
96
|
+
"maxLength": 256
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
"required": ["to", "subject"],
|
|
101
|
+
"additionalProperties": false
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
"type": "email.sent",
|
|
106
|
+
"version": 1,
|
|
107
|
+
"schema": {
|
|
108
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
109
|
+
"type": "object",
|
|
110
|
+
"properties": {
|
|
111
|
+
"requestId": {
|
|
112
|
+
"type": "string",
|
|
113
|
+
"format": "uuid",
|
|
114
|
+
"pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
|
|
115
|
+
},
|
|
116
|
+
"sentAt": {
|
|
117
|
+
"type": "string",
|
|
118
|
+
"format": "date-time",
|
|
119
|
+
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
|
|
120
|
+
},
|
|
121
|
+
"attempts": {
|
|
122
|
+
"type": "integer",
|
|
123
|
+
"minimum": 1,
|
|
124
|
+
"maximum": 9007199254740991
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
"required": ["requestId", "sentAt", "attempts"]
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
"type": "email.failed",
|
|
132
|
+
"version": 1,
|
|
133
|
+
"schema": {
|
|
134
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
135
|
+
"type": "object",
|
|
136
|
+
"properties": {
|
|
137
|
+
"requestId": {
|
|
138
|
+
"type": "string",
|
|
139
|
+
"format": "uuid",
|
|
140
|
+
"pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
|
|
141
|
+
},
|
|
142
|
+
"failedAt": {
|
|
143
|
+
"type": "string",
|
|
144
|
+
"format": "date-time",
|
|
145
|
+
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
|
|
146
|
+
},
|
|
147
|
+
"attempts": {
|
|
148
|
+
"type": "integer",
|
|
149
|
+
"minimum": 0,
|
|
150
|
+
"maximum": 9007199254740991
|
|
151
|
+
},
|
|
152
|
+
"reason": {
|
|
153
|
+
"type": "string",
|
|
154
|
+
"enum": ["rejected", "attempts_exhausted", "retry_limit_reached", "sender_forbidden"]
|
|
155
|
+
},
|
|
156
|
+
"smtpCode": {
|
|
157
|
+
"anyOf": [
|
|
158
|
+
{
|
|
159
|
+
"type": "integer",
|
|
160
|
+
"minimum": 200,
|
|
161
|
+
"maximum": 599
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
"type": "null"
|
|
165
|
+
}
|
|
166
|
+
]
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
"required": ["requestId", "failedAt", "attempts", "reason", "smtpCode"]
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
]
|
|
173
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zigordev/notifications-events",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "The events of the notifications service: the request an app sends to have an email delivered, the outcome the service sends back, and the topics they travel on",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=26"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"events.json"
|
|
13
|
+
],
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@zigordev/events": "^1.26.0",
|
|
22
|
+
"zod": "^4.6.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "26.6.6",
|
|
26
|
+
"@zigordev/dev": "1.26.0",
|
|
27
|
+
"@zigordev/events": "1.26.0",
|
|
28
|
+
"typescript": "7.0.2",
|
|
29
|
+
"vitest": "5.1.1",
|
|
30
|
+
"zod": "4.6.7"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/zigordev/notifications.git",
|
|
38
|
+
"directory": "packages/notifications-events"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsc -p tsconfig.build.json",
|
|
42
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
43
|
+
"contract": "node scripts/contract.ts"
|
|
44
|
+
}
|
|
6
45
|
}
|