@riddl/core 0.2.5 → 0.2.6
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/adapters.js +2 -0
- package/dist/api.d.ts +1 -1
- package/dist/api.js +10 -3
- package/dist/log.d.ts +2 -0
- package/dist/log.js +4 -0
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
package/dist/adapters.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import express from "express";
|
|
2
2
|
import { toFetchRequest } from "./utils.js";
|
|
3
3
|
import * as set_cookie_parser from "set-cookie-parser";
|
|
4
|
+
import { logError } from "./log.js";
|
|
4
5
|
export const mountApi = (app, prefix, apiHandler) => app.use(prefix, expressAdapter(apiHandler));
|
|
5
6
|
export const setResponse = async (res, response) => {
|
|
6
7
|
for (const [key, value] of response.headers) {
|
|
@@ -10,6 +11,7 @@ export const setResponse = async (res, response) => {
|
|
|
10
11
|
: value);
|
|
11
12
|
}
|
|
12
13
|
catch (error) {
|
|
14
|
+
logError("failed to set response header", error);
|
|
13
15
|
res.getHeaderNames().forEach((name) => res.removeHeader(name));
|
|
14
16
|
res.writeHead(500).end(String(error));
|
|
15
17
|
return;
|
package/dist/api.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { APIOptions, API } from "./types";
|
|
2
|
-
export declare const api: ({ routeModuleMap, config, base, cors, registry, }: APIOptions) => Promise<API>;
|
|
2
|
+
export declare const api: ({ routeModuleMap, config, base, cors, registry, onInternalError, }: APIOptions) => Promise<API>;
|
package/dist/api.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { OpenAPIRegistry } from "@asteasolutions/zod-to-openapi";
|
|
2
2
|
import { json, recursiveAwait } from "./utils.js";
|
|
3
3
|
import { CORS_DEFAULT, METHOD } from "./constants.js";
|
|
4
|
-
import { log } from "./log.js";
|
|
4
|
+
import { log, logError } from "./log.js";
|
|
5
5
|
import { parseBody, parseOutput, parseParam, parseQuery } from "./validation.js";
|
|
6
6
|
import { cleanupTempFiles } from "./multipart.js";
|
|
7
7
|
import { parseRouteId } from "./parser.js";
|
|
8
8
|
import { buildRouter } from "./routing.js";
|
|
9
9
|
import { openapiHandler, registerRoutes } from "./openapi.js";
|
|
10
10
|
import { error } from "./errors.js";
|
|
11
|
-
export const api = async ({ routeModuleMap, config, base = "", cors = true, registry = new OpenAPIRegistry(), }) => {
|
|
11
|
+
export const api = async ({ routeModuleMap, config, base = "", cors = true, registry = new OpenAPIRegistry(), onInternalError, }) => {
|
|
12
|
+
const reportInternalError = (error) => {
|
|
13
|
+
logError("internal error", error);
|
|
14
|
+
onInternalError === null || onInternalError === void 0 ? void 0 : onInternalError(error);
|
|
15
|
+
};
|
|
12
16
|
const { routes, middlewares } = await Object.entries(routeModuleMap)
|
|
13
17
|
.reduce(async (accPromise, [path, module]) => {
|
|
14
18
|
var _a, _b, _c;
|
|
@@ -120,11 +124,14 @@ export const api = async ({ routeModuleMap, config, base = "", cors = true, regi
|
|
|
120
124
|
}
|
|
121
125
|
catch (e) {
|
|
122
126
|
if ('__HTTP_ERROR__' in e) {
|
|
127
|
+
if (typeof e.status === "number" && e.status >= 500) {
|
|
128
|
+
reportInternalError(e);
|
|
129
|
+
}
|
|
123
130
|
return json(e, {
|
|
124
131
|
status: e.status,
|
|
125
132
|
});
|
|
126
133
|
}
|
|
127
|
-
|
|
134
|
+
reportInternalError(e);
|
|
128
135
|
return json({ message: 'Internal server error', status: 500 }, {
|
|
129
136
|
status: 500,
|
|
130
137
|
});
|
package/dist/log.d.ts
CHANGED
package/dist/log.js
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -76,6 +76,8 @@ export interface APIOptions {
|
|
|
76
76
|
base?: string;
|
|
77
77
|
cors?: boolean | CORS;
|
|
78
78
|
registry?: OpenAPIRegistry;
|
|
79
|
+
/** Called for unexpected throws and HTTP errors with status >= 500. */
|
|
80
|
+
onInternalError?: (error: unknown) => void;
|
|
79
81
|
}
|
|
80
82
|
export interface OpenAPIOptions {
|
|
81
83
|
base?: string;
|