@sebspark/openapi-express 5.0.3 → 5.0.4
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.d.mts +8 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +43 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +7 -7
- package/dist/index.d.ts +0 -6
- package/dist/index.js +0 -59
- package/dist/index.js.map +0 -1
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { APIServerDefinition, APIServerOptions } from "@sebspark/openapi-core";
|
|
2
|
+
import * as express_serve_static_core0 from "express-serve-static-core";
|
|
3
|
+
|
|
4
|
+
//#region src/router.d.ts
|
|
5
|
+
declare const TypedRouter: (api: APIServerDefinition, options?: APIServerOptions) => express_serve_static_core0.Router;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { TypedRouter };
|
|
8
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/router.ts"],"sourcesContent":[],"mappings":";;;;cAiBa,mBACN,+BACI,qBAAqB,0BAAA,CAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createHttpError } from "@sebspark/openapi-core";
|
|
2
|
+
import { Router, json } from "express";
|
|
3
|
+
|
|
4
|
+
//#region src/router.ts
|
|
5
|
+
const TypedRouter = (api, options = {}) => {
|
|
6
|
+
const router = Router();
|
|
7
|
+
router.use(json());
|
|
8
|
+
const preUsings = Array.isArray(options.pre) ? options.pre : options.pre ? [options.pre] : [];
|
|
9
|
+
for (const pre of preUsings) router.use(pre);
|
|
10
|
+
for (const [url, methods] of Object.entries(api)) for (const [method, route] of Object.entries(methods)) {
|
|
11
|
+
const handler = async (req, res, next) => {
|
|
12
|
+
try {
|
|
13
|
+
const [status, response] = await route.handler(req);
|
|
14
|
+
res.status(status);
|
|
15
|
+
if (!response) {
|
|
16
|
+
res.end();
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const { headers, data } = response;
|
|
20
|
+
if (headers) for (const [name, value] of Object.entries(headers)) res.setHeader(name, value);
|
|
21
|
+
if (data) res.send(data);
|
|
22
|
+
else res.end();
|
|
23
|
+
} catch (error) {
|
|
24
|
+
next(error);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const handlers = (Array.isArray(route.pre) ? route.pre : route.pre ? [route.pre] : []).concat(handler);
|
|
28
|
+
router[method](url, ...handlers);
|
|
29
|
+
}
|
|
30
|
+
router.use(errorHandler);
|
|
31
|
+
return router;
|
|
32
|
+
};
|
|
33
|
+
const errorHandler = (err, _req, res, next) => {
|
|
34
|
+
let error = err;
|
|
35
|
+
if (!error.message || !error.statusCode) error = createHttpError(500, void 0, err instanceof Error ? err : typeof err === "string" ? new Error(err) : new Error(JSON.stringify(err || "")));
|
|
36
|
+
const showStack = process.env.NODE_ENV !== "production";
|
|
37
|
+
res.status(error.statusCode).send(error.toJSON(showStack));
|
|
38
|
+
next(error);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
export { TypedRouter };
|
|
43
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["errorHandler: ErrorRequestHandler","error: HttpError"],"sources":["../src/router.ts"],"sourcesContent":["import {\n type APIResponse,\n type APIServerDefinition,\n type APIServerOptions,\n createHttpError,\n type HttpError,\n type Verb,\n} from '@sebspark/openapi-core'\nimport { json, Router } from 'express'\nimport type {\n ErrorRequestHandler,\n NextFunction,\n Request,\n RequestHandler,\n Response,\n} from 'express-serve-static-core'\n\nexport const TypedRouter = (\n api: APIServerDefinition,\n options: APIServerOptions = {}\n) => {\n const router = Router()\n\n router.use(json() as unknown as RequestHandler)\n\n // Add global pre to router\n const preUsings = Array.isArray(options.pre)\n ? options.pre\n : options.pre\n ? [options.pre]\n : []\n for (const pre of preUsings) {\n router.use(pre)\n }\n\n // loop through urls on server definition\n for (const [url, methods] of Object.entries(api)) {\n // loop through methods on url\n for (const [method, route] of Object.entries(methods)) {\n // Build handler for url/method\n const handler = async (\n req: Request,\n res: Response,\n next: NextFunction\n ) => {\n try {\n const [status, response] = await route.handler(req)\n res.status(status)\n\n if (!response) {\n res.end()\n return\n }\n\n const { headers, data } = response as APIResponse<\n unknown,\n Record<string, string>\n >\n\n if (headers) {\n for (const [name, value] of Object.entries(headers)) {\n res.setHeader(name, value)\n }\n }\n\n if (data) {\n res.send(data)\n } else {\n res.end()\n }\n } catch (error) {\n next(error)\n }\n }\n\n const pre = Array.isArray(route.pre)\n ? route.pre\n : route.pre\n ? [route.pre]\n : []\n const handlers = pre.concat(handler as RequestHandler)\n\n router[method as Verb](url, ...handlers)\n }\n }\n\n router.use(errorHandler)\n\n return router\n}\n\nconst errorHandler: ErrorRequestHandler = (err, _req, res, next) => {\n let error: HttpError = err\n\n if (!error.message || !error.statusCode) {\n const internal =\n err instanceof Error\n ? err\n : typeof err === 'string'\n ? new Error(err)\n : new Error(JSON.stringify(err || ''))\n error = createHttpError(500, undefined, internal)\n }\n\n const showStack = process.env.NODE_ENV !== 'production'\n res.status(error.statusCode).send(error.toJSON(showStack))\n next(error)\n}\n"],"mappings":";;;;AAiBA,MAAa,eACX,KACA,UAA4B,EAAE,KAC3B;CACH,MAAM,SAAS,QAAQ;AAEvB,QAAO,IAAI,MAAM,CAA8B;CAG/C,MAAM,YAAY,MAAM,QAAQ,QAAQ,IAAI,GACxC,QAAQ,MACR,QAAQ,MACN,CAAC,QAAQ,IAAI,GACb,EAAE;AACR,MAAK,MAAM,OAAO,UAChB,QAAO,IAAI,IAAI;AAIjB,MAAK,MAAM,CAAC,KAAK,YAAY,OAAO,QAAQ,IAAI,CAE9C,MAAK,MAAM,CAAC,QAAQ,UAAU,OAAO,QAAQ,QAAQ,EAAE;EAErD,MAAM,UAAU,OACd,KACA,KACA,SACG;AACH,OAAI;IACF,MAAM,CAAC,QAAQ,YAAY,MAAM,MAAM,QAAQ,IAAI;AACnD,QAAI,OAAO,OAAO;AAElB,QAAI,CAAC,UAAU;AACb,SAAI,KAAK;AACT;;IAGF,MAAM,EAAE,SAAS,SAAS;AAK1B,QAAI,QACF,MAAK,MAAM,CAAC,MAAM,UAAU,OAAO,QAAQ,QAAQ,CACjD,KAAI,UAAU,MAAM,MAAM;AAI9B,QAAI,KACF,KAAI,KAAK,KAAK;QAEd,KAAI,KAAK;YAEJ,OAAO;AACd,SAAK,MAAM;;;EASf,MAAM,YALM,MAAM,QAAQ,MAAM,IAAI,GAChC,MAAM,MACN,MAAM,MACJ,CAAC,MAAM,IAAI,GACX,EAAE,EACa,OAAO,QAA0B;AAEtD,SAAO,QAAgB,KAAK,GAAG,SAAS;;AAI5C,QAAO,IAAI,aAAa;AAExB,QAAO;;AAGT,MAAMA,gBAAqC,KAAK,MAAM,KAAK,SAAS;CAClE,IAAIC,QAAmB;AAEvB,KAAI,CAAC,MAAM,WAAW,CAAC,MAAM,WAO3B,SAAQ,gBAAgB,KAAK,QAL3B,eAAe,QACX,MACA,OAAO,QAAQ,WACb,IAAI,MAAM,IAAI,GACd,IAAI,MAAM,KAAK,UAAU,OAAO,GAAG,CAAC,CACK;CAGnD,MAAM,YAAY,QAAQ,IAAI,aAAa;AAC3C,KAAI,OAAO,MAAM,WAAW,CAAC,KAAK,MAAM,OAAO,UAAU,CAAC;AAC1D,MAAK,MAAM"}
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sebspark/openapi-express",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.4",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "dist/index.
|
|
7
|
-
"types": "dist/index.d.
|
|
6
|
+
"main": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.mts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"types": "./dist/index.d.
|
|
11
|
-
"import": "./dist/index.
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"import": "./dist/index.mjs"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
15
|
"dist"
|
|
16
16
|
],
|
|
17
17
|
"scripts": {
|
|
18
|
-
"build": "
|
|
18
|
+
"build": "tsdown src/index.ts",
|
|
19
19
|
"depcheck": "depcheck",
|
|
20
20
|
"dev": "tsc --watch --noEmit",
|
|
21
21
|
"lint": "biome check .",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"@types/supertest": "6.0.3",
|
|
29
29
|
"express": "5.1.0",
|
|
30
30
|
"supertest": "7.1.4",
|
|
31
|
-
"
|
|
31
|
+
"tsdown": "0.16.0",
|
|
32
32
|
"vitest": "4.0.6"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
package/dist/index.d.ts
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.js
DELETED
|
@@ -1,59 +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
|
-
};
|
|
59
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/router.ts"],"sourcesContent":["import {\n type APIResponse,\n type APIServerDefinition,\n type APIServerOptions,\n createHttpError,\n type HttpError,\n type Verb,\n} from '@sebspark/openapi-core'\nimport { json, Router } from 'express'\nimport type {\n ErrorRequestHandler,\n NextFunction,\n Request,\n RequestHandler,\n Response,\n} from 'express-serve-static-core'\n\nexport const TypedRouter = (\n api: APIServerDefinition,\n options: APIServerOptions = {}\n) => {\n const router = Router()\n\n router.use(json() as unknown as RequestHandler)\n\n // Add global pre to router\n const preUsings = Array.isArray(options.pre)\n ? options.pre\n : options.pre\n ? [options.pre]\n : []\n for (const pre of preUsings) {\n router.use(pre)\n }\n\n // loop through urls on server definition\n for (const [url, methods] of Object.entries(api)) {\n // loop through methods on url\n for (const [method, route] of Object.entries(methods)) {\n // Build handler for url/method\n const handler = async (\n req: Request,\n res: Response,\n next: NextFunction\n ) => {\n try {\n const [status, response] = await route.handler(req)\n res.status(status)\n\n if (!response) {\n res.end()\n return\n }\n\n const { headers, data } = response as APIResponse<\n unknown,\n Record<string, string>\n >\n\n if (headers) {\n for (const [name, value] of Object.entries(headers)) {\n res.setHeader(name, value)\n }\n }\n\n if (data) {\n res.send(data)\n } else {\n res.end()\n }\n } catch (error) {\n next(error)\n }\n }\n\n const pre = Array.isArray(route.pre)\n ? route.pre\n : route.pre\n ? [route.pre]\n : []\n const handlers = pre.concat(handler as RequestHandler)\n\n router[method as Verb](url, ...handlers)\n }\n }\n\n router.use(errorHandler)\n\n return router\n}\n\nconst errorHandler: ErrorRequestHandler = (err, _req, res, next) => {\n let error: HttpError = err\n\n if (!error.message || !error.statusCode) {\n const internal =\n err instanceof Error\n ? err\n : typeof err === 'string'\n ? new Error(err)\n : new Error(JSON.stringify(err || ''))\n error = createHttpError(500, undefined, internal)\n }\n\n const showStack = process.env.NODE_ENV !== 'production'\n res.status(error.statusCode).send(error.toJSON(showStack))\n next(error)\n}\n"],"mappings":";AAAA;AAAA,EAIE;AAAA,OAGK;AACP,SAAS,MAAM,cAAc;AAStB,IAAM,cAAc,CACzB,KACA,UA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