@spfn/core 0.1.0-alpha.7 → 0.1.0-alpha.74

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 (59) hide show
  1. package/README.md +168 -195
  2. package/dist/auto-loader-JFaZ9gON.d.ts +80 -0
  3. package/dist/cache/index.d.ts +211 -0
  4. package/dist/cache/index.js +992 -0
  5. package/dist/cache/index.js.map +1 -0
  6. package/dist/client/index.d.ts +92 -92
  7. package/dist/client/index.js +80 -85
  8. package/dist/client/index.js.map +1 -1
  9. package/dist/codegen/generators/index.d.ts +19 -0
  10. package/dist/codegen/generators/index.js +1500 -0
  11. package/dist/codegen/generators/index.js.map +1 -0
  12. package/dist/codegen/index.d.ts +76 -60
  13. package/dist/codegen/index.js +1486 -736
  14. package/dist/codegen/index.js.map +1 -1
  15. package/dist/database-errors-BNNmLTJE.d.ts +86 -0
  16. package/dist/db/index.d.ts +844 -44
  17. package/dist/db/index.js +1262 -1309
  18. package/dist/db/index.js.map +1 -1
  19. package/dist/env/index.d.ts +508 -0
  20. package/dist/env/index.js +1106 -0
  21. package/dist/env/index.js.map +1 -0
  22. package/dist/error-handler-wjLL3v-a.d.ts +44 -0
  23. package/dist/errors/index.d.ts +136 -0
  24. package/dist/errors/index.js +172 -0
  25. package/dist/errors/index.js.map +1 -0
  26. package/dist/index-DHiAqhKv.d.ts +101 -0
  27. package/dist/index.d.ts +3 -374
  28. package/dist/index.js +2394 -2176
  29. package/dist/index.js.map +1 -1
  30. package/dist/logger/index.d.ts +94 -0
  31. package/dist/logger/index.js +774 -0
  32. package/dist/logger/index.js.map +1 -0
  33. package/dist/middleware/index.d.ts +33 -0
  34. package/dist/middleware/index.js +890 -0
  35. package/dist/middleware/index.js.map +1 -0
  36. package/dist/route/index.d.ts +21 -53
  37. package/dist/route/index.js +1234 -219
  38. package/dist/route/index.js.map +1 -1
  39. package/dist/server/index.d.ts +18 -0
  40. package/dist/server/index.js +2390 -2058
  41. package/dist/server/index.js.map +1 -1
  42. package/dist/types-Dzggq1Yb.d.ts +170 -0
  43. package/package.json +59 -15
  44. package/dist/auto-loader-C44TcLmM.d.ts +0 -125
  45. package/dist/bind-pssq1NRT.d.ts +0 -34
  46. package/dist/postgres-errors-CY_Es8EJ.d.ts +0 -1703
  47. package/dist/scripts/index.d.ts +0 -24
  48. package/dist/scripts/index.js +0 -1201
  49. package/dist/scripts/index.js.map +0 -1
  50. package/dist/scripts/templates/api-index.template.txt +0 -10
  51. package/dist/scripts/templates/api-tag.template.txt +0 -11
  52. package/dist/scripts/templates/contract.template.txt +0 -87
  53. package/dist/scripts/templates/entity-type.template.txt +0 -31
  54. package/dist/scripts/templates/entity.template.txt +0 -19
  55. package/dist/scripts/templates/index.template.txt +0 -10
  56. package/dist/scripts/templates/repository.template.txt +0 -37
  57. package/dist/scripts/templates/routes-id.template.txt +0 -59
  58. package/dist/scripts/templates/routes-index.template.txt +0 -44
  59. package/dist/types-SlzTr8ZO.d.ts +0 -143
