weifuwu 0.23.4 → 0.24.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 +234 -63
- package/cli.ts +3 -0
- package/dist/ai/provider.d.ts +10 -1
- package/dist/cli.js +3 -0
- package/dist/csrf.d.ts +11 -5
- package/dist/env.d.ts +33 -0
- package/dist/flash.d.ts +5 -0
- package/dist/i18n.d.ts +27 -2
- package/dist/iii/types.d.ts +2 -0
- package/dist/index.d.ts +8 -10
- package/dist/index.js +321 -268
- package/dist/rate-limit.d.ts +2 -1
- package/dist/request-id.d.ts +16 -7
- package/dist/router.d.ts +3 -0
- package/dist/theme.d.ts +25 -2
- package/dist/trace.d.ts +44 -0
- package/dist/types.d.ts +0 -17
- package/dist/upload.d.ts +5 -0
- package/dist/user/client.d.ts +11 -3
- package/dist/user/types.d.ts +19 -4
- package/dist/validate.d.ts +5 -0
- package/package.json +1 -1
package/dist/rate-limit.d.ts
CHANGED
package/dist/request-id.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import type { Context, Middleware } from './types.ts';
|
|
2
|
+
declare module './types.ts' {
|
|
3
|
+
interface Context {
|
|
4
|
+
requestId: string;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
2
7
|
/** Options for {@link requestId}. */
|
|
3
8
|
export interface RequestIdOptions {
|
|
4
9
|
/** Header name for request ID (default: `'X-Request-ID'`). */
|
|
@@ -9,18 +14,22 @@ export interface RequestIdOptions {
|
|
|
9
14
|
/**
|
|
10
15
|
* Request ID middleware.
|
|
11
16
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* injects `ctx.requestId`.
|
|
17
|
+
* @deprecated Use `trace()` from 'weifuwu' instead — it injects `ctx.trace.requestId`
|
|
18
|
+
* along with `traceId` and `elapsed()` in a single middleware.
|
|
15
19
|
*
|
|
16
20
|
* ```ts
|
|
17
|
-
*
|
|
21
|
+
* // Old:
|
|
18
22
|
* app.use(requestId())
|
|
23
|
+
* ctx.requestId
|
|
19
24
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
25
|
+
* // New:
|
|
26
|
+
* app.use(trace())
|
|
27
|
+
* ctx.trace.requestId
|
|
23
28
|
* ```
|
|
29
|
+
*
|
|
30
|
+
* Reads an incoming `X-Request-ID` header (or custom header name) from the
|
|
31
|
+
* request. If absent, generates a new UUID. Sets the response header and
|
|
32
|
+
* injects `ctx.requestId`.
|
|
24
33
|
*/
|
|
25
34
|
export declare function requestId(options?: RequestIdOptions): Middleware<Context, Context & {
|
|
26
35
|
requestId: string;
|
package/dist/router.d.ts
CHANGED
|
@@ -25,6 +25,9 @@ export declare class Router<T extends Context = Context> {
|
|
|
25
25
|
use<Out extends Context>(mw: Middleware<Context, Out>): Router<T & Out>;
|
|
26
26
|
use(path: string, mw: Middleware<T, T>): Router<T>;
|
|
27
27
|
use(path: string, router: Router<any>): Router<T>;
|
|
28
|
+
use(mod: Router & {
|
|
29
|
+
middleware: () => Middleware;
|
|
30
|
+
}): Router<T>;
|
|
28
31
|
get(path: string, ...args: [...Middleware<T, T>[], Handler<T> | Router<any>]): Router<T>;
|
|
29
32
|
post(path: string, ...args: [...Middleware<T, T>[], Handler<T> | Router<any>]): Router<T>;
|
|
30
33
|
put(path: string, ...args: [...Middleware<T, T>[], Handler<T> | Router<any>]): Router<T>;
|
package/dist/theme.d.ts
CHANGED
|
@@ -1,8 +1,31 @@
|
|
|
1
|
-
import type { Middleware } from './types.ts';
|
|
1
|
+
import type { Context, Middleware } from './types.ts';
|
|
2
|
+
import { Router } from './router.ts';
|
|
3
|
+
declare module './types.ts' {
|
|
4
|
+
interface Context {
|
|
5
|
+
theme: ThemeInjected;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export interface ThemeInjected {
|
|
9
|
+
value: string;
|
|
10
|
+
set: (value: string, loc?: string) => Response;
|
|
11
|
+
}
|
|
2
12
|
export interface ThemeOptions {
|
|
3
13
|
/** Default theme value (default: 'system'). */
|
|
4
14
|
default?: string;
|
|
5
15
|
/** Cookie name (default: 'theme'). Set to empty string to disable cookie. */
|
|
6
16
|
cookie?: string;
|
|
7
17
|
}
|
|
8
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Theme module. Returns a Router with an attached `.middleware()` method.
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* const t = theme()
|
|
23
|
+
* app.use(t.middleware()) // → ctx.theme = { value, set }
|
|
24
|
+
* app.use('/', t) // → GET /__theme/dark (switch route)
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export interface ThemeModule extends Router {
|
|
28
|
+
/** Middleware that injects `ctx.theme = { value, set }`. */
|
|
29
|
+
middleware: () => Middleware<Context, Context & ThemeInjected>;
|
|
30
|
+
}
|
|
31
|
+
export declare function theme(options?: ThemeOptions): ThemeModule;
|
package/dist/trace.d.ts
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
import type { Context, Middleware } from './types.ts';
|
|
2
|
+
declare module './types.ts' {
|
|
3
|
+
interface Context {
|
|
4
|
+
trace: TraceInjected;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export interface TraceInjected {
|
|
8
|
+
/** Unique request identifier (from X-Request-ID header or auto-generated). */
|
|
9
|
+
requestId: string;
|
|
10
|
+
/** Unique trace identifier for the request. */
|
|
11
|
+
traceId: string;
|
|
12
|
+
/** Milliseconds elapsed since the trace started. */
|
|
13
|
+
elapsed: () => number;
|
|
14
|
+
/** Timestamp (ms) when the trace started. */
|
|
15
|
+
startTime: number;
|
|
16
|
+
}
|
|
1
17
|
export interface TraceContext {
|
|
2
18
|
/** Unique identifier for the current request trace. */
|
|
3
19
|
traceId: string;
|
|
@@ -49,3 +65,31 @@ export declare function runWithTrace<T>(incomingTraceId: string | null, fn: () =
|
|
|
49
65
|
* ```
|
|
50
66
|
*/
|
|
51
67
|
export declare function traceElapsed(): number;
|
|
68
|
+
/** Options for {@link trace}. */
|
|
69
|
+
export interface TraceOptions {
|
|
70
|
+
/** Header name for request ID (default: `'X-Request-ID'`). */
|
|
71
|
+
header?: string;
|
|
72
|
+
/** Custom ID generator (default: `crypto.randomUUID`). */
|
|
73
|
+
generator?: () => string;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Request tracing middleware.
|
|
77
|
+
*
|
|
78
|
+
* Injects `ctx.trace = { requestId, traceId, elapsed, startTime }`.
|
|
79
|
+
* Reads/writes `X-Request-ID` header. Combines the functionality of `requestId()`
|
|
80
|
+
* with the per-request tracing from `AsyncLocalStorage`.
|
|
81
|
+
*
|
|
82
|
+
* ```ts
|
|
83
|
+
* import { trace } from 'weifuwu'
|
|
84
|
+
* app.use(trace())
|
|
85
|
+
*
|
|
86
|
+
* app.get('/', (req, ctx) => {
|
|
87
|
+
* console.log(ctx.trace.requestId) // 550e8400-e29b-...
|
|
88
|
+
* console.log(ctx.trace.traceId) // same as currentTraceId()
|
|
89
|
+
* console.log(ctx.trace.elapsed()) // ms since request start
|
|
90
|
+
* })
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare function trace(options?: TraceOptions): Middleware<Context, Context & {
|
|
94
|
+
trace: TraceInjected;
|
|
95
|
+
}>;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,24 +1,7 @@
|
|
|
1
1
|
export interface Context {
|
|
2
2
|
params: Record<string, string>;
|
|
3
3
|
query: Record<string, string>;
|
|
4
|
-
user?: unknown;
|
|
5
|
-
parsed?: Record<string, unknown>;
|
|
6
4
|
mountPath?: string;
|
|
7
|
-
theme?: {
|
|
8
|
-
value: string;
|
|
9
|
-
set?: (value: string, loc?: string) => Response;
|
|
10
|
-
};
|
|
11
|
-
flash?: {
|
|
12
|
-
value: unknown;
|
|
13
|
-
set: (data: unknown, location?: string) => Response;
|
|
14
|
-
};
|
|
15
|
-
i18n?: {
|
|
16
|
-
locale: string;
|
|
17
|
-
messages?: Record<string, unknown>;
|
|
18
|
-
t: (key: string, params?: Record<string, string>, fallback?: string) => string;
|
|
19
|
-
set?: (value: string, loc?: string) => Response;
|
|
20
|
-
};
|
|
21
|
-
env?: Record<string, string>;
|
|
22
5
|
layoutStack?: {
|
|
23
6
|
path: string;
|
|
24
7
|
component: any;
|
package/dist/upload.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import type { Middleware } from './types.ts';
|
|
2
|
+
declare module './types.ts' {
|
|
3
|
+
interface Context {
|
|
4
|
+
parsed: Record<string, unknown>;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
2
7
|
/** A parsed file from a multipart upload. */
|
|
3
8
|
export interface UploadedFile {
|
|
4
9
|
/** Original filename from the client. */
|
package/dist/user/client.d.ts
CHANGED
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
import type { UserOptions, UserModule } from './types.ts';
|
|
2
2
|
/**
|
|
3
3
|
* User authentication module — local register/login, JWT verification, OAuth2 server, social login.
|
|
4
|
+
* Supports DB-less auth via tokens/verify/proxy options.
|
|
4
5
|
*
|
|
5
6
|
* ```ts
|
|
7
|
+
* // Full auth with DB
|
|
6
8
|
* import { user, postgres } from 'weifuwu'
|
|
7
|
-
*
|
|
8
9
|
* const pg = postgres({ connection: DATABASE_URL })
|
|
9
10
|
* const auth = user({ pg, jwtSecret: process.env.JWT_SECRET })
|
|
10
11
|
*
|
|
11
12
|
* await auth.migrate()
|
|
13
|
+
* app.use(auth.middleware()) // inject ctx.user
|
|
14
|
+
* app.use('/', auth) // /register, /login
|
|
15
|
+
*
|
|
16
|
+
* // DB-less token auth
|
|
17
|
+
* const auth = user({ tokens: ['sk-123', 'sk-456'] })
|
|
18
|
+
* app.use(auth.middleware()) // injects ctx.user for valid tokens
|
|
12
19
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
20
|
+
* // DB-less custom verify
|
|
21
|
+
* const auth = user({ verify: async (token) => validateToken(token) })
|
|
22
|
+
* app.use(auth.middleware())
|
|
15
23
|
* ```
|
|
16
24
|
*/
|
|
17
25
|
export declare function user(options: UserOptions): UserModule;
|
package/dist/user/types.d.ts
CHANGED
|
@@ -58,14 +58,29 @@ export interface OAuthProviderConfig {
|
|
|
58
58
|
}
|
|
59
59
|
/** Options for {@link user}. */
|
|
60
60
|
export interface UserOptions {
|
|
61
|
-
/** PostgreSQL client for user storage. */
|
|
62
|
-
pg
|
|
63
|
-
/** Secret key for JWT signing. */
|
|
64
|
-
jwtSecret
|
|
61
|
+
/** PostgreSQL client for user storage. Omit for DB-less token/verify/proxy auth. */
|
|
62
|
+
pg?: PostgresClient;
|
|
63
|
+
/** Secret key for JWT signing. Required for JWT auth and login/register routes. */
|
|
64
|
+
jwtSecret?: string;
|
|
65
65
|
/** Custom table name for users (default: `'users'`). */
|
|
66
66
|
table?: string;
|
|
67
67
|
/** JWT expiration time (default: `'7d'`). */
|
|
68
68
|
expiresIn?: string | number;
|
|
69
|
+
/** Static token(s) for simple bearer auth. No DB or JWT needed. */
|
|
70
|
+
tokens?: string[];
|
|
71
|
+
/** Custom verify function. Receives the token and request, returns user data or null. */
|
|
72
|
+
verify?: (token: string, req: Request) => unknown | Promise<unknown>;
|
|
73
|
+
/** Proxy auth — forward request to an external auth service for validation. */
|
|
74
|
+
proxy?: string | URL;
|
|
75
|
+
/** Custom header name for token extraction (default: `'Authorization'`). */
|
|
76
|
+
header?: string;
|
|
77
|
+
/**
|
|
78
|
+
* Function to load user data from a user ID stored in the session.
|
|
79
|
+
* Called when `ctx.session.userId` is present. Only used when `pg` is not provided.
|
|
80
|
+
* Return a falsy value to reject (e.g. if the user was deleted).
|
|
81
|
+
* If not provided and no pg, `ctx.user` is set to `{ id: userId }`.
|
|
82
|
+
*/
|
|
83
|
+
resolveUser?: (userId: unknown) => unknown | Promise<unknown>;
|
|
69
84
|
/** Enable OAuth2 server mode (authorization code flow). */
|
|
70
85
|
oauth2?: OAuth2ServerOptions;
|
|
71
86
|
/**
|
package/dist/validate.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { ZodSchema } from 'zod';
|
|
2
2
|
import type { Middleware } from './types.ts';
|
|
3
|
+
declare module './types.ts' {
|
|
4
|
+
interface Context {
|
|
5
|
+
parsed: Record<string, unknown>;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
3
8
|
export interface ValidationSchemas {
|
|
4
9
|
body?: ZodSchema;
|
|
5
10
|
query?: ZodSchema;
|