@coherent.js/fastify 1.0.0-beta.2

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,362 @@
1
+ /**
2
+ * Coherent.js Fastify Integration Types
3
+ * TypeScript definitions for Fastify framework integration
4
+ *
5
+ * @version 1.0.0-beta.1
6
+ */
7
+
8
+ import { FastifyInstance, FastifyPluginOptions, FastifyRequest, FastifyReply, FastifyPluginCallback } from 'fastify';
9
+ import { CoherentNode } from '@coherent/core';
10
+
11
+ // ============================================================================
12
+ // Fastify Plugin Types
13
+ // ============================================================================
14
+
15
+ /** Coherent Fastify plugin options */
16
+ export interface CoherentFastifyOptions extends FastifyPluginOptions {
17
+ enablePerformanceMonitoring?: boolean;
18
+ template?: string;
19
+ cache?: boolean;
20
+ development?: boolean;
21
+ renderOptions?: {
22
+ pretty?: boolean;
23
+ doctype?: string;
24
+ };
25
+ errorHandler?: (error: Error, request: FastifyRequest, reply: FastifyReply) => void | Promise<void>;
26
+ }
27
+
28
+ /** Enhanced Fastify reply with Coherent.js methods */
29
+ export interface CoherentFastifyReply extends FastifyReply {
30
+ isCoherentObject(obj: any): boolean;
31
+ coherent(component: CoherentNode, renderOptions?: RenderOptions): void;
32
+ renderComponent<P = any>(component: (props: P) => CoherentNode, props?: P): string;
33
+ sendComponent<P = any>(component: (props: P) => CoherentNode, props?: P): void;
34
+ streamComponent<P = any>(component: (props: P) => CoherentNode, props?: P): void;
35
+ }
36
+
37
+ /** Enhanced Fastify request with Coherent.js utilities */
38
+ export interface CoherentFastifyRequest extends FastifyRequest {
39
+ getComponent<P = any>(name: string, props?: P): CoherentNode | undefined;
40
+ coherentState?: any;
41
+ setCoherentState?(state: any): void;
42
+ getCoherentState?(): any;
43
+ }
44
+
45
+ /** Render options for Coherent.js components */
46
+ export interface RenderOptions {
47
+ enablePerformanceMonitoring?: boolean;
48
+ template?: string;
49
+ cache?: boolean;
50
+ minify?: boolean;
51
+ pretty?: boolean;
52
+ doctype?: string;
53
+ }
54
+
55
+ /** Fastify instance with Coherent.js support */
56
+ export interface CoherentFastifyInstance extends FastifyInstance {
57
+ coherent: {
58
+ render(component: CoherentNode, options?: RenderOptions): string;
59
+ renderToStream(component: CoherentNode, options?: RenderOptions): ReadableStream;
60
+ registerComponent(name: string, component: CoherentNode | ((props: any) => CoherentNode)): void;
61
+ getComponent(name: string): CoherentNode | ((props: any) => CoherentNode) | undefined;
62
+ };
63
+ }
64
+
65
+ // ============================================================================
66
+ // Route Handler Types
67
+ // ============================================================================
68
+
69
+ /** Component factory for route handlers */
70
+ export type ComponentFactory<P = any> = (
71
+ request: CoherentFastifyRequest,
72
+ reply: CoherentFastifyReply
73
+ ) => CoherentNode | Promise<CoherentNode>;
74
+
75
+ /** Handler options for component routes */
76
+ export interface CoherentHandlerOptions {
77
+ enablePerformanceMonitoring?: boolean;
78
+ template?: string;
79
+ cache?: boolean;
80
+ cacheKey?: (request: FastifyRequest) => string;
81
+ cacheTTL?: number;
82
+ layout?: (props: any) => CoherentNode;
83
+ meta?: {
84
+ title?: string;
85
+ description?: string;
86
+ keywords?: string[];
87
+ };
88
+ }
89
+
90
+ /** Route handler with Coherent.js component rendering */
91
+ export type CoherentRouteHandler = (
92
+ request: CoherentFastifyRequest,
93
+ reply: CoherentFastifyReply
94
+ ) => void | Promise<void> | CoherentNode | Promise<CoherentNode>;
95
+
96
+ /** Component route configuration */
97
+ export interface ComponentRoute {
98
+ url: string;
99
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
100
+ component: ComponentFactory;
101
+ schema?: any;
102
+ preHandler?: any;
103
+ handler?: CoherentRouteHandler;
104
+ options?: CoherentHandlerOptions;
105
+ }
106
+
107
+ // ============================================================================
108
+ // Middleware and Hooks Types
109
+ // ============================================================================
110
+
111
+ /** Performance monitoring configuration */
112
+ export interface PerformanceConfig {
113
+ enabled?: boolean;
114
+ threshold?: number;
115
+ logSlow?: boolean;
116
+ metrics?: {
117
+ renderTime?: boolean;
118
+ componentCount?: boolean;
119
+ htmlSize?: boolean;
120
+ };
121
+ }
122
+
123
+ /** SSR configuration for Fastify */
124
+ export interface FastifySSRConfig {
125
+ enabled?: boolean;
126
+ cache?: boolean;
127
+ cacheMaxAge?: number;
128
+ bundlePath?: string;
129
+ templatePath?: string;
130
+ clientManifest?: any;
131
+ serverBundle?: any;
132
+ renderToStream?: boolean;
133
+ preload?: string[];
134
+ }
135
+
136
+ /** SSR context for Fastify */
137
+ export interface FastifySSRContext {
138
+ request: FastifyRequest;
139
+ reply: FastifyReply;
140
+ url: string;
141
+ params?: Record<string, any>;
142
+ query?: Record<string, any>;
143
+ state: any;
144
+ meta: {
145
+ title?: string;
146
+ description?: string;
147
+ keywords?: string[];
148
+ og?: Record<string, string>;
149
+ twitter?: Record<string, string>;
150
+ };
151
+ assets: {
152
+ css: string[];
153
+ js: string[];
154
+ preload: string[];
155
+ };
156
+ }
157
+
158
+ // ============================================================================
159
+ // Plugin System Types
160
+ // ============================================================================
161
+
162
+ /** Coherent Fastify plugin */
163
+ export interface CoherentFastifyPlugin extends FastifyPluginCallback<CoherentFastifyOptions> {
164
+ coherentFastify: symbol;
165
+ options?: CoherentFastifyOptions;
166
+ }
167
+
168
+ /** Component registry for Fastify */
169
+ export interface ComponentRegistry {
170
+ register(name: string, component: CoherentNode | ComponentFactory): void;
171
+ unregister(name: string): boolean;
172
+ get(name: string): (CoherentNode | ComponentFactory) | undefined;
173
+ has(name: string): boolean;
174
+ list(): string[];
175
+ clear(): void;
176
+ }
177
+
178
+ // ============================================================================
179
+ // Decorators and Extensions
180
+ // ============================================================================
181
+
182
+ /** Fastify decorators added by Coherent.js */
183
+ export interface CoherentDecorators {
184
+ request: {
185
+ coherentState: any;
186
+ getComponent: <P = any>(name: string, props?: P) => CoherentNode | undefined;
187
+ };
188
+ reply: {
189
+ isCoherentObject: (obj: any) => boolean;
190
+ coherent: (component: CoherentNode, options?: RenderOptions) => void;
191
+ renderComponent: <P = any>(component: (props: P) => CoherentNode, props?: P) => string;
192
+ };
193
+ instance: {
194
+ coherent: {
195
+ render: (component: CoherentNode, options?: RenderOptions) => string;
196
+ renderToStream: (component: CoherentNode, options?: RenderOptions) => ReadableStream;
197
+ registry: ComponentRegistry;
198
+ };
199
+ };
200
+ }
201
+
202
+ // ============================================================================
203
+ // Error Handling Types
204
+ // ============================================================================
205
+
206
+ /** Coherent error handler for Fastify */
207
+ export type CoherentErrorHandler = (
208
+ error: Error,
209
+ request: FastifyRequest,
210
+ reply: FastifyReply
211
+ ) => void | Promise<void>;
212
+
213
+ /** Error page component props */
214
+ export interface ErrorPageProps {
215
+ error: Error;
216
+ statusCode: number;
217
+ message: string;
218
+ stack?: string;
219
+ request: {
220
+ url: string;
221
+ method: string;
222
+ headers: Record<string, string | string[] | undefined>;
223
+ };
224
+ }
225
+
226
+ // ============================================================================
227
+ // Development Features
228
+ // ============================================================================
229
+
230
+ /** Development mode options */
231
+ export interface DevModeOptions {
232
+ enabled?: boolean;
233
+ errorOverlay?: boolean;
234
+ hmr?: boolean;
235
+ logging?: {
236
+ renderTime?: boolean;
237
+ componentTree?: boolean;
238
+ stateChanges?: boolean;
239
+ };
240
+ }
241
+
242
+ /** HMR configuration for Fastify */
243
+ export interface FastifyHMRConfig {
244
+ enabled?: boolean;
245
+ port?: number;
246
+ path?: string;
247
+ websocket?: boolean;
248
+ clientEntry?: string;
249
+ }
250
+
251
+ // ============================================================================
252
+ // Main Functions
253
+ // ============================================================================
254
+
255
+ /** Coherent.js Fastify plugin */
256
+ export const coherentFastify: CoherentFastifyPlugin;
257
+
258
+ /** Setup Coherent.js with Fastify instance */
259
+ export function setupCoherent(
260
+ fastify: FastifyInstance,
261
+ options?: CoherentFastifyOptions
262
+ ): void;
263
+
264
+ /** Create a Fastify route handler for Coherent.js components */
265
+ export function createHandler(
266
+ componentFactory: ComponentFactory,
267
+ options?: CoherentHandlerOptions
268
+ ): CoherentRouteHandler;
269
+
270
+ /** Register component routes */
271
+ export function registerComponentRoutes(
272
+ fastify: FastifyInstance,
273
+ routes: ComponentRoute[]
274
+ ): void;
275
+
276
+ /** Create SSR middleware for Fastify */
277
+ export function ssrMiddleware(config?: FastifySSRConfig): FastifyPluginCallback;
278
+
279
+ /** Create error handler for Coherent.js */
280
+ export function createErrorHandler(options?: {
281
+ showStack?: boolean;
282
+ logErrors?: boolean;
283
+ errorComponent?: (props: ErrorPageProps) => CoherentNode;
284
+ }): CoherentErrorHandler;
285
+
286
+ /** Create component registry */
287
+ export function createComponentRegistry(): ComponentRegistry;
288
+
289
+ /** Performance monitoring plugin */
290
+ export function performancePlugin(config?: PerformanceConfig): FastifyPluginCallback;
291
+
292
+ /** Development mode plugin */
293
+ export function devModePlugin(options?: DevModeOptions): FastifyPluginCallback;
294
+
295
+ /** HMR plugin for development */
296
+ export function hmrPlugin(config?: FastifyHMRConfig): FastifyPluginCallback;
297
+
298
+ // ============================================================================
299
+ // Utility Functions
300
+ // ============================================================================
301
+
302
+ /** Create enhanced Fastify app with Coherent.js */
303
+ export function createCoherentApp(options?: CoherentFastifyOptions): CoherentFastifyInstance;
304
+
305
+ /** Check if object is a Coherent component */
306
+ export function isCoherentObject(obj: any): boolean;
307
+
308
+ /** Render component to HTML string */
309
+ export function renderComponent(component: CoherentNode, options?: RenderOptions): string;
310
+
311
+ /** Render component to stream */
312
+ export function renderToStream(component: CoherentNode, options?: RenderOptions): ReadableStream;
313
+
314
+ /** Cache utilities */
315
+ export const cache: {
316
+ get(key: string): string | undefined;
317
+ set(key: string, value: string, ttl?: number): void;
318
+ has(key: string): boolean;
319
+ delete(key: string): boolean;
320
+ clear(): void;
321
+ size(): number;
322
+ };
323
+
324
+ // ============================================================================
325
+ // Type Guards
326
+ // ============================================================================
327
+
328
+ /** Check if reply has Coherent.js decorators */
329
+ export function isCoherentReply(reply: FastifyReply): reply is CoherentFastifyReply;
330
+
331
+ /** Check if request has Coherent.js decorators */
332
+ export function isCoherentRequest(request: FastifyRequest): request is CoherentFastifyRequest;
333
+
334
+ /** Check if instance has Coherent.js support */
335
+ export function isCoherentInstance(fastify: FastifyInstance): fastify is CoherentFastifyInstance;
336
+
337
+ // ============================================================================
338
+ // Default Export
339
+ // ============================================================================
340
+
341
+ declare const coherentFastifyPlugin: {
342
+ coherentFastify: typeof coherentFastify;
343
+ setupCoherent: typeof setupCoherent;
344
+ createHandler: typeof createHandler;
345
+ registerComponentRoutes: typeof registerComponentRoutes;
346
+ ssrMiddleware: typeof ssrMiddleware;
347
+ createErrorHandler: typeof createErrorHandler;
348
+ createComponentRegistry: typeof createComponentRegistry;
349
+ performancePlugin: typeof performancePlugin;
350
+ devModePlugin: typeof devModePlugin;
351
+ hmrPlugin: typeof hmrPlugin;
352
+ createCoherentApp: typeof createCoherentApp;
353
+ isCoherentObject: typeof isCoherentObject;
354
+ renderComponent: typeof renderComponent;
355
+ renderToStream: typeof renderToStream;
356
+ cache: typeof cache;
357
+ isCoherentReply: typeof isCoherentReply;
358
+ isCoherentRequest: typeof isCoherentRequest;
359
+ isCoherentInstance: typeof isCoherentInstance;
360
+ };
361
+
362
+ export default coherentFastifyPlugin;