Comparing version
@@ -173,13 +173,12 @@ import { CookieSerializeOptions } from 'cookie-es'; | ||
* @extends Error | ||
* @property {Number} statusCode An Integer indicating the HTTP response status code. | ||
* @property {String} statusMessage A String representing the HTTP status message | ||
* @property {String} fatal Indicates if the error is a fatal error. | ||
* @property {String} unhandled Indicates if the error was unhandled and auto captured. | ||
* @property {Any} data An extra data that will includes in the response.<br> | ||
* This can be used to pass additional information about the error. | ||
* @property {Boolean} internal Setting this property to <code>true</code> will mark error as an internal error | ||
* @property {number} statusCode - An integer indicating the HTTP response status code. | ||
* @property {string} statusMessage - A string representing the HTTP status message. | ||
* @property {boolean} fatal - Indicates if the error is a fatal error. | ||
* @property {boolean} unhandled - Indicates if the error was unhandled and auto captured. | ||
* @property {any} data - An extra data that will be included in the response. | ||
* This can be used to pass additional information about the error. | ||
* @property {boolean} internal - Setting this property to `true` will mark the error as an internal error. | ||
*/ | ||
declare class H3Error extends Error { | ||
static __h3_error__: boolean; | ||
toJSON(): Pick<H3Error, "data" | "statusCode" | "statusMessage" | "message">; | ||
statusCode: number; | ||
@@ -190,8 +189,9 @@ fatal: boolean; | ||
data?: any; | ||
toJSON(): Pick<H3Error, "data" | "statusCode" | "statusMessage" | "message">; | ||
} | ||
/** | ||
* Creates new `Error` that can be used to handle both internal and runtime errors. | ||
* Creates a new `Error` that can be used to handle both internal and runtime errors. | ||
* | ||
* @param input {Partial<H3Error>} | ||
* @return {H3Error} An instance of the H3Error | ||
* @param input {string | (Partial<H3Error> & { status?: number; statusText?: string })} - The error message or an object containing error properties. | ||
* @return {H3Error} - An instance of H3Error. | ||
*/ | ||
@@ -203,12 +203,18 @@ declare function createError(input: string | (Partial<H3Error> & { | ||
/** | ||
* Receive an error and return the corresponding response.<br> | ||
* H3 internally uses this function to handle unhandled errors.<br> | ||
* Note that calling this function will close the connection and no other data will be sent to client afterwards. | ||
* Receives an error and returns the corresponding response. | ||
* H3 internally uses this function to handle unhandled errors. | ||
* Note that calling this function will close the connection and no other data will be sent to the client afterwards. | ||
* | ||
@param event {H3Event} H3 event or req passed by h3 handler | ||
* @param error {H3Error|Error} Raised error | ||
* @param debug {Boolean} Whether application is in debug mode.<br> | ||
* In the debug mode the stack trace of errors will be return in response. | ||
* @param event {H3Event} - H3 event or req passed by h3 handler. | ||
* @param error {Error | H3Error} - The raised error. | ||
* @param debug {boolean} - Whether the application is in debug mode. | ||
* In the debug mode, the stack trace of errors will be returned in the response. | ||
*/ | ||
declare function sendError(event: H3Event, error: Error | H3Error, debug?: boolean): void; | ||
/** | ||
* Checks if the given input is an instance of H3Error. | ||
* | ||
* @param input {*} - The input to check. | ||
* @return {boolean} - Returns true if the input is an instance of H3Error, false otherwise. | ||
*/ | ||
declare function isError(input: any): input is H3Error; | ||
@@ -328,7 +334,7 @@ | ||
declare function getProxyRequestHeaders(event: H3Event): any; | ||
declare function fetchWithEvent(event: H3Event, req: RequestInfo | URL, init?: RequestInit & { | ||
declare function fetchWithEvent<T = unknown, _R = any, F extends (req: RequestInfo | URL, opts?: any) => any = typeof fetch>(event: H3Event, req: RequestInfo | URL, init?: RequestInit & { | ||
context?: H3EventContext; | ||
}, options?: { | ||
fetch: typeof fetch; | ||
}): Promise<Response>; | ||
fetch: F; | ||
}): unknown extends T ? ReturnType<F> : T; | ||
@@ -335,0 +341,0 @@ declare function getQuery(event: H3Event): ufo.QueryObject; |
{ | ||
"name": "h3", | ||
"version": "1.7.0", | ||
"version": "1.7.1", | ||
"description": "Tiny JavaScript Server", | ||
@@ -22,12 +22,2 @@ "repository": "unjs/h3", | ||
], | ||
"scripts": { | ||
"build": "unbuild", | ||
"dev": "vitest", | ||
"lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test playground", | ||
"lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test playground -w", | ||
"play": "jiti ./playground/index.ts", | ||
"profile": "0x -o -D .profile -P 'autocannon -c 100 -p 10 -d 40 http://localhost:$PORT' ./playground/server.cjs", | ||
"release": "pnpm test && pnpm build && changelogen --release && pnpm publish && git push --follow-tags", | ||
"test": "pnpm lint && vitest run --coverage" | ||
}, | ||
"dependencies": { | ||
@@ -64,3 +54,13 @@ "cookie-es": "^1.0.0", | ||
}, | ||
"packageManager": "pnpm@8.6.3" | ||
"packageManager": "pnpm@8.6.3", | ||
"scripts": { | ||
"build": "unbuild", | ||
"dev": "vitest", | ||
"lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test playground", | ||
"lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test playground -w", | ||
"play": "jiti ./playground/index.ts", | ||
"profile": "0x -o -D .profile -P 'autocannon -c 100 -p 10 -d 40 http://localhost:$PORT' ./playground/server.cjs", | ||
"release": "pnpm test && pnpm build && changelogen --release && pnpm publish && git push --follow-tags", | ||
"test": "pnpm lint && vitest run --coverage" | ||
} | ||
} |
@@ -71,3 +71,3 @@ # H3 | ||
The `app` instance created by `h3` uses a middleware stack (see [how it works](#how-it-works)) with the ability to match route prefix and apply matched middleware. | ||
The `app` instance created by `h3` uses a middleware stack (see [how it works](./src/app.ts)) with the ability to match route prefix and apply matched middleware. | ||
@@ -102,32 +102,56 @@ To opt-in using a more advanced and convenient routing system, we can create a router instance and register it to app instance. | ||
// Handle can directly return object or Promise<object> for JSON response | ||
app.use('/api', eventHandler((event) => ({ url: event.node.req.url }))) | ||
app.use( | ||
"/api", | ||
eventHandler((event) => ({ url: event.node.req.url })) | ||
); | ||
// We can have better matching other than quick prefix match | ||
app.use('/odd', eventHandler(() => 'Is odd!'), { match: url => url.substr(1) % 2 }) | ||
app.use( | ||
"/odd", | ||
eventHandler(() => "Is odd!"), | ||
{ match: (url) => url.substr(1) % 2 } | ||
); | ||
// Handle can directly return string for HTML response | ||
app.use(eventHandler(() => '<h1>Hello world!</h1>')) | ||
app.use(eventHandler(() => "<h1>Hello world!</h1>")); | ||
// We can chain calls to .use() | ||
app.use('/1', eventHandler(() => '<h1>Hello world!</h1>')) | ||
.use('/2', eventHandler(() => '<h1>Goodbye!</h1>')) | ||
app | ||
.use( | ||
"/1", | ||
eventHandler(() => "<h1>Hello world!</h1>") | ||
) | ||
.use( | ||
"/2", | ||
eventHandler(() => "<h1>Goodbye!</h1>") | ||
); | ||
// We can proxy requests and rewrite cookie's domain and path | ||
app.use('/api', eventHandler((event) => proxyRequest(event, 'https://example.com', { | ||
// f.e. keep one domain unchanged, rewrite one domain and remove other domains | ||
cookieDomainRewrite: { | ||
"example.com": "example.com", | ||
"example.com": "somecompany.co.uk", | ||
"*": "", | ||
}, | ||
cookiePathRewrite: { | ||
"/": "/api" | ||
}, | ||
}))) | ||
app.use( | ||
"/api", | ||
eventHandler((event) => | ||
proxyRequest(event, "https://example.com", { | ||
// f.e. keep one domain unchanged, rewrite one domain and remove other domains | ||
cookieDomainRewrite: { | ||
"example.com": "example.com", | ||
"example.com": "somecompany.co.uk", | ||
"*": "", | ||
}, | ||
cookiePathRewrite: { | ||
"/": "/api", | ||
}, | ||
}) | ||
) | ||
); | ||
// Legacy middleware with 3rd argument are automatically promisified | ||
app.use(fromNodeMiddleware((req, res, next) => { req.setHeader('x-foo', 'bar'); next() })) | ||
app.use( | ||
fromNodeMiddleware((req, res, next) => { | ||
req.setHeader("x-foo", "bar"); | ||
next(); | ||
}) | ||
); | ||
// Lazy loaded routes using { lazy: true } | ||
app.use('/big', () => import('./big-handler'), { lazy: true }) | ||
app.use("/big", () => import("./big-handler"), { lazy: true }); | ||
``` | ||
@@ -134,0 +158,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
166596
38.57%7
16.67%4979
45.5%245
10.86%