@krak-stack/registry 0.10.5 → 0.10.7

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.
@@ -0,0 +1,469 @@
1
+ // ../../src/lib/email-document/render.tsx
2
+ import { Effect, Schema as Schema5 } from "effect";
3
+ import { renderToStaticMarkup } from "react-dom/server";
4
+
5
+ // ../../src/lib/rich-text/schema.ts
6
+ import { Schema } from "effect";
7
+ var RichTextLink = Schema.String.check(Schema.isMaxLength(2000), Schema.makeFilter((value) => {
8
+ if (/[\s\p{Cc}\\{}]/u.test(value))
9
+ return false;
10
+ const url = URL.parse(value);
11
+ return url !== null && ["https:", "mailto:"].includes(url.protocol) && !url.username && !url.password;
12
+ })).annotate({ identifier: "RichTextLink" });
13
+ var RichTextMarks = Schema.Struct({
14
+ bold: Schema.optional(Schema.Boolean),
15
+ italic: Schema.optional(Schema.Boolean),
16
+ underline: Schema.optional(Schema.Boolean),
17
+ href: Schema.optional(RichTextLink)
18
+ }).annotate({ identifier: "RichTextMarks" });
19
+ var RichTextInline = Schema.Union([
20
+ Schema.Struct({
21
+ type: Schema.Literal("text"),
22
+ text: Schema.String.check(Schema.isMaxLength(20000)),
23
+ marks: Schema.optional(RichTextMarks)
24
+ }),
25
+ Schema.Struct({
26
+ type: Schema.Literal("variable"),
27
+ key: Schema.String.check(Schema.isPattern(/^[a-zA-Z][a-zA-Z0-9_.]{0,79}$/)),
28
+ marks: Schema.optional(RichTextMarks)
29
+ })
30
+ ]).annotate({ identifier: "RichTextInline" });
31
+ var RichTextBlockKind = Schema.Literals([
32
+ "paragraph",
33
+ "heading",
34
+ "bullet",
35
+ "numbered"
36
+ ]).annotate({ identifier: "RichTextBlockKind" });
37
+ var RichTextDocument = Schema.Struct({
38
+ version: Schema.Literal(1),
39
+ blocks: Schema.Array(Schema.Struct({
40
+ type: RichTextBlockKind,
41
+ content: Schema.Array(RichTextInline).check(Schema.isMaxLength(2000))
42
+ })).check(Schema.isMinLength(1), Schema.isMaxLength(100))
43
+ }).check(Schema.makeFilter((value) => value.blocks.reduce((size, block) => size + block.content.reduce((count, inline) => count + (inline.type === "text" ? inline.text.length : 1), 0), 0) <= 20000)).annotate({ identifier: "RichTextDocument" });
44
+ var RichTextVariable = Schema.Struct({
45
+ key: Schema.String.check(Schema.isPattern(/^[a-zA-Z][a-zA-Z0-9_.]{0,79}$/)),
46
+ label: Schema.String,
47
+ type: Schema.Literals(["text", "url"]),
48
+ sample: Schema.String
49
+ }).annotate({ identifier: "RichTextVariable" });
50
+
51
+ // ../../src/lib/rich-text/model.ts
52
+ import { Schema as Schema2 } from "effect";
53
+ var sameMarks = Schema2.toEquivalence(RichTextMarks);
54
+ var richTextPlainText = (value, variables) => value.blocks.map((block) => block.content.map((inline) => inline.type === "text" ? inline.text : variables?.[inline.key] ?? `{{${inline.key}}}`).join("")).join(`
55
+ `);
56
+
57
+ // ../../src/lib/email-document/schema.ts
58
+ import { Schema as Schema3 } from "effect";
59
+ var EmailImageUrl = RichTextLink.check(Schema3.isPattern(/^https:\/\//)).annotate({ identifier: "EmailImageUrl" });
60
+ var EmailColor = Schema3.String.check(Schema3.isPattern(/^#[0-9a-fA-F]{6}$/)).annotate({ identifier: "EmailColor" });
61
+ var EmailAlignment = Schema3.Literals([
62
+ "left",
63
+ "center",
64
+ "right"
65
+ ]).annotate({ identifier: "EmailAlignment" });
66
+ var EmailFont = Schema3.Literals(["sans", "serif", "mono"]).annotate({
67
+ identifier: "EmailFont"
68
+ });
69
+ var spacing = Schema3.Int.check(Schema3.isBetween({ minimum: 0, maximum: 96 }));
70
+ var common = {
71
+ id: Schema3.NonEmptyString.check(Schema3.isMaxLength(80)),
72
+ alignment: EmailAlignment,
73
+ paddingTop: spacing,
74
+ paddingBottom: spacing
75
+ };
76
+ var EmailDestination = Schema3.Union([
77
+ Schema3.Struct({ type: Schema3.Literal("url"), url: RichTextLink }),
78
+ Schema3.Struct({
79
+ type: Schema3.Literal("variable"),
80
+ key: RichTextVariable.fields.key
81
+ })
82
+ ]).annotate({ identifier: "EmailDestination" });
83
+ var EmailElement = Schema3.Union([
84
+ Schema3.Struct({
85
+ ...common,
86
+ type: Schema3.Literal("text"),
87
+ content: RichTextDocument
88
+ }),
89
+ Schema3.Struct({
90
+ ...common,
91
+ type: Schema3.Literal("button"),
92
+ label: Schema3.String.check(Schema3.isMaxLength(200)),
93
+ destination: EmailDestination
94
+ }),
95
+ Schema3.Struct({
96
+ ...common,
97
+ type: Schema3.Literal("code"),
98
+ variable: RichTextVariable.fields.key
99
+ }),
100
+ Schema3.Struct({
101
+ ...common,
102
+ type: Schema3.Literals(["logo", "image"]),
103
+ src: EmailImageUrl,
104
+ alt: Schema3.String.check(Schema3.isMaxLength(200)),
105
+ width: Schema3.Int.check(Schema3.isBetween({ minimum: 32, maximum: 560 }))
106
+ }),
107
+ Schema3.Struct({ ...common, type: Schema3.Literal("divider") }),
108
+ Schema3.Struct({ ...common, type: Schema3.Literal("spacer"), height: spacing })
109
+ ]).annotate({ identifier: "EmailElement" });
110
+ var subject = Schema3.String.check(Schema3.isMaxLength(200), Schema3.isPattern(/^[^\r\n]*$/));
111
+ var EmailDocument = Schema3.Struct({
112
+ version: Schema3.Literal(1),
113
+ subject,
114
+ preview: subject,
115
+ appearance: Schema3.Struct({
116
+ background: EmailColor,
117
+ surface: EmailColor,
118
+ text: EmailColor,
119
+ primary: EmailColor,
120
+ primaryText: EmailColor,
121
+ font: EmailFont,
122
+ radius: Schema3.Int.check(Schema3.isBetween({ minimum: 0, maximum: 32 }))
123
+ }),
124
+ elements: Schema3.Array(EmailElement).check(Schema3.isMaxLength(40))
125
+ }).check(Schema3.makeFilter((document) => new Set(document.elements.map((element) => element.id)).size === document.elements.length)).annotate({ identifier: "EmailDocument" });
126
+ var EmailRenderInput = Schema3.Struct({
127
+ document: EmailDocument,
128
+ variables: Schema3.Array(RichTextVariable),
129
+ values: Schema3.Record(Schema3.String, Schema3.String),
130
+ requiredVariables: Schema3.optional(Schema3.Array(Schema3.String))
131
+ }).annotate({ identifier: "EmailRenderInput" });
132
+ var EmailRenderResult = Schema3.Struct({
133
+ subject: Schema3.String,
134
+ html: Schema3.String,
135
+ text: Schema3.String
136
+ }).annotate({ identifier: "EmailRenderResult" });
137
+
138
+ // ../../src/lib/email-document/model.ts
139
+ import { Schema as Schema4 } from "effect";
140
+ var interpolateEmailText = (text, values) => text.replace(/\{\{([a-zA-Z][a-zA-Z0-9_.]*)\}\}/g, (token, key) => values[key] ?? token);
141
+ var textReferences = (text) => [...text.matchAll(/\{\{([a-zA-Z][a-zA-Z0-9_.]*)\}\}/g)].map((match) => match[1] ?? "");
142
+ var emailDocumentReferences = (document) => [
143
+ ...new Set([
144
+ ...textReferences(document.subject),
145
+ ...textReferences(document.preview),
146
+ ...document.elements.flatMap((element) => {
147
+ switch (element.type) {
148
+ case "text":
149
+ return element.content.blocks.flatMap((block) => block.content.flatMap((inline) => inline.type === "variable" ? [inline.key] : []));
150
+ case "code":
151
+ return [element.variable];
152
+ case "button":
153
+ return [
154
+ ...textReferences(element.label),
155
+ ...element.destination.type === "variable" ? [element.destination.key] : []
156
+ ];
157
+ default:
158
+ return [];
159
+ }
160
+ })
161
+ ])
162
+ ];
163
+ var validateEmailDocument = ({
164
+ document,
165
+ variables,
166
+ values,
167
+ requiredVariables = []
168
+ }) => {
169
+ if (!Schema4.is(EmailDocument)(document))
170
+ return [{ code: "invalid-document" }];
171
+ const references = emailDocumentReferences(document);
172
+ const issues = [];
173
+ for (const key of references) {
174
+ if (!variables.some((variable) => variable.key === key))
175
+ issues.push({ code: "unknown-variable", key });
176
+ if (values && !values[key])
177
+ issues.push({ code: "missing-variable", key });
178
+ }
179
+ for (const element of document.elements) {
180
+ if (element.type === "button" && element.destination.type === "variable") {
181
+ const key = element.destination.key;
182
+ if (variables.find((variable) => variable.key === key)?.type !== "url" || values && !Schema4.is(RichTextLink)(values[key]))
183
+ issues.push({ code: "invalid-url", key });
184
+ }
185
+ }
186
+ for (const key of requiredVariables)
187
+ if (!document.elements.some((element) => element.type === "code" && element.variable === key || element.type === "button" && element.destination.type === "variable" && element.destination.key === key || element.type === "text" && element.content.blocks.some((block) => block.content.some((inline) => inline.type === "variable" && inline.key === key))))
188
+ issues.push({ code: "missing-required-variable", key });
189
+ return issues;
190
+ };
191
+
192
+ // ../../src/lib/email-document/render.tsx
193
+ import { jsx, jsxs } from "react/jsx-runtime";
194
+
195
+ class EmailDocumentRenderError extends Schema5.TaggedError()("EmailDocumentRenderError", { reason: Schema5.String }) {
196
+ }
197
+ var fonts = {
198
+ sans: "Arial, Helvetica, sans-serif",
199
+ serif: "Georgia, 'Times New Roman', serif",
200
+ mono: "'Courier New', monospace"
201
+ };
202
+ var inline = (node, values) => {
203
+ let content = node.type === "text" ? node.text : values[node.key];
204
+ if (node.marks?.bold)
205
+ content = /* @__PURE__ */ jsx("strong", {
206
+ children: content
207
+ });
208
+ if (node.marks?.italic)
209
+ content = /* @__PURE__ */ jsx("em", {
210
+ children: content
211
+ });
212
+ if (node.marks?.underline)
213
+ content = /* @__PURE__ */ jsx("u", {
214
+ children: content
215
+ });
216
+ if (node.marks?.href)
217
+ content = /* @__PURE__ */ jsx("a", {
218
+ href: node.marks.href,
219
+ style: { color: "inherit" },
220
+ children: content
221
+ });
222
+ return content;
223
+ };
224
+ var richContent = (value, values) => value.blocks.flatMap((block, index) => {
225
+ if ((block.type === "bullet" || block.type === "numbered") && value.blocks[index - 1]?.type === block.type)
226
+ return [];
227
+ const content = block.content.map((node, key) => /* @__PURE__ */ jsx("span", {
228
+ children: inline(node, values)
229
+ }, key));
230
+ if (block.type === "heading")
231
+ return /* @__PURE__ */ jsx("h2", {
232
+ style: { margin: "0 0 12px", fontSize: 26, lineHeight: 1.25 },
233
+ children: content
234
+ }, index);
235
+ if (block.type === "bullet" || block.type === "numbered") {
236
+ const List = block.type === "bullet" ? "ul" : "ol";
237
+ const end = value.blocks.findIndex((next, nextIndex) => nextIndex > index && next.type !== block.type);
238
+ const items = value.blocks.slice(index, end < 0 ? undefined : end);
239
+ return /* @__PURE__ */ jsx(List, {
240
+ style: { margin: "0 0 8px", paddingLeft: 24 },
241
+ children: items.map((item, itemIndex) => /* @__PURE__ */ jsx("li", {
242
+ children: item.content.map((node, key) => /* @__PURE__ */ jsx("span", {
243
+ children: inline(node, values)
244
+ }, key))
245
+ }, itemIndex))
246
+ }, index);
247
+ }
248
+ return /* @__PURE__ */ jsx("p", {
249
+ style: { margin: "0 0 12px", minHeight: 20 },
250
+ children: content.length ? content : /* @__PURE__ */ jsx("br", {})
251
+ }, index);
252
+ });
253
+ var renderEmailDocument = Effect.fn("EmailDocument.render")(function* (input) {
254
+ const decoded = yield* Schema5.decodeEffect(EmailRenderInput)(input).pipe(Effect.mapError(() => new EmailDocumentRenderError({ reason: "invalid-document" })));
255
+ const issues = validateEmailDocument(decoded);
256
+ if (issues.length)
257
+ return yield* new EmailDocumentRenderError({
258
+ reason: issues.map((issue) => `${issue.code}${issue.key ? `:${issue.key}` : ""}`).join(", ")
259
+ });
260
+ const { document, values } = decoded;
261
+ const theme = document.appearance;
262
+ const interpolate = (value) => interpolateEmailText(value, values);
263
+ const subject = interpolate(document.subject).replace(/[\r\n]/g, " ");
264
+ const renderedElements = document.elements.map((element) => {
265
+ let content;
266
+ switch (element.type) {
267
+ case "text":
268
+ content = richContent(element.content, values);
269
+ break;
270
+ case "button": {
271
+ const href = element.destination.type === "url" ? element.destination.url : values[element.destination.key];
272
+ if (!Schema5.is(RichTextLink)(href))
273
+ return null;
274
+ content = /* @__PURE__ */ jsx("table", {
275
+ role: "presentation",
276
+ cellPadding: 0,
277
+ cellSpacing: 0,
278
+ style: { display: "inline-table" },
279
+ children: /* @__PURE__ */ jsx("tbody", {
280
+ children: /* @__PURE__ */ jsx("tr", {
281
+ children: /* @__PURE__ */ jsx("td", {
282
+ style: {
283
+ backgroundColor: theme.primary,
284
+ borderRadius: 6,
285
+ padding: "13px 24px"
286
+ },
287
+ children: /* @__PURE__ */ jsx("a", {
288
+ href,
289
+ style: {
290
+ color: theme.primaryText,
291
+ fontWeight: 600,
292
+ textDecoration: "none",
293
+ display: "inline-block"
294
+ },
295
+ children: interpolate(element.label)
296
+ })
297
+ })
298
+ })
299
+ })
300
+ });
301
+ break;
302
+ }
303
+ case "code":
304
+ content = /* @__PURE__ */ jsx("div", {
305
+ style: {
306
+ padding: "18px 24px",
307
+ border: "1px solid #e5e7eb",
308
+ borderRadius: 8,
309
+ fontFamily: fonts.mono,
310
+ fontSize: 32,
311
+ letterSpacing: 6,
312
+ fontWeight: 700
313
+ },
314
+ children: values[element.variable]
315
+ });
316
+ break;
317
+ case "logo":
318
+ case "image":
319
+ content = /* @__PURE__ */ jsx("img", {
320
+ src: element.src,
321
+ alt: element.alt,
322
+ width: element.width,
323
+ style: {
324
+ maxWidth: "100%",
325
+ height: "auto",
326
+ display: "inline-block",
327
+ border: 0
328
+ }
329
+ });
330
+ break;
331
+ case "divider":
332
+ content = /* @__PURE__ */ jsx("hr", {
333
+ style: { border: 0, borderTop: "1px solid #e5e7eb", margin: 0 }
334
+ });
335
+ break;
336
+ case "spacer":
337
+ content = /* @__PURE__ */ jsx("div", {
338
+ style: {
339
+ height: element.height,
340
+ lineHeight: `${element.height}px`
341
+ },
342
+ children: " "
343
+ });
344
+ break;
345
+ }
346
+ return /* @__PURE__ */ jsx("tr", {
347
+ children: /* @__PURE__ */ jsx("td", {
348
+ style: {
349
+ paddingTop: element.paddingTop,
350
+ paddingBottom: element.paddingBottom,
351
+ textAlign: element.alignment
352
+ },
353
+ children: content
354
+ })
355
+ }, element.id);
356
+ });
357
+ const bodyStyle = {
358
+ margin: 0,
359
+ padding: 0,
360
+ backgroundColor: theme.background,
361
+ color: theme.text,
362
+ fontFamily: fonts[theme.font]
363
+ };
364
+ const html = yield* Effect.try({
365
+ try: () => "<!DOCTYPE html>" + renderToStaticMarkup(/* @__PURE__ */ jsxs("html", {
366
+ children: [
367
+ /* @__PURE__ */ jsxs("head", {
368
+ children: [
369
+ /* @__PURE__ */ jsx("meta", {
370
+ charSet: "utf-8"
371
+ }),
372
+ /* @__PURE__ */ jsx("meta", {
373
+ name: "viewport",
374
+ content: "width=device-width, initial-scale=1"
375
+ }),
376
+ /* @__PURE__ */ jsx("title", {
377
+ children: subject
378
+ })
379
+ ]
380
+ }),
381
+ /* @__PURE__ */ jsxs("body", {
382
+ style: bodyStyle,
383
+ children: [
384
+ /* @__PURE__ */ jsx("div", {
385
+ style: {
386
+ display: "none",
387
+ maxHeight: 0,
388
+ overflow: "hidden",
389
+ opacity: 0
390
+ },
391
+ children: interpolate(document.preview)
392
+ }),
393
+ /* @__PURE__ */ jsx("table", {
394
+ role: "presentation",
395
+ width: "100%",
396
+ cellPadding: 0,
397
+ cellSpacing: 0,
398
+ style: bodyStyle,
399
+ children: /* @__PURE__ */ jsx("tbody", {
400
+ children: /* @__PURE__ */ jsx("tr", {
401
+ children: /* @__PURE__ */ jsx("td", {
402
+ align: "center",
403
+ style: { padding: "32px 12px" },
404
+ children: /* @__PURE__ */ jsx("table", {
405
+ role: "presentation",
406
+ width: "100%",
407
+ cellPadding: 0,
408
+ cellSpacing: 0,
409
+ style: {
410
+ maxWidth: 600,
411
+ backgroundColor: theme.surface,
412
+ borderRadius: theme.radius
413
+ },
414
+ children: /* @__PURE__ */ jsx("tbody", {
415
+ children: /* @__PURE__ */ jsx("tr", {
416
+ children: /* @__PURE__ */ jsx("td", {
417
+ style: {
418
+ padding: 32,
419
+ fontSize: 16,
420
+ lineHeight: 1.6
421
+ },
422
+ children: /* @__PURE__ */ jsx("table", {
423
+ role: "presentation",
424
+ width: "100%",
425
+ cellPadding: 0,
426
+ cellSpacing: 0,
427
+ children: /* @__PURE__ */ jsx("tbody", {
428
+ children: renderedElements
429
+ })
430
+ })
431
+ })
432
+ })
433
+ })
434
+ })
435
+ })
436
+ })
437
+ })
438
+ })
439
+ ]
440
+ })
441
+ ]
442
+ })),
443
+ catch: () => new EmailDocumentRenderError({ reason: "render-failed" })
444
+ });
445
+ const text = document.elements.map((element) => {
446
+ switch (element.type) {
447
+ case "text":
448
+ return richTextPlainText(element.content, values);
449
+ case "button":
450
+ return `${interpolate(element.label)}: ${element.destination.type === "url" ? element.destination.url : values[element.destination.key]}`;
451
+ case "code":
452
+ return values[element.variable];
453
+ case "image":
454
+ case "logo":
455
+ return element.alt;
456
+ case "divider":
457
+ return "---";
458
+ case "spacer":
459
+ return "";
460
+ }
461
+ }).filter(Boolean).join(`
462
+
463
+ `);
464
+ return { subject, html, text };
465
+ });
466
+ export {
467
+ EmailDocumentRenderError,
468
+ renderEmailDocument
469
+ };