@sebspark/openapi-express 3.0.1 → 4.0.1
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/index.js +9 -34
- package/package.json +4 -4
- package/dist/index.d.mts +0 -6
- package/dist/index.mjs +0 -58
package/dist/index.js
CHANGED
|
@@ -1,35 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
TypedRouter: () => TypedRouter
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(index_exports);
|
|
26
|
-
|
|
27
1
|
// src/router.ts
|
|
28
|
-
|
|
29
|
-
|
|
2
|
+
import {
|
|
3
|
+
createHttpError
|
|
4
|
+
} from "@sebspark/openapi-core";
|
|
5
|
+
import { json, Router } from "express";
|
|
30
6
|
var TypedRouter = (api, options = {}) => {
|
|
31
|
-
const router =
|
|
32
|
-
router.use(
|
|
7
|
+
const router = Router();
|
|
8
|
+
router.use(json());
|
|
33
9
|
const preUsings = Array.isArray(options.pre) ? options.pre : options.pre ? [options.pre] : [];
|
|
34
10
|
for (const pre of preUsings) {
|
|
35
11
|
router.use(pre);
|
|
@@ -71,13 +47,12 @@ var errorHandler = (err, _req, res, next) => {
|
|
|
71
47
|
let error = err;
|
|
72
48
|
if (!error.message || !error.statusCode) {
|
|
73
49
|
const internal = err instanceof Error ? err : typeof err === "string" ? new Error(err) : new Error(JSON.stringify(err || ""));
|
|
74
|
-
error =
|
|
50
|
+
error = createHttpError(500, void 0, internal);
|
|
75
51
|
}
|
|
76
52
|
const showStack = process.env.NODE_ENV !== "production";
|
|
77
53
|
res.status(error.statusCode).send(error.toJSON(showStack));
|
|
78
54
|
next(error);
|
|
79
55
|
};
|
|
80
|
-
|
|
81
|
-
0 && (module.exports = {
|
|
56
|
+
export {
|
|
82
57
|
TypedRouter
|
|
83
|
-
}
|
|
58
|
+
};
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sebspark/openapi-express",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
|
-
"build": "tsup-node src/index.ts --format esm
|
|
12
|
+
"build": "tsup-node src/index.ts --format esm --target node22 --dts",
|
|
13
13
|
"dev": "tsc --watch --noEmit",
|
|
14
14
|
"lint": "biome check .",
|
|
15
15
|
"test": "vitest run --passWithNoTests --coverage",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"express-serve-static-core": "0.1.1"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@sebspark/otel": ">=
|
|
30
|
+
"@sebspark/otel": ">=2.0.1",
|
|
31
31
|
"@sebspark/tsconfig": "*"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/dist/index.d.mts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import * as express_serve_static_core from 'express-serve-static-core';
|
|
2
|
-
import { APIServerDefinition, APIServerOptions } from '@sebspark/openapi-core';
|
|
3
|
-
|
|
4
|
-
declare const TypedRouter: (api: APIServerDefinition, options?: APIServerOptions) => express_serve_static_core.Router;
|
|
5
|
-
|
|
6
|
-
export { TypedRouter };
|
package/dist/index.mjs
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
// src/router.ts
|
|
2
|
-
import {
|
|
3
|
-
createHttpError
|
|
4
|
-
} from "@sebspark/openapi-core";
|
|
5
|
-
import { json, Router } from "express";
|
|
6
|
-
var TypedRouter = (api, options = {}) => {
|
|
7
|
-
const router = Router();
|
|
8
|
-
router.use(json());
|
|
9
|
-
const preUsings = Array.isArray(options.pre) ? options.pre : options.pre ? [options.pre] : [];
|
|
10
|
-
for (const pre of preUsings) {
|
|
11
|
-
router.use(pre);
|
|
12
|
-
}
|
|
13
|
-
for (const [url, methods] of Object.entries(api)) {
|
|
14
|
-
for (const [method, route] of Object.entries(methods)) {
|
|
15
|
-
const handler = async (req, res, next) => {
|
|
16
|
-
try {
|
|
17
|
-
const [status, response] = await route.handler(req);
|
|
18
|
-
res.status(status);
|
|
19
|
-
if (!response) {
|
|
20
|
-
res.end();
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
const { headers, data } = response;
|
|
24
|
-
if (headers) {
|
|
25
|
-
for (const [name, value] of Object.entries(headers)) {
|
|
26
|
-
res.setHeader(name, value);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
if (data) {
|
|
30
|
-
res.send(data);
|
|
31
|
-
} else {
|
|
32
|
-
res.end();
|
|
33
|
-
}
|
|
34
|
-
} catch (error) {
|
|
35
|
-
next(error);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
const pre = Array.isArray(route.pre) ? route.pre : route.pre ? [route.pre] : [];
|
|
39
|
-
const handlers = pre.concat(handler);
|
|
40
|
-
router[method](url, ...handlers);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
router.use(errorHandler);
|
|
44
|
-
return router;
|
|
45
|
-
};
|
|
46
|
-
var errorHandler = (err, _req, res, next) => {
|
|
47
|
-
let error = err;
|
|
48
|
-
if (!error.message || !error.statusCode) {
|
|
49
|
-
const internal = err instanceof Error ? err : typeof err === "string" ? new Error(err) : new Error(JSON.stringify(err || ""));
|
|
50
|
-
error = createHttpError(500, void 0, internal);
|
|
51
|
-
}
|
|
52
|
-
const showStack = process.env.NODE_ENV !== "production";
|
|
53
|
-
res.status(error.statusCode).send(error.toJSON(showStack));
|
|
54
|
-
next(error);
|
|
55
|
-
};
|
|
56
|
-
export {
|
|
57
|
-
TypedRouter
|
|
58
|
-
};
|