@@ -0,0 +1,170 @@
1
+ import { Context } from 'hono';
2
+ import { ContentfulStatusCode } from 'hono/utils/http-status';
3
+ import * as _sinclair_typebox from '@sinclair/typebox';
4
+ import { TSchema, Static } from '@sinclair/typebox';
5
+ import { b as ErrorResponse } from './error-handler-wjLL3v-a.js';
6
+
7
+ /**
8
+ * Success response wrapper
9
+ */
10
+ interface ApiSuccessResponse<T = any> {
11
+ success: true;
12
+ data: T;
13
+ meta?: {
14
+ timestamp?: string;
15
+ requestId?: string;
16
+ pagination?: {
17
+ page: number;
18
+ limit: number;
19
+ total: number;
20
+ totalPages: number;
21
+ };
22
+ [key: string]: any;
23
+ };
24
+ }
25
+ /**
26
+ * Error response type (re-exported from ErrorHandler for consistency)
27
+ */
28
+ type ApiErrorResponse = ErrorResponse;
29
+ /**
30
+ * Unified API response type
31
+ */
32
+ type ApiResponse<T = any> = ApiSuccessResponse<T> | ApiErrorResponse;
33
+ /**
34
+ * Creates a TypeBox schema for ApiSuccessResponse<T>
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const UserSchema = Type.Object({
39
+ * id: Type.String(),
40
+ * name: Type.String(),
41
+ * });
42
+ *
43
+ * const contract = {
44
+ * response: ApiSuccessSchema(UserSchema),
45
+ * };
46
+ * ```
47
+ */
48
+ declare function ApiSuccessSchema<T extends TSchema>(dataSchema: T): _sinclair_typebox.TObject<{
49
+ success: _sinclair_typebox.TLiteral<true>;
50
+ data: T;
51
+ meta: _sinclair_typebox.TOptional<_sinclair_typebox.TObject<{
52
+ timestamp: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
53
+ requestId: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
54
+ pagination: _sinclair_typebox.TOptional<_sinclair_typebox.TObject<{
55
+ page: _sinclair_typebox.TNumber;
56
+ limit: _sinclair_typebox.TNumber;
57
+ total: _sinclair_typebox.TNumber;
58
+ totalPages: _sinclair_typebox.TNumber;
59
+ }>>;
60
+ }>>;
61
+ }>;
62
+ /**
63
+ * Creates a TypeBox schema for ApiErrorResponse
64
+ */
65
+ declare function ApiErrorSchema(): _sinclair_typebox.TObject<{
66
+ success: _sinclair_typebox.TLiteral<false>;
67
+ error: _sinclair_typebox.TObject<{
68
+ message: _sinclair_typebox.TString;
69
+ type: _sinclair_typebox.TString;
70
+ statusCode: _sinclair_typebox.TNumber;
71
+ stack: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
72
+ details: _sinclair_typebox.TOptional<_sinclair_typebox.TAny>;
73
+ }>;
74
+ }>;
75
+ /**
76
+ * Creates a TypeBox union schema for ApiResponse<T>
77
+ *
78
+ * Use this in your route contract's response field for standardized responses.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * const contract = {
83
+ * method: 'GET',
84
+ * path: '/users/:id',
85
+ * response: ApiResponseSchema(Type.Object({
86
+ * id: Type.String(),
87
+ * name: Type.String(),
88
+ * })),
89
+ * };
90
+ * ```
91
+ */
92
+ declare function ApiResponseSchema<T extends TSchema>(dataSchema: T): _sinclair_typebox.TUnion<[_sinclair_typebox.TObject<{
93
+ success: _sinclair_typebox.TLiteral<true>;
94
+ data: T;
95
+ meta: _sinclair_typebox.TOptional<_sinclair_typebox.TObject<{
96
+ timestamp: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
97
+ requestId: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
98
+ pagination: _sinclair_typebox.TOptional<_sinclair_typebox.TObject<{
99
+ page: _sinclair_typebox.TNumber;
100
+ limit: _sinclair_typebox.TNumber;
101
+ total: _sinclair_typebox.TNumber;
102
+ totalPages: _sinclair_typebox.TNumber;
103
+ }>>;
104
+ }>>;
105
+ }>, _sinclair_typebox.TObject<{
106
+ success: _sinclair_typebox.TLiteral<false>;
107
+ error: _sinclair_typebox.TObject<{
108
+ message: _sinclair_typebox.TString;
109
+ type: _sinclair_typebox.TString;
110
+ statusCode: _sinclair_typebox.TNumber;
111
+ stack: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
112
+ details: _sinclair_typebox.TOptional<_sinclair_typebox.TAny>;
113
+ }>;
114
+ }>]>;
115
+
116
+ /**
117
+ * File-based Routing System Type Definitions
118
+ */
119
+ type HeaderRecord = Record<string, string | string[]>;
120
+ type RouteMeta = {
121
+ public?: boolean;
122
+ skipMiddlewares?: string[];
123
+ tags?: string[];
124
+ description?: string;
125
+ deprecated?: boolean;
126
+ };
127
+ /**
128
+ * Route Contract: TypeBox-based type-safe route definition
129
+ *
130
+ * Defines the shape of request/response for a route endpoint
131
+ */
132
+ type RouteContract = {
133
+ method: HttpMethod;
134
+ path: string;
135
+ params?: TSchema;
136
+ query?: TSchema;
137
+ body?: TSchema;
138
+ response: TSchema;
139
+ meta?: RouteMeta;
140
+ };
141
+ /**
142
+ * Infer types from RouteContract
143
+ *
144
+ * Extracts TypeScript types from TypeBox schemas
145
+ */
146
+ type InferContract<TContract extends RouteContract> = {
147
+ params: TContract['params'] extends TSchema ? Static<TContract['params']> : Record<string, never>;
148
+ query: TContract['query'] extends TSchema ? Static<TContract['query']> : Record<string, never>;
149
+ body: TContract['body'] extends TSchema ? Static<TContract['body']> : Record<string, never>;
150
+ response: TContract['response'] extends TSchema ? Static<TContract['response']> : unknown;
151
+ };
152
+ /**
153
+ * RouteContext: Route Handler Dedicated Context
154
+ *
155
+ * Generic version with contract-based type inference
156
+ */
157
+ type RouteContext<TContract extends RouteContract = any> = {
158
+ params: InferContract<TContract>['params'];
159
+ query: InferContract<TContract>['query'];
160
+ data(): Promise<InferContract<TContract>['body']>;
161
+ json(data: InferContract<TContract>['response'], status?: ContentfulStatusCode, headers?: HeaderRecord): Response;
162
+ success<T>(data: T, meta?: ApiSuccessResponse<T>['meta'], status?: number): Response;
163
+ paginated<T>(data: T[], page: number, limit: number, total: number): Response;
164
+ raw: Context;
165
+ };
166
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
167
+ type RouteHandler<TContract extends RouteContract = any> = (c: RouteContext<TContract>) => Response | Promise<Response>;
168
+ declare function isHttpMethod(value: unknown): value is HttpMethod;
169
+
170
+ export { ApiSuccessSchema as A, type HttpMethod as H, type InferContract as I, type RouteContext as R, type RouteContract as a, type RouteHandler as b, ApiErrorSchema as c, ApiResponseSchema as d, type ApiSuccessResponse as e, type ApiErrorResponse as f, type ApiResponse as g, type HeaderRecord as h, isHttpMethod as i, type RouteMeta as j };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spfn/core",
3
- "version": "0.1.0-alpha.7",
3
+ "version": "0.1.0-alpha.74",
4
4
  "description": "SPFN Framework Core - File-based routing, transactions, repository pattern",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -31,15 +31,45 @@
