weifuwu 0.32.1 → 0.33.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.
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Base — dynamic data storage engine for weifuwu.
3
+ *
4
+ * Each "base" is a collection of user-defined tables backed by fixed physical slots
5
+ * (text001..064, number001..032, etc.) mapped through a column_map table.
6
+ *
7
+ * Requires `postgres()` and `user()` middleware registered first.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { serve, Router, postgres, user, base } from 'weifuwu'
12
+ *
13
+ * const app = new Router()
14
+ * app.use(postgres())
15
+ * app.use(user())
16
+ * app.use(base())
17
+ *
18
+ * // Create a base with tables
19
+ * app.post('/api/bases', async (req, ctx) => {
20
+ * const b = await ctx.base.create(await req.json())
21
+ * return Response.json(b, { status: 201 })
22
+ * })
23
+ *
24
+ * // Insert data
25
+ * app.post('/api/bases/:id/:table', async (req, ctx) => {
26
+ * const row = await ctx.base.insert(ctx.params.id, ctx.params.table, await req.json())
27
+ * return Response.json(row, { status: 201 })
28
+ * })
29
+ *
30
+ * // Query with filters
31
+ * app.get('/api/bases/:id/:table', async (req, ctx) => {
32
+ * const url = new URL(req.url)
33
+ * const filter = url.searchParams.get('filter')
34
+ * return Response.json(await ctx.base.query(ctx.params.id, ctx.params.table, {
35
+ * filter: filter ? JSON.parse(filter) : undefined,
36
+ * limit: parseInt(url.searchParams.get('limit') || '50'),
37
+ * }))
38
+ * })
39
+ * ```
40
+ */
41
+ export { Base } from './module.ts';
42
+ export type { BaseAPI, BaseDef, BaseOptions, TableSchema, FieldSchema, FieldType, ColumnMap, CreateBaseInput, UpdateBaseInput, QueryOptions, } from './types.ts';
43
+ import type { Context, Middleware } from '../types.ts';
44
+ import type { BaseAPI, BaseOptions } from './types.ts';
45
+ /**
46
+ * Base factory — creates the Base instance and returns a middleware
47
+ * that injects `ctx.base`.
48
+ *
49
+ * Must be used **after** `postgres()` and `user()`.
50
+ *
51
+ * ```ts
52
+ * app.use(postgres())
53
+ * app.use(user())
54
+ * app.use(base())
55
+ * ```
56
+ */
57
+ export declare function base(opts?: BaseOptions): Middleware<Context, Context & {
58
+ base: BaseAPI;
59
+ }>;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Base — dynamic data storage engine for weifuwu.
3
+ *
4
+ * Each "base" is a collection of user-defined tables with fixed physical slots
5
+ * (text001..064, number001..032, etc.) mapped via a column_map.
6
+ *
7
+ * Depends on `postgres()` and `user()` middleware registered first.
8
+ *
9
+ * ```ts
10
+ * import { serve, Router, postgres, user, base } from 'weifuwu'
11
+ *
12
+ * const app = new Router()
13
+ * app.use(postgres())
14
+ * app.use(user())
15
+ * app.use(base())
16
+ *
17
+ * app.post('/api/bases', async (req, ctx) => {
18
+ * const b = await ctx.base.create(await req.json())
19
+ * return Response.json(b, { status: 201 })
20
+ * })
21
+ * ```
22
+ */
23
+ import type { Context, Handler, SqlClient } from '../types.ts';
24
+ import type { BaseAPI, BaseOptions } from './types.ts';
25
+ export declare class Base {
26
+ private migrated;
27
+ hasVector: boolean;
28
+ readonly managementSchema: string;
29
+ readonly usersTable: string;
30
+ constructor(opts?: BaseOptions);
31
+ private ms;
32
+ migrate(sql: SqlClient): Promise<void>;
33
+ private ensureMigrated;
34
+ private allocateSlot;
35
+ private physicalType;
36
+ private getColumnMap;
37
+ private splitFields;
38
+ private reconstructRow;
39
+ private buildFilter;
40
+ bind(ctx: Context): BaseAPI;
41
+ middleware(req: Request, ctx: Context, next: Handler): Promise<Response>;
42
+ }
@@ -0,0 +1,116 @@
1
+ declare module '../types.ts' {
2
+ interface Context {
3
+ /** Base module — dynamic data storage engine. */
4
+ base: import('./types.ts').BaseAPI;
5
+ }
6
+ }
7
+ export type FieldType = 'string' | 'text' | 'number' | 'boolean' | 'date' | 'datetime' | 'vector' | 'search' | 'relation' | 'json';
8
+ export interface FieldSchema {
9
+ type: FieldType;
10
+ required?: boolean;
11
+ unique?: boolean;
12
+ maxLength?: number;
13
+ minimum?: number;
14
+ maximum?: number;
15
+ enum?: string[];
16
+ dimensions?: number;
17
+ target?: string;
18
+ description?: string;
19
+ }
20
+ export interface TableSchema {
21
+ name: string;
22
+ fields: Record<string, FieldSchema>;
23
+ }
24
+ export interface BaseDef {
25
+ id: string;
26
+ name: string;
27
+ slug: string;
28
+ description: string | null;
29
+ tables: TableSchema[];
30
+ created_by: string;
31
+ created_at: Date;
32
+ updated_at: Date;
33
+ }
34
+ export interface ColumnMap {
35
+ field_name: string;
36
+ physical: string;
37
+ field_type: FieldType;
38
+ }
39
+ export interface CreateBaseInput {
40
+ name: string;
41
+ slug?: string;
42
+ description?: string;
43
+ tables?: TableSchema[];
44
+ }
45
+ export interface UpdateBaseInput {
46
+ name?: string;
47
+ description?: string;
48
+ }
49
+ export interface QueryOptions {
50
+ filter?: Record<string, unknown>;
51
+ sort?: string;
52
+ order?: 'asc' | 'desc';
53
+ limit?: number;
54
+ offset?: number;
55
+ include?: string[];
56
+ }
57
+ export interface BaseOptions {
58
+ /** Schema name for management tables (default: 'public'). */
59
+ managementSchema?: string;
60
+ /** User table name (default: 'users'). */
61
+ usersTable?: string;
62
+ }
63
+ export interface BaseAPI {
64
+ /** Create a new base with optional table definitions. */
65
+ create(input: CreateBaseInput): Promise<BaseDef>;
66
+ /** List all bases. */
67
+ list(): Promise<BaseDef[]>;
68
+ /** Get a base by id. */
69
+ get(id: string): Promise<BaseDef | null>;
70
+ /** Get a base by slug. */
71
+ getBySlug(slug: string): Promise<BaseDef | null>;
72
+ /** Update base metadata. */
73
+ update(id: string, input: UpdateBaseInput): Promise<BaseDef | null>;
74
+ /** Delete a base and all its data. */
75
+ delete(id: string): Promise<boolean>;
76
+ /** Define a new table in a base. */
77
+ defineTable(baseId: string, schema: TableSchema): Promise<BaseDef>;
78
+ /** Update a table's schema (adds new fields, updates metadata). */
79
+ updateTable(baseId: string, tableName: string, schema: TableSchema): Promise<BaseDef | null>;
80
+ /** Remove a table and all its data. */
81
+ removeTable(baseId: string, tableName: string): Promise<BaseDef | null>;
82
+ /** Insert a row into a table. */
83
+ insert(baseId: string, table: string, data: Record<string, unknown>): Promise<Record<string, unknown> & {
84
+ id: string;
85
+ }>;
86
+ /** Get a row by id. */
87
+ getRow(baseId: string, table: string, id: string): Promise<(Record<string, unknown> & {
88
+ id: string;
89
+ }) | null>;
90
+ /** Update a row by id. */
91
+ updateRow(baseId: string, table: string, id: string, data: Partial<Record<string, unknown>>): Promise<(Record<string, unknown> & {
92
+ id: string;
93
+ }) | null>;
94
+ /** Delete a row by id. */
95
+ deleteRow(baseId: string, table: string, id: string): Promise<boolean>;
96
+ /** Query rows with filtering, sorting, and pagination. */
97
+ query(baseId: string, table: string, opts?: QueryOptions): Promise<(Record<string, unknown> & {
98
+ id: string;
99
+ })[]>;
100
+ /** Vector similarity search. */
101
+ similaritySearch(baseId: string, table: string, field: string, vector: number[], opts?: {
102
+ limit?: number;
103
+ }): Promise<(Record<string, unknown> & {
104
+ id: string;
105
+ distance: number;
106
+ })[]>;
107
+ /** Full-text search. */
108
+ search(baseId: string, table: string, field: string, query: string, opts?: {
109
+ limit?: number;
110
+ offset?: number;
111
+ }): Promise<(Record<string, unknown> & {
112
+ id: string;
113
+ rank: number;
114
+ headline?: string;
115
+ })[]>;
116
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * CMS — content management module for weifuwu.
3
+ *
4
+ * Requires `postgres()` and `user()` middleware registered first.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { serve, Router, postgres, user, cms, requireRole } from 'weifuwu'
9
+ *
10
+ * const app = new Router()
11
+ * app.use(postgres())
12
+ * app.use(user())
13
+ * app.use(cms())
14
+ *
15
+ * // Public: list published posts
16
+ * app.get('/api/posts', async (req, ctx) => {
17
+ * const posts = await ctx.cms.list({ type: 'post', status: 'published' })
18
+ * return Response.json(posts)
19
+ * })
20
+ *
21
+ * // Public: get single post
22
+ * app.get('/api/posts/:slug', async (req, ctx) => {
23
+ * const post = await ctx.cms.get(ctx.params.slug)
24
+ * if (!post) return new Response('Not found', { status: 404 })
25
+ * return Response.json(post)
26
+ * })
27
+ *
28
+ * // Admin: create post
29
+ * app.post('/api/admin/posts', requireRole('admin'), async (req, ctx) => {
30
+ * const post = await ctx.cms.create(await req.json())
31
+ * return Response.json(post, { status: 201 })
32
+ * })
33
+ *
34
+ * // Admin: update post
35
+ * app.patch('/api/admin/posts/:id', requireRole('admin'), async (req, ctx) => {
36
+ * const post = await ctx.cms.update(ctx.params.id, await req.json())
37
+ * if (!post) return new Response('Not found', { status: 404 })
38
+ * return Response.json(post)
39
+ * })
40
+ *
41
+ * // Tags
42
+ * app.get('/api/tags', async (req, ctx) => {
43
+ * return Response.json(await ctx.cms.listTags())
44
+ * })
45
+ *
46
+ * serve(app)
47
+ * ```
48
+ */
49
+ export { CMS } from './module.ts';
50
+ export type { CMSAPI, CMSOptions, Content, ContentStatus, ContentType, Tag, TagWithCount, CreateContentInput, UpdateContentInput, ListContentOptions, } from './types.ts';
51
+ import type { Context, Middleware } from '../types.ts';
52
+ import type { CMSAPI, CMSOptions } from './types.ts';
53
+ /**
54
+ * CMS factory — creates the CMS instance and returns a middleware
55
+ * that injects `ctx.cms`.
56
+ *
57
+ * Must be used **after** `postgres()` and `user()`.
58
+ *
59
+ * ```ts
60
+ * app.use(postgres())
61
+ * app.use(user())
62
+ * app.use(cms())
63
+ * ```
64
+ */
65
+ export declare function cms(opts?: CMSOptions): Middleware<Context, Context & {
66
+ cms: CMSAPI;
67
+ }>;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * CMS — content management module for weifuwu.
3
+ *
4
+ * Depends on `postgres()` and `user()` middleware registered first.
5
+ *
6
+ * ```ts
7
+ * import { serve, Router, postgres, user, cms } from 'weifuwu'
8
+ *
9
+ * const app = new Router()
10
+ * app.use(postgres())
11
+ * app.use(user())
12
+ * app.use(cms())
13
+ *
14
+ * app.get('/api/posts', async (req, ctx) => {
15
+ * return Response.json(await ctx.cms.list({ type: 'post', status: 'published' }))
16
+ * })
17
+ *
18
+ * app.get('/api/posts/:slug', async (req, ctx) => {
19
+ * const post = await ctx.cms.get(ctx.params.slug)
20
+ * if (!post) return new Response('Not found', { status: 404 })
21
+ * return Response.json(post)
22
+ * })
23
+ * ```
24
+ */
25
+ import type { Context, Handler, SqlClient } from '../types.ts';
26
+ import type { CMSAPI, CMSOptions } from './types.ts';
27
+ export declare class CMS {
28
+ private migrated;
29
+ private prefix;
30
+ private usersTable;
31
+ constructor(opts?: CMSOptions);
32
+ private q;
33
+ migrate(sql: SqlClient): Promise<void>;
34
+ private ensureMigrated;
35
+ bind(ctx: Context): CMSAPI;
36
+ private _uniqueSlug;
37
+ middleware(req: Request, ctx: Context, next: Handler): Promise<Response>;
38
+ }
@@ -0,0 +1,96 @@
1
+ declare module '../types.ts' {
2
+ interface Context {
3
+ /** CMS instance for content management. */
4
+ cms: import('./types.ts').CMSAPI;
5
+ }
6
+ }
7
+ export type ContentStatus = 'draft' | 'published' | 'archived';
8
+ export type ContentType = string;
9
+ export interface Content {
10
+ id: string;
11
+ slug: string;
12
+ type: ContentType;
13
+ parent_id: string | null;
14
+ title: string;
15
+ body: string;
16
+ excerpt: string | null;
17
+ cover_image: string | null;
18
+ status: ContentStatus;
19
+ author_id: string;
20
+ author_name?: string;
21
+ published_at: Date | null;
22
+ created_at: Date;
23
+ updated_at: Date;
24
+ tags?: Tag[];
25
+ }
26
+ export interface Tag {
27
+ id: string;
28
+ name: string;
29
+ slug: string;
30
+ }
31
+ export interface TagWithCount extends Tag {
32
+ content_count: number;
33
+ }
34
+ export interface CreateContentInput {
35
+ slug?: string;
36
+ type: ContentType;
37
+ parent_id?: string;
38
+ title: string;
39
+ body: string;
40
+ excerpt?: string;
41
+ cover_image?: string;
42
+ status?: ContentStatus;
43
+ tags?: string[];
44
+ }
45
+ export interface UpdateContentInput {
46
+ slug?: string;
47
+ type?: ContentType;
48
+ parent_id?: string | null;
49
+ title?: string;
50
+ body?: string;
51
+ excerpt?: string | null;
52
+ cover_image?: string | null;
53
+ status?: ContentStatus;
54
+ tags?: string[];
55
+ }
56
+ export interface ListContentOptions {
57
+ type?: ContentType;
58
+ status?: ContentStatus;
59
+ tag?: string;
60
+ author_id?: string;
61
+ parent_id?: string | null;
62
+ /** Cursor pagination: return items before this cursor (created_at DESC). */
63
+ before?: string;
64
+ /** Page size (default: 20, max: 100). */
65
+ limit?: number;
66
+ }
67
+ export interface CMSOptions {
68
+ /** PostgreSQL table prefix (default: ''). */
69
+ tablePrefix?: string;
70
+ /** User table name for author name lookups (default: 'users'). */
71
+ usersTable?: string;
72
+ }
73
+ export interface CMSAPI {
74
+ /** List content with filters and cursor pagination. */
75
+ list(opts?: ListContentOptions): Promise<Content[]>;
76
+ /** Get a single content by slug. Returns null if not found or not published (for non-admin). */
77
+ get(slug: string): Promise<Content | null>;
78
+ /** Get a single content by id. */
79
+ getById(id: string): Promise<Content | null>;
80
+ /** Create content. Requires admin role. */
81
+ create(input: CreateContentInput): Promise<Content>;
82
+ /** Update content. Requires admin role. Returns null if not found. */
83
+ update(id: string, input: UpdateContentInput): Promise<Content | null>;
84
+ /** Delete content (hard delete). Requires admin role. */
85
+ delete(id: string): Promise<boolean>;
86
+ /** Publish content (set status to 'published' and set published_at). */
87
+ publish(id: string): Promise<Content | null>;
88
+ /** Unpublish content (set status to 'draft'). */
89
+ unpublish(id: string): Promise<Content | null>;
90
+ /** List all tags with content count. */
91
+ listTags(): Promise<TagWithCount[]>;
92
+ /** Create a tag. */
93
+ createTag(name: string): Promise<Tag>;
94
+ /** Delete a tag. */
95
+ deleteTag(id: string): Promise<boolean>;
96
+ }
package/dist/index.d.ts CHANGED
@@ -11,8 +11,6 @@ export { logger } from './core/logger.ts';
11
11
  export type { LoggerOptions } from './core/logger.ts';
