weifuwu 0.23.4 → 0.24.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.
Files changed (46) hide show
  1. package/README.md +431 -152
  2. package/cli.ts +3 -0
  3. package/dist/agent/types.d.ts +2 -1
  4. package/dist/ai/provider.d.ts +10 -1
  5. package/dist/analytics.d.ts +2 -2
  6. package/dist/cache.d.ts +4 -4
  7. package/dist/cli.js +3 -0
  8. package/dist/cors.d.ts +2 -2
  9. package/dist/csrf.d.ts +11 -5
  10. package/dist/deploy/types.d.ts +2 -2
  11. package/dist/env.d.ts +33 -0
  12. package/dist/flash.d.ts +5 -0
  13. package/dist/helmet.d.ts +2 -2
  14. package/dist/hub.d.ts +2 -1
  15. package/dist/i18n.d.ts +27 -2
  16. package/dist/iii/register-worker.d.ts +1 -1
  17. package/dist/iii/types.d.ts +5 -3
  18. package/dist/index.d.ts +8 -10
  19. package/dist/index.js +434 -359
  20. package/dist/kb/types.d.ts +8 -0
  21. package/dist/logdb/types.d.ts +2 -1
  22. package/dist/mailer.d.ts +2 -1
  23. package/dist/messager/types.d.ts +2 -1
  24. package/dist/opencode/types.d.ts +2 -1
  25. package/dist/permissions.d.ts +2 -2
  26. package/dist/postgres/module.d.ts +2 -1
  27. package/dist/postgres/types.d.ts +2 -2
  28. package/dist/queue/types.d.ts +2 -2
  29. package/dist/rate-limit.d.ts +3 -2
  30. package/dist/react.js +6 -6
  31. package/dist/redis/types.d.ts +2 -2
  32. package/dist/request-id.d.ts +16 -7
  33. package/dist/router.d.ts +3 -0
  34. package/dist/seo.d.ts +2 -2
  35. package/dist/serve.d.ts +1 -1
  36. package/dist/session.d.ts +9 -5
  37. package/dist/tailwind.d.ts +9 -0
  38. package/dist/tenant/types.d.ts +3 -3
  39. package/dist/theme.d.ts +25 -2
  40. package/dist/trace.d.ts +44 -0
  41. package/dist/types.d.ts +8 -17
  42. package/dist/upload.d.ts +9 -2
  43. package/dist/user/client.d.ts +11 -3
  44. package/dist/user/types.d.ts +21 -6
  45. package/dist/validate.d.ts +5 -0
  46. package/package.json +9 -9
