@arkstack/driver-h3 0.3.10 → 0.3.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/dist/error-handler-nV_ImqPa.d.ts +14 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +31 -4
- package/dist/index.js.map +1 -1
- package/dist/middlewares/index.d.ts +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { H3Event, HTTPError, HTTPResponse } from "h3";
|
|
2
|
+
|
|
3
|
+
//#region src/error-handler.d.ts
|
|
4
|
+
declare const defaultErrorHandler: (err: HTTPError | Error | string, event: H3Event) => HTTPResponse | {
|
|
5
|
+
error: boolean;
|
|
6
|
+
message: string;
|
|
7
|
+
status: "error";
|
|
8
|
+
code: number;
|
|
9
|
+
errors?: unknown;
|
|
10
|
+
stack?: string;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { defaultErrorHandler as t };
|
|
14
|
+
//# sourceMappingURL=error-handler-nV_ImqPa.d.ts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { t as defaultErrorHandler } from "./error-handler-nV_ImqPa.js";
|
|
1
2
|
import { ArkstackKitDriver, ArkstackMiddlewareConfig, PromiseOrValue } from "@arkstack/contract";
|
|
2
|
-
import { H3 } from "h3";
|
|
3
|
+
import { H3, H3Event } from "h3";
|
|
3
4
|
import { Middleware } from "clear-router/types/h3";
|
|
4
5
|
|
|
5
6
|
//#region src/index.d.ts
|
|
@@ -8,6 +9,7 @@ interface H3DriverOptions {
|
|
|
8
9
|
bindRouter: (app: H3) => PromiseOrValue<void>;
|
|
9
10
|
mountPublicAssets?: (app: H3, publicPath: string) => PromiseOrValue<void>;
|
|
10
11
|
createApp?: () => H3;
|
|
12
|
+
onError?: (err: Error | string, event: H3Event) => unknown;
|
|
11
13
|
}
|
|
12
14
|
declare class H3EventResponse {
|
|
13
15
|
response: Response;
|
|
@@ -27,5 +29,5 @@ declare class H3Driver extends ArkstackKitDriver<H3, H3Middleware> {
|
|
|
27
29
|
start(app: H3, port: number): void;
|
|
28
30
|
}
|
|
29
31
|
//#endregion
|
|
30
|
-
export { H3Driver, H3DriverOptions, H3EventResponse, H3Middleware };
|
|
32
|
+
export { H3Driver, H3DriverOptions, H3EventResponse, H3Middleware, defaultErrorHandler };
|
|
31
33
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,35 @@
|
|
|
1
1
|
import { staticAssetHandler } from "./middlewares/index.js";
|
|
2
2
|
import { ArkstackKitDriver } from "@arkstack/contract";
|
|
3
|
-
import { H3, serve, toResponse } from "h3";
|
|
4
|
-
import { Logger } from "@arkstack/common";
|
|
3
|
+
import { H3, HTTPResponse, serve, toResponse } from "h3";
|
|
4
|
+
import { Logger, buildHtmlErrorResponse, createErrorPayload, logUnhandledError, normalizeStatusCode, shouldLogError } from "@arkstack/common";
|
|
5
5
|
|
|
6
|
+
//#region src/error-handler.ts
|
|
7
|
+
const defaultErrorHandler = (err, event) => {
|
|
8
|
+
const responseBody = createErrorPayload(err);
|
|
9
|
+
if (shouldLogError(err)) logUnhandledError(err, {
|
|
10
|
+
headers: Object.fromEntries(event.req.headers.entries()),
|
|
11
|
+
method: event.req.method,
|
|
12
|
+
url: event.req.url
|
|
13
|
+
}, "Unhandled H3 request error");
|
|
14
|
+
if (process.env.NODE_ENV === "development") console.error(responseBody);
|
|
15
|
+
const code = normalizeStatusCode(responseBody.code);
|
|
16
|
+
event.res.status = code;
|
|
17
|
+
if ((event.req.headers.get("accept") ?? "").includes("application/json") || event.req._url?.pathname?.startsWith("/api")) return {
|
|
18
|
+
...responseBody,
|
|
19
|
+
error: true,
|
|
20
|
+
message: responseBody.message
|
|
21
|
+
};
|
|
22
|
+
return new HTTPResponse(buildHtmlErrorResponse({
|
|
23
|
+
message: String(responseBody.message),
|
|
24
|
+
stack: typeof responseBody.stack === "string" ? responseBody.stack : void 0,
|
|
25
|
+
code
|
|
26
|
+
}), {
|
|
27
|
+
status: code,
|
|
28
|
+
headers: { "Content-Type": "text/html" }
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
6
33
|
//#region src/index.ts
|
|
7
34
|
var H3EventResponse = class {
|
|
8
35
|
status = 200;
|
|
@@ -37,7 +64,7 @@ var H3Driver = class extends ArkstackKitDriver {
|
|
|
37
64
|
* @returns
|
|
38
65
|
*/
|
|
39
66
|
createApp() {
|
|
40
|
-
return this.options.createApp?.() ?? new H3();
|
|
67
|
+
return this.options.createApp?.() ?? new H3({ onError: this.options.onError ?? defaultErrorHandler });
|
|
41
68
|
}
|
|
42
69
|
/**
|
|
43
70
|
* Mounts static assets from the specified public path to the H3 application.
|
|
@@ -98,5 +125,5 @@ var H3Driver = class extends ArkstackKitDriver {
|
|
|
98
125
|
};
|
|
99
126
|
|
|
100
127
|
//#endregion
|
|
101
|
-
export { H3Driver, H3EventResponse };
|
|
128
|
+
export { H3Driver, H3EventResponse, defaultErrorHandler };
|
|
102
129
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { ArkstackKitDriver, ArkstackMiddlewareConfig, PromiseOrValue } from '@arkstack/contract'\nimport { H3, serve, toResponse } from 'h3'\n\nimport { Middleware as H3BaseMiddleware } from 'clear-router/types/h3'\nimport { Logger } from '@arkstack/common'\nimport { staticAssetHandler } from './middlewares'\n\n// oxlint-disable-next-line typescript/no-explicit-any\nexport type H3Middleware = H3BaseMiddleware | [H3BaseMiddleware, Record<string, any>];\n\nexport interface H3DriverOptions {\n bindRouter: (app: H3) => PromiseOrValue<void>;\n mountPublicAssets?: (app: H3, publicPath: string) => PromiseOrValue<void>;\n createApp?: () => H3;\n}\n\nexport class H3EventResponse {\n status: number = 200\n statusText?: string\n\n constructor(public response: Response) {\n this.status = response.status\n this.statusText = response.statusText\n }\n\n get headers (): Headers {\n return this.response.headers\n }\n}\n\n/**\n * The H3Driver class implements the ArkstackKitDriver contract for the H3 framework.\n */\nexport class H3Driver extends ArkstackKitDriver<H3, H3Middleware> {\n readonly name = 'h3'\n private readonly options: H3DriverOptions\n\n /**\n * Creates an instance of H3Driver.\n * \n * @param options \n */\n constructor(options: H3DriverOptions) {\n super()\n this.options = options\n }\n\n /**\n * Creates an H3 application instance.\n * \n * @returns \n */\n createApp (): H3 {\n return this.options.createApp?.() ?? new H3()\n }\n\n /**\n * Mounts static assets from the specified public path to the H3 application.\n * \n * @param app \n * @param publicPath \n */\n mountPublicAssets (app: H3, publicPath: string): PromiseOrValue<void> {\n if (this.options.mountPublicAssets) {\n return this.options.mountPublicAssets(app, publicPath)\n }\n\n app.use(staticAssetHandler(publicPath))\n }\n\n /**\n * Binds the router to the H3 application using the provided bindRouter function.\n * \n * @param app \n */\n bindRouter (app: H3): PromiseOrValue<void> {\n return this.options.bindRouter(app)\n }\n\n /**\n * Applies middleware to the H3 application.\n * \n * @param app \n * @param middleware \n */\n applyMiddleware (\n app: H3,\n middleware: H3Middleware | ArkstackMiddlewareConfig<H3Middleware>,\n ): void {\n const mw = Array.isArray(middleware) ? middleware[0] : middleware\n const conf = Array.isArray(middleware) && middleware[1] ? middleware[1] : {}\n\n if (typeof mw === 'function') {\n app.use(mw, conf)\n\n return\n }\n\n for (const [pos, entries] of Object.entries(middleware) as [string, H3Middleware[]][]) {\n for (const entry of entries) {\n const mw = Array.isArray(entry) ? entry[0] : entry\n const conf = Array.isArray(entry) && entry[1] ? entry[1] : {}\n\n if (pos === 'after') {\n app.use(async (evt, next) => {\n const response = await toResponse(await next(), evt)\n // evt.res.status = response.status\n evt[Symbol.for('h3.internal.event.res') as never] = new H3EventResponse(response) as never\n await mw(evt, next)\n next()\n }, conf)\n } else {\n app.use(mw, conf)\n }\n }\n }\n }\n\n /**\n * Starts the H3 server on the specified port.\n * \n * @param app \n * @param port \n */\n start (app: H3, port: number): void {\n serve(app, { port, silent: true }).ready().then(() => {\n Logger.log([\n ['Server is running on', 'white'],\n [`http://localhost:${port}`, 'cyan']\n ], ' ')\n })\n }\n}\n"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/error-handler.ts","../src/index.ts"],"sourcesContent":["import { H3Event, HTTPError, HTTPResponse } from 'h3'\nimport {\n buildHtmlErrorResponse,\n createErrorPayload,\n logUnhandledError,\n normalizeStatusCode,\n shouldLogError,\n} from '@arkstack/common'\n\nexport const defaultErrorHandler = (err: HTTPError | Error | string, event: H3Event) => {\n const responseBody = createErrorPayload(err)\n\n if (shouldLogError(err)) {\n logUnhandledError(err, {\n headers: Object.fromEntries(event.req.headers.entries()),\n method: event.req.method,\n url: event.req.url,\n }, 'Unhandled H3 request error')\n }\n\n if (process.env.NODE_ENV === 'development') console.error(responseBody)\n\n const code = normalizeStatusCode(responseBody.code)\n event.res.status = code\n\n const acceptsHeader = event.req.headers.get('accept') ?? ''\n const expectsJson = acceptsHeader.includes('application/json') || event.req._url?.pathname?.startsWith('/api')\n\n if (expectsJson) {\n return {\n ...responseBody,\n error: true,\n message: responseBody.message,\n }\n }\n\n return new HTTPResponse(buildHtmlErrorResponse({\n message: String(responseBody.message),\n stack: typeof responseBody.stack === 'string' ? responseBody.stack : undefined,\n code,\n }), {\n status: code,\n headers: {\n 'Content-Type': 'text/html',\n },\n })\n}\n\nexport default defaultErrorHandler","import { ArkstackKitDriver, ArkstackMiddlewareConfig, PromiseOrValue } from '@arkstack/contract'\nimport { H3, H3Event, serve, toResponse } from 'h3'\n\nimport { Middleware as H3BaseMiddleware } from 'clear-router/types/h3'\nimport { Logger } from '@arkstack/common'\nimport { defaultErrorHandler } from './error-handler'\nimport { staticAssetHandler } from './middlewares'\n\n// oxlint-disable-next-line typescript/no-explicit-any\nexport type H3Middleware = H3BaseMiddleware | [H3BaseMiddleware, Record<string, any>];\n\nexport interface H3DriverOptions {\n bindRouter: (app: H3) => PromiseOrValue<void>;\n mountPublicAssets?: (app: H3, publicPath: string) => PromiseOrValue<void>;\n createApp?: () => H3;\n onError?: (err: Error | string, event: H3Event) => unknown;\n}\n\nexport class H3EventResponse {\n status: number = 200\n statusText?: string\n\n constructor(public response: Response) {\n this.status = response.status\n this.statusText = response.statusText\n }\n\n get headers (): Headers {\n return this.response.headers\n }\n}\n\n/**\n * The H3Driver class implements the ArkstackKitDriver contract for the H3 framework.\n */\nexport class H3Driver extends ArkstackKitDriver<H3, H3Middleware> {\n readonly name = 'h3'\n private readonly options: H3DriverOptions\n\n /**\n * Creates an instance of H3Driver.\n * \n * @param options \n */\n constructor(options: H3DriverOptions) {\n super()\n this.options = options\n }\n\n /**\n * Creates an H3 application instance.\n * \n * @returns \n */\n createApp (): H3 {\n return this.options.createApp?.() ?? new H3({\n onError: this.options.onError ?? defaultErrorHandler,\n })\n }\n\n /**\n * Mounts static assets from the specified public path to the H3 application.\n * \n * @param app \n * @param publicPath \n */\n mountPublicAssets (app: H3, publicPath: string): PromiseOrValue<void> {\n if (this.options.mountPublicAssets) {\n return this.options.mountPublicAssets(app, publicPath)\n }\n\n app.use(staticAssetHandler(publicPath))\n }\n\n /**\n * Binds the router to the H3 application using the provided bindRouter function.\n * \n * @param app \n */\n bindRouter (app: H3): PromiseOrValue<void> {\n return this.options.bindRouter(app)\n }\n\n /**\n * Applies middleware to the H3 application.\n * \n * @param app \n * @param middleware \n */\n applyMiddleware (\n app: H3,\n middleware: H3Middleware | ArkstackMiddlewareConfig<H3Middleware>,\n ): void {\n const mw = Array.isArray(middleware) ? middleware[0] : middleware\n const conf = Array.isArray(middleware) && middleware[1] ? middleware[1] : {}\n\n if (typeof mw === 'function') {\n app.use(mw, conf)\n\n return\n }\n\n for (const [pos, entries] of Object.entries(middleware) as [string, H3Middleware[]][]) {\n for (const entry of entries) {\n const mw = Array.isArray(entry) ? entry[0] : entry\n const conf = Array.isArray(entry) && entry[1] ? entry[1] : {}\n\n if (pos === 'after') {\n app.use(async (evt, next) => {\n const response = await toResponse(await next(), evt)\n // evt.res.status = response.status\n evt[Symbol.for('h3.internal.event.res') as never] = new H3EventResponse(response) as never\n await mw(evt, next)\n next()\n }, conf)\n } else {\n app.use(mw, conf)\n }\n }\n }\n }\n\n /**\n * Starts the H3 server on the specified port.\n * \n * @param app \n * @param port \n */\n start (app: H3, port: number): void {\n serve(app, { port, silent: true }).ready().then(() => {\n Logger.log([\n ['Server is running on', 'white'],\n [`http://localhost:${port}`, 'cyan']\n ], ' ')\n })\n }\n}\n\nexport * from './error-handler'\n"],"mappings":";;;;;;AASA,MAAa,uBAAuB,KAAiC,UAAmB;CACpF,MAAM,eAAe,mBAAmB,IAAI;AAE5C,KAAI,eAAe,IAAI,CACnB,mBAAkB,KAAK;EACnB,SAAS,OAAO,YAAY,MAAM,IAAI,QAAQ,SAAS,CAAC;EACxD,QAAQ,MAAM,IAAI;EAClB,KAAK,MAAM,IAAI;EAClB,EAAE,6BAA6B;AAGpC,KAAI,QAAQ,IAAI,aAAa,cAAe,SAAQ,MAAM,aAAa;CAEvE,MAAM,OAAO,oBAAoB,aAAa,KAAK;AACnD,OAAM,IAAI,SAAS;AAKnB,MAHsB,MAAM,IAAI,QAAQ,IAAI,SAAS,IAAI,IACvB,SAAS,mBAAmB,IAAI,MAAM,IAAI,MAAM,UAAU,WAAW,OAAO,CAG1G,QAAO;EACH,GAAG;EACH,OAAO;EACP,SAAS,aAAa;EACzB;AAGL,QAAO,IAAI,aAAa,uBAAuB;EAC3C,SAAS,OAAO,aAAa,QAAQ;EACrC,OAAO,OAAO,aAAa,UAAU,WAAW,aAAa,QAAQ;EACrE;EACH,CAAC,EAAE;EACA,QAAQ;EACR,SAAS,EACL,gBAAgB,aACnB;EACJ,CAAC;;;;;AC3BN,IAAa,kBAAb,MAA6B;CACzB,SAAiB;CACjB;CAEA,YAAY,AAAO,UAAoB;EAApB;AACf,OAAK,SAAS,SAAS;AACvB,OA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;AAChC,QAAM,KAAK;GAAE;GAAM,QAAQ;GAAM,CAAC,CAAC,OAAO,CAAC,WAAW;AAClD,UAAO,IAAI,CACP,CAAC,wBAAwB,QAAQ,EACjC,CAAC,oBAAoB,QAAQ,OAAO,CACvC,EAAE,IAAI;IACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arkstack/driver-h3",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.11",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "H3 driver package for Arkstack providing H3-specific implementations of core Arkstack features such as routing, middleware, and database integration.",
|
|
6
6
|
"homepage": "https://arkstack.toneflix.net",
|
|
@@ -36,12 +36,12 @@
|
|
|
36
36
|
"./package.json": "./package.json"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@arkstack/contract": "^0.3.
|
|
39
|
+
"@arkstack/contract": "^0.3.11"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"clear-router": "^2.3.3",
|
|
43
43
|
"h3": "2.0.1-rc.16",
|
|
44
|
-
"@arkstack/common": "^0.3.
|
|
44
|
+
"@arkstack/common": "^0.3.11"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "tsdown",
|