@hyperspan/framework 1.0.24 → 1.0.25

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/src/types.ts CHANGED
@@ -9,18 +9,60 @@ export namespace Hyperspan {
9
9
  _config: Hyperspan.Config;
10
10
  _routes: Array<Hyperspan.Route>;
11
11
  _middleware: Record<Hyperspan.MiddlewareMethod, Array<Hyperspan.MiddlewareFunction>>;
12
- use: (middleware: Hyperspan.MiddlewareFunction, opts?: Hyperspan.MiddlewareMethodOptions) => Hyperspan.Server;
13
- get: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
14
- post: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
15
- put: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
16
- patch: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
17
- delete: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
18
- options: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
19
- all: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
20
- };
12
+ use: (
13
+ middleware: Hyperspan.MiddlewareFunction,
14
+ opts?: Hyperspan.MiddlewareMethodOptions
15
+ ) => Hyperspan.Server;
16
+ get: (
17
+ path: string,
18
+ handler: Hyperspan.RouteHandler,
19
+ handlerOptions?: Hyperspan.RouteHandlerOptions
20
+ ) => Hyperspan.Route;
21
+ post: (
22
+ path: string,
23
+ handler: Hyperspan.RouteHandler,
24
+ handlerOptions?: Hyperspan.RouteHandlerOptions
25
+ ) => Hyperspan.Route;
26
+ put: (
27
+ path: string,
28
+ handler: Hyperspan.RouteHandler,
29
+ handlerOptions?: Hyperspan.RouteHandlerOptions
30
+ ) => Hyperspan.Route;
31
+ patch: (
32
+ path: string,
33
+ handler: Hyperspan.RouteHandler,
34
+ handlerOptions?: Hyperspan.RouteHandlerOptions
35
+ ) => Hyperspan.Route;
36
+ delete: (
37
+ path: string,
38
+ handler: Hyperspan.RouteHandler,
39
+ handlerOptions?: Hyperspan.RouteHandlerOptions
40
+ ) => Hyperspan.Route;
41
+ options: (
42
+ path: string,
43
+ handler: Hyperspan.RouteHandler,
44
+ handlerOptions?: Hyperspan.RouteHandlerOptions
45
+ ) => Hyperspan.Route;
46
+ all: (
47
+ path: string,
48
+ handler: Hyperspan.RouteHandler,
49
+ handlerOptions?: Hyperspan.RouteHandlerOptions
50
+ ) => Hyperspan.Route;
51
+ }
21
52
 
22
53
  export type Plugin = (config: Hyperspan.Config) => Promise<void> | void;
23
54
 
