create-rudder-app 0.4.0 → 0.6.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.
Files changed (96) hide show
  1. package/dist/index.js +3 -5
  2. package/dist/index.js.map +1 -1
  3. package/dist/templates/app/service-provider.d.ts +1 -1
  4. package/dist/templates/app/service-provider.d.ts.map +1 -1
  5. package/dist/templates/app/service-provider.js +23 -2
  6. package/dist/templates/app/service-provider.js.map +1 -1
  7. package/dist/templates/configs/index.d.ts.map +1 -1
  8. package/dist/templates/configs/index.js +0 -4
  9. package/dist/templates/configs/index.js.map +1 -1
  10. package/dist/templates/demos/avatar.d.ts +3 -0
  11. package/dist/templates/demos/avatar.d.ts.map +1 -0
  12. package/dist/templates/demos/avatar.js +182 -0
  13. package/dist/templates/demos/avatar.js.map +1 -0
  14. package/dist/templates/demos/cache.d.ts +3 -0
  15. package/dist/templates/demos/cache.d.ts.map +1 -0
  16. package/dist/templates/demos/cache.js +99 -0
  17. package/dist/templates/demos/cache.js.map +1 -0
  18. package/dist/templates/demos/fibonacci.d.ts +7 -0
  19. package/dist/templates/demos/fibonacci.d.ts.map +1 -0
  20. package/dist/templates/demos/fibonacci.js +172 -0
  21. package/dist/templates/demos/fibonacci.js.map +1 -0
  22. package/dist/templates/demos/http.d.ts +3 -0
  23. package/dist/templates/demos/http.d.ts.map +1 -0
  24. package/dist/templates/demos/http.js +117 -0
  25. package/dist/templates/demos/http.js.map +1 -0
  26. package/dist/templates/demos/index-view.d.ts.map +1 -1
  27. package/dist/templates/demos/index-view.js +16 -29
  28. package/dist/templates/demos/index-view.js.map +1 -1
  29. package/dist/templates/demos/localization.d.ts +4 -0
  30. package/dist/templates/demos/localization.d.ts.map +1 -0
  31. package/dist/templates/demos/localization.js +130 -0
  32. package/dist/templates/demos/localization.js.map +1 -0
  33. package/dist/templates/demos/mail.d.ts +4 -0
  34. package/dist/templates/demos/mail.d.ts.map +1 -0
  35. package/dist/templates/demos/mail.js +127 -0
  36. package/dist/templates/demos/mail.js.map +1 -0
  37. package/dist/templates/demos/notifications.d.ts +4 -0
  38. package/dist/templates/demos/notifications.d.ts.map +1 -0
  39. package/dist/templates/demos/notifications.js +133 -0
  40. package/dist/templates/demos/notifications.js.map +1 -0
  41. package/dist/templates/demos/pennant.d.ts +8 -0
  42. package/dist/templates/demos/pennant.d.ts.map +1 -0
  43. package/dist/templates/demos/pennant.js +138 -0
  44. package/dist/templates/demos/pennant.js.map +1 -0
  45. package/dist/templates/demos/queue.d.ts +4 -0
  46. package/dist/templates/demos/queue.d.ts.map +1 -0
  47. package/dist/templates/demos/queue.js +107 -0
  48. package/dist/templates/demos/queue.js.map +1 -0
  49. package/dist/templates/demos/registry.d.ts +13 -0
  50. package/dist/templates/demos/registry.d.ts.map +1 -1
  51. package/dist/templates/demos/registry.js +120 -3
  52. package/dist/templates/demos/registry.js.map +1 -1
  53. package/dist/templates/demos/rudder-socket.d.ts +2 -0
  54. package/dist/templates/demos/rudder-socket.d.ts.map +1 -0
  55. package/dist/templates/demos/{bk-socket.js → rudder-socket.js} +8 -8
  56. package/dist/templates/demos/rudder-socket.js.map +1 -0
  57. package/dist/templates/demos/sync.d.ts +2 -0
  58. package/dist/templates/demos/sync.d.ts.map +1 -0
  59. package/dist/templates/demos/{live.js → sync.js} +4 -4
  60. package/dist/templates/demos/sync.js.map +1 -0
  61. package/dist/templates/demos/system-info.d.ts +3 -0
  62. package/dist/templates/demos/system-info.d.ts.map +1 -0
  63. package/dist/templates/demos/system-info.js +142 -0
  64. package/dist/templates/demos/system-info.js.map +1 -0
  65. package/dist/templates/demos/todos.d.ts +6 -0
  66. package/dist/templates/demos/todos.d.ts.map +1 -0
  67. package/dist/templates/demos/todos.js +246 -0
  68. package/dist/templates/demos/todos.js.map +1 -0
  69. package/dist/templates/demos/ws.js +4 -4
  70. package/dist/templates/env.d.ts.map +1 -1
  71. package/dist/templates/env.js +0 -14
  72. package/dist/templates/env.js.map +1 -1
  73. package/dist/templates/package-json.d.ts.map +1 -1
  74. package/dist/templates/package-json.js +1 -3
  75. package/dist/templates/package-json.js.map +1 -1
  76. package/dist/templates/routes/api.d.ts.map +1 -1
  77. package/dist/templates/routes/api.js +48 -0
  78. package/dist/templates/routes/api.js.map +1 -1
  79. package/dist/templates/routes/web.d.ts.map +1 -1
  80. package/dist/templates/routes/web.js +49 -2
  81. package/dist/templates/routes/web.js.map +1 -1
  82. package/dist/templates.d.ts +2 -3
  83. package/dist/templates.d.ts.map +1 -1
  84. package/dist/templates.js +70 -10
  85. package/dist/templates.js.map +1 -1
  86. package/package.json +5 -1
  87. package/dist/templates/configs/cashier.d.ts +0 -2
  88. package/dist/templates/configs/cashier.d.ts.map +0 -1
  89. package/dist/templates/configs/cashier.js +0 -22
  90. package/dist/templates/configs/cashier.js.map +0 -1
  91. package/dist/templates/demos/bk-socket.d.ts +0 -2
  92. package/dist/templates/demos/bk-socket.d.ts.map +0 -1
  93. package/dist/templates/demos/bk-socket.js.map +0 -1
  94. package/dist/templates/demos/live.d.ts +0 -2
  95. package/dist/templates/demos/live.d.ts.map +0 -1
  96. package/dist/templates/demos/live.js.map +0 -1
