@hyperspan/framework 1.0.0-alpha.10 → 1.0.0-alpha.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperspan/framework",
3
- "version": "1.0.0-alpha.10",
3
+ "version": "1.0.0-alpha.11",
4
4
  "description": "Hyperspan Web Framework",
5
5
  "main": "src/server.ts",
6
6
  "types": "src/server.ts",
package/src/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { createConfig, createContext, createRoute, createServer, getRunnableRoute, StreamResponse, IS_PROD, HTTPException } from './server';
1
+ export { createConfig, createContext, createRoute, createServer, getRunnableRoute, StreamResponse, IS_PROD, HTTPResponseException } from './server';
2
2
  export type { Hyperspan } from './types';
package/src/server.ts CHANGED
@@ -5,12 +5,15 @@ import { parsePath } from './utils';
5
5
  import { Cookies } from './cookies';
6
6
 
7
7
  import type { Hyperspan as HS } from './types';
8
+ import { RequestOptions } from 'node:http';
8
9
 
9
10
  export const IS_PROD = process.env.NODE_ENV === 'production';
10
11
 
11
- export class HTTPException extends Error {
12
- constructor(public status: number, message?: string) {
13
- super(message);
12
+ export class HTTPResponseException extends Error {
13
+ public _response?: Response;
14
+ constructor(body: string | undefined, options?: ResponseInit) {
15
+ super(body);
16
+ this._response = new Response(body, options);
14
17
  }
15
18
  }
16
19
 
@@ -74,12 +77,12 @@ export function createContext(req: Request, route?: HS.Route): HS.Context {
74
77
  cookies: new Cookies(req, headers),
75
78
  headers,
76
79
  raw: new Response(),
77
- html: (html: string, options?: { status?: number; headers?: Headers | Record<string, string> }) => merge(new Response(html, { ...options, headers: { 'Content-Type': 'text/html; charset=UTF-8', ...options?.headers } })),
78
- json: (json: any, options?: { status?: number; headers?: Headers | Record<string, string> }) => merge(new Response(JSON.stringify(json), { ...options, headers: { 'Content-Type': 'application/json', ...options?.headers } })),
79
- text: (text: string, options?: { status?: number; headers?: Headers | Record<string, string> }) => merge(new Response(text, { ...options, headers: { 'Content-Type': 'text/plain; charset=UTF-8', ...options?.headers } })),
80
- redirect: (url: string, options?: { status?: number; headers?: Headers | Record<string, string> }) => merge(new Response(null, { status: 302, headers: { Location: url, ...options?.headers } })),
81
- error: (error: Error, options?: { status?: number; headers?: Headers | Record<string, string> }) => merge(new Response(error.message, { status: 500, ...options })),
82
- notFound: (options?: { status?: number; headers?: Headers | Record<string, string> }) => merge(new Response('Not Found', { status: 404, ...options })),
80
+ html: (html: string, options?: ResponseInit) => merge(new Response(html, { ...options, headers: { 'Content-Type': 'text/html; charset=UTF-8', ...options?.headers } })),
81
+ json: (json: any, options?: ResponseInit) => merge(new Response(JSON.stringify(json), { ...options, headers: { 'Content-Type': 'application/json', ...options?.headers } })),
82
+ text: (text: string, options?: ResponseInit) => merge(new Response(text, { ...options, headers: { 'Content-Type': 'text/plain; charset=UTF-8', ...options?.headers } })),
83
+ redirect: (url: string, options?: ResponseInit) => merge(new Response(null, { status: 302, headers: { Location: url, ...options?.headers } })),
84
+ error: (error: Error, options?: ResponseInit) => merge(new Response(error.message, { status: 500, ...options })),
85
+ notFound: (options?: ResponseInit) => merge(new Response('Not Found', { status: 404, ...options })),
83
86
  merge,
84
87
  },
85
88
  };
@@ -398,8 +401,8 @@ async function showErrorReponse(
398
401
  const message = err.message || 'Internal Server Error';
399
402
 
400
403
  // Send correct status code if HTTPException
401
- if (err instanceof HTTPException) {
402
- status = err.status;
404
+ if (err instanceof HTTPResponseException) {
405
+ status = err._response?.status ?? 500;
403
406
  }
404
407
 
405
408
  const stack = !IS_PROD && err.stack ? err.stack.split('\n').slice(1).join('\n') : '';
package/src/types.ts CHANGED
@@ -49,6 +49,32 @@ export namespace Hyperspan {
49
49
  delete: (name: string) => void;
50
50
  }
51
51
 
52
+ export type HSRequest = {
53
+ url: URL;
54
+ raw: Request;
55
+ method: string; // Always uppercase
56
+ headers: Headers; // Case-insensitive
57
+ query: URLSearchParams;
58
+ cookies: Hyperspan.Cookies;
59
+ text: () => Promise<string>;
60
+ json<T = unknown>(): Promise<T>;
61
+ formData(): Promise<FormData>;
62
+ urlencoded(): Promise<URLSearchParams>;
63
+ };
64
+
65
+ export type HSResponse = {
66
+ cookies: Hyperspan.Cookies;
67
+ headers: Headers; // Headers to merge with final outgoing response
68
+ html: (html: string, options?: ResponseInit) => Response
69
+ json: (json: any, options?: ResponseInit) => Response;
70
+ text: (text: string, options?: ResponseInit) => Response;
71
+ redirect: (url: string, options?: ResponseInit) => Response;
72
+ error: (error: Error, options?: ResponseInit) => Response;
73
+ notFound: (options?: ResponseInit) => Response;
74
+ merge: (response: Response) => Response;
75
+ raw: Response;
76
+ };
77
+
52
78
  export interface Context {
53
79
  vars: Record<string, any>;
54
80
  route: {
@@ -56,30 +82,8 @@ export namespace Hyperspan {
56
82
  params: Record<string, string>;
57
83
  cssImports?: string[];
58
84
  }
59
- req: {
60
- url: URL;
61
- raw: Request;
62
- method: string; // Always uppercase
63
- headers: Headers; // Case-insensitive
64
- query: URLSearchParams;
65
- cookies: Hyperspan.Cookies;
66
- text: () => Promise<string>;
67
- json<T = unknown>(): Promise<T>;
68
- formData(): Promise<FormData>;
69
- urlencoded(): Promise<URLSearchParams>;
70
- };
71
- res: {
72
- cookies: Hyperspan.Cookies;
73
- headers: Headers; // Headers to merge with final outgoing response
74
- html: (html: string, options?: { status?: number; headers?: Record<string, string> }) => Response
75
- json: (json: any, options?: { status?: number; headers?: Record<string, string> }) => Response;
76
- text: (text: string, options?: { status?: number; headers?: Record<string, string> }) => Response;
77
- redirect: (url: string, options?: { status?: number; headers?: Record<string, string> }) => Response;
78
- error: (error: Error, options?: { status?: number; headers?: Record<string, string> }) => Response;
79
- notFound: (options?: { status?: number; headers?: Record<string, string> }) => Response;
80
- merge: (response: Response) => Response;
81
- raw: Response;
82
- };
85
+ req: HSRequest;
86
+ res: HSResponse;
83
87
  };
84
88
 
85
89
  export type ClientIslandOptions = {