31
31
  "import": "./dist/server/index.js",
32
32
  "require": "./dist/server/index.js"
33
33
  },
34
- "./scripts": {
35
- "types": "./dist/scripts/index.d.ts",
36
- "import": "./dist/scripts/index.js",
37
- "require": "./dist/scripts/index.js"
34
+ "./errors": {
35
+ "types": "./dist/errors/index.d.ts",
36
+ "import": "./dist/errors/index.js",
37
+ "require": "./dist/errors/index.js"
38
+ },
39
+ "./middleware": {
40
+ "types": "./dist/middleware/index.d.ts",
41
+ "import": "./dist/middleware/index.js",
42
+ "require": "./dist/middleware/index.js"
43
+ },
44
+ "./cache": {
45
+ "types": "./dist/cache/index.d.ts",
46
+ "import": "./dist/cache/index.js",
47
+ "require": "./dist/cache/index.js"
38
48
  },
39
49
  "./codegen": {
40
50
  "types": "./dist/codegen/index.d.ts",
41
51
  "import": "./dist/codegen/index.js",
42
52
  "require": "./dist/codegen/index.js"
53
+ },
54
+ "./env": {
55
+ "types": "./dist/env/index.d.ts",
56
+ "import": "./dist/env/index.js",
57
+ "require": "./dist/env/index.js"
58
+ },
59
+ "./logger": {
60
+ "types": "./dist/logger/index.d.ts",
61
+ "import": "./dist/logger/index.js",
62
+ "require": "./dist/logger/index.js"
63
+ },
64
+ "./generators": {
65
+ "types": "./dist/codegen/generators/index.d.ts",
66
+ "import": "./dist/codegen/generators/index.js",
67
+ "require": "./dist/codegen/generators/index.js"
68
+ },
69
+ "./codegen/generators": {
70
+ "types": "./dist/codegen/generators/index.d.ts",
71
+ "import": "./dist/codegen/generators/index.js",
72
+ "require": "./dist/codegen/generators/index.js"
43
73
  }
44
74
  },
