@spfn/core 0.2.0-beta.2 → 0.2.0-beta.21
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 +262 -1092
- package/dist/{boss-D-fGtVgM.d.ts → boss-DI1r4kTS.d.ts} +68 -11
- package/dist/codegen/index.d.ts +55 -8
- package/dist/codegen/index.js +179 -5
- package/dist/codegen/index.js.map +1 -1
- package/dist/config/index.d.ts +204 -6
- package/dist/config/index.js +44 -11
- package/dist/config/index.js.map +1 -1
- package/dist/db/index.d.ts +13 -0
- package/dist/db/index.js +92 -33
- package/dist/db/index.js.map +1 -1
- package/dist/env/index.d.ts +83 -3
- package/dist/env/index.js +83 -15
- package/dist/env/index.js.map +1 -1
- package/dist/env/loader.d.ts +95 -0
- package/dist/env/loader.js +78 -0
- package/dist/env/loader.js.map +1 -0
- package/dist/event/index.d.ts +29 -70
- package/dist/event/index.js +15 -1
- package/dist/event/index.js.map +1 -1
- package/dist/event/sse/client.d.ts +157 -0
- package/dist/event/sse/client.js +169 -0
- package/dist/event/sse/client.js.map +1 -0
- package/dist/event/sse/index.d.ts +46 -0
- package/dist/event/sse/index.js +205 -0
- package/dist/event/sse/index.js.map +1 -0
- package/dist/job/index.d.ts +54 -8
- package/dist/job/index.js +61 -12
- package/dist/job/index.js.map +1 -1
- package/dist/middleware/index.d.ts +124 -11
- package/dist/middleware/index.js +41 -7
- package/dist/middleware/index.js.map +1 -1
- package/dist/nextjs/index.d.ts +2 -2
- package/dist/nextjs/index.js +37 -5
- package/dist/nextjs/index.js.map +1 -1
- package/dist/nextjs/server.d.ts +45 -24
- package/dist/nextjs/server.js +87 -66
- package/dist/nextjs/server.js.map +1 -1
- package/dist/route/index.d.ts +207 -14
- package/dist/route/index.js +304 -31
- package/dist/route/index.js.map +1 -1
- package/dist/route/types.d.ts +2 -31
- package/dist/router-Di7ENoah.d.ts +151 -0
- package/dist/server/index.d.ts +321 -10
- package/dist/server/index.js +798 -189
- package/dist/server/index.js.map +1 -1
- package/dist/{types-DRG2XMTR.d.ts → types-7Mhoxnnt.d.ts} +97 -4
- package/dist/types-DHQMQlcb.d.ts +305 -0
- package/docs/cache.md +133 -0
- package/docs/codegen.md +74 -0
- package/docs/database.md +346 -0
- package/docs/entity.md +539 -0
- package/docs/env.md +499 -0
- package/docs/errors.md +319 -0
- package/docs/event.md +432 -0
- package/docs/file-upload.md +717 -0
- package/docs/job.md +131 -0
- package/docs/logger.md +108 -0
- package/docs/middleware.md +337 -0
- package/docs/nextjs.md +247 -0
- package/docs/repository.md +496 -0
- package/docs/route.md +497 -0
- package/docs/server.md +429 -0
- package/package.json +19 -3
|
@@ -2,24 +2,112 @@ import { TSchema, Static } from '@sinclair/typebox';
|
|
|
2
2
|
import { ErrorRegistry, ErrorRegistryInput } from '@spfn/core/errors';
|
|
3
3
|
import { RouteDef, RouteInput } from '@spfn/core/route';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Convert File types in schema to actual File for client usage
|
|
7
|
+
*
|
|
8
|
+
* TypeBox File schemas become actual File objects on the client side.
|
|
9
|
+
*/
|
|
10
|
+
type ConvertFileTypes<T> = T extends File ? File : T extends File[] ? File[] : T;
|
|
11
|
+
/**
|
|
12
|
+
* Extract form data input type with File support
|
|
13
|
+
*
|
|
14
|
+
* Maps schema types to runtime types, converting FileSchema to File.
|
|
15
|
+
*/
|
|
16
|
+
type FormDataInput<T> = {
|
|
17
|
+
[K in keyof T]: ConvertFileTypes<T[K]>;
|
|
18
|
+
};
|
|
5
19
|
/**
|
|
6
20
|
* Extract structured input from RouteInput
|
|
21
|
+
*
|
|
22
|
+
* Converts TypeBox schemas to their static types for each input field.
|
|
7
23
|
*/
|
|
8
24
|
type StructuredInput<TInput extends RouteInput> = {
|
|
9
25
|
params: TInput['params'] extends TSchema ? Static<TInput['params']> : {};
|
|
10
26
|
query: TInput['query'] extends TSchema ? Static<TInput['query']> : {};
|
|
11
27
|
body: TInput['body'] extends TSchema ? Static<TInput['body']> : {};
|
|
28
|
+
formData: TInput['formData'] extends TSchema ? FormDataInput<Static<TInput['formData']>> : {};
|
|
12
29
|
headers: TInput['headers'] extends TSchema ? Static<TInput['headers']> : {};
|
|
13
30
|
cookies: TInput['cookies'] extends TSchema ? Static<TInput['cookies']> : {};
|
|
14
31
|
};
|
|
15
32
|
/**
|
|
16
|
-
* Infer route input type
|
|
33
|
+
* Infer route input type from RouteDef
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* // Server route definition
|
|
38
|
+
* const getUser = route.get('/users/:id')
|
|
39
|
+
* .input({ params: Type.Object({ id: Type.String() }) })
|
|
40
|
+
* .handler(...);
|
|
41
|
+
*
|
|
42
|
+
* // Client: extract input type
|
|
43
|
+
* type Input = InferRouteInput<typeof getUser>;
|
|
44
|
+
* // { params: { id: string }, query: {}, body: {}, ... }
|
|
45
|
+
* ```
|
|
17
46
|
*/
|
|
18
47
|
type InferRouteInput<TRoute> = TRoute extends RouteDef<infer TInput, any, any> ? StructuredInput<TInput> : never;
|
|
19
48
|
/**
|
|
20
|
-
* Infer route output type
|
|
49
|
+
* Infer route output type from RouteDef
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* // Server route definition
|
|
54
|
+
* const getUser = route.get('/users/:id')
|
|
55
|
+
* .handler(async (c) => {
|
|
56
|
+
* return { id: '1', name: 'John' };
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* // Client: extract output type
|
|
60
|
+
* type Output = InferRouteOutput<typeof getUser>;
|
|
61
|
+
* // { id: string, name: string }
|
|
62
|
+
* ```
|
|
21
63
|
*/
|
|
22
64
|
type InferRouteOutput<TRoute> = TRoute extends RouteDef<any, any, infer TResponse> ? TResponse : never;
|
|
65
|
+
/**
|
|
66
|
+
* Extract routes from Router type
|
|
67
|
+
* Router<TRoutes> has routes in `_routes` property
|
|
68
|
+
*/
|
|
69
|
+
type ExtractRoutes<TRouter> = TRouter extends {
|
|
70
|
+
_routes: infer TRoutes;
|
|
71
|
+
} ? TRoutes : TRouter;
|
|
72
|
+
/**
|
|
73
|
+
* Extract output type for a specific route from router
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* import type { RouterOutput } from '@spfn/core/nextjs';
|
|
78
|
+
* import type { AppRouter } from '@/server/router';
|
|
79
|
+
*
|
|
80
|
+
* // Get output type for a specific route
|
|
81
|
+
* type ListData = RouterOutput<AppRouter, 'listExamples'>;
|
|
82
|
+
*
|
|
83
|
+
* // Use in props
|
|
84
|
+
* interface Props {
|
|
85
|
+
* data: RouterOutput<AppRouter, 'listExamples'>;
|
|
86
|
+
* }
|
|
87
|
+
*
|
|
88
|
+
* // Extract item type from paginated response
|
|
89
|
+
* type Example = RouterOutput<AppRouter, 'listExamples'>['items'][number];
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
type RouterOutput<TRouter, K extends keyof ExtractRoutes<TRouter>> = InferRouteOutput<ExtractRoutes<TRouter>[K]>;
|
|
93
|
+
/**
|
|
94
|
+
* Extract input type for a specific route from router
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* import type { RouterInput } from '@spfn/core/nextjs';
|
|
99
|
+
* import type { AppRouter } from '@/server/router';
|
|
100
|
+
*
|
|
101
|
+
* // Get input type for a specific route
|
|
102
|
+
* type CreateInput = RouterInput<AppRouter, 'createExample'>;
|
|
103
|
+
*
|
|
104
|
+
* // Use in function parameter
|
|
105
|
+
* function submitForm(data: RouterInput<AppRouter, 'createExample'>['body']) {
|
|
106
|
+
* // ...
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
type RouterInput<TRouter, K extends keyof ExtractRoutes<TRouter>> = InferRouteInput<ExtractRoutes<TRouter>[K]>;
|
|
23
111
|
/**
|
|
24
112
|
* Cookie options for setCookie
|
|
25
113
|
*/
|
|
@@ -71,7 +159,7 @@ interface ApiConfig {
|
|
|
71
159
|
/**
|
|
72
160
|
* Request timeout in milliseconds
|
|
73
161
|
*
|
|
74
|
-
* @default
|
|
162
|
+
* @default env.SERVER_TIMEOUT (120000)
|
|
75
163
|
*/
|
|
76
164
|
timeout?: number;
|
|
77
165
|
/**
|
|
@@ -114,6 +202,11 @@ interface ApiConfig {
|
|
|
114
202
|
* Per-call options
|
|
115
203
|
*/
|
|
116
204
|
interface CallOptions {
|
|
205
|
+
/**
|
|
206
|
+
* Request timeout in milliseconds
|
|
207
|
+
* Overrides the global timeout set in ApiConfig
|
|
208
|
+
*/
|
|
209
|
+
timeout?: number;
|
|
117
210
|
/**
|
|
118
211
|
* Additional headers for this request
|
|
119
212
|
*/
|
|
@@ -154,4 +247,4 @@ interface CallOptions {
|
|
|
154
247
|
};
|
|
155
248
|
}
|
|
156
249
|
|
|
157
|
-
export type { ApiConfig as A, CallOptions as C, InferRouteInput as I, RequestInterceptor as R,
|
|
250
|
+
export type { ApiConfig as A, CallOptions as C, InferRouteInput as I, RequestInterceptor as R, StructuredInput as S, ResponseInterceptor as a, InferRouteOutput as b, RouterInput as c, RouterOutput as d, CookieOptions as e, SetCookie as f };
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { Context } from 'hono';
|
|
2
|
+
import { E as EventRouterDef, I as InferEventNames, b as InferEventPayload } from './router-Di7ENoah.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* SSE Token Manager
|
|
6
|
+
*
|
|
7
|
+
* Auth-agnostic token issuance and verification for SSE connections.
|
|
8
|
+
* Issues one-time-use tokens with TTL for Token Exchange pattern.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const manager = new SSETokenManager({ ttl: 30000 });
|
|
13
|
+
*
|
|
14
|
+
* // Issue token for authenticated user
|
|
15
|
+
* const token = await manager.issue('user-123');
|
|
16
|
+
*
|
|
17
|
+
* // Verify and consume token (one-time use)
|
|
18
|
+
* const subject = await manager.verify(token); // 'user-123'
|
|
19
|
+
* const again = await manager.verify(token); // null (already consumed)
|
|
20
|
+
*
|
|
21
|
+
* // Cleanup on shutdown
|
|
22
|
+
* manager.destroy();
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Stored SSE token data
|
|
27
|
+
*/
|
|
28
|
+
interface SSEToken {
|
|
29
|
+
token: string;
|
|
30
|
+
subject: string;
|
|
31
|
+
expiresAt: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Token storage interface
|
|
35
|
+
*
|
|
36
|
+
* Implement this for custom storage backends (e.g., Redis for multi-instance).
|
|
37
|
+
*/
|
|
38
|
+
interface SSETokenStore {
|
|
39
|
+
/** Store a token */
|
|
40
|
+
set(token: string, data: SSEToken): Promise<void>;
|
|
41
|
+
/** Get and delete a token (one-time use) */
|
|
42
|
+
consume(token: string): Promise<SSEToken | null>;
|
|
43
|
+
/** Remove expired tokens */
|
|
44
|
+
cleanup(): Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* SSETokenManager configuration
|
|
48
|
+
*/
|
|
49
|
+
interface SSETokenManagerConfig {
|
|
50
|
+
/**
|
|
51
|
+
* Token time-to-live in milliseconds
|
|
52
|
+
* @default 30000
|
|
53
|
+
*/
|
|
54
|
+
ttl?: number;
|
|
55
|
+
/**
|
|
56
|
+
* Custom token store (default: in-memory Map)
|
|
57
|
+
*/
|
|
58
|
+
store?: SSETokenStore;
|
|
59
|
+
/**
|
|
60
|
+
* Cleanup interval in milliseconds
|
|
61
|
+
* @default 60000
|
|
62
|
+
*/
|
|
63
|
+
cleanupInterval?: number;
|
|
64
|
+
}
|
|
65
|
+
declare class SSETokenManager {
|
|
66
|
+
private store;
|
|
67
|
+
private ttl;
|
|
68
|
+
private cleanupTimer;
|
|
69
|
+
constructor(config?: SSETokenManagerConfig);
|
|
70
|
+
/**
|
|
71
|
+
* Issue a new one-time-use token for the given subject
|
|
72
|
+
*/
|
|
73
|
+
issue(subject: string): Promise<string>;
|
|
74
|
+
/**
|
|
75
|
+
* Verify and consume a token
|
|
76
|
+
* @returns subject string if valid, null if invalid/expired/already consumed
|
|
77
|
+
*/
|
|
78
|
+
verify(token: string): Promise<string | null>;
|
|
79
|
+
/**
|
|
80
|
+
* Cleanup timer and resources
|
|
81
|
+
*/
|
|
82
|
+
destroy(): void;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* SSE Types
|
|
87
|
+
*
|
|
88
|
+
* Type definitions for Server-Sent Events
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* SSE message sent from server
|
|
93
|
+
*/
|
|
94
|
+
interface SSEMessage<TEvent extends string = string, TPayload = unknown> {
|
|
95
|
+
/** Event name */
|
|
96
|
+
event: TEvent;
|
|
97
|
+
/** Event payload */
|
|
98
|
+
data: TPayload;
|
|
99
|
+
/** Optional message ID for reconnection */
|
|
100
|
+
id?: string;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* SSE auth configuration (internal, non-generic)
|
|
104
|
+
*
|
|
105
|
+
* Stored in SSEHandlerConfig. Generic user-facing version is SSEAuthConfig.
|
|
106
|
+
*/
|
|
107
|
+
interface SSEHandlerAuthConfig {
|
|
108
|
+
/**
|
|
109
|
+
* Enable SSE token authentication
|
|
110
|
+
* @default false
|
|
111
|
+
*/
|
|
112
|
+
enabled?: boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Token TTL in milliseconds
|
|
115
|
+
* @default 30000
|
|
116
|
+
*/
|
|
117
|
+
tokenTtl?: number;
|
|
118
|
+
/**
|
|
119
|
+
* Custom token store (e.g., Redis for multi-instance)
|
|
120
|
+
*/
|
|
121
|
+
store?: SSETokenStore;
|
|
122
|
+
/**
|
|
123
|
+
* Extract subject (user ID) from Hono context
|
|
124
|
+
* @default (c) => c.get('auth')?.userId ?? null
|
|
125
|
+
*/
|
|
126
|
+
getSubject?: (c: Context) => string | null;
|
|
127
|
+
/**
|
|
128
|
+
* Subscription authorization hook (called once on connect)
|
|
129
|
+
*
|
|
130
|
+
* Return allowed events subset. Empty array = 403 rejection.
|
|
131
|
+
*/
|
|
132
|
+
authorize?: (subject: string, events: string[]) => Promise<string[]> | string[];
|
|
133
|
+
/**
|
|
134
|
+
* Per-event payload filter map (called on every event emission)
|
|
135
|
+
*
|
|
136
|
+
* Return false to skip sending the event to this user.
|
|
137
|
+
*/
|
|
138
|
+
filter?: Record<string, (subject: string, payload: unknown) => boolean>;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* SSE auth configuration (user-facing, generic)
|
|
142
|
+
*
|
|
143
|
+
* Provides type-safe event names and payload inference from EventRouter.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* .events(eventRouter, {
|
|
148
|
+
* auth: {
|
|
149
|
+
* enabled: true,
|
|
150
|
+
* authorize: async (subject, events) => {
|
|
151
|
+
* // events: ('userCreated' | 'orderUpdated')[]
|
|
152
|
+
* return events.filter(e => hasPermission(subject, e));
|
|
153
|
+
* },
|
|
154
|
+
* filter: {
|
|
155
|
+
* orderUpdated: (subject, payload) => {
|
|
156
|
+
* // payload: { orderId: string; userId: string }
|
|
157
|
+
* return payload.userId === subject;
|
|
158
|
+
* },
|
|
159
|
+
* },
|
|
160
|
+
* },
|
|
161
|
+
* })
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
interface SSEAuthConfig<TRouter extends EventRouterDef<any>> {
|
|
165
|
+
enabled?: boolean;
|
|
166
|
+
tokenTtl?: number;
|
|
167
|
+
store?: SSETokenStore;
|
|
168
|
+
getSubject?: (c: Context) => string | null;
|
|
169
|
+
authorize?: (subject: string, events: InferEventNames<TRouter>[]) => Promise<InferEventNames<TRouter>[]> | InferEventNames<TRouter>[];
|
|
170
|
+
filter?: {
|
|
171
|
+
[K in InferEventNames<TRouter>]?: (subject: string, payload: InferEventPayload<TRouter, K>) => boolean;
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* SSE Handler configuration
|
|
176
|
+
*/
|
|
177
|
+
interface SSEHandlerConfig {
|
|
178
|
+
/**
|
|
179
|
+
* Keep-alive ping interval in milliseconds
|
|
180
|
+
* @default 30000
|
|
181
|
+
*/
|
|
182
|
+
pingInterval?: number;
|
|
183
|
+
/**
|
|
184
|
+
* Custom headers for SSE response
|
|
185
|
+
*/
|
|
186
|
+
headers?: Record<string, string>;
|
|
187
|
+
/**
|
|
188
|
+
* Authentication and authorization configuration
|
|
189
|
+
*/
|
|
190
|
+
auth?: SSEHandlerAuthConfig;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* SSE Client configuration
|
|
194
|
+
*/
|
|
195
|
+
interface SSEClientConfig {
|
|
196
|
+
/**
|
|
197
|
+
* Backend API host URL
|
|
198
|
+
* @default NEXT_PUBLIC_SPFN_API_URL || 'http://localhost:8790'
|
|
199
|
+
* @example 'http://localhost:8790'
|
|
200
|
+
* @example 'https://api.example.com'
|
|
201
|
+
*/
|
|
202
|
+
host?: string;
|
|
203
|
+
/**
|
|
204
|
+
* SSE endpoint pathname
|
|
205
|
+
* @default '/events/stream'
|
|
206
|
+
*/
|
|
207
|
+
pathname?: string;
|
|
208
|
+
/**
|
|
209
|
+
* Full URL (overrides host + pathname)
|
|
210
|
+
* @deprecated Use host and pathname instead
|
|
211
|
+
* @example 'http://localhost:8790/events/stream'
|
|
212
|
+
*/
|
|
213
|
+
url?: string;
|
|
214
|
+
/**
|
|
215
|
+
* Auto reconnect on disconnect
|
|
216
|
+
* @default true
|
|
217
|
+
*/
|
|
218
|
+
reconnect?: boolean;
|
|
219
|
+
/**
|
|
220
|
+
* Reconnect delay in milliseconds
|
|
221
|
+
* @default 3000
|
|
222
|
+
*/
|
|
223
|
+
reconnectDelay?: number;
|
|
224
|
+
/**
|
|
225
|
+
* Maximum reconnect attempts (0 = infinite)
|
|
226
|
+
* @default 0
|
|
227
|
+
*/
|
|
228
|
+
maxReconnectAttempts?: number;
|
|
229
|
+
/**
|
|
230
|
+
* Include credentials (cookies) in request
|
|
231
|
+
* @default false
|
|
232
|
+
*/
|
|
233
|
+
withCredentials?: boolean;
|
|
234
|
+
/**
|
|
235
|
+
* Acquire a one-time SSE token before connecting.
|
|
236
|
+
*
|
|
237
|
+
* Called on every (re)connect. The returned token is appended
|
|
238
|
+
* to the SSE URL as `?token=...`.
|
|
239
|
+
*
|
|
240
|
+
* For automatic token acquisition via RPC proxy, use `createAuthSSEClient` instead.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* // Recommended: use createAuthSSEClient for automatic token handling
|
|
245
|
+
* import { createAuthSSEClient } from '@spfn/core/event/sse/client';
|
|
246
|
+
* const client = createAuthSSEClient<EventRouter>();
|
|
247
|
+
*
|
|
248
|
+
* // Manual: provide acquireToken directly
|
|
249
|
+
* acquireToken: async () => {
|
|
250
|
+
* const res = await fetch('/api/rpc/eventsToken', {
|
|
251
|
+
* method: 'POST',
|
|
252
|
+
* credentials: 'include',
|
|
253
|
+
* });
|
|
254
|
+
* const data = await res.json();
|
|
255
|
+
* return data.token;
|
|
256
|
+
* }
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
acquireToken?: () => Promise<string>;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Event handler function
|
|
263
|
+
*/
|
|
264
|
+
type SSEEventHandler<TPayload> = (payload: TPayload) => void;
|
|
265
|
+
/**
|
|
266
|
+
* Event handlers map for EventRouter
|
|
267
|
+
*/
|
|
268
|
+
type SSEEventHandlers<TRouter extends EventRouterDef<any>> = {
|
|
269
|
+
[K in InferEventNames<TRouter>]?: SSEEventHandler<InferEventPayload<TRouter, K>>;
|
|
270
|
+
};
|
|
271
|
+
/**
|
|
272
|
+
* Subscription options
|
|
273
|
+
*/
|
|
274
|
+
interface SSESubscribeOptions<TRouter extends EventRouterDef<any>> {
|
|
275
|
+
/**
|
|
276
|
+
* Events to subscribe
|
|
277
|
+
*/
|
|
278
|
+
events: InferEventNames<TRouter>[];
|
|
279
|
+
/**
|
|
280
|
+
* Event handlers
|
|
281
|
+
*/
|
|
282
|
+
handlers: SSEEventHandlers<TRouter>;
|
|
283
|
+
/**
|
|
284
|
+
* Called when connection opens
|
|
285
|
+
*/
|
|
286
|
+
onOpen?: () => void;
|
|
287
|
+
/**
|
|
288
|
+
* Called on connection error
|
|
289
|
+
*/
|
|
290
|
+
onError?: (error: Event) => void;
|
|
291
|
+
/**
|
|
292
|
+
* Called when reconnecting
|
|
293
|
+
*/
|
|
294
|
+
onReconnect?: (attempt: number) => void;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* SSE connection state
|
|
298
|
+
*/
|
|
299
|
+
type SSEConnectionState = 'connecting' | 'open' | 'closed' | 'error';
|
|
300
|
+
/**
|
|
301
|
+
* Unsubscribe function
|
|
302
|
+
*/
|
|
303
|
+
type SSEUnsubscribe = () => void;
|
|
304
|
+
|
|
305
|
+
export { type SSEHandlerConfig as S, type SSEAuthConfig as a, SSETokenManager as b, type SSEToken as c, type SSETokenStore as d, type SSETokenManagerConfig as e, type SSEMessage as f, type SSEHandlerAuthConfig as g, type SSEClientConfig as h, type SSEEventHandler as i, type SSEEventHandlers as j, type SSESubscribeOptions as k, type SSEConnectionState as l, type SSEUnsubscribe as m };
|
package/docs/cache.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# Cache
|
|
2
|
+
|
|
3
|
+
Redis caching with type-safe operations.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
REDIS_URL=redis://localhost:6379
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Basic Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { cache } from '@spfn/core/cache';
|
|
15
|
+
|
|
16
|
+
// Set
|
|
17
|
+
await cache.set('user:123', { id: '123', name: 'John' });
|
|
18
|
+
await cache.set('session:abc', data, { ttl: 3600 }); // 1 hour
|
|
19
|
+
|
|
20
|
+
// Get
|
|
21
|
+
const user = await cache.get<User>('user:123');
|
|
22
|
+
|
|
23
|
+
// Delete
|
|
24
|
+
await cache.del('user:123');
|
|
25
|
+
|
|
26
|
+
// Check existence
|
|
27
|
+
const exists = await cache.exists('user:123');
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## TTL Options
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
// Set with TTL (seconds)
|
|
34
|
+
await cache.set('key', value, { ttl: 60 }); // 1 minute
|
|
35
|
+
await cache.set('key', value, { ttl: 3600 }); // 1 hour
|
|
36
|
+
await cache.set('key', value, { ttl: 86400 }); // 1 day
|
|
37
|
+
|
|
38
|
+
// No expiration
|
|
39
|
+
await cache.set('key', value); // Permanent until deleted
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Patterns
|
|
43
|
+
|
|
44
|
+
### Cache-Aside
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
async function getUser(id: string): Promise<User>
|
|
48
|
+
{
|
|
49
|
+
const cached = await cache.get<User>(`user:${id}`);
|
|
50
|
+
if (cached)
|
|
51
|
+
{
|
|
52
|
+
return cached;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const user = await userRepo.findById(id);
|
|
56
|
+
if (user)
|
|
57
|
+
{
|
|
58
|
+
await cache.set(`user:${id}`, user, { ttl: 3600 });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return user;
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Cache Invalidation
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
async function updateUser(id: string, data: Partial<User>)
|
|
69
|
+
{
|
|
70
|
+
const user = await userRepo.update(id, data);
|
|
71
|
+
await cache.del(`user:${id}`); // Invalidate cache
|
|
72
|
+
return user;
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Cache with Prefix
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const userCache = cache.prefix('user');
|
|
80
|
+
|
|
81
|
+
await userCache.set('123', user); // Key: user:123
|
|
82
|
+
await userCache.get('123');
|
|
83
|
+
await userCache.del('123');
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Hash Operations
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
// Set hash field
|
|
90
|
+
await cache.hset('user:123', 'name', 'John');
|
|
91
|
+
|
|
92
|
+
// Get hash field
|
|
93
|
+
const name = await cache.hget('user:123', 'name');
|
|
94
|
+
|
|
95
|
+
// Get all hash fields
|
|
96
|
+
const user = await cache.hgetall('user:123');
|
|
97
|
+
|
|
98
|
+
// Delete hash field
|
|
99
|
+
await cache.hdel('user:123', 'name');
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## List Operations
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// Push to list
|
|
106
|
+
await cache.lpush('queue', item);
|
|
107
|
+
await cache.rpush('queue', item);
|
|
108
|
+
|
|
109
|
+
// Pop from list
|
|
110
|
+
const item = await cache.lpop('queue');
|
|
111
|
+
const item = await cache.rpop('queue');
|
|
112
|
+
|
|
113
|
+
// Get list range
|
|
114
|
+
const items = await cache.lrange('queue', 0, -1); // All items
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Best Practices
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
// 1. Use consistent key naming
|
|
121
|
+
`user:${id}`
|
|
122
|
+
`session:${token}`
|
|
123
|
+
`cache:posts:${page}`
|
|
124
|
+
|
|
125
|
+
// 2. Set appropriate TTL
|
|
126
|
+
{ ttl: 300 } // 5 min for frequently changing data
|
|
127
|
+
{ ttl: 3600 } // 1 hour for stable data
|
|
128
|
+
{ ttl: 86400 } // 1 day for rarely changing data
|
|
129
|
+
|
|
130
|
+
// 3. Invalidate on write
|
|
131
|
+
await userRepo.update(id, data);
|
|
132
|
+
await cache.del(`user:${id}`);
|
|
133
|
+
```
|
package/docs/codegen.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Codegen
|
|
2
|
+
|
|
3
|
+
Automatic API client generation from route definitions.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
### Configure Generator
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
// codegen.config.ts
|
|
11
|
+
import { defineCodegenConfig } from '@spfn/core/codegen';
|
|
12
|
+
|
|
13
|
+
export default defineCodegenConfig({
|
|
14
|
+
generators: [
|
|
15
|
+
{
|
|
16
|
+
name: 'api-client',
|
|
17
|
+
output: './src/client/api.ts',
|
|
18
|
+
router: './src/server/server.config.ts'
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Generate Client
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# Generate once
|
|
28
|
+
pnpm spfn codegen run
|
|
29
|
+
|
|
30
|
+
# Watch mode (dev server includes this)
|
|
31
|
+
pnpm spfn:dev
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Generated Client
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
// src/client/api.ts (generated)
|
|
38
|
+
import { createApi } from '@spfn/core/nextjs';
|
|
39
|
+
import type { AppRouter } from '@/server/server.config';
|
|
40
|
+
|
|
41
|
+
export const api = createApi<AppRouter>();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { api } from '@/client/api';
|
|
48
|
+
|
|
49
|
+
// Type-safe API calls
|
|
50
|
+
const user = await api.getUser.call({
|
|
51
|
+
params: { id: '123' }
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const users = await api.getUsers.call({
|
|
55
|
+
query: { page: 1, limit: 20 }
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const created = await api.createUser.call({
|
|
59
|
+
body: { email: 'user@example.com', name: 'User' }
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## CLI Commands
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
# Generate API client
|
|
67
|
+
pnpm spfn codegen run
|
|
68
|
+
|
|
69
|
+
# List registered generators
|
|
70
|
+
pnpm spfn codegen list
|
|
71
|
+
|
|
72
|
+
# Run specific generator
|
|
73
|
+
pnpm spfn codegen run --name api-client
|
|
74
|
+
```
|