@chidchanun/bcp 0.1.16 → 0.1.18

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/docs/README.md ADDED
@@ -0,0 +1,325 @@
1
+ # BCP Framework Documentation Source
2
+
3
+ This directory is the documentation source of truth for the BCP Framework documentation website.
4
+
5
+ The recommended documentation website project name is `bcp-docs`.
6
+
7
+ > Documentation target: BCP Framework `0.1.18`
8
+
9
+ ## Purpose
10
+
11
+ The `bcp-docs` website should turn the Markdown files in this directory into a structured developer documentation experience without duplicating framework behavior in a separate source of truth.
12
+
13
+ When framework behavior changes:
14
+
15
+ 1. update the framework source and tests,
16
+ 2. update the relevant file in `docs/`,
17
+ 3. update the release note in `docs/releases/`,
18
+ 4. then publish or synchronize the content to `bcp-docs`.
19
+
20
+ The website should not invent API behavior that is not documented or tested in the framework repository.
21
+
22
+ ## Recommended website navigation
23
+
24
+ ### Getting Started
25
+
26
+ | Website route | Source file | Description |
27
+ | --- | --- | --- |
28
+ | `/docs/getting-started` | `getting-started.md` | Create and run a BCP application |
29
+ | `/docs/configuration` | `configuration.md` | Framework configuration |
30
+ | `/docs/application-modules` | `application-modules.md` | Client/server module boundaries |
31
+ | `/docs/deployment` | `deployment.md` | Production and deployment guidance |
32
+ | `/docs/updating` | `updating.md` | Upgrade an existing BCP project |
33
+
34
+ ### Routing and Data
35
+
36
+ | Website route | Source file | Description |
37
+ | --- | --- | --- |
38
+ | `/docs/routing` | `routing.md` | File-based routing |
39
+ | `/docs/server-data-loaders` | `server-data-loaders.md` | Server-side page data |
40
+ | `/docs/route-guards` | `route-guards.md` | Scoped page authorization |
41
+ | `/docs/form-actions` | `form-actions.md` | Server mutations and forms |
42
+ | `/docs/server-request-apis` | `server-request-apis.md` | Request, cookie, redirect and response APIs |
43
+
44
+ ### Authentication
45
+
46
+ | Website route | Source file | Description |
47
+ | --- | --- | --- |
48
+ | `/docs/authentication` | `authentication.md` | Authentication Core and `bcp/auth` |
49
+ | `/docs/auth-route-guards` | `auth-route-guards.md` | `requireAuth`, `requireRole` and guard integration |
50
+ | `/docs/session-auth` | `session-auth.md` | Low-level JWT cookie sessions |
51
+
52
+ ### Database
53
+
54
+ | Website route | Source file | Description |
55
+ | --- | --- | --- |
56
+ | `/docs/database` | `database.md` | `bcp/database`, MySQL and transactions |
57
+ | `/docs/database-migrations` | `database-migrations.md` | Migration CLI and migration files |
58
+
59
+ ### Runtime and Infrastructure
60
+
61
+ | Website route | Source file | Description |
62
+ | --- | --- | --- |
63
+ | `/docs/middleware` | `middleware.md` | Middleware System v2 and v1 compatibility |
64
+ | `/docs/caching` | `caching.md` | Cache and revalidation |
65
+ | `/docs/security` | `security.md` | Framework security defaults |
66
+ | `/docs/development-logging` | `development-logging.md` | Development request logging |
67
+
68
+ ### Releases
69
+
70
+ Use the files under `docs/releases/` for release pages.
71
+
72
+ Recommended route format:
73
+
74
+ ```text
75
+ /releases/0.1.16
76
+ /releases/0.1.17
77
+ /releases/0.1.18
78
+ ```
79
+
80
+ The newest release should also be available from `/releases`.
81
+
82
+ ## Homepage content for bcp-docs
83
+
84
+ The documentation homepage should describe BCP as a React full-stack framework and highlight the currently implemented capabilities.
85
+
86
+ Recommended feature groups:
87
+
88
+ ### Application
89
+
90
+ - File-based routing
91
+ - React SSR
92
+ - SPA navigation
93
+ - Persistent layouts
94
+ - Loading and error UI
95
+ - API routes
96
+ - Client islands / partial hydration
97
+
98
+ ### Server data and mutations
99
+
100
+ - Server data loaders
101
+ - Route guards
102
+ - Form actions
103
+ - Request-scoped server APIs
104
+ - Cookies and redirects
105
+
106
+ ### Database
107
+
108
+ - `bcp/database`
109
+ - Lazy MySQL connection pool
110
+ - Queries and prepared execution
111
+ - Transactions
112
+ - Database migrations
113
+ - Migration status and rollback
114
+
115
+ ### Authentication
116
+
117
+ - `bcp/auth`
118
+ - JWT cookie sessions
119
+ - Typed users and session data
120
+ - Session rotation
121
+ - `requireAuth()`
122
+ - `requireRole()`
123
+ - Auth-aware route guards
124
+
125
+ ### Middleware
126
+
127
+ - Middleware System v2
128
+ - Middleware chains
129
+ - Real `await next()` downstream execution
130
+ - Before/after response interception
131
+ - Shared request context state
132
+ - Matchers
133
+ - Rewrite and redirect
134
+ - Backward-compatible Middleware v1 handlers
135
+
136
+ ### Production and DX
137
+
138
+ - `create-bcp-app`
139
+ - Interactive project setup
140
+ - Tailwind preset
141
+ - Database/auth presets
142
+ - `bcp update`
143
+ - Standalone production builds
144
+ - Critical CSS
145
+ - Cache/revalidation
146
+ - Security defaults
147
+ - Unit/integration/E2E/package smoke testing in the framework repository
148
+
149
+ ## Suggested homepage hero
150
+
151
+ Suggested content, not a required exact copy:
152
+
153
+ ```text
154
+ BCP Framework
155
+ A React full-stack framework for building server-rendered applications with routing,
156
+ data loading, server mutations, authentication, databases, middleware and production tooling.
157
+ ```
158
+
159
+ Primary actions:
160
+
161
+ ```text
162
+ Get Started
163
+ Read the Docs
164
+ View on GitHub
165
+ ```
166
+
167
+ The website may display the current release version from project metadata instead of hard-coding it in multiple UI files.
168
+
169
+ ## Suggested documentation sidebar
170
+
171
+ ```text
172
+ Getting Started
173
+ Introduction
174
+ Installation
175
+ Project Structure
176
+ Configuration
177
+ Updating
178
+
179
+ Core
180
+ Routing
181
+ Application Modules
182
+ Server Request APIs
183
+ Server Data Loaders
184
+ Route Guards
185
+ Form Actions
186
+
187
+ Authentication
188
+ Authentication Core
189
+ Auth Route Guards
190
+ JWT Sessions
191
+
192
+ Database
193
+ Database
194
+ Migrations
195
+
196
+ Runtime
197
+ Middleware
198
+ Caching
199
+ Security
200
+
201
+ Production
202
+ Deployment
203
+ Development Logging
204
+
205
+ Releases
206
+ 0.1.18
207
+ 0.1.17
208
+ 0.1.16
209
+ ```
210
+
211
+ ## Suggested documentation page layout
212
+
213
+ Each documentation page should support:
214
+
215
+ - title
216
+ - short summary
217
+ - previous/next page navigation
218
+ - table of contents generated from headings
219
+ - syntax-highlighted code blocks
220
+ - copy-code button
221
+ - deep links to headings
222
+ - responsive sidebar
223
+ - mobile navigation
224
+ - light/dark appearance
225
+ - framework version label
226
+ - link to the corresponding source Markdown file in GitHub
227
+
228
+ Optional later additions:
229
+
230
+ - documentation search
231
+ - version selector
232
+ - API symbol index
233
+ - interactive examples
234
+ - edit-on-GitHub links
235
+
236
+ ## Code examples
237
+
238
+ Examples should use the public package entrypoints that application developers actually install.
239
+
240
+ Preferred:
241
+
242
+ ```ts
243
+ import {
244
+ auth,
245
+ requireAuth,
246
+ } from "bcp/auth";
247
+
248
+ import {
249
+ db,
250
+ } from "bcp/database";
251
+
252
+ import {
253
+ type MiddlewarePipelineHandler,
254
+ } from "bcp/middleware";
255
+ ```
256
+
257
+ Avoid documenting internal monorepo imports such as:
258
+
259
+ ```text
260
+ packages/server/src/...
261
+ packages/client/src/...
262
+ ```
263
+
264
+ unless a page is explicitly explaining framework internals.
265
+
266
+ ## Version labels
267
+
268
+ The website should distinguish:
269
+
270
+ - current stable npm version
271
+ - documentation target version
272
+ - unreleased/main documentation if it is shown
273
+
274
+ Do not label unreleased `main` behavior as already available from npm until the corresponding package has been published.
275
+
276
+ ## Framework commands to document
277
+
278
+ ```bash
279
+ bcp dev
280
+ bcp routes
281
+ bcp build
282
+ bcp start
283
+ bcp update
284
+ bcp version
285
+
286
+ bcp db create create_users
287
+ bcp db migrate
288
+ bcp db status
289
+ bcp db rollback
290
+ ```
291
+
292
+ Project creation:
293
+
294
+ ```bash
295
+ npx create-bcp-app@latest my-app
296
+ ```
297
+
298
+ ## Middleware v2 documentation priority
299
+
300
+ BCP 0.1.18 introduces a meaningful middleware architecture change, so the `bcp-docs` website should make the distinction between v1 and v2 clear.
301
+
302
+ The middleware page should prominently explain:
303
+
304
+ ```text
305
+ v1 next() = return a pass-through instruction
306
+ v2 await next() = execute downstream middleware/application and receive Response
307
+ ```
308
+
309
+ Existing v1 code remains supported, so migration documentation should describe v2 as an additive upgrade rather than a mandatory rewrite.
310
+
311
+ See `middleware.md` for the full API and examples.
312
+
313
+ ## Release workflow for documentation
314
+
315
+ Before publishing a new BCP release:
316
+
317
+ ```bash
318
+ npm run typecheck
319
+ npm run test:unit
320
+ npm run rc:check
321
+ ```
322
+
323
+ After npm publication, the docs website can update its displayed stable version.
324
+
325
+ The framework repository remains authoritative for API correctness; `bcp-docs` is the presentation layer for that content.
@@ -0,0 +1,216 @@
1
+ # Auth Route Guards
2
+
3
+ BCP Framework `0.1.17` integrates the Authentication Core with route guards through the server-only `bcp/auth` entrypoint.
4
+
5
+ ## Protect a route tree
6
+
7
+ Create `guard.ts` in the route directory you want to protect:
8
+
9
+ ```ts
10
+ import {
11
+ createAuthGuard,
12
+ } from "bcp/auth";
13
+
14
+ export const guard =
15
+ createAuthGuard({
16
+ redirectTo:
17
+ "/login",
18
+ });
19
+ ```
20
+
21
+ The guard redirects unauthenticated requests to `/login` with status `303` by default. A `303` is suitable for guards that may run before form actions because it converts a redirected mutation request into a normal GET request to the login page.
22
+
23
+ To return `401 Unauthorized` instead of redirecting:
24
+
25
+ ```ts
26
+ import {
27
+ createAuthGuard,
28
+ } from "bcp/auth";
29
+
30
+ export const guard =
31
+ createAuthGuard({
32
+ redirectTo:
33
+ null,
34
+ });
35
+ ```
36
+
37
+ ## Require a role
38
+
39
+ ```ts
40
+ import {
41
+ createRoleGuard,
42
+ } from "bcp/auth";
43
+
44
+ interface User {
45
+ id: number;
46
+ email: string;
47
+ role: string;
48
+ }
49
+
50
+ export const guard =
51
+ createRoleGuard<User>(
52
+ "admin",
53
+ {
54
+ redirectTo:
55
+ "/login",
56
+ }
57
+ );
58
+ ```
59
+
60
+ An authenticated user without the required role receives `403 Forbidden` by default.
61
+
62
+ You can redirect forbidden users instead:
63
+
64
+ ```ts
65
+ export const guard =
66
+ createRoleGuard<User>(
67
+ "admin",
68
+ {
69
+ forbiddenRedirectTo:
70
+ "/forbidden",
71
+ }
72
+ );
73
+ ```
74
+
75
+ ## Require one of several roles
76
+
77
+ The default role matching mode is `any`:
78
+
79
+ ```ts
80
+ export const guard =
81
+ createRoleGuard<User>([
82
+ "admin",
83
+ "manager",
84
+ ]);
85
+ ```
86
+
87
+ A user with either role is allowed.
88
+
89
+ ## Require all permissions
90
+
91
+ `requireRole()` and `createRoleGuard()` can read another user field and require every value:
92
+
93
+ ```ts
94
+ interface User {
95
+ id: number;
96
+ permissions: string[];
97
+ }
98
+
99
+ export const guard =
100
+ createRoleGuard<User>(
101
+ [
102
+ "users.read",
103
+ "users.write",
104
+ ],
105
+ {
106
+ roleField:
107
+ "permissions",
108
+ match:
109
+ "all",
110
+ }
111
+ );
112
+ ```
113
+
114
+ ## Inline guard logic
115
+
116
+ Use `requireAuth()` when a guard needs additional application-specific checks:
117
+
118
+ ```ts
119
+ import {
120
+ requireAuth,
121
+ } from "bcp/auth";
122
+
123
+ export async function guard() {
124
+ const result =
125
+ await requireAuth<AppUser>();
126
+
127
+ if (
128
+ result instanceof
129
+ Response
130
+ ) {
131
+ return result;
132
+ }
133
+
134
+ if (
135
+ !result.auth.user.active
136
+ ) {
137
+ return new Response(
138
+ "Forbidden",
139
+ {
140
+ status:
141
+ 403,
142
+ }
143
+ );
144
+ }
145
+
146
+ return result;
147
+ }
148
+ ```
149
+
150
+ ## Auth data in child guards and loaders
151
+
152
+ Successful auth helpers return guard data in this shape:
153
+
154
+ ```ts
155
+ {
156
+ auth: {
157
+ sid,
158
+ user,
159
+ data,
160
+ iat,
161
+ exp,
162
+ iss,
163
+ aud,
164
+ },
165
+ }
166
+ ```
167
+
168
+ Because BCP merges parent guard data into child guards and page loaders, descendants can read the authenticated session without verifying the cookie again.
169
+
170
+ Use `getGuardAuth()` for typed access:
171
+
172
+ ```ts
173
+ import {
174
+ getGuardAuth,
175
+ } from "bcp/auth";
176
+
177
+ export async function loader({
178
+ guardData,
179
+ }) {
180
+ const session =
181
+ getGuardAuth<AppUser>(
182
+ guardData
183
+ );
184
+
185
+ if (!session) {
186
+ throw new Error(
187
+ "Protected loader did not receive auth guard data."
188
+ );
189
+ }
190
+
191
+ return {
192
+ userId:
193
+ session.user.id,
194
+ };
195
+ }
196
+ ```
197
+
198
+ ## Low-level role checks
199
+
200
+ `requireRole()` is also available directly:
201
+
202
+ ```ts
203
+ import {
204
+ requireRole,
205
+ } from "bcp/auth";
206
+
207
+ export function guard() {
208
+ return requireRole<AppUser>(
209
+ "admin"
210
+ );
211
+ }
212
+ ```
213
+
214
+ ## Security boundary
215
+
216
+ `bcp/auth` is server-only. BCP blocks it from page/client graphs and maps its browser export to the server-only runtime guard. Authentication and authorization checks should stay in guards, loaders, actions, API routes, or other server modules.