@spfn/core 0.1.0-alpha.68 → 0.1.0-alpha.72

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,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.68",
3
+ "version": "0.1.0-alpha.72",
4
4
  "description": "SPFN Framework Core - File-based routing, transactions, repository pattern",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,57 +0,0 @@
1
- import { Context } from 'hono';
2
- import { ContentfulStatusCode } from 'hono/utils/http-status';
3
- import { TSchema, Static } from '@sinclair/typebox';
4
-
5
- /**
6
- * File-based Routing System Type Definitions
7
- */
8
- type HeaderRecord = Record<string, string | string[]>;
9
- type RouteMeta = {
10
- public?: boolean;
11
- skipMiddlewares?: string[];
12
- tags?: string[];
13
- description?: string;
14
- deprecated?: boolean;
15
- };
16
- /**
17
- * Route Contract: TypeBox-based type-safe route definition
18
- *
19
- * Defines the shape of request/response for a route endpoint
20
- */
21
- type RouteContract = {
22
- method: HttpMethod;
23
- path: string;
24
- params?: TSchema;
25
- query?: TSchema;
26
- body?: TSchema;
27
- response: TSchema;
28
- meta?: RouteMeta;
29
- };
30
- /**
31
- * Infer types from RouteContract
32
- *
33
- * Extracts TypeScript types from TypeBox schemas
34
- */
35
- type InferContract<TContract extends RouteContract> = {
36
- params: TContract['params'] extends TSchema ? Static<TContract['params']> : Record<string, never>;
37
- query: TContract['query'] extends TSchema ? Static<TContract['query']> : Record<string, never>;
38
- body: TContract['body'] extends TSchema ? Static<TContract['body']> : Record<string, never>;
39
- response: TContract['response'] extends TSchema ? Static<TContract['response']> : unknown;
40
- };
41
- /**
42
- * RouteContext: Route Handler Dedicated Context
43
- *
44
- * Generic version with contract-based type inference
45
- */
46
- type RouteContext<TContract extends RouteContract = any> = {
47
- params: InferContract<TContract>['params'];
48
- query: InferContract<TContract>['query'];
49
- data(): Promise<InferContract<TContract>['body']>;
50
- json(data: InferContract<TContract>['response'], status?: ContentfulStatusCode, headers?: HeaderRecord): Response;
51
- raw: Context;
52
- };
53
- type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
54
- type RouteHandler<TContract extends RouteContract = any> = (c: RouteContext<TContract>) => Response | Promise<Response>;
55
- declare function isHttpMethod(value: unknown): value is HttpMethod;
56
-
57
- export { type HttpMethod as H, type InferContract as I, type RouteContext as R, type RouteContract as a, type RouteHandler as b, type HeaderRecord as c, type RouteMeta as d, isHttpMethod as i };