55
+ export type DisableStreamingFn = (
56
+ context: Hyperspan.Context,
57
+ options: {
58
+ hyperspanDisableStreaming: (context: Hyperspan.Context) => boolean;
59
+ }
60
+ ) => boolean;
61
+
62
+ export type ResponseOptions = {
63
+ disableStreaming?: DisableStreamingFn;
64
+ };
65
+
24
66
  export type Config = {
25
67
  appDir: string;
26
68
  publicDir: string;
@@ -28,9 +70,7 @@ export namespace Hyperspan {
28
70
  // For customizing the routes and adding your own...
29
71
  beforeRoutesAdded?: (server: Hyperspan.Server) => void;
30
72
  afterRoutesAdded?: (server: Hyperspan.Server) => void;
31
- responseOptions?: {
32
- disableStreaming?: (context: Hyperspan.Context) => boolean;
33
- };
73
+ responseOptions?: ResponseOptions;
34
74
  };
35
75
 
36
76
  export type CookieOptions = {
@@ -51,7 +91,7 @@ export namespace Hyperspan {
51
91
  get: (name: string) => string | undefined;
52
92
  set: (name: string, value: string, options?: CookieOptions) => void;
53
93
  delete: (name: string) => void;
54
- }
94
+ };
55
95
 
56
96
  export type HSRequest = {
57
97
  url: URL;
@@ -84,7 +124,7 @@ export namespace Hyperspan {
84
124
  route: RouteConfig;
85
125
  req: HSRequest;
86
126
  res: HSResponse;
87
- };
127
+ }
88
128
 
89
129
  export type ClientIslandOptions = {
90
130
  ssr?: boolean;
@@ -96,9 +136,7 @@ export namespace Hyperspan {
96
136
  path: string;
97
137
  params: Record<string, string | undefined>;
98
138
  cssImports: string[];
99
- responseOptions?: {
100
- disableStreaming?: (context: Hyperspan.Context) => boolean;
101
- };
139
+ responseOptions?: ResponseOptions;
102
140
  };
103
141
 
104
142
  export type RouteHandlerReturn =
@@ -117,17 +155,20 @@ export namespace Hyperspan {
117
155
  ) => RouteHandlerReturn | Promise<RouteHandlerReturn>;
118
156
  export type RouteHandlerOptions = {
119
157
  middleware?: Hyperspan.MiddlewareFunction[];
120
- }
158
+ };
121
159
 
122
160
  // TypeScript inference for typed route params
123
161
  // Source - https://stackoverflow.com/a/78170543
124
162
  // Posted by jcalz
125
163
  // Retrieved 2025-11-12, License - CC BY-SA 4.0
126
- export type RouteParamsParser<T extends string, A = unknown> =
127
- T extends `${string}:${infer F}/${infer R}` ? RouteParamsParser<R, A & Record<F, string>> :
128
- (A & (T extends `${string}:${infer F}` ? Record<F, string> : unknown)) extends
129
- infer U ? { [K in keyof U]: U[K] } : never
130
-
164
+ export type RouteParamsParser<
165
+ T extends string,
166
+ A = unknown,
167
+ > = T extends `${string}:${infer F}/${infer R}`
168
+ ? RouteParamsParser<R, A & Record<F, string>>
169
+ : A & (T extends `${string}:${infer F}` ? Record<F, string> : unknown) extends infer U
170
+ ? { [K in keyof U]: U[K] }
171
+ : never;
131
172
 
132
173
  /**
133
174
  * Next function type for middleware
@@ -147,7 +188,15 @@ export namespace Hyperspan {
147
188
  context: Hyperspan.Context,
148
189
  next: Hyperspan.NextFunction
149
190
  ) => Promise<Response> | Response;
150
- export type MiddlewareMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | '*';
191
+ export type MiddlewareMethod =
192
+ | 'GET'
193
+ | 'POST'
194
+ | 'PUT'
195
+ | 'PATCH'
196
+ | 'DELETE'
197
+ | 'HEAD'
198
+ | 'OPTIONS'
199
+ | '*';
151
200
  export type MiddlewareMethodOptions = {
152
201
  methods?: Hyperspan.MiddlewareMethod[];
153
202
  };
@@ -159,18 +208,45 @@ export namespace Hyperspan {
159
208
  _middleware: Record<MiddlewareMethod, Array<Hyperspan.MiddlewareFunction>>;
160
209
  _path(): string;
161
210
  _methods(): string[];
162
- get: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
163
- post: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
164
- put: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
165
- patch: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
166
- delete: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
167
- options: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
168
- all: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
211
+ get: (
212
+ handler: Hyperspan.RouteHandler,
213
+ handlerOptions?: Hyperspan.RouteHandlerOptions
214
+ ) => Hyperspan.Route;
215
+ post: (
216
+ handler: Hyperspan.RouteHandler,
217
+ handlerOptions?: Hyperspan.RouteHandlerOptions
218
+ ) => Hyperspan.Route;
219
+ put: (
220
+ handler: Hyperspan.RouteHandler,
221
+ handlerOptions?: Hyperspan.RouteHandlerOptions
222
+ ) => Hyperspan.Route;
223
+ patch: (
224
+ handler: Hyperspan.RouteHandler,
225
+ handlerOptions?: Hyperspan.RouteHandlerOptions
226
+ ) => Hyperspan.Route;
227
+ delete: (
228
+ handler: Hyperspan.RouteHandler,
229
+ handlerOptions?: Hyperspan.RouteHandlerOptions
230
+ ) => Hyperspan.Route;
231
+ options: (
232
+ handler: Hyperspan.RouteHandler,
233
+ handlerOptions?: Hyperspan.RouteHandlerOptions
234
+ ) => Hyperspan.Route;
235
+ all: (
236
+ handler: Hyperspan.RouteHandler,
237
+ handlerOptions?: Hyperspan.RouteHandlerOptions
238
+ ) => Hyperspan.Route;
169
239
  errorHandler: (handler: Hyperspan.ErrorHandler) => Hyperspan.Route;
170
- use: (middleware: Hyperspan.MiddlewareFunction, opts?: Hyperspan.MiddlewareMethodOptions) => Hyperspan.Route;
171
- middleware: (middleware: Array<Hyperspan.MiddlewareFunction>, opts?: Hyperspan.MiddlewareMethodOptions) => Hyperspan.Route;
240
+ use: (
241
+ middleware: Hyperspan.MiddlewareFunction,
242
+ opts?: Hyperspan.MiddlewareMethodOptions
243
+ ) => Hyperspan.Route;
244
+ middleware: (
245
+ middleware: Array<Hyperspan.MiddlewareFunction>,
246
+ opts?: Hyperspan.MiddlewareMethodOptions
247
+ ) => Hyperspan.Route;
172
248
  fetch: (request: Request) => Promise<Response>;
173
- };
249
+ }
174
250
 
175
251
  /**
176
252
  * Action = Form + route handler
@@ -178,8 +254,9 @@ export namespace Hyperspan {
178
254
  /** Raw form field values when an action has no schema */
179
255
  export type ActionFormValues = Record<string, string | string[]>;
180
256
  /** Infer validated action data from an optional schema */
181
- export type InferActionData<S extends z.ZodType | undefined> =
182
- S extends z.ZodType ? z.output<S> : ActionFormValues;
257
+ export type InferActionData<S extends z.ZodType | undefined> = S extends z.ZodType
258
+ ? z.output<S>
259
+ : ActionFormValues;
183
260
  // Form renderer
184
261
  export type ActionFormResponse = HSHtml | void | null | Promise<HSHtml | void | null>;
185
262
  export type ActionFormProps<S extends z.ZodType | undefined = undefined> = {
@@ -187,7 +264,8 @@ export namespace Hyperspan {
187
264
  error?: ZodValidationError;
188
265
  };
189
266
  export type ActionForm<S extends z.ZodType | undefined = undefined> = (
190
- c: Context, props: ActionFormProps<S>
267
+ c: Context,
268
+ props: ActionFormProps<S>
191
269
  ) => ActionFormResponse;
192
270
  // Form handler
193
271
  export type ActionFormHandlerReturn = RouteHandlerReturn;
@@ -196,20 +274,28 @@ export namespace Hyperspan {
196
274
  error?: ZodValidationError | Error;
197
275
  };
198
276
  export type ActionFormHandler<S extends z.ZodType | undefined = undefined> = (
199
- c: Context, props: ActionFormHandlerProps<S>
277
+ c: Context,
278
+ props: ActionFormHandlerProps<S>
200
279
  ) => ActionFormHandlerReturn | Promise<ActionFormHandlerReturn>;
201
280
  // Action API
202
281
  export interface Action<S extends z.ZodType | undefined = undefined> {
203
282
  _kind: 'hsAction';
204
283
  _config: Partial<Hyperspan.RouteConfig>;
284
+ _serverConfig?: Hyperspan.Config;
205
285
  _path(): string;
206
286
  _form: null | ActionForm<S>;
207
287
  form(form: ActionForm<S>): Action<S>;
208
288
  render: (c: Context, props?: ActionFormProps<S>) => ActionFormResponse;
209
289
  post: (handler: ActionFormHandler<S>) => Action<S>;
210
290
  errorHandler: (handler: ActionFormHandler<S>) => Action<S>;
211
- use: (middleware: Hyperspan.MiddlewareFunction, opts?: Hyperspan.MiddlewareMethodOptions) => Action<S>;
212
- middleware: (middleware: Array<Hyperspan.MiddlewareFunction>, opts?: Hyperspan.MiddlewareMethodOptions) => Action<S>;
291
+ use: (
292
+ middleware: Hyperspan.MiddlewareFunction,
293
+ opts?: Hyperspan.MiddlewareMethodOptions
294
+ ) => Action<S>;
295
+ middleware: (
296
+ middleware: Array<Hyperspan.MiddlewareFunction>,
297
+ opts?: Hyperspan.MiddlewareMethodOptions
298
+ ) => Action<S>;
213
299
  fetch: (request: Request) => Promise<Response>;
214
300
  }
215
301
 
@@ -225,8 +311,10 @@ export namespace Hyperspan {
225
311
  * @param loadScript - A function that loads the module or a string of code to load the module
226
312
  * @returns HSHtml Template with the <script type="module"> tag
227
313
  */
228
- renderScriptTag: (loadScript?: ((module: unknown) => HSHtml | string | void) | string) => HSHtml;
229
- }
314
+ renderScriptTag: (
315
+ loadScript?: ((module: unknown) => HSHtml | string | void) | string
316
+ ) => HSHtml;
317
+ };
230
318
 
231
319
  /**
232
320
  * Zod validation error type. Used in actions and validation middleware.