45
75
  "keywords": [
@@ -89,6 +119,8 @@
89
119
  "drizzle-orm": "^0.44.6",
90
120
  "drizzle-typebox": "^0.1.0",
91
121
  "hono": "^4.9.0",
122
+ "jiti": "^2.6.1",
123
+ "micromatch": "^4.0.8",
92
124
  "pino": "^10.0.0",
93
125
  "postgres": "^3.4.0",
94
126
  "typescript": "^5.3.3",
@@ -98,9 +130,12 @@
98
130
  "ioredis": "^5.4.1"
99
131
  },
100
132
  "devDependencies": {
133
+ "@types/micromatch": "^4.0.9",
101
134
  "@types/node": "^20.11.0",
135
+ "@vitest/coverage-v8": "^4.0.6",
136
+ "drizzle-kit": "^0.31.5",
102
137
  "tsup": "^8.0.0",
103
- "vitest": "^3.2.4"
138
+ "vitest": "^4.0.6"
104
139
  },
105
140
  "files": [
106
141
  "dist",
@@ -114,15 +149,24 @@
114
149
  },
115
150
  "scripts": {
116
151
  "build": "tsup",
117
- "dev": "tsup --watch",
152
+ "dev": "tsup",
118
153
  "test": "vitest",
119
- "test:codegen": "vitest run --config vitest.codegen.config.ts",
120
- "test:db": "dotenv -e .env.test -- vitest src/tests/db",
121
- "docker:test:up": "docker compose -f ../../docker-compose.test.yml up -d",
122
- "docker:test:down": "docker compose -f ../../docker-compose.test.yml down",
123
- "docker:test:logs": "docker compose -f ../../docker-compose.test.yml logs -f",
124
- "type-check": "tsc --noEmit",
125
- "generate:client": "node dist/scripts/generate-client.js",
126
- "generate:client:watch": "node dist/scripts/generate-client.js --watch"
154
+ "test:unit": "vitest --config vitest.unit.config.ts",
155
+ "test:integration": "vitest --config vitest.integration.config.ts",
156
+ "test:coverage": "vitest run --config vitest.unit.config.ts --coverage",
157
+ "test:logger": "vitest src/logger",
158
+ "test:errors": "vitest src/errors",
159
+ "test:codegen": "vitest src/codegen",
160
+ "test:route": "vitest src/route",
161
+ "test:client": "vitest src/client",
162
+ "test:middleware": "vitest src/middleware",
163
+ "test:env": "vitest src/env",
164
+ "test:cache": "vitest src/cache --config vitest.integration.config.ts",
165
+ "test:db": "vitest src/db --config vitest.integration.config.ts",
166
+ "test:server": "vitest src/server --config vitest.integration.config.ts",
167
+ "docker:test:up": "docker compose -f docker-compose.test.yml up -d",
168
+ "docker:test:down": "docker compose -f docker-compose.test.yml down",
169
+ "docker:test:logs": "docker compose -f docker-compose.test.yml logs -f",
170
+ "type-check": "tsc --noEmit"
127
171
  }
128
172
  }