package/cli.ts CHANGED
@@ -92,6 +92,9 @@ async function cmdInit(name: string, opts: { minimal?: boolean; skipInstall?: bo
92
92
  const deps: Record<string, string> = { weifuwu: `^${v}` }
93
93
  const devDeps: Record<string, string> = {}
94
94
  if (!opts.minimal) {
95
+ deps['react'] = '^19'
96
+ deps['react-dom'] = '^19'
97
+ deps['tailwindcss'] = '^4'
95
98
  devDeps['@types/react'] = depVer('@types/react')
96
99
  devDeps['@types/react-dom'] = depVer('@types/react-dom')
97
100
  }
@@ -1,6 +1,7 @@
1
1
  import type { Router } from '../router.ts';
2
2
  import type { LanguageModel, EmbeddingModel, Tool } from 'ai';
3
3
  import type { AIProvider } from '../ai/provider.ts';
4
+ import type { Closeable } from '../types.ts';
4
5
  export interface AgentConfig {
5
6
  id: number;
6
7
  tenant_id: string | null;
@@ -46,7 +47,7 @@ export interface AgentOptions {
46
47
  embeddingDimension?: number;
47
48
  tools?: Record<string, Tool>;
48
49
  }
49
- export interface AgentModule extends Router {
50
+ export interface AgentModule extends Router, Closeable {
50
51
  migrate: () => Promise<void>;
51
52
  run: (agentId: number, params: RunParams) => Promise<RunResult>;
52
53
  addKnowledge: (agentId: number, title: string, content: string) => Promise<KnowledgeDoc>;
@@ -1,4 +1,13 @@
1
+ import type { Context, Middleware } from '../types.ts';
1
2
  import { generateText as aiGenerateText, streamText as aiStreamText, type LanguageModel, type EmbeddingModel } from 'ai';
3
+ declare module '../types.ts' {
4
+ interface Context {
5
+ ai: AIProvider;
6
+ }
7
+ }
8
+ export interface AIProviderInjected {
9
+ ai: AIProvider;
10
+ }
2
11
  export interface AIProviderOptions {
3
12
  /** API base URL (default: OPENAI_BASE_URL env or http://localhost:11434/v1). */
4
13
  baseURL?: string;
@@ -33,4 +42,4 @@ export interface AIProvider {
33
42
  */
34
43
  streamText(params: Omit<Parameters<typeof aiStreamText>[0], 'model'>): ReturnType<typeof aiStreamText>;
35
44
  }
36
- export declare function aiProvider(options?: AIProviderOptions): AIProvider;
45
+ export declare function aiProvider(options?: AIProviderOptions): Middleware<Context, Context & AIProviderInjected> & AIProvider;
@@ -1,4 +1,4 @@
1
- import type { Middleware } from './types.ts';
1
+ import type { Middleware, Closeable } from './types.ts';
2
2
  import { Router } from './router.ts';
3
3
  /** Options for {@link analytics}. */
4
4
  export interface AnalyticsOptions {
@@ -11,7 +11,7 @@ export interface AnalyticsOptions {
11
11
  };
12
12
  }
13
13
  /** Analytics module returned by {@link analytics}. */
14
- export interface AnalyticsModule extends Router {
14
+ export interface AnalyticsModule extends Router, Closeable {
15
15
  /** Middleware that records page views. */
16
16
  middleware: () => Middleware;
17
17
  /** Create/update the analytics database tables. */
package/dist/cache.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Context, Middleware } from './types.ts';
1
+ import type { Context, Middleware, Closeable } from './types.ts';
2
2
  import type { Redis } from './vendor.ts';
3
3
  export interface CachedResponse {
4
4
  status: number;
@@ -33,13 +33,13 @@ export interface CacheOptions {
33
33
  /** Maximum number of bytes per cached body. Default: 1MB. Larger bodies are skipped. */
34
34
  maxBodySize?: number;
35
35
  }
36
- export interface CacheMiddleware extends Middleware {
36
+ export interface CacheMiddleware extends Middleware, Closeable {
37
37
  /** Invalidate all entries with a given tag. */
38
38
  invalidate(tag: string): Promise<void>;
39
39
  /** Flush all cached entries. */
40
40
  flush(): Promise<void>;
41
41
  /** Cleanup. */
42
- close(): void;
42
+ close(): Promise<void>;
43
43
  /** Store reference (for testing). */
44
44
  store: CacheStore;
45
45
  }
@@ -54,7 +54,7 @@ export declare class MemoryCache implements CacheStore {
54
54
  invalidate(tag: string): Promise<void>;
55
55
  flush(): Promise<void>;
56
56
  private cleanup;
57
- close(): void;
57
+ close(): Promise<void>;
58
58
  /** Testing only. */
59
59
  get size(): number;
60
60
  }
package/dist/cli.js CHANGED
@@ -74,6 +74,9 @@ async function cmdInit(name, opts) {
74
74
  const deps = { weifuwu: `^${v}` };
75
75
  const devDeps = {};
76
76
  if (!opts.minimal) {
77
+ deps["react"] = "^19";
78
+ deps["react-dom"] = "^19";
79
+ deps["tailwindcss"] = "^4";
77
80
  devDeps["@types/react"] = depVer("@types/react");
78
81
  devDeps["@types/react-dom"] = depVer("@types/react-dom");
79
82
  }
package/dist/cors.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Middleware } from './types.ts';
1
+ import type { Middleware, Context } from './types.ts';
2
2
  /** Options for {@link cors}. */
3
3
  export interface CORSOptions {
4
4
  /** Allowed origin(s). Default `'*'`. If `credentials: true`, reflects the request origin. */
@@ -22,4 +22,4 @@ export interface CORSOptions {
22
22
  * app.use(cors({ origin: 'https://myapp.com', credentials: true }))
23
23
  * ```
24
24
  */
25
- export declare function cors(options?: CORSOptions): Middleware;
25
+ export declare function cors(options?: CORSOptions): Middleware<Context, Context>;
package/dist/csrf.d.ts CHANGED
@@ -1,4 +1,12 @@
1
1
  import type { Context, Middleware } from './types.ts';
2
+ declare module './types.ts' {
3
+ interface Context {
4
+ csrf: CsrfInjected;
5
+ }
6
+ }
7
+ export interface CsrfInjected {
8
+ token: string;
9
+ }
2
10
  /** Options for {@link csrf}. */
3
11
  export interface CsrfOptions {
4
12
  /** Cookie name for CSRF token (default: `'_csrf'`). */
@@ -17,7 +25,7 @@ export interface CsrfOptions {
17
25
  * in a cookie. On other methods, validates the token from header or body
18
26
  * against the cookie.
19
27
  *
20
- * Injects `ctx.csrfToken` for use in forms.
28
+ * Injects `ctx.csrf.token` for use in forms.
21
29
  *
22
30
  * ```ts
23
31
  * import { csrf } from 'weifuwu'
@@ -27,13 +35,11 @@ export interface CsrfOptions {
27
35
  * app.get('/form', (req, ctx) => {
28
36
  * return new Response(`
29
37
  * <form method="POST">
30
- * <input type="hidden" name="_csrf" value="${ctx.csrfToken}" />
38
+ * <input type="hidden" name="_csrf" value="${ctx.csrf.token}" />
31
39
  * <input type="submit" />
32
40
  * </form>
33
41
  * `, { headers: { 'content-type': 'text/html' } })
34
42
  * })
35
43
  * ```
36
44
  */
37
- export declare function csrf(options?: CsrfOptions): Middleware<Context, Context & {
38
- csrfToken: string;
39
- }>;
45
+ export declare function csrf(options?: CsrfOptions): Middleware<Context, Context & CsrfInjected>;
@@ -1,6 +1,6 @@
1
1
  import type { IncomingMessage } from 'node:http';
2
2
  import type { Duplex } from 'node:stream';
3
- import type { Handler } from '../types.ts';
3
+ import type { Handler, Closeable } from '../types.ts';
4
4
  export interface DeployConfig {
5
5
  domain?: string;
6
6
  port?: number;
@@ -27,7 +27,7 @@ export interface AppStatus {
27
27
  uptime?: number;
28
28
  error?: string;
29
29
  }
30
- export interface DeployServer {
30
+ export interface DeployServer extends Closeable {
31
31
  close(): Promise<void>;
32
32
  ready: Promise<void>;
33
33
  url: string;
package/dist/env.d.ts CHANGED
@@ -1,3 +1,19 @@
1
+ import type { Context, Middleware } from './types.ts';
2
+ /**
3
+ * Get all public environment variables (those prefixed with `WEIFUWU_PUBLIC_`),
4
+ * with the prefix stripped.
5
+ *
6
+ * ```ts
7
+ * const pub = getPublicEnv()
8
+ * // WEIFUWU_PUBLIC_API_URL=http://api.example.com → { API_URL: 'http://api.example.com' }
9
+ * ```
10
+ */
11
+ export declare function getPublicEnv(): Record<string, string>;
12
+ declare module './types.ts' {
13
+ interface Context {
14
+ env?: Record<string, string>;
15
+ }
16
+ }
1
17
  /**
2
18
  * Whether this code is running from the compiled `dist/index.js` bundle.
3
19
  * `false` when running TypeScript source directly (dev workflow in weifuwu repo).
@@ -34,3 +50,20 @@ export declare function isProd(): boolean;
34
50
  * ```
35
51
  */
36
52
  export declare function loadEnv(path?: string): void;
53
+ /**
54
+ * Public env middleware.
55
+ *
56
+ * Injects `ctx.env` with all environment variables prefixed with `WEIFUWU_PUBLIC_`,
57
+ * with the prefix stripped. Safe to expose to the client.
58
+ *
59
+ * ```ts
60
+ * import { env } from 'weifuwu'
61
+ * app.use(env())
62
+ *
63
+ * // .env: WEIFUWU_PUBLIC_API_URL=https://api.example.com
64
+ * // ctx: ctx.env.API_URL === 'https://api.example.com'
65
+ * ```
66
+ */
67
+ export declare function env(): Middleware<Context, Context & {
68
+ env: Record<string, string>;
69
+ }>;
package/dist/flash.d.ts CHANGED
@@ -24,6 +24,11 @@
24
24
  * ```
25
25
  */
26
26
  import type { Context, Middleware } from './types.ts';
27
+ declare module './types.ts' {
28
+ interface Context {
29
+ flash: FlashInjected;
30
+ }
31
+ }
27
32
  /** Options for {@link flash}. */
28
33
  export interface FlashOptions {
29
34
  /**
package/dist/helmet.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Middleware } from './types.ts';
1
+ import type { Middleware, Context } from './types.ts';
2
2
  /** Options for {@link helmet}. Set any header to `false` to omit it. */
3
3
  export interface HelmetOptions {
4
4
  /** `Content-Security-Policy` header value. */
@@ -30,4 +30,4 @@ export interface HelmetOptions {
30
30
  /** `Permissions-Policy` header value. */
31
31
  permissionsPolicy?: string | false;
32
32
  }
33
- export declare function helmet(options?: HelmetOptions): Middleware;
33
+ export declare function helmet(options?: HelmetOptions): Middleware<Context, Context>;
package/dist/hub.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { Redis, WebSocket } from './vendor.ts';
2
+ import type { Closeable } from './types.ts';
2
3
  /** Options for {@link createHub}. */
3
4
  export interface HubOptions {
4
5
  /** Optional Redis client for cross-process pub/sub broadcast. */
@@ -10,7 +11,7 @@ export interface HubOptions {
10
11
  * In-memory (and optionally Redis-backed) pub/sub hub for WebSocket rooms.
11
12
  *
12
13
  * Used internally by the WebSocket handler to implement `ctx.ws.join()` / `ctx.ws.sendRoom()`. */
13
- export interface Hub {
14
+ export interface Hub extends Closeable {
14
15
  /** Subscribe a WebSocket to a room/group. */
15
16
  join(key: string, ws: WebSocket): void;
16
17
  /** Unsubscribe a WebSocket from all rooms. */
package/dist/i18n.d.ts CHANGED
@@ -1,4 +1,16 @@
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
+ i18n: I18nInjected;
6
+ }
7
+ }
8
+ export interface I18nInjected {
9
+ locale: string;
10
+ messages?: Record<string, unknown>;
11
+ t: (key: string, params?: Record<string, string>, fallback?: string) => string;
12
+ set?: (value: string, loc?: string) => Response;
13
+ }
2
14
  export interface I18nOptions {
3
15
  /** Default locale (default: 'en'). */
4
16
  default?: string;
@@ -11,4 +23,17 @@ export interface I18nOptions {
11
23
  /** Whether to detect locale from Accept-Language header (default: true). */
12
24
  fromAcceptLanguage?: boolean;
13
25
  }
14
- export declare function i18n(options?: I18nOptions): Middleware;
26
+ /**
27
+ * i18n module. Returns a Router with an attached `.middleware()` method.
28
+ *
29
+ * ```ts
30
+ * const l = i18n({ dir: './locales' })
31
+ * app.use(l.middleware()) // → ctx.i18n = { locale, t, set }
32
+ * app.use('/', l) // → GET /__lang/:locale (switch route)
33
+ * ```
34
+ */
35
+ export interface I18nModule extends Router {
36
+ /** Middleware that injects `ctx.i18n = { locale, t, set }`. */
37
+ middleware: () => Middleware<Context, Context & I18nInjected>;
38
+ }
39
+ export declare function i18n(options?: I18nOptions): I18nModule;
@@ -6,5 +6,5 @@ export declare function registerWorker(url: string): {
6
6
  unregisterTrigger(functionId: string): void;
7
7
  trigger(request: TriggerRequest): Promise<unknown>;
8
8
  onStream(handler: (data: any) => void): void;
9
- shutdown(): void;
9
+ close(): void;
10
10
  };
@@ -1,4 +1,5 @@
1
1
  import type { Router } from '../router.ts';
2
+ import type { Closeable } from '../types.ts';
2
3
  import type { Redis } from '../vendor.ts';
3
4
  import type { PostgresClient } from '../postgres/types.ts';
4
5
  export type FunctionHandler = (payload: unknown, ctx: FunctionContext) => unknown | Promise<unknown>;
@@ -30,7 +31,7 @@ export interface TriggerOptions {
30
31
  action?: 'sync' | 'void';
31
32
  timeout_ms?: number;
32
33
  }
33
- export interface IIIModule extends Router {
34
+ export interface IIIModule extends Router, Closeable {
34
35
  wsHandler: () => any;
35
36
  addWorker: (worker: Worker) => void;
36
37
  trigger: (request: TriggerRequest) => Promise<unknown>;
@@ -39,7 +40,7 @@ export interface IIIModule extends Router {
39
40
  listFunctions: () => FunctionInfo[];
40
41
  listTriggers: () => TriggerInfo[];
41
42
  migrate: () => Promise<void>;
42
- shutdown: () => Promise<void>;
43
+ close: () => Promise<void>;
43
44
  }
44
45
  export interface WorkerInfo {
45
46
  id: string;
@@ -85,7 +86,8 @@ export interface RemoteWorker {
85
86
  registerTrigger: (input: TriggerInput) => void;
86
87
  unregisterTrigger: (functionId: string) => void;
87
88
  trigger: (request: TriggerRequest) => Promise<unknown>;
88
- shutdown: () => void;
89
+ /** Cleanup worker resources. */
90
+ close: () => void;
89
91
  }
90
92
  export interface FunctionRegistration {
91
93
  id: string;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export type { Context, Handler, Middleware, ErrorHandler } from './types.ts';
2
- export { currentTraceId, currentTrace, runWithTrace, traceElapsed } from './trace.ts';
3
- export type { TraceContext } from './trace.ts';
4
- export { loadEnv, isDev, isProd, isBundled } from './env.ts';
2
+ export { currentTraceId, currentTrace, runWithTrace, traceElapsed, trace } from './trace.ts';
3
+ export type { TraceContext, TraceInjected, TraceOptions } from './trace.ts';
4
+ export { loadEnv, isDev, isProd, isBundled, getPublicEnv, env } from './env.ts';
5
5
  export { serve, createTestServer, DEFAULT_MAX_BODY } from './serve.ts';
6
6
  export type { ServeOptions, Server } from './serve.ts';
7
7
  export { Router } from './router.ts';
@@ -11,8 +11,6 @@ export { logger } from './logger.ts';
11
11
  export type { LoggerOptions } from './logger.ts';
12
12
  export { cors } from './cors.ts';
13
13
  export type { CORSOptions } from './cors.ts';
14
- export { auth } from './auth.ts';
15
- export type { AuthOptions } from './auth.ts';
16
14
  export { serveStatic } from './static.ts';
17
15
  export type { ServeStaticOptions } from './static.ts';
18
16
  export { validate } from './validate.ts';
@@ -39,7 +37,7 @@ export { aiStream } from './ai.ts';
39
37
  export type { AIHandler } from './ai.ts';
40
38
  export { runWorkflow } from './ai/workflow.ts';
41
39
  export { aiProvider } from './ai/provider.ts';
42
- export type { AIProvider, AIProviderOptions } from './ai/provider.ts';
40
+ export type { AIProvider, AIProviderOptions, AIProviderInjected } from './ai/provider.ts';
43
41
  export { streamText, generateText, generateObject, streamObject, tool, embed, embedMany, smoothStream, openai, createOpenAI, } from './ai-sdk.ts';
44
42
  export { postgres, MIGRATIONS_TABLE } from './postgres/index.ts';
45
43
  export type { PostgresOptions, PostgresClient, PostgresInjected } from './postgres/types.ts';
@@ -67,17 +65,17 @@ export type { HealthOptions } from './health.ts';
67
65
  export { analytics } from './analytics.ts';
68
66
  export type { AnalyticsOptions, AnalyticsModule } from './analytics.ts';
69
67
  export { theme } from './theme.ts';
70
- export type { ThemeOptions } from './theme.ts';
68
+ export type { ThemeOptions, ThemeInjected } from './theme.ts';
71
69
  export { i18n } from './i18n.ts';
72
- export type { I18nOptions } from './i18n.ts';
70
+ export type { I18nOptions, I18nInjected } from './i18n.ts';
73
71
  export { flash } from './flash.ts';
74
- export type { FlashOptions } from './flash.ts';
72
+ export type { FlashOptions, FlashInjected } from './flash.ts';
75
73
  export { seo, seoMiddleware, seoTags } from './seo.ts';
76
74
  export type { SeoOptions, RobotsRule, SitemapUrl, SitemapConfig, SeoHeadersConfig, SeoTagsConfig, } from './seo.ts';
77
75
  export { mailer } from './mailer.ts';
78
76
  export type { MailerOptions, MailOptions, Mailer } from './mailer.ts';
79
77
  export { csrf } from './csrf.ts';
80
- export type { CsrfOptions } from './csrf.ts';
78
+ export type { CsrfOptions, CsrfInjected } from './csrf.ts';
81
79
  export { logdb } from './logdb/index.ts';
82
80
  export type { LogdbOptions, LogdbModule, LogEntry, LogEntryInput, } from './logdb/types.ts';
83
81
  export { iii, createWorker, registerWorker } from './iii/index.ts';