12
12
  export { cors } from './middleware/cors.ts';
13
13
  export type { CORSOptions } from './middleware/cors.ts';
14
- export { auth } from './middleware/auth.ts';
15
- export type { AuthOptions } from './middleware/auth.ts';
16
14
  export { serveStatic } from './middleware/static.ts';
17
15
  export type { ServeStaticOptions } from './middleware/static.ts';
18
16
  export { upload } from './middleware/upload.ts';
@@ -39,10 +37,18 @@ export { createHub } from './hub.ts';
39
37
  export type { Hub, HubOptions } from './hub.ts';
40
38
  export { queue } from './queue/index.ts';
41
39
  export type { QueueOptions, QueueJob, Queue, QueueInjected } from './queue/types.ts';
42
- export { react, reactRouter } from './react/index.ts';
43
- export type { ReactOptions, RenderOptions, ReactRouterOptions, ReactAppOptions } from './react/types.ts';
44
- export { useServerData, ServerDataContext, Link, ErrorBoundary } from './react/index.ts';
45
40
  export { ai } from './ai/index.ts';
46
41
  export type { AiOptions, Ai } from './ai/index.ts';
47
42
  export { agent } from './ai/agent.ts';
48
43
  export type { AgentOptions, Agent } from './ai/agent.ts';
44
+ export { user, UserModule } from './user/index.ts';
45
+ export type { UserModuleOptions, UserRecord, CreateUserInput, UpdateUserInput, TokenPayload, } from './user/types.ts';
46
+ export { messager, Messager } from './messager/index.ts';
47
+ export type { MessagerAPI, MessagerOptions, Conversation, ConversationType, Participant, ParticipantUser, Message, MessagePreview, } from './messager/types.ts';
48
+ export { base, Base } from './base/index.ts';
49
+ export { cms, CMS } from './cms/index.ts';
50
+ export type { CMSAPI, CMSOptions, Content, ContentStatus, ContentType, Tag, TagWithCount, CreateContentInput, UpdateContentInput, ListContentOptions, } from './cms/types.ts';
51
+ export { kb, KB } from './kb/index.ts';
52
+ export type { KBAPI, KBOptions, Document, Chunk, SearchResult, ImportOptions, SearchOptions, } from './kb/types.ts';
53
+ export type { BaseAPI, BaseDef, BaseOptions, TableSchema, FieldSchema, FieldType, ColumnMap, CreateBaseInput, UpdateBaseInput, QueryOptions, } from './base/types.ts';
54
+ export { requireRole } from './user/types.ts';