@express-tools/oas 0.0.3 → 0.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/README.md +23 -13
- package/bin/definition.js +1 -4
- package/bin/index.js +3 -16
- package/bin/router.js +36 -32
- package/bin/utils.js +3 -8
- package/package.json +8 -2
- package/types.d.ts +13 -73
- package/bin/documentation.js +0 -10
- package/bin/response.js +0 -21
- package/bin/validation.js +0 -21
package/README.md
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
*Bring your Express API to the modern web*
|
|
4
4
|
|
|
5
|
-
- Express 5 compatible
|
|
5
|
+
- Express 5 compatible - no wrappers or monkey-patching
|
|
6
6
|
- Request/response validation
|
|
7
7
|
- Dynamically generated documentation
|
|
8
8
|
- Define or redefine as much of your spec
|
|
9
|
+
- Supports OpenAPI 2.x, 3.x
|
|
9
10
|
|
|
10
11
|
## Getting started
|
|
11
12
|
|
|
@@ -21,16 +22,24 @@ Add the plugin to your express app:
|
|
|
21
22
|
```javascript
|
|
22
23
|
|
|
23
24
|
import Express from 'express';
|
|
24
|
-
import {definition,
|
|
25
|
+
import {definition, router as OASRouter} from '@express-tools/oas';
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
const server = express();
|
|
28
|
+
|
|
29
|
+
const OAS = OASRouter(server, {
|
|
30
|
+
/* An OpenAPI spec JSON Object*/
|
|
29
31
|
});
|
|
30
32
|
|
|
33
|
+
// Route definitions...
|
|
34
|
+
|
|
35
|
+
OAS.compile();
|
|
36
|
+
server.listen(3000);
|
|
37
|
+
|
|
31
38
|
```
|
|
32
39
|
|
|
33
|
-
|
|
40
|
+
Before calling `listen` on your Express server you need to `compile` your routes.
|
|
41
|
+
|
|
42
|
+
The definition middleware enables you to define or override the definition of a route in your spec.
|
|
34
43
|
|
|
35
44
|
```javascript
|
|
36
45
|
|
|
@@ -48,20 +57,21 @@ server.get('/user/:id', definition({
|
|
|
48
57
|
},
|
|
49
58
|
}), (req, res, next) => { /* Your business logic handler */ });
|
|
50
59
|
|
|
60
|
+
|
|
51
61
|
```
|
|
52
62
|
|
|
53
|
-
The validation middleware
|
|
63
|
+
The validation middleware uses the fast [swagger-route-validator](https://github.com/fed135/swagger-route-validator) to check that the request matches the spec and overrides.
|
|
54
64
|
|
|
55
65
|
```javascript
|
|
56
66
|
|
|
57
67
|
server.post(
|
|
58
68
|
'/user',
|
|
59
|
-
validation
|
|
69
|
+
OAS.validation,
|
|
60
70
|
(req, res, next) => { /* Your business logic handler */ });
|
|
61
71
|
|
|
62
72
|
```
|
|
63
73
|
|
|
64
|
-
The response middleware adds response validation to your route
|
|
74
|
+
The response middleware adds response validation to your route, just before writing to the socket.
|
|
65
75
|
|
|
66
76
|
```javascript
|
|
67
77
|
|
|
@@ -80,16 +90,16 @@ server.post(
|
|
|
80
90
|
default: { schema: { $ref: 'user' } },
|
|
81
91
|
},
|
|
82
92
|
}),
|
|
83
|
-
response
|
|
93
|
+
OAS.response,
|
|
84
94
|
(req, res, next) => { /* Your business logic handler */ });
|
|
85
95
|
|
|
86
96
|
```
|
|
87
97
|
|
|
88
|
-
The documentation plugin prints out your spec and all overrides
|
|
98
|
+
The documentation plugin prints out your spec and all overrides as JSON
|
|
89
99
|
|
|
90
100
|
```javascript
|
|
91
101
|
|
|
92
|
-
server.get('/
|
|
102
|
+
server.get('/openapi.json', OAS.documentation);
|
|
93
103
|
|
|
94
104
|
```
|
|
95
105
|
|
|
@@ -100,4 +110,4 @@ If you want to contribute, feel free to ping @fed135.
|
|
|
100
110
|
|
|
101
111
|
## License
|
|
102
112
|
|
|
103
|
-
Apache-2.0 -
|
|
113
|
+
Apache-2.0 - 2026
|
package/bin/definition.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = Definition;
|
|
4
|
-
function Definition(routeDefinition) {
|
|
1
|
+
export default function Definition(routeDefinition) {
|
|
5
2
|
// Basic validation - TypeScript types handle detailed schema validation
|
|
6
3
|
if (!routeDefinition || typeof routeDefinition !== 'object') {
|
|
7
4
|
throw new Error('Route definition must be an object');
|
package/bin/index.js
CHANGED
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.validation = exports.router = exports.response = exports.documentation = exports.definition = void 0;
|
|
7
|
-
const definition_ts_1 = __importDefault(require("./definition.js"));
|
|
8
|
-
exports.definition = definition_ts_1.default;
|
|
9
|
-
const documentation_ts_1 = __importDefault(require("./documentation.js"));
|
|
10
|
-
exports.documentation = documentation_ts_1.default;
|
|
11
|
-
const response_ts_1 = __importDefault(require("./response.js"));
|
|
12
|
-
exports.response = response_ts_1.default;
|
|
13
|
-
const router_ts_1 = __importDefault(require("./router.js"));
|
|
14
|
-
exports.router = router_ts_1.default;
|
|
15
|
-
const validation_ts_1 = __importDefault(require("./validation.js"));
|
|
16
|
-
exports.validation = validation_ts_1.default;
|
|
1
|
+
import definition from './definition.js';
|
|
2
|
+
import router from './router.js';
|
|
3
|
+
export { definition, router, };
|
package/bin/router.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const utils_ts_1 = require("./utils.js");
|
|
5
|
-
function router(expressApp, spec) {
|
|
6
|
-
let scanned = false;
|
|
1
|
+
import { expressRequestValidation, expressResponseValidation } from 'swagger-route-validator';
|
|
2
|
+
import { formatPathToOAS, mergeDefinitions } from './utils.js';
|
|
3
|
+
export default function router(expressApp, spec) {
|
|
7
4
|
// Store a working copy of the spec in express app
|
|
8
|
-
|
|
5
|
+
const state = {
|
|
9
6
|
openapi: '3.1.0',
|
|
10
7
|
info: {},
|
|
11
8
|
servers: [],
|
|
@@ -14,21 +11,11 @@ function router(expressApp, spec) {
|
|
|
14
11
|
components: {},
|
|
15
12
|
tags: [],
|
|
16
13
|
};
|
|
17
|
-
|
|
18
|
-
// Append config to request to avoid redundant lookups
|
|
19
|
-
function OASRouter(req, res, next) {
|
|
20
|
-
req._oas = expressApp['_oas'];
|
|
21
|
-
return next();
|
|
22
|
-
}
|
|
23
|
-
OASRouter.OASType = 'router';
|
|
24
|
-
expressApp.use(OASRouter);
|
|
14
|
+
mergeDefinitions(state, spec);
|
|
25
15
|
if (spec.basePath && spec.basePath !== '/') {
|
|
26
16
|
expressApp.mountpath = spec.basePath;
|
|
27
17
|
}
|
|
28
|
-
function
|
|
29
|
-
if (scanned)
|
|
30
|
-
throw new Error('OAS Router has already scanned for routes. Make sure it is only invoked once.');
|
|
31
|
-
scanned = true;
|
|
18
|
+
function compile() {
|
|
32
19
|
const stack = expressApp.stack || expressApp.router.stack;
|
|
33
20
|
// Scan routes for definition middleware
|
|
34
21
|
for (let i = 0; i < stack.length; i++) {
|
|
@@ -41,24 +28,41 @@ function router(expressApp, spec) {
|
|
|
41
28
|
if (spec.basePath && spec.basePath !== '/' && operationId.indexOf(expressApp.mountpath) === 0)
|
|
42
29
|
operationId = operationId.replace(expressApp.mountpath, '');
|
|
43
30
|
// Check if path level or method level
|
|
44
|
-
|
|
31
|
+
state.paths[formatPathToOAS(operationId)] = state.paths[formatPathToOAS(operationId)] || {};
|
|
45
32
|
if (override[definitionPlugin.method])
|
|
46
|
-
|
|
33
|
+
mergeDefinitions(state.paths[formatPathToOAS(operationId)], override);
|
|
47
34
|
else
|
|
48
|
-
|
|
35
|
+
mergeDefinitions(state.paths[formatPathToOAS(operationId)], { [definitionPlugin.method]: override });
|
|
49
36
|
}
|
|
50
37
|
}
|
|
51
38
|
}
|
|
52
39
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
40
|
+
function documentation(req, res) {
|
|
41
|
+
res.json(state);
|
|
42
|
+
}
|
|
43
|
+
function validation(req, res, next) {
|
|
44
|
+
if (!req.route)
|
|
45
|
+
throw new Error('Request validation middleware must be added to a route');
|
|
46
|
+
let operationId = formatPathToOAS(req.route.path);
|
|
47
|
+
// subtract spec basePath if exists
|
|
48
|
+
if (state.basePath && state.basePath !== '/' && operationId.indexOf(state.basePath) === 0)
|
|
49
|
+
operationId = operationId.replace(state.basePath, '');
|
|
50
|
+
const matchingSpec = state.paths?.[operationId]?.[req.method.toLowerCase()];
|
|
51
|
+
if (matchingSpec)
|
|
52
|
+
return expressRequestValidation(matchingSpec, state)(req, res, next);
|
|
53
|
+
throw new Error(`Request validation middleware added, but no definition could be found for ${operationId}.${req.method.toLowerCase()}`);
|
|
54
|
+
}
|
|
55
|
+
function response(req, res, next) {
|
|
56
|
+
if (!req.route)
|
|
57
|
+
throw new Error('Response validation middleware must be added to a route');
|
|
58
|
+
let operationId = formatPathToOAS(req.route.path);
|
|
59
|
+
// subtract spec basePath if exists
|
|
60
|
+
if (state.basePath && state.basePath !== '/' && operationId.indexOf(state.basePath) === 0)
|
|
61
|
+
operationId = operationId.replace(state.basePath, '');
|
|
62
|
+
const matchingSpec = state.paths?.[operationId]?.[req.method.toLowerCase()];
|
|
63
|
+
if (matchingSpec)
|
|
64
|
+
return expressResponseValidation(matchingSpec, {}, state)(req, res, next);
|
|
65
|
+
throw new Error(`Response validation middleware added, but no definition could be found for ${operationId}.${req.method.toLowerCase()}`);
|
|
62
66
|
}
|
|
63
|
-
return
|
|
67
|
+
return { compile, documentation, validation, response };
|
|
64
68
|
}
|
package/bin/utils.js
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatPathToOAS = formatPathToOAS;
|
|
4
|
-
exports.formatPathFromOAS = formatPathFromOAS;
|
|
5
|
-
exports.mergeDefinitions = mergeDefinitions;
|
|
6
|
-
function formatPathToOAS(path) {
|
|
1
|
+
export function formatPathToOAS(path) {
|
|
7
2
|
return path.replace(/:([a-zA-Z0-9_]+)/g, '{$1}');
|
|
8
3
|
}
|
|
9
|
-
function formatPathFromOAS(path) {
|
|
4
|
+
export function formatPathFromOAS(path) {
|
|
10
5
|
return path.replace(/\{([^}]+)\}/g, ':$1');
|
|
11
6
|
}
|
|
12
|
-
function mergeDefinitions(original, updates) {
|
|
7
|
+
export function mergeDefinitions(original, updates) {
|
|
13
8
|
original = original || {};
|
|
14
9
|
for (const key of Object.keys(updates)) {
|
|
15
10
|
if (!Object.hasOwn(original, key) || typeof updates[key] !== 'object')
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@express-tools/oas",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "OpenAPI plugin for express",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "bin/index.js",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"keywords": [
|
|
8
9
|
"express",
|
|
9
10
|
"open",
|
|
@@ -29,9 +30,14 @@
|
|
|
29
30
|
"devDependencies": {
|
|
30
31
|
"express": "^5.2.0"
|
|
31
32
|
},
|
|
33
|
+
"overrides": {
|
|
34
|
+
"express": {
|
|
35
|
+
"router": "file:/home/fred/Documents/workspace/router"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
32
38
|
"typings": "types.d.ts",
|
|
33
39
|
"scripts": {
|
|
34
|
-
"build": "rm -rf ./bin && mkdir ./bin && tsc --
|
|
40
|
+
"build": "rm -rf ./bin && mkdir ./bin && tsc --noCheck --outDir ./bin ./src/index.ts && find ./bin -name '*.js' -exec sed -i \"s/\\.ts'/\\.js'/g; s/\\.ts\\\"/\\.js\\\"/g\" {} \\;",
|
|
35
41
|
"test:unit": "node --test src/*.test.ts",
|
|
36
42
|
"test:integration": "node --test tests/integration/*.test.ts",
|
|
37
43
|
"test": "npm run test:unit && npm run test:integration",
|
package/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { OpenApiSpecification } from 'swagger-route-validator';
|
|
2
|
-
import {
|
|
2
|
+
import { Request, RequestHandler } from 'express';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* OpenAPI parameter definition
|
|
@@ -160,19 +160,15 @@ export interface OASSpec extends Partial<OpenApiSpecification> {
|
|
|
160
160
|
/**
|
|
161
161
|
* Express router enhanced with OAS functionality
|
|
162
162
|
*/
|
|
163
|
-
export interface OASRouter
|
|
163
|
+
export interface OASRouter {
|
|
164
164
|
/**
|
|
165
165
|
* Manually trigger route scanning to build the OpenAPI specification.
|
|
166
|
-
* This is automatically called when listen() is invoked.
|
|
167
166
|
* Use this when working with subrouters that won't call listen() directly.
|
|
168
167
|
*/
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
* @internal
|
|
174
|
-
*/
|
|
175
|
-
_oas?: OASSpec
|
|
168
|
+
compile: () => void
|
|
169
|
+
documentation: (req, res, next) => void
|
|
170
|
+
validation: (req, res, next) => void
|
|
171
|
+
response: (req, res, next) => void
|
|
176
172
|
}
|
|
177
173
|
|
|
178
174
|
/**
|
|
@@ -213,49 +209,18 @@ declare module '@express-tools/oas' {
|
|
|
213
209
|
*/
|
|
214
210
|
export function definition(override: RouteDefinitionOverride): RequestHandler;
|
|
215
211
|
|
|
216
|
-
/**
|
|
217
|
-
* Generate and return the complete OpenAPI specification as JSON.
|
|
218
|
-
* Includes all route definitions and overrides.
|
|
219
|
-
*
|
|
220
|
-
* @returns Express middleware that responds with the OpenAPI spec
|
|
221
|
-
*
|
|
222
|
-
* @example
|
|
223
|
-
* ```typescript
|
|
224
|
-
* server.get('/docs', documentation());
|
|
225
|
-
* ```
|
|
226
|
-
*/
|
|
227
|
-
export function documentation(): RequestHandler;
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Validate the response body against the OpenAPI specification.
|
|
231
|
-
* Throws an error if the response doesn't match the defined schema.
|
|
232
|
-
*
|
|
233
|
-
* @returns Express middleware that validates responses
|
|
234
|
-
*
|
|
235
|
-
* @example
|
|
236
|
-
* ```typescript
|
|
237
|
-
* server.get('/user/:id',
|
|
238
|
-
* definition({ responses: { 200: { schema: { $ref: '#/components/schemas/User' } } } }),
|
|
239
|
-
* response(),
|
|
240
|
-
* (req, res) => {
|
|
241
|
-
* res.json({ id: '123', name: 'John' });
|
|
242
|
-
* }
|
|
243
|
-
* );
|
|
244
|
-
* ```
|
|
245
|
-
*/
|
|
246
|
-
export function response(): RequestHandler;
|
|
247
|
-
|
|
248
212
|
/**
|
|
249
213
|
* Wrap an Express app or router with OpenAPI functionality.
|
|
250
214
|
* Automatically scans routes when listen() is called.
|
|
251
|
-
*
|
|
252
|
-
* @param app - Express application or router to enhance
|
|
215
|
+
* @param expressApp - Your express application
|
|
253
216
|
* @param spec - OpenAPI specification (optional)
|
|
254
|
-
* @returns
|
|
217
|
+
* @returns OASRouter
|
|
255
218
|
*
|
|
256
219
|
* @example
|
|
257
220
|
* ```typescript
|
|
258
|
-
* const server =
|
|
221
|
+
* const server = express();
|
|
222
|
+
*
|
|
223
|
+
* server.use(router({
|
|
259
224
|
* info: {
|
|
260
225
|
* title: 'My API',
|
|
261
226
|
* version: '1.0.0'
|
|
@@ -271,35 +236,10 @@ declare module '@express-tools/oas' {
|
|
|
271
236
|
* }
|
|
272
237
|
* }
|
|
273
238
|
* }
|
|
274
|
-
* });
|
|
275
|
-
* ```
|
|
276
|
-
*/
|
|
277
|
-
export function router(app: Express, spec?: OASSpec): OASRouter;
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Validate incoming requests against the OpenAPI specification.
|
|
281
|
-
* Validates path parameters, query parameters, headers, and request body.
|
|
282
|
-
*
|
|
283
|
-
* @returns Express middleware that validates requests
|
|
284
|
-
*
|
|
285
|
-
* @example
|
|
286
|
-
* ```typescript
|
|
287
|
-
* server.post('/user',
|
|
288
|
-
* definition({
|
|
289
|
-
* parameters: [{
|
|
290
|
-
* name: 'body',
|
|
291
|
-
* in: 'body',
|
|
292
|
-
* schema: { $ref: '#/components/schemas/User' }
|
|
293
|
-
* }]
|
|
294
|
-
* }),
|
|
295
|
-
* validation(),
|
|
296
|
-
* (req, res) => {
|
|
297
|
-
* // req.body is validated
|
|
298
|
-
* }
|
|
299
|
-
* );
|
|
239
|
+
* }));
|
|
300
240
|
* ```
|
|
301
241
|
*/
|
|
302
|
-
export function
|
|
242
|
+
export function router(expressApp: Express.Application, spec?: OASSpec): OASRouter;
|
|
303
243
|
|
|
304
244
|
// Export types for external use
|
|
305
245
|
export type {
|
package/bin/documentation.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = documentation;
|
|
4
|
-
function documentation() {
|
|
5
|
-
function OASDocumentation(req, res) {
|
|
6
|
-
res.json(req._oas);
|
|
7
|
-
}
|
|
8
|
-
OASDocumentation.OASType = 'documentation';
|
|
9
|
-
return OASDocumentation;
|
|
10
|
-
}
|
package/bin/response.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = response;
|
|
4
|
-
const swagger_route_validator_1 = require("swagger-route-validator");
|
|
5
|
-
const utils_ts_1 = require("./utils.js");
|
|
6
|
-
function response() {
|
|
7
|
-
function OASResponse(req, res, next) {
|
|
8
|
-
if (!req.route)
|
|
9
|
-
throw new Error('Response validation middleware must be added to a route');
|
|
10
|
-
let operationId = (0, utils_ts_1.formatPathToOAS)(req.route.path);
|
|
11
|
-
// subtract spec basePath if exists
|
|
12
|
-
if (req._oas?.basePath && req._oas?.basePath !== '/' && operationId.indexOf(req._oas?.basePath) === 0)
|
|
13
|
-
operationId = operationId.replace(req._oas?.basePath, '');
|
|
14
|
-
const matchingSpec = req._oas?.paths?.[operationId]?.[req.method.toLowerCase()];
|
|
15
|
-
if (matchingSpec)
|
|
16
|
-
return (0, swagger_route_validator_1.expressResponseValidation)(matchingSpec, {}, req._oas)(req, res, next);
|
|
17
|
-
throw new Error(`Response validation middleware added, but no definition could be found for ${operationId}.${req.method.toLowerCase()}`);
|
|
18
|
-
}
|
|
19
|
-
OASResponse.OASType = 'response';
|
|
20
|
-
return OASResponse;
|
|
21
|
-
}
|
package/bin/validation.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = validate;
|
|
4
|
-
const swagger_route_validator_1 = require("swagger-route-validator");
|
|
5
|
-
const utils_ts_1 = require("./utils.js");
|
|
6
|
-
function validate() {
|
|
7
|
-
function OASValidation(req, res, next) {
|
|
8
|
-
if (!req.route)
|
|
9
|
-
throw new Error('Request validation middleware must be added to a route');
|
|
10
|
-
let operationId = (0, utils_ts_1.formatPathToOAS)(req.route.path);
|
|
11
|
-
// subtract spec basePath if exists
|
|
12
|
-
if (req._oas?.basePath && req._oas?.basePath !== '/' && operationId.indexOf(req._oas?.basePath) === 0)
|
|
13
|
-
operationId = operationId.replace(req._oas?.basePath, '');
|
|
14
|
-
const matchingSpec = req._oas?.paths?.[operationId]?.[req.method.toLowerCase()];
|
|
15
|
-
if (matchingSpec)
|
|
16
|
-
return (0, swagger_route_validator_1.expressRequestValidation)(matchingSpec, req._oas)(req, res, next);
|
|
17
|
-
throw new Error(`Request validation middleware added, but no definition could be found for ${operationId}.${req.method.toLowerCase()}`);
|
|
18
|
-
}
|
|
19
|
-
OASValidation.OASType = 'validation';
|
|
20
|
-
return OASValidation;
|
|
21
|
-
}
|