@@ -1,125 +0,0 @@
1
- import { Hono } from 'hono';
2
-
3
- /**
4
- * Extend Hono Context to support skipMiddlewares metadata
5
- */
6
- declare module 'hono' {
7
- interface ContextVariableMap {
8
- _skipMiddlewares?: string[];
9
- }
10
- }
11
- /**
12
- * AutoRouteLoader: Simplified File-based Routing System
13
- *
14
- * ## Features
15
- * - 📁 Auto-discovery: Scans routes directory and auto-registers
16
- * - 🔄 Dynamic routes: [id] → :id, [...slug] → *
17
- * - 📊 Statistics: Route registration stats for dashboard
18
- * - 🏷️ Grouping: Natural grouping by directory structure
19
- *
20
- * ## Usage
21
- * ```typescript
22
- * const app = new Hono();
23
- * await loadRoutes(app);
24
- * ```
25
- */
26
- type RouteInfo = {
27
- /** URL path (e.g., /users/:id) */
28
- path: string;
29
- /** File path relative to routes dir */
30
- file: string;
31
- /** Route metadata from export */
32
- meta?: {
33
- description?: string;
34
- tags?: string[];
35
- auth?: boolean;
36
- [key: string]: unknown;
37
- };
38
- /** Priority (1=static, 2=dynamic, 3=catch-all) */
39
- priority: number;
40
- };
41
- type RouteStats = {
42
- total: number;
43
- byPriority: {
44
- static: number;
45
- dynamic: number;
46
- catchAll: number;
47
- };
48
- byTag: Record<string, number>;
49
- routes: RouteInfo[];
50
- };
51
- declare class AutoRouteLoader {
52
- private routesDir;
53
- private routes;
54
- private registeredRoutes;
55
- private debug;
56
- private readonly middlewares;
57
- constructor(routesDir: string, debug?: boolean, middlewares?: Array<{
58
- name: string;
59
- handler: any;
60
- }>);
61
- /**
62
- * Load all routes from directory
63
- */
64
- load(app: Hono): Promise<RouteStats>;
65
- /**
66
- * Get route statistics
67
- */
68
- getStats(): RouteStats;
69
- /**
70
- * Recursively scan directory for .ts files
71
- */
72
- private scanFiles;
73
- /**
74
- * Check if file is a valid route file
75
- */
76
- private isValidRouteFile;
77
- /**
78
- * Load and register a single route
79
- * Returns true if successful, false if failed
80
- */
81
- private loadRoute;
82
- /**
83
- * Convert file path to URL path
84
- *
85
- * Examples:
86
- * - users/index.ts → /users
87
- * - users/[id].ts → /users/:id
88
- * - posts/[...slug].ts → /posts/*
89
- */
90
- private fileToPath;
91
- /**
92
- * Calculate route priority
93
- * 1 = static, 2 = dynamic, 3 = catch-all
94
- */
95
- private calculatePriority;
96
- /**
97
- * Normalize path for conflict detection
98
- *
99
- * Converts dynamic parameter names to generic placeholders:
100
- * - /users/:id → /users/:param
101
- * - /users/:userId → /users/:param (conflict!)
102
- * - /posts/* → /posts/* (unchanged)
103
- *
104
- * This allows detection of routes with different param names
105
- * that would match the same URL patterns.
106
- */
107
- private normalizePath;
108
- /**
109
- * Log statistics
110
- */
111
- private logStats;
112
- }
113
- /**
114
- * Load routes from default location (src/server/routes)
115
- */
116
- declare function loadRoutes(app: Hono, options?: {
117
- routesDir?: string;
118
- debug?: boolean;
119
- middlewares?: Array<{
120
- name: string;
121
- handler: any;
122
- }>;
123
- }): Promise<RouteStats>;
124
-
125
- export { AutoRouteLoader as A, type RouteInfo as R, type RouteStats as a, loadRoutes as l };
@@ -1,34 +0,0 @@
1
- import { Context } from 'hono';
2
- import { R as RouteContract, c as RouteContext } from './types-SlzTr8ZO.js';
3
-
4
- /**
5
- * Contract-based Route Handler Wrapper
6
- *
7
- * Binds a contract to a route handler, providing automatic validation
8
- * and type-safe context creation.
9
- *
10
- * ## Features
11
- * - ✅ Automatic params/query/body validation using TypeBox
12
- * - ✅ Type-safe RouteContext with contract-based inference
13
- * - ✅ Clean separation: bind() for validation, Hono for middleware
14
- *
15
- * ## Usage
16
- *
17
- * ```typescript
18
- * // Basic usage
19
- * export const GET = bind(contract, async (c) => {
20
- * return c.json({ data: 'public' });
21
- * });
22
- *
23
- * // For middleware, use Hono's app-level or route-level middleware:
24
- * // app.use('/api/*', authMiddleware);
25
- * // app.get('/users/:id', authMiddleware, bind(contract, handler));
26
- * ```
27
- *
28
- * @param contract - Route contract defining params, query, body, response schemas
29
- * @param handler - Route handler function
30
- * @returns Hono-compatible handler function
31
- */
32
- declare function bind<TContract extends RouteContract>(contract: TContract, handler: (c: RouteContext<TContract>) => Response | Promise<Response>): (rawContext: Context) => Promise<Response>;
33
-
34
- export { bind as b };