@20baht/cli 0.3.0 → 0.3.1
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 +5 -3
- package/package.json +1 -1
- package/templates/angular/apps/server/package.json +2 -0
- package/templates/angular/apps/server/src/app.ts +10 -0
- package/templates/angular/apps/server/src/forms/contact.form.ts +34 -0
- package/templates/angular/apps/server/test/app.test.ts +29 -0
- package/templates/standalone/README.md.hbs +8 -0
- package/templates/standalone/apps/web/src/app/app.routes.ts.hbs +6 -0
- package/templates/standalone/apps/web/src/app/core/navigation/navigation.ts +4 -1
- package/templates/standalone/apps/web/src/app/modules/contact/contact.component.ts.hbs +87 -0
- package/templates/standalone/apps/web/src/app/services/contact.service.ts.hbs +19 -0
package/README.md
CHANGED
|
@@ -78,9 +78,11 @@ Add a matching entry to `knip.ts` so dead-code checks cover it.
|
|
|
78
78
|
Same flags as `new`. The `new` toolchain, plus an app on the family's main stack:
|
|
79
79
|
|
|
80
80
|
```
|
|
81
|
-
apps/web Angular (:4200): @20baht/angular app shell and navigator,
|
|
82
|
-
GET /api/hello, a
|
|
83
|
-
|
|
81
|
+
apps/web Angular (:4200): @20baht/angular app shell and navigator, a page calling
|
|
82
|
+
GET /api/hello, a Contact page (a <baht-form> from the server's schema), a
|
|
83
|
+
not-found page
|
|
84
|
+
apps/server Elysia under /api (:3001); exports `type App`, emitted to dist/types for the web app.
|
|
85
|
+
One form as data (src/forms/contact.form.ts): served, and validated on submit
|
|
84
86
|
packages/kit typed ngx-eden client + error text, compiled into the app as @<name>/kit
|
|
85
87
|
bahtui.json { "frontend": "angular", "apps": { "web": "apps/web", ... } }
|
|
86
88
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@20baht/cli",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Scaffold apps on the @20baht libraries: Angular + Elysia (20baht create), optionally as Native Federation microfrontends, a bare bun workspace (20baht new), plus bahtui generators.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { cors } from '@elysiajs/cors'
|
|
2
|
+
import { validateSubmission } from '@20baht/validator'
|
|
2
3
|
import { Elysia } from 'elysia'
|
|
4
|
+
import { contactForm } from './forms/contact.form'
|
|
3
5
|
|
|
4
6
|
/** In dev the host and the remotes run on their own localhost ports; deployed, all share one origin. */
|
|
5
7
|
const DEV_ORIGIN = /^http:\/\/localhost:\d+$/
|
|
@@ -12,3 +14,11 @@ export const buildApp = () =>
|
|
|
12
14
|
new Elysia({ prefix: '/api' })
|
|
13
15
|
.use(cors({ origin: DEV_ORIGIN, credentials: true }))
|
|
14
16
|
.get('/hello', () => ({ message: 'Hello from the server' }))
|
|
17
|
+
// A form: the schema the browser renders, and the same schema checking what comes back.
|
|
18
|
+
.get('/contact/form', () => contactForm)
|
|
19
|
+
.post('/contact', ({ body, status }) => {
|
|
20
|
+
const result = validateSubmission(contactForm, body)
|
|
21
|
+
if (!result.valid) return status(422, { message: 'Check the form', issues: result.issues })
|
|
22
|
+
// `result.data` is what to keep: defaults filled, anything the rules hide cleared.
|
|
23
|
+
return { received: result.data }
|
|
24
|
+
})
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { FormDefinition } from '@20baht/engine'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One form, as JSON-shaped data: the web app renders it with `<baht-form>` and `POST /api/contact`
|
|
5
|
+
* validates a submission against it with the same rules. Add a field here and both sides have it.
|
|
6
|
+
*/
|
|
7
|
+
export const contactForm: FormDefinition = {
|
|
8
|
+
form_id: 'contact',
|
|
9
|
+
form_name: 'Contact',
|
|
10
|
+
fields: [
|
|
11
|
+
{
|
|
12
|
+
order: 1,
|
|
13
|
+
type: 'text',
|
|
14
|
+
key: 'name',
|
|
15
|
+
label: 'Name',
|
|
16
|
+
validators: [{ type: 'required' }],
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
order: 2,
|
|
20
|
+
type: 'email',
|
|
21
|
+
key: 'email',
|
|
22
|
+
label: 'Email',
|
|
23
|
+
validators: [{ type: 'required' }],
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
order: 3,
|
|
27
|
+
type: 'textarea',
|
|
28
|
+
key: 'message',
|
|
29
|
+
label: 'Message',
|
|
30
|
+
max_length: 500,
|
|
31
|
+
validators: [{ type: 'required' }],
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
}
|
|
@@ -2,6 +2,15 @@ import { describe, expect, test } from 'bun:test'
|
|
|
2
2
|
|
|
3
3
|
import { buildApp } from '../src/app'
|
|
4
4
|
|
|
5
|
+
const post = (body: unknown) =>
|
|
6
|
+
buildApp().handle(
|
|
7
|
+
new Request('http://localhost/api/contact', {
|
|
8
|
+
method: 'POST',
|
|
9
|
+
headers: { 'content-type': 'application/json' },
|
|
10
|
+
body: JSON.stringify(body),
|
|
11
|
+
}),
|
|
12
|
+
)
|
|
13
|
+
|
|
5
14
|
describe('api', () => {
|
|
6
15
|
test('says hello', async () => {
|
|
7
16
|
const response = await buildApp().handle(new Request('http://localhost/api/hello'))
|
|
@@ -9,4 +18,24 @@ describe('api', () => {
|
|
|
9
18
|
expect(response.status).toBe(200)
|
|
10
19
|
expect(await response.json()).toEqual({ message: 'Hello from the server' })
|
|
11
20
|
})
|
|
21
|
+
|
|
22
|
+
test('serves the contact form the browser renders', async () => {
|
|
23
|
+
const response = await buildApp().handle(new Request('http://localhost/api/contact/form'))
|
|
24
|
+
|
|
25
|
+
expect(response.status).toBe(200)
|
|
26
|
+
expect(await response.json()).toMatchObject({ form_name: 'Contact' })
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('keeps a valid submission and refuses an invalid one with the field at fault', async () => {
|
|
30
|
+
const good = await post({ name: 'Ada', email: 'ada@example.com', message: 'Hello' })
|
|
31
|
+
expect(good.status).toBe(200)
|
|
32
|
+
expect(await good.json()).toEqual({
|
|
33
|
+
received: { name: 'Ada', email: 'ada@example.com', message: 'Hello' },
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const bad = await post({ name: 'Ada', email: 'not-an-email' })
|
|
37
|
+
expect(bad.status).toBe(422)
|
|
38
|
+
const refused = (await bad.json()) as { issues: { ref: string }[] }
|
|
39
|
+
expect(refused.issues.map((issue) => issue.ref).sort()).toEqual(['email', 'message'])
|
|
40
|
+
})
|
|
12
41
|
})
|
|
@@ -17,6 +17,14 @@ bun run dev # every apps/* dev script in parallel
|
|
|
17
17
|
bun run check # lint, format, typecheck, madge, boundaries, knip, dup, build, test
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
+
## The form
|
|
21
|
+
|
|
22
|
+
`apps/server/src/forms/contact.form.ts` is one form as data. The server serves it
|
|
23
|
+
(`GET /api/contact/form`) and validates a submission against it (`POST /api/contact`,
|
|
24
|
+
`validateSubmission`); `apps/web/src/app/modules/contact` renders it with `<baht-form>`. That is all
|
|
25
|
+
a form needs: no workflow, no approval step. Add a field to the schema and both sides have it. For
|
|
26
|
+
the next form, copy the three pieces.
|
|
27
|
+
|
|
20
28
|
## Add a page
|
|
21
29
|
|
|
22
30
|
A component under `apps/web/src/app/modules/<page>/`, a route in `app.routes.ts`, an entry in
|
|
@@ -8,6 +8,12 @@ export const routes: Routes = [
|
|
|
8
8
|
title: '{{name}}',
|
|
9
9
|
loadComponent: () => import('./modules/home/home.component').then((m) => m.HomeComponent),
|
|
10
10
|
},
|
|
11
|
+
{
|
|
12
|
+
path: 'contact',
|
|
13
|
+
title: 'Contact · {{name}}',
|
|
14
|
+
loadComponent: () =>
|
|
15
|
+
import('./modules/contact/contact.component').then((m) => m.ContactComponent),
|
|
16
|
+
},
|
|
11
17
|
{
|
|
12
18
|
path: '**',
|
|
13
19
|
title: 'Not found · {{name}}',
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import type { ShellNavItem } from '@20baht/ui'
|
|
2
2
|
|
|
3
3
|
/** The navigator: one entry per page. `key` is the page's path, without the leading slash. */
|
|
4
|
-
export const NAV: ShellNavItem[] = [
|
|
4
|
+
export const NAV: ShellNavItem[] = [
|
|
5
|
+
{ key: '', label: 'Home', href: '/' },
|
|
6
|
+
{ key: 'contact', label: 'Contact', href: '/contact' },
|
|
7
|
+
]
|
|
5
8
|
|
|
6
9
|
/** The key of the entry `url` sits under, the longest one first; `''` (Home) when none does. */
|
|
7
10
|
export const currentKey = (url: string, keys: string[]): string => {
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { ChangeDetectionStrategy, Component, computed, inject, signal } from '@angular/core'
|
|
2
|
+
import { describeError } from '@{{name}}/kit'
|
|
3
|
+
import { BAHT_UI } from '@20baht/angular'
|
|
4
|
+
import { ContactService } from '../../services/contact.service'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A form from a schema: `<baht-form>` renders and checks it in the browser, the server checks
|
|
8
|
+
* the same schema again. This is the whole of "a form" in this app; nothing else is needed.
|
|
9
|
+
*/
|
|
10
|
+
@Component({
|
|
11
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
12
|
+
imports: [BAHT_UI],
|
|
13
|
+
selector: 'web-contact',
|
|
14
|
+
styles: `
|
|
15
|
+
.actions {
|
|
16
|
+
display: flex;
|
|
17
|
+
gap: var(--baht-space-3);
|
|
18
|
+
margin-top: var(--baht-space-4);
|
|
19
|
+
}
|
|
20
|
+
`,
|
|
21
|
+
template: `
|
|
22
|
+
<div class="app-page">
|
|
23
|
+
<baht-page-header
|
|
24
|
+
heading="Contact"
|
|
25
|
+
description="One schema: rendered here, validated again on the server."
|
|
26
|
+
></baht-page-header>
|
|
27
|
+
<baht-card heading="Send a message">
|
|
28
|
+
@if (schema.error(); as error) {
|
|
29
|
+
<baht-error-state
|
|
30
|
+
heading="The form could not be loaded"
|
|
31
|
+
[message]="describeError(error)"
|
|
32
|
+
retry-label="Try again"
|
|
33
|
+
(bahtRetry)="schema.reload()"
|
|
34
|
+
></baht-error-state>
|
|
35
|
+
} @else if (schema.value(); as form) {
|
|
36
|
+
<baht-form
|
|
37
|
+
#contact="bahtForm"
|
|
38
|
+
[form]="form"
|
|
39
|
+
[busy]="sending()"
|
|
40
|
+
submit-on-enter
|
|
41
|
+
(bahtSubmit)="send($event.data)"
|
|
42
|
+
></baht-form>
|
|
43
|
+
<div class="actions">
|
|
44
|
+
<baht-button [loading]="sending()" (click)="contact.element.submit()">Send</baht-button>
|
|
45
|
+
</div>
|
|
46
|
+
} @else {
|
|
47
|
+
<baht-skeleton variant="text"></baht-skeleton>
|
|
48
|
+
}
|
|
49
|
+
</baht-card>
|
|
50
|
+
@if (failed(); as message) {
|
|
51
|
+
<baht-alert tone="danger" [message]="message"></baht-alert>
|
|
52
|
+
}
|
|
53
|
+
@if (received(); as text) {
|
|
54
|
+
<baht-card heading="What the server kept">
|
|
55
|
+
<pre [textContent]="text"></pre>
|
|
56
|
+
</baht-card>
|
|
57
|
+
}
|
|
58
|
+
</div>
|
|
59
|
+
`,
|
|
60
|
+
})
|
|
61
|
+
export class ContactComponent {
|
|
62
|
+
private readonly contact = inject(ContactService)
|
|
63
|
+
protected readonly schema = this.contact.form()
|
|
64
|
+
protected readonly describeError = describeError
|
|
65
|
+
protected readonly sending = signal(false)
|
|
66
|
+
protected readonly failed = signal<string | null>(null)
|
|
67
|
+
private readonly kept = signal<Record<string, unknown> | null>(null)
|
|
68
|
+
protected readonly received = computed(() => {
|
|
69
|
+
const kept = this.kept()
|
|
70
|
+
return kept === null ? null : JSON.stringify(kept, null, 2)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
protected send(data: Record<string, unknown>): void {
|
|
74
|
+
this.sending.set(true)
|
|
75
|
+
this.failed.set(null)
|
|
76
|
+
this.contact.send(data).subscribe({
|
|
77
|
+
next: (answer) => {
|
|
78
|
+
this.kept.set(answer.received)
|
|
79
|
+
this.sending.set(false)
|
|
80
|
+
},
|
|
81
|
+
error: (error: unknown) => {
|
|
82
|
+
this.failed.set(describeError(error))
|
|
83
|
+
this.sending.set(false)
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Injectable, inject } from '@angular/core'
|
|
2
|
+
import { Api } from '@{{name}}/kit'
|
|
3
|
+
import { edenResource } from '@20baht/ngx-eden'
|
|
4
|
+
|
|
5
|
+
/** The contact form: its schema from the server, and a submission back to it. */
|
|
6
|
+
@Injectable({ providedIn: 'root' })
|
|
7
|
+
export class ContactService {
|
|
8
|
+
private readonly api = inject(Api).client.api
|
|
9
|
+
|
|
10
|
+
/** `GET /api/contact/form` as a resource; create it in an injection context (a field initializer). */
|
|
11
|
+
form() {
|
|
12
|
+
return edenResource(() => this.api.contact.form.get.$request())
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** `POST /api/contact`: the server validates with the schema the form was rendered from. */
|
|
16
|
+
send(data: Record<string, unknown>) {
|
|
17
|
+
return this.api.contact.post(data)
|
|
18
|
+
}
|
|
19
|
+
}
|