@@ -0,0 +1,172 @@
1
+ // Fibonacci demo — sequential vs worker-pool parallel via @rudderjs/concurrency.
2
+ export function demosFibonacciView() {
3
+ return `import { useState } from 'react'
4
+ import '@/index.css'
5
+
6
+ interface FibResponse {
7
+ n: number
8
+ count: number
9
+ result: number
10
+ sequentialMs: number
11
+ parallelMs: number
12
+ workers: number
13
+ }
14
+
15
+ export default function FibonacciDemo() {
16
+ const [n, setN ] = useState(36)
17
+ const [count, setCount ] = useState(4)
18
+ const [data, setData ] = useState<FibResponse | null>(null)
19
+ const [loading, setLoading] = useState(false)
20
+ const [error, setError ] = useState<string | null>(null)
21
+
22
+ async function run() {
23
+ setLoading(true); setError(null); setData(null)
24
+ try {
25
+ const res = await fetch(\`/api/fib?n=\${n}&count=\${count}\`)
26
+ const body = await res.json() as FibResponse | { message: string }
27
+ if (!res.ok) throw new Error((body as { message: string }).message ?? 'Failed')
28
+ setData(body as FibResponse)
29
+ } catch (e) {
30
+ setError((e as Error).message)
31
+ } finally {
32
+ setLoading(false)
33
+ }
34
+ }
35
+
36
+ return (
37
+ <div className="page">
38
+ <nav className="page-nav">
39
+ <div className="brand">
40
+ <span className="brand-dot" />
41
+ RudderJS
42
+ </div>
43
+ <div className="nav-right">
44
+ <a href="/demos" className="nav-link">Demos</a>
45
+ <a href="/" className="nav-link">Home</a>
46
+ </div>
47
+ </nav>
48
+
49
+ <section className="hero">
50
+ <h1 className="hero-title">Worker Threads</h1>
51
+ <p className="hero-lead">
52
+ Compute <code className="inline-code">fib(n)</code> N times — sequentially on the main thread, then
53
+ in parallel via <code className="inline-code">@rudderjs/concurrency</code>'s worker pool. Watch the
54
+ parallel cost stay flat as N grows (until you saturate workers).
55
+ </p>
56
+ </section>
57
+
58
+ <section className="feature-section">
59
+ <div className="form-card">
60
+ <div style={{ display: 'flex', gap: '1rem', marginBottom: '1rem' }}>
61
+ <div style={{ flex: 1 }}>
62
+ <p className="form-label">n (Fibonacci index)</p>
63
+ <input
64
+ className="form-input"
65
+ type="number"
66
+ min={20}
67
+ max={42}
68
+ value={n}
69
+ onChange={e => setN(Number(e.target.value))}
70
+ />
71
+ </div>
72
+ <div style={{ flex: 1 }}>
73
+ <p className="form-label">count (parallel calls)</p>
74
+ <input
75
+ className="form-input"
76
+ type="number"
77
+ min={1}
78
+ max={16}
79
+ value={count}
80
+ onChange={e => setCount(Number(e.target.value))}
81
+ />
82
+ </div>
83
+ </div>
84
+
85
+ <button
86
+ className="form-submit"
87
+ onClick={run}
88
+ disabled={loading}
89
+ style={{ marginBottom: '1rem' }}
90
+ >
91
+ {loading ? 'Computing…' : \`Run \${count} × fib(\${n})\`}
92
+ </button>
93
+
94
+ {error && (
95
+ <p className="form-error">{error}</p>
96
+ )}
97
+
98
+ {data && (
99
+ <>
100
+ <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '1rem', marginBottom: '0.75rem' }}>
101
+ <div style={{ padding: '0.75rem', borderRadius: '0.375rem', border: '1px solid var(--border, #e5e7eb)' }}>
102
+ <p className="form-label" style={{ marginBottom: '0.25rem' }}>Sequential (main thread)</p>
103
+ <p style={{ fontSize: '1.25rem', fontWeight: 600, margin: 0 }}>{data.sequentialMs}ms</p>
104
+ <p className="feature-desc" style={{ fontSize: '0.7rem', marginTop: '0.25rem' }}>
105
+ {data.count} × fib({data.n}) one after another, blocking the event loop
106
+ </p>
107
+ </div>
108
+ <div style={{ padding: '0.75rem', borderRadius: '0.375rem', border: '1px solid var(--border, #e5e7eb)' }}>
109
+ <p className="form-label" style={{ marginBottom: '0.25rem' }}>Parallel ({data.workers} workers)</p>
110
+ <p style={{ fontSize: '1.25rem', fontWeight: 600, margin: 0 }}>{data.parallelMs}ms</p>
111
+ <p className="feature-desc" style={{ fontSize: '0.7rem', marginTop: '0.25rem' }}>
112
+ same {data.count} tasks via <code>Concurrency.run([...])</code>
113
+ </p>
114
+ </div>
115
+ </div>
116
+
117
+ <p className="feature-desc" style={{ fontSize: '0.85rem' }}>
118
+ Result: <code>fib({data.n}) = {data.result.toLocaleString()}</code>
119
+ {' · '}
120
+ <strong>{Math.round((data.sequentialMs / Math.max(data.parallelMs, 1)) * 10) / 10}× faster</strong>
121
+ {' '}via worker pool
122
+ </p>
123
+ </>
124
+ )}
125
+ </div>
126
+ </section>
127
+ </div>
128
+ )
129
+ }
130
+ `;
131
+ }
132
+ /**
133
+ * Returns the inline lines that live in routes/api.ts to handle GET /api/fib.
134
+ * Inlined (not a separate Service) to keep the demo single-file readable.
135
+ */
136
+ export function demosFibonacciApiBlock() {
137
+ return `// GET /api/fib?n=36&count=4 — sequential vs worker-pool parallel via @rudderjs/concurrency.
138
+ router.get('/api/fib', async (req, res) => {
139
+ const n = Math.max(1, Math.min(42, Number((req.query as Record<string, string>)['n'] ?? 36)))
140
+ const count = Math.max(1, Math.min(16, Number((req.query as Record<string, string>)['count'] ?? 4)))
141
+
142
+ const { Concurrency } = await import('@rudderjs/concurrency')
143
+ const { cpus } = await import('node:os')
144
+
145
+ // The task body must be self-contained — closures don't capture variables across the
146
+ // worker boundary. Inline 'fib' and bind 'n' via a Function-style template.
147
+ const buildTask = (val: number): (() => number) => {
148
+ const src = \`
149
+ function fib(k) { return k < 2 ? k : fib(k - 1) + fib(k - 2) }
150
+ return fib(\${val})
151
+ \`
152
+ return new Function(src) as () => number
153
+ }
154
+
155
+ const seqStart = Date.now()
156
+ let result = 0
157
+ for (let i = 0; i < count; i++) result = buildTask(n)()
158
+ const sequentialMs = Date.now() - seqStart
159
+
160
+ const parStart = Date.now()
161
+ const tasks = Array.from({ length: count }, () => buildTask(n))
162
+ const results = await Concurrency.run(tasks)
163
+ const parallelMs = Date.now() - parStart
164
+ result = results[0] ?? result
165
+
166
+ res.json({
167
+ n, count, result, sequentialMs, parallelMs,
168
+ workers: Math.min(count, cpus().length),
169
+ })
170
+ })`;
171
+ }
172
+ //# sourceMappingURL=fibonacci.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fibonacci.js","sourceRoot":"","sources":["../../../src/templates/demos/fibonacci.ts"],"names":[],"mappings":"AAAA,iFAAiF;AAEjF,MAAM,UAAU,kBAAkB;IAChC,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+HR,CAAA;AACD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,sBAAsB;IACpC,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCN,CAAA;AACH,CAAC"}
@@ -0,0 +1,3 @@
1
+ export declare function demosHttpView(): string;
2
+ export declare function demosHttpApiBlock(): string;
3
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../src/templates/demos/http.ts"],"names":[],"mappings":"AAEA,wBAAgB,aAAa,IAAI,MAAM,CAyFtC;AAED,wBAAgB,iBAAiB,IAAI,MAAM,CAwB1C"}
@@ -0,0 +1,117 @@
1
+ // HTTP client demo — fluent fetch with retry + timeout against a public API.
2
+ export function demosHttpView() {
3
+ return `import { useState } from 'react'
4
+ import '@/index.css'
5
+
6
+ interface HttpResponseShape {
7
+ status: number
8
+ ok: boolean
9
+ durationMs: number
10
+ url: string
11
+ body: unknown
12
+ }
13
+
14
+ const ENDPOINTS = [
15
+ { url: 'https://jsonplaceholder.typicode.com/todos/1', label: 'Todo (works)' },
16
+ { url: 'https://jsonplaceholder.typicode.com/users/1', label: 'User (works)' },
17
+ { url: 'https://httpstat.us/500?sleep=300', label: 'Force 500 (retries 3×)' },
18
+ ]
19
+
20
+ export default function HttpDemo() {
21
+ const [url, setUrl] = useState(ENDPOINTS[0]!.url)
22
+ const [data, setData] = useState<HttpResponseShape | null>(null)
23
+ const [loading, setLoading] = useState(false)
24
+ const [error, setError] = useState<string | null>(null)
25
+
26
+ async function fetchUrl() {
27
+ setLoading(true); setError(null)
28
+ try {
29
+ const res = await fetch('/api/http/fetch?url=' + encodeURIComponent(url))
30
+ const body = await res.json() as HttpResponseShape | { message: string }
31
+ if (!res.ok) throw new Error((body as { message: string }).message ?? 'Failed')
32
+ setData(body as HttpResponseShape)
33
+ } catch (e) {
34
+ setError((e as Error).message)
35
+ } finally {
36
+ setLoading(false)
37
+ }
38
+ }
39
+
40
+ return (
41
+ <div className="page">
42
+ <nav className="page-nav">
43
+ <div className="brand">
44
+ <span className="brand-dot" />
45
+ RudderJS
46
+ </div>
47
+ <div className="nav-right">
48
+ <a href="/demos" className="nav-link">Demos</a>
49
+ <a href="/" className="nav-link">Home</a>
50
+ </div>
51
+ </nav>
52
+
53
+ <section className="hero">
54
+ <h1 className="hero-title">HTTP client</h1>
55
+ <p className="hero-lead">
56
+ Server-side <code className="inline-code">Http.retry(3, 200).timeout(5000).get(url)</code>{' '}
57
+ against a public API. Pick a URL — the 500 endpoint exercises the retry path.
58
+ </p>
59
+ </section>
60
+
61
+ <section className="feature-section" style={{ maxWidth: '40rem', margin: '0 auto' }}>
62
+ <div className="form-card">
63
+ <label className="form-label" htmlFor="http-url">URL</label>
64
+ <select id="http-url" className="form-input" value={url} onChange={e => setUrl(e.target.value)} style={{ marginBottom: '0.75rem' }}>
65
+ {ENDPOINTS.map(e => (
66
+ <option key={e.url} value={e.url}>{e.label}</option>
67
+ ))}
68
+ </select>
69
+ <button className="form-submit" onClick={fetchUrl} disabled={loading}>
70
+ {loading ? 'Fetching…' : 'Fetch'}
71
+ </button>
72
+ {error && (
73
+ <p className="form-error" style={{ marginTop: '1rem' }}>{error}</p>
74
+ )}
75
+ {data && (
76
+ <div style={{ marginTop: '1rem' }}>
77
+ <p className="feature-desc">
78
+ <strong>{data.status}</strong> · {data.durationMs}ms · ok={String(data.ok)}
79
+ </p>
80
+ <pre style={{ marginTop: '0.5rem', padding: '0.75rem', borderRadius: '0.375rem', background: 'var(--muted, #f4f4f5)', fontSize: '0.7rem', whiteSpace: 'pre-wrap', wordBreak: 'break-all' }}>
81
+ {JSON.stringify(data.body, null, 2).slice(0, 600)}
82
+ </pre>
83
+ </div>
84
+ )}
85
+ </div>
86
+ </section>
87
+ </div>
88
+ )
89
+ }
90
+ `;
91
+ }
92
+ export function demosHttpApiBlock() {
93
+ return `// GET /api/http/fetch?url=… — server-side HTTP with retry + timeout.
94
+ router.get('/api/http/fetch', async (req, res) => {
95
+ const url = (req.query as Record<string, string>)['url']
96
+ if (!url) return res.status(422).json({ message: 'url is required' })
97
+ if (!/^https?:\\/\\//.test(url)) return res.status(422).json({ message: 'url must be http(s)' })
98
+
99
+ const { Http } = await import('@rudderjs/http')
100
+ const t0 = Date.now()
101
+ try {
102
+ const response = await Http.retry(3, 200).timeout(5000).get(url)
103
+ let body: unknown = null
104
+ try { body = response.json() } catch { body = response.body.slice(0, 600) }
105
+ res.json({
106
+ status: response.status,
107
+ ok: response.ok(),
108
+ durationMs: Date.now() - t0,
109
+ url,
110
+ body,
111
+ })
112
+ } catch (e) {
113
+ res.status(502).json({ message: (e as Error).message ?? 'Request failed', durationMs: Date.now() - t0, url })
114
+ }
115
+ })`;
116
+ }
117
+ //# sourceMappingURL=http.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.js","sourceRoot":"","sources":["../../../src/templates/demos/http.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAE7E,MAAM,UAAU,aAAa;IAC3B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAuFR,CAAA;AACD,CAAC;AAED,MAAM,UAAU,iBAAiB;IAC/B,OAAO;;;;;;;;;;;;;;;;;;;;;;GAsBN,CAAA;AACH,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index-view.d.ts","sourceRoot":"","sources":["../../../src/templates/demos/index-view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAE7E,wBAAgB,cAAc,CAAC,GAAG,EAAE,eAAe,GAAG,MAAM,CAkE3D"}
1
+ {"version":3,"file":"index-view.d.ts","sourceRoot":"","sources":["../../../src/templates/demos/index-view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAG7E,wBAAgB,cAAc,CAAC,GAAG,EAAE,eAAe,GAAG,MAAM,CA6C3D"}
@@ -1,33 +1,14 @@
1
1
  import { shouldScaffoldDemo } from '../../templates.js';
2
+ import { DEMOS, demoHref, demoTitle } from './registry.js';
2
3
  export function demosIndexView(ctx) {
3
- const cards = [
4
- {
5
- title: 'Contact form',
6
- desc: 'CSRF-protected form with Zod validation. Demonstrates getCsrfToken() and FormRequest-style error handling.',
7
- href: '/demos/contact',
8
- pkgs: '@rudderjs/middleware · @rudderjs/core',
9
- show: shouldScaffoldDemo(ctx, 'contact'),
10
- },
11
- {
12
- title: 'WebSocket chat',
13
- desc: 'Real-time chat + presence using @rudderjs/broadcast — multi-channel pub/sub over a single WebSocket connection.',
14
- href: '/demos/ws',
15
- pkgs: '@rudderjs/broadcast',
16
- show: shouldScaffoldDemo(ctx, 'ws'),
17
- },
18
- {
19
- title: 'Collaborative editor',
20
- desc: 'Yjs CRDT live document with awareness cursors. Open in two tabs to see real-time sync over @rudderjs/sync.',
21
- href: '/demos/live',
22
- pkgs: '@rudderjs/sync',
23
- show: shouldScaffoldDemo(ctx, 'live'),
24
- },
25
- ].filter(c => c.show);
26
- const cardsJsx = cards.map(c => ` <a key="${c.href}" href="${c.href}" className="feature-card">
27
- <h3 className="feature-title">${c.title}</h3>
28
- <p className="feature-desc">${c.desc}</p>
29
- <p className="feature-desc" style={{ fontSize: '0.7rem', opacity: 0.7 }}>${c.pkgs}</p>
30
- </a>`).join('\n');
4
+ const cards = DEMOS
5
+ .filter(d => shouldScaffoldDemo(ctx, d.value))
6
+ .map(d => ` <a key="${demoHref(d)}" href="${demoHref(d)}" className="feature-card">
7
+ <h3 className="feature-title">${demoTitle(d)}</h3>
8
+ <p className="feature-desc">${escapeJsxText(d.description)}</p>
9
+ <p className="feature-desc" style={{ fontSize: '0.7rem', opacity: 0.7 }}>${d.packages.join(' · ')}</p>
10
+ </a>`)
11
+ .join('\n');
31
12
  return `import '@/index.css'
32
13
 
33
14
  // Override the id-derived URL ('/demos/index') so SPA nav matches the controller ('/demos').
@@ -56,7 +37,7 @@ export default function DemosIndex() {
56
37
 
57
38
  <section className="feature-section">
58
39
  <div className="feature-grid">
59
- ${cardsJsx}
40
+ ${cards}
60
41
  </div>
61
42
  </section>
62
43
  </div>
@@ -64,4 +45,10 @@ ${cardsJsx}
64
45
  }
65
46
  `;
66
47
  }
48
+ // Description strings live as plain text in the registry, but they're emitted
49
+ // inside JSX text nodes. Escape '<' and '>' so a raw '<name>' (e.g. in the
50
+ // localization description) doesn't get parsed as a JSX element.
51
+ function escapeJsxText(s) {
52
+ return s.replace(/</g, '&lt;').replace(/>/g, '&gt;');
53
+ }
67
54
  //# sourceMappingURL=index-view.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index-view.js","sourceRoot":"","sources":["../../../src/templates/demos/index-view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAwB,MAAM,oBAAoB,CAAA;AAE7E,MAAM,UAAU,cAAc,CAAC,GAAoB;IACjD,MAAM,KAAK,GAAiF;QAC1F;YACE,KAAK,EAAE,cAAc;YACrB,IAAI,EAAG,4GAA4G;YACnH,IAAI,EAAG,gBAAgB;YACvB,IAAI,EAAG,uCAAuC;YAC9C,IAAI,EAAG,kBAAkB,CAAC,GAAG,EAAE,SAAS,CAAC;SAC1C;QACD;YACE,KAAK,EAAE,gBAAgB;YACvB,IAAI,EAAG,iHAAiH;YACxH,IAAI,EAAG,WAAW;YAClB,IAAI,EAAG,qBAAqB;YAC5B,IAAI,EAAG,kBAAkB,CAAC,GAAG,EAAE,IAAI,CAAC;SACrC;QACD;YACE,KAAK,EAAE,sBAAsB;YAC7B,IAAI,EAAG,4GAA4G;YACnH,IAAI,EAAG,aAAa;YACpB,IAAI,EAAG,gBAAgB;YACvB,IAAI,EAAG,kBAAkB,CAAC,GAAG,EAAE,MAAM,CAAC;SACvC;KACF,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA;IAErB,MAAM,QAAQ,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,mBAAmB,CAAC,CAAC,IAAI,WAAW,CAAC,CAAC,IAAI;0CAClC,CAAC,CAAC,KAAK;wCACT,CAAC,CAAC,IAAI;qFACuC,CAAC,CAAC,IAAI;aAC9E,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAEvB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA4BP,QAAQ;;;;;;CAMT,CAAA;AACD,CAAC"}
1
+ {"version":3,"file":"index-view.js","sourceRoot":"","sources":["../../../src/templates/demos/index-view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAwB,MAAM,oBAAoB,CAAA;AAC7E,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAE1D,MAAM,UAAU,cAAc,CAAC,GAAoB;IACjD,MAAM,KAAK,GAAG,KAAK;SAChB,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,kBAAkB,CAAC,GAAG,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;SAC7C,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,mBAAmB,QAAQ,CAAC,CAAC,CAAC,WAAW,QAAQ,CAAC,CAAC,CAAC;0CACxB,SAAS,CAAC,CAAC,CAAC;wCACd,aAAa,CAAC,CAAC,CAAC,WAAW,CAAC;qFACiB,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC;aAC9F,CAAC;SACT,IAAI,CAAC,IAAI,CAAC,CAAA;IAEb,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA4BP,KAAK;;;;;;CAMN,CAAA;AACD,CAAC;AAED,8EAA8E;AAC9E,2EAA2E;AAC3E,iEAAiE;AACjE,SAAS,aAAa,CAAC,CAAS;IAC9B,OAAO,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;AACtD,CAAC"}
@@ -0,0 +1,4 @@
1
+ export declare function demosLocalizationView(): string;
2
+ export declare function demosLocalizationApiBlock(): string;
3
+ export declare function langMessages(locale: 'en' | 'es' | 'ar'): string;
4
+ //# sourceMappingURL=localization.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"localization.d.ts","sourceRoot":"","sources":["../../../src/templates/demos/localization.ts"],"names":[],"mappings":"AAIA,wBAAgB,qBAAqB,IAAI,MAAM,CAkF9C;AAED,wBAAgB,yBAAyB,IAAI,MAAM,CAkBlD;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,MAAM,CAuB/D"}
@@ -0,0 +1,130 @@
1
+ // Localization demo — language switcher hits /api/i18n?locale=…
2
+ // The route uses runWithLocale + setLocale + trans() to render strings.
3
+ // Lang files at lang/<locale>/messages.json.
4
+ export function demosLocalizationView() {
5
+ return `import { useEffect, useState } from 'react'
6
+ import '@/index.css'
7
+
8
+ interface I18nResponse {
9
+ locale: string
10
+ greeting: string
11
+ items: string
12
+ welcome: string
13
+ }
14
+
15
+ const LOCALES = [
16
+ { code: 'en', label: 'English' },
17
+ { code: 'es', label: 'Español' },
18
+ { code: 'ar', label: 'العربية' },
19
+ ]
20
+
21
+ export default function LocalizationDemo() {
22
+ const [locale, setLocale] = useState('en')
23
+ const [data, setData] = useState<I18nResponse | null>(null)
24
+
25
+ async function load(loc: string) {
26
+ const res = await fetch(\`/api/i18n?locale=\${loc}\`)
27
+ setData(await res.json() as I18nResponse)
28
+ }
29
+
30
+ useEffect(() => { load(locale) }, [locale])
31
+
32
+ return (
33
+ <div className="page">
34
+ <nav className="page-nav">
35
+ <div className="brand">
36
+ <span className="brand-dot" />
37
+ RudderJS
38
+ </div>
39
+ <div className="nav-right">
40
+ <a href="/demos" className="nav-link">Demos</a>
41
+ <a href="/" className="nav-link">Home</a>
42
+ </div>
43
+ </nav>
44
+
45
+ <section className="hero">
46
+ <h1 className="hero-title">Localization</h1>
47
+ <p className="hero-lead">
48
+ Pick a locale to fetch the same keys via{' '}
49
+ <code className="inline-code">trans()</code> on the server. Strings live in{' '}
50
+ <code className="inline-code">lang/&lt;locale&gt;/messages.json</code>.
51
+ </p>
52
+ </section>
53
+
54
+ <section className="feature-section" style={{ maxWidth: '32rem', margin: '0 auto' }}>
55
+ <div className="form-card">
56
+ <div style={{ marginBottom: '1rem' }}>
57
+ <label className="form-label" htmlFor="locale">Locale</label>
58
+ <select
59
+ id="locale"
60
+ className="form-input"
61
+ value={locale}
62
+ onChange={e => setLocale(e.target.value)}
63
+ >
64
+ {LOCALES.map(l => (
65
+ <option key={l.code} value={l.code}>{l.label} ({l.code})</option>
66
+ ))}
67
+ </select>
68
+ </div>
69
+
70
+ {data && (
71
+ <div style={{ display: 'grid', gap: '0.5rem' }}>
72
+ <p className="feature-desc"><strong>welcome:</strong> {data.welcome}</p>
73
+ <p className="feature-desc"><strong>greeting:</strong> {data.greeting}</p>
74
+ <p className="feature-desc"><strong>items (3):</strong> {data.items}</p>
75
+ <p className="feature-desc" style={{ fontSize: '0.7rem', opacity: 0.7 }}>
76
+ resolved server-side with locale = <code>{data.locale}</code>
77
+ </p>
78
+ </div>
79
+ )}
80
+ </div>
81
+ </section>
82
+ </div>
83
+ )
84
+ }
85
+ `;
86
+ }
87
+ export function demosLocalizationApiBlock() {
88
+ return `// GET /api/i18n?locale=… — resolves the same keys in the requested locale.
89
+ router.get('/api/i18n', async (req, res) => {
90
+ const { runWithLocale, setLocale, getLocale, trans } = await import('@rudderjs/localization')
91
+ const requested = (req.query as Record<string, string>)['locale'] ?? 'en'
92
+
93
+ const payload = await runWithLocale(requested, async () => {
94
+ setLocale(requested)
95
+ return {
96
+ locale: getLocale(),
97
+ welcome: await trans('messages.welcome'),
98
+ greeting: await trans('messages.greeting', { name: 'World' }),
99
+ items: await trans('messages.items', 3),
100
+ }
101
+ })
102
+
103
+ res.json(payload)
104
+ })`;
105
+ }
106
+ export function langMessages(locale) {
107
+ if (locale === 'es') {
108
+ return `{
109
+ "welcome": "¡Bienvenido a RudderJS!",
110
+ "greeting": "Hola, :name!",
111
+ "items": "{0} sin elementos|{1} un elemento|{n} :count elementos"
112
+ }
113
+ `;
114
+ }
115
+ if (locale === 'ar') {
116
+ return `{
117
+ "welcome": "مرحباً بك في RudderJS!",
118
+ "greeting": "مرحباً، :name!",
119
+ "items": "{0} لا توجد عناصر|{1} عنصر واحد|{n} :count عناصر"
120
+ }
121
+ `;
122
+ }
123
+ return `{
124
+ "welcome": "Welcome to RudderJS!",
125
+ "greeting": "Hello, :name!",
126
+ "items": "{0} no items|{1} one item|{n} :count items"
127
+ }
128
+ `;
129
+ }
130
+ //# sourceMappingURL=localization.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"localization.js","sourceRoot":"","sources":["../../../src/templates/demos/localization.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAChE,wEAAwE;AACxE,6CAA6C;AAE7C,MAAM,UAAU,qBAAqB;IACnC,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgFR,CAAA;AACD,CAAC;AAED,MAAM,UAAU,yBAAyB;IACvC,OAAO;;;;;;;;;;;;;;;;GAgBN,CAAA;AACH,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,MAA0B;IACrD,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QACpB,OAAO;;;;;CAKV,CAAA;IACC,CAAC;IACD,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QACpB,OAAO;;;;;CAKV,CAAA;IACC,CAAC;IACD,OAAO;;;;;CAKR,CAAA;AACD,CAAC"}
@@ -0,0 +1,4 @@
1
+ export declare function demosMailView(): string;
2
+ export declare function demoMailable(): string;
3
+ export declare function demosMailApiBlock(): string;
4
+ //# sourceMappingURL=mail.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mail.d.ts","sourceRoot":"","sources":["../../../src/templates/demos/mail.ts"],"names":[],"mappings":"AAGA,wBAAgB,aAAa,IAAI,MAAM,CAqFtC;AAED,wBAAgB,YAAY,IAAI,MAAM,CAkBrC;AAED,wBAAgB,iBAAiB,IAAI,MAAM,CAkB1C"}
@@ -0,0 +1,127 @@
1
+ // Mail send demo — defines a Mailable and posts to /api/mail/send.
2
+ // Default driver is `log`, so the email lands in the dev terminal.
3
+ export function demosMailView() {
4
+ return `import { useState } from 'react'
5
+ import '@/index.css'
6
+
7
+ interface MailResponse {
8
+ ok: boolean
9
+ to: string
10
+ subject: string
11
+ driver: string
12
+ }
13
+
14
+ export default function MailDemo() {
15
+ const [to, setTo] = useState('user@example.com')
16
+ const [subject, setSubject] = useState('Hello from RudderJS')
17
+ const [data, setData] = useState<MailResponse | null>(null)
18
+ const [loading, setLoading] = useState(false)
19
+ const [error, setError] = useState<string | null>(null)
20
+
21
+ async function send() {
22
+ setLoading(true); setError(null)
23
+ try {
24
+ const res = await fetch('/api/mail/send', {
25
+ method: 'POST',
26
+ headers: { 'Content-Type': 'application/json' },
27
+ body: JSON.stringify({ to, subject }),
28
+ })
29
+ const body = await res.json() as MailResponse | { message: string }
30
+ if (!res.ok) throw new Error((body as { message: string }).message ?? 'Send failed')
31
+ setData(body as MailResponse)
32
+ } catch (e) {
33
+ setError((e as Error).message)
34
+ } finally {
35
+ setLoading(false)
36
+ }
37
+ }
38
+
39
+ return (
40
+ <div className="page">
41
+ <nav className="page-nav">
42
+ <div className="brand">
43
+ <span className="brand-dot" />
44
+ RudderJS
45
+ </div>
46
+ <div className="nav-right">
47
+ <a href="/demos" className="nav-link">Demos</a>
48
+ <a href="/" className="nav-link">Home</a>
49
+ </div>
50
+ </nav>
51
+
52
+ <section className="hero">
53
+ <h1 className="hero-title">Mail send</h1>
54
+ <p className="hero-lead">
55
+ Sends a <code className="inline-code">DemoMail</code> via{' '}
56
+ <code className="inline-code">@rudderjs/mail</code>. Default driver is{' '}
57
+ <code className="inline-code">log</code> — check the dev server terminal for output.
58
+ </p>
59
+ </section>
60
+
61
+ <section className="feature-section" style={{ maxWidth: '32rem', margin: '0 auto' }}>
62
+ <div className="form-card">
63
+ <div style={{ marginBottom: '1rem' }}>
64
+ <label className="form-label" htmlFor="mail-to">To</label>
65
+ <input id="mail-to" className="form-input" value={to} onChange={e => setTo(e.target.value)} />
66
+ </div>
67
+ <div style={{ marginBottom: '1rem' }}>
68
+ <label className="form-label" htmlFor="mail-subject">Subject</label>
69
+ <input id="mail-subject" className="form-input" value={subject} onChange={e => setSubject(e.target.value)} />
70
+ </div>
71
+ <button className="form-submit" onClick={send} disabled={loading}>
72
+ {loading ? 'Sending…' : 'Send mail'}
73
+ </button>
74
+ {error && (
75
+ <p className="form-error" style={{ marginTop: '1rem' }}>{error}</p>
76
+ )}
77
+ {data && (
78
+ <p className="form-success" style={{ marginTop: '1rem' }}>
79
+ Sent to <code>{data.to}</code> via <code>{data.driver}</code> driver — check the terminal.
80
+ </p>
81
+ )}
82
+ </div>
83
+ </section>
84
+ </div>
85
+ )
86
+ }
87
+ `;
88
+ }
89
+ export function demoMailable() {
90
+ return `import { Mailable } from '@rudderjs/mail'
91
+
92
+ /**
93
+ * Demo mailable — built dynamically from the to/subject the user enters.
94
+ * Replace with whatever copy your real templates need (HTML + text).
95
+ */
96
+ export class DemoMail extends Mailable {
97
+ constructor(private readonly heading: string) { super() }
98
+
99
+ build(): this {
100
+ return this
101
+ .subject(this.heading)
102
+ .html(\`<h1>\${this.heading}</h1><p>Sent from the RudderJS mail demo.</p>\`)
103
+ .text(\`\${this.heading}\\n\\nSent from the RudderJS mail demo.\`)
104
+ }
105
+ }
106
+ `;
107
+ }
108
+ export function demosMailApiBlock() {
109
+ return `// POST /api/mail/send — sends a DemoMail to the user-supplied address.
110
+ router.post('/api/mail/send', async (req, res) => {
111
+ const body = (req.body ?? {}) as { to?: string; subject?: string }
112
+ if (!body.to || !body.subject) {
113
+ return res.status(422).json({ message: 'Body must be { to, subject }' })
114
+ }
115
+ const { Mail } = await import('@rudderjs/mail')
116
+ const { DemoMail } = await import('../app/Mail/DemoMail.ts')
117
+ const { config } = await import('@rudderjs/core')
118
+ await Mail.to(body.to).send(new DemoMail(body.subject))
119
+ res.json({
120
+ ok: true,
121
+ to: body.to,
122
+ subject: body.subject,
123
+ driver: config<string>('mail.default', 'log'),
124
+ })
125
+ })`;
126
+ }
127
+ //# sourceMappingURL=mail.js.map