@davaux/swagger 0.8.0 → 0.8.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.d.ts +40 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +163 -0
- package/dist/index.js.map +1 -0
- package/package.json +6 -3
- package/CLAUDE.md +0 -95
- package/src/index.ts +0 -205
- package/tsconfig.json +0 -17
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { MiddlewareFn } from 'davaux';
|
|
2
|
+
/** OpenAPI metadata used in the `info` block of the generated spec. */
|
|
3
|
+
export interface SwaggerInfo {
|
|
4
|
+
title?: string;
|
|
5
|
+
version?: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface SwaggerOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Directory containing compiled route files to introspect.
|
|
11
|
+
* In production: the davaux build output directory (default: './dist').
|
|
12
|
+
* In development: './.davaux/routes' (the dev server's compiled output).
|
|
13
|
+
*/
|
|
14
|
+
routesDir: string;
|
|
15
|
+
/** OpenAPI info block. */
|
|
16
|
+
info?: SwaggerInfo;
|
|
17
|
+
/** URL path for the Swagger UI page. Default: '/docs' */
|
|
18
|
+
path?: string;
|
|
19
|
+
/** URL path for the raw OpenAPI JSON spec. Default: '/openapi.json' */
|
|
20
|
+
specPath?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* OpenAPI/Swagger middleware. Introspects compiled route files to build an
|
|
24
|
+
* OpenAPI 3.0 spec and serves it at `specPath` (default: `/openapi.json`),
|
|
25
|
+
* plus a Swagger UI page at `path` (default: `/docs`).
|
|
26
|
+
*
|
|
27
|
+
* The spec is generated once on the first request and cached for the lifetime
|
|
28
|
+
* of the process. When `zod-to-json-schema` is installed, mutating routes that
|
|
29
|
+
* export a `*Schema` named export will have their request body documented
|
|
30
|
+
* automatically.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* import { swagger } from '@davaux/swagger'
|
|
34
|
+
* export default swagger({
|
|
35
|
+
* routesDir: './dist/routes',
|
|
36
|
+
* info: { title: 'My API', version: '1.0.0' },
|
|
37
|
+
* })
|
|
38
|
+
*/
|
|
39
|
+
export declare function swagger(options: SwaggerOptions): MiddlewareFn;
|
|
40
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAA;AAE1C,uEAAuE;AACvE,MAAM,WAAW,WAAW;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,MAAM,WAAW,cAAc;IAC7B;;;;OAIG;IACH,SAAS,EAAE,MAAM,CAAA;IACjB,0BAA0B;IAC1B,IAAI,CAAC,EAAE,WAAW,CAAA;IAClB,yDAAyD;IACzD,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,uEAAuE;IACvE,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAwID;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,YAAY,CA2B7D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
const TYPE_TO_METHOD = {
|
|
4
|
+
get: 'get',
|
|
5
|
+
post: 'post',
|
|
6
|
+
put: 'put',
|
|
7
|
+
patch: 'patch',
|
|
8
|
+
delete: 'delete',
|
|
9
|
+
head: 'head',
|
|
10
|
+
options: 'options',
|
|
11
|
+
page: undefined, // HTML page routes are not API endpoints
|
|
12
|
+
};
|
|
13
|
+
function toOpenApiPath(pattern) {
|
|
14
|
+
return pattern
|
|
15
|
+
.replace(/:(\w+)/g, '{$1}') // :id → {id}
|
|
16
|
+
.replace(/\*(\w+)/g, '{$1}'); // *slug → {slug} (catch-all)
|
|
17
|
+
}
|
|
18
|
+
function buildInfo(info) {
|
|
19
|
+
return {
|
|
20
|
+
title: info?.title ?? 'API',
|
|
21
|
+
version: info?.version ?? '1.0.0',
|
|
22
|
+
...(info?.description ? { description: info.description } : {}),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function escapeHtml(s) {
|
|
26
|
+
return s
|
|
27
|
+
.replace(/&/g, '&')
|
|
28
|
+
.replace(/</g, '<')
|
|
29
|
+
.replace(/>/g, '>')
|
|
30
|
+
.replace(/"/g, '"');
|
|
31
|
+
}
|
|
32
|
+
function swaggerHtml(specPath, title) {
|
|
33
|
+
return `<!DOCTYPE html>
|
|
34
|
+
<html lang="en">
|
|
35
|
+
<head>
|
|
36
|
+
<meta charset="utf-8" />
|
|
37
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
38
|
+
<title>${escapeHtml(title)}</title>
|
|
39
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.18.2/swagger-ui.css" integrity="sha384-rcbEi6xgdPk0iWkAQzT2F3FeBJXdG+ydrawGlfHAFIZG7wU6aKbQaRewysYpmrlW" crossorigin="anonymous" />
|
|
40
|
+
</head>
|
|
41
|
+
<body>
|
|
42
|
+
<div id="swagger-ui"></div>
|
|
43
|
+
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.18.2/swagger-ui-bundle.js" integrity="sha384-NXtFPpN61oWCuN4D42K6Zd5Rt2+uxeIT36R7kpXBuY9tLnZorzrJ4ykpqwJfgjpZ" crossorigin="anonymous"></script>
|
|
44
|
+
<script>
|
|
45
|
+
window.onload = () => {
|
|
46
|
+
SwaggerUIBundle({
|
|
47
|
+
url: ${JSON.stringify(specPath)},
|
|
48
|
+
dom_id: '#swagger-ui',
|
|
49
|
+
presets: [SwaggerUIBundle.presets.apis, SwaggerUIBundle.SwaggerUIStandalonePreset],
|
|
50
|
+
layout: 'StandaloneLayout',
|
|
51
|
+
deepLinking: true,
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
</script>
|
|
55
|
+
</body>
|
|
56
|
+
</html>`;
|
|
57
|
+
}
|
|
58
|
+
async function buildSpec(options) {
|
|
59
|
+
const routesDir = resolve(options.routesDir);
|
|
60
|
+
if (!existsSync(routesDir)) {
|
|
61
|
+
console.warn(`[@davaux/swagger] routesDir not found: ${routesDir} — serving empty spec`);
|
|
62
|
+
return { openapi: '3.0.0', info: buildInfo(options.info), paths: {} };
|
|
63
|
+
}
|
|
64
|
+
const { scanRoutes } = await import('davaux/scanner');
|
|
65
|
+
const { routes } = await scanRoutes(routesDir);
|
|
66
|
+
const paths = {};
|
|
67
|
+
for (const route of routes) {
|
|
68
|
+
const method = TYPE_TO_METHOD[route.type];
|
|
69
|
+
if (!method)
|
|
70
|
+
continue;
|
|
71
|
+
const openApiPath = toOpenApiPath(route.urlPattern);
|
|
72
|
+
if (!paths[openApiPath])
|
|
73
|
+
paths[openApiPath] = {};
|
|
74
|
+
const parameters = route.params.map((param) => ({
|
|
75
|
+
name: param,
|
|
76
|
+
in: 'path',
|
|
77
|
+
required: true,
|
|
78
|
+
schema: { type: 'string' },
|
|
79
|
+
}));
|
|
80
|
+
const operation = {
|
|
81
|
+
parameters,
|
|
82
|
+
responses: {
|
|
83
|
+
'200': { description: 'Success' },
|
|
84
|
+
'400': { description: 'Bad Request' },
|
|
85
|
+
'500': { description: 'Internal Server Error' },
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
// For mutating methods, try to extract the first named *Schema export from the
|
|
89
|
+
// compiled route module and convert it to a JSON Schema request body.
|
|
90
|
+
// Requires zod-to-json-schema to be installed — silently skipped otherwise.
|
|
91
|
+
if (method !== 'get' && method !== 'head' && method !== 'options') {
|
|
92
|
+
try {
|
|
93
|
+
const mod = (await import(route.filePath));
|
|
94
|
+
const schemaEntry = Object.entries(mod).find(([k]) => k.endsWith('Schema'));
|
|
95
|
+
if (schemaEntry) {
|
|
96
|
+
// Dynamic import by variable so TypeScript doesn't statically resolve
|
|
97
|
+
// the optional peer dep (zod-to-json-schema may not be installed).
|
|
98
|
+
const depId = 'zod-to-json-schema';
|
|
99
|
+
const ztjs = (await import(depId));
|
|
100
|
+
operation.requestBody = {
|
|
101
|
+
required: true,
|
|
102
|
+
content: {
|
|
103
|
+
'application/json': {
|
|
104
|
+
schema: ztjs.zodToJsonSchema(schemaEntry[1], { target: 'openapi3' }),
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
// zod-to-json-schema not installed or module import failed — skip
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
paths[openApiPath][method] = operation;
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
openapi: '3.0.0',
|
|
118
|
+
info: buildInfo(options.info),
|
|
119
|
+
paths,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* OpenAPI/Swagger middleware. Introspects compiled route files to build an
|
|
124
|
+
* OpenAPI 3.0 spec and serves it at `specPath` (default: `/openapi.json`),
|
|
125
|
+
* plus a Swagger UI page at `path` (default: `/docs`).
|
|
126
|
+
*
|
|
127
|
+
* The spec is generated once on the first request and cached for the lifetime
|
|
128
|
+
* of the process. When `zod-to-json-schema` is installed, mutating routes that
|
|
129
|
+
* export a `*Schema` named export will have their request body documented
|
|
130
|
+
* automatically.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* import { swagger } from '@davaux/swagger'
|
|
134
|
+
* export default swagger({
|
|
135
|
+
* routesDir: './dist/routes',
|
|
136
|
+
* info: { title: 'My API', version: '1.0.0' },
|
|
137
|
+
* })
|
|
138
|
+
*/
|
|
139
|
+
export function swagger(options) {
|
|
140
|
+
const docsPath = options.path ?? '/docs';
|
|
141
|
+
const specPath = options.specPath ?? '/openapi.json';
|
|
142
|
+
const title = options.info?.title ?? 'API Docs';
|
|
143
|
+
// Spec is generated once on the first request and cached for the process lifetime.
|
|
144
|
+
let specPromise = null;
|
|
145
|
+
return async (ctx, next) => {
|
|
146
|
+
const pathname = ctx.req.url?.split('?')[0] ?? '/';
|
|
147
|
+
if (pathname === specPath) {
|
|
148
|
+
if (!specPromise)
|
|
149
|
+
specPromise = buildSpec(options);
|
|
150
|
+
const spec = await specPromise;
|
|
151
|
+
ctx.res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
152
|
+
ctx.res.end(JSON.stringify(spec, null, 2));
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (pathname === docsPath) {
|
|
156
|
+
ctx.res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
157
|
+
ctx.res.end(swaggerHtml(specPath, title));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
return next();
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAyBnC,MAAM,cAAc,GAAuC;IACzD,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,MAAM;IACZ,GAAG,EAAE,KAAK;IACV,KAAK,EAAE,OAAO;IACd,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE,SAAS;IAClB,IAAI,EAAE,SAAS,EAAE,yCAAyC;CAC3D,CAAA;AAED,SAAS,aAAa,CAAC,OAAe;IACpC,OAAO,OAAO;SACX,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAA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AElD,IAAI,QAAQ,KAAK,QAAQ,EAAE,CAAC;YAC1B,IAAI,CAAC,WAAW;gBAAE,WAAW,GAAG,SAAS,CAAC,OAAO,CAAC,CAAA;YAClD,MAAM,IAAI,GAAG,MAAM,WAAW,CAAA;YAC9B,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,iCAAiC,EAAE,CAAC,CAAA;YAC7E,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAA;YAC1C,OAAM;QACR,CAAC;QAED,IAAI,QAAQ,KAAK,QAAQ,EAAE,CAAC;YAC1B,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,0BAA0B,EAAE,CAAC,CAAA;YACtE,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,WAAW,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAA;YACzC,OAAM;QACR,CAAC;QAED,OAAO,IAAI,EAAE,CAAA;IACf,CAAC,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@davaux/swagger",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Auto-generated OpenAPI/Swagger documentation for Davaux APIs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "David L Dyess II",
|
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
"url": "https://codeberg.org/davaux/davaux/issues"
|
|
14
14
|
},
|
|
15
15
|
"homepage": "https://codeberg.org/davaux/davaux#readme",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
16
19
|
"exports": {
|
|
17
20
|
".": {
|
|
18
21
|
"import": "./dist/index.js",
|
|
@@ -24,7 +27,7 @@
|
|
|
24
27
|
"typecheck": "tsc --noEmit"
|
|
25
28
|
},
|
|
26
29
|
"peerDependencies": {
|
|
27
|
-
"davaux": ">=0.8.
|
|
30
|
+
"davaux": ">=0.8.1"
|
|
28
31
|
},
|
|
29
32
|
"peerDependenciesMetadata": {
|
|
30
33
|
"zod-to-json-schema": {
|
|
@@ -36,4 +39,4 @@
|
|
|
36
39
|
"davaux": "*",
|
|
37
40
|
"typescript": "^6.0.3"
|
|
38
41
|
}
|
|
39
|
-
}
|
|
42
|
+
}
|
package/CLAUDE.md
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
<!-- pka-generated -->
|
|
2
|
-
# @davaux/swagger
|
|
3
|
-
|
|
4
|
-
> Generated by Project Knowledge Analyzer on 2026-06-06T21:53:10.470Z
|
|
5
|
-
|
|
6
|
-
## Overview
|
|
7
|
-
|
|
8
|
-
Auto-generated OpenAPI/Swagger documentation for Davaux APIs
|
|
9
|
-
|
|
10
|
-
**Version**: 0.8.0
|
|
11
|
-
**Author**: David L Dyess II
|
|
12
|
-
**License**: MIT
|
|
13
|
-
**Repository**: https://codeberg.org/davaux/davaux#readme
|
|
14
|
-
|
|
15
|
-
## Tech Stack
|
|
16
|
-
|
|
17
|
-
- **Language**: TypeScript
|
|
18
|
-
- **Module System**: ESM (`type: module`)
|
|
19
|
-
|
|
20
|
-
## Commands
|
|
21
|
-
|
|
22
|
-
- `npm run build` — tsc
|
|
23
|
-
- `npm run typecheck` — tsc --noEmit
|
|
24
|
-
|
|
25
|
-
## Project Structure
|
|
26
|
-
|
|
27
|
-
```
|
|
28
|
-
├── CLAUDE.md
|
|
29
|
-
├── README.md
|
|
30
|
-
├── package.json
|
|
31
|
-
├── tsconfig.json
|
|
32
|
-
└── src/
|
|
33
|
-
└── index.ts
|
|
34
|
-
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
## Entry Points
|
|
38
|
-
|
|
39
|
-
- `src/index.ts`
|
|
40
|
-
|
|
41
|
-
## Files by Type
|
|
42
|
-
|
|
43
|
-
### Documentation (2)
|
|
44
|
-
- `CLAUDE.md`
|
|
45
|
-
- `README.md`
|
|
46
|
-
|
|
47
|
-
### Config (2)
|
|
48
|
-
- `package.json`
|
|
49
|
-
- `tsconfig.json`
|
|
50
|
-
|
|
51
|
-
### Module (1)
|
|
52
|
-
- `src/index.ts`
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
## Git
|
|
56
|
-
- **Branch**: main
|
|
57
|
-
- **Last Commit**: chore: Add alpha status note to README
|
|
58
|
-
- **Author**: David Dyess II
|
|
59
|
-
- **Date**: 2026-06-06 15:52:27 -0600
|
|
60
|
-
- **Remote**: https://codeberg.org/davaux/davaux.git
|
|
61
|
-
|
|
62
|
-
### Recent Commits
|
|
63
|
-
```
|
|
64
|
-
f527031 chore: Add alpha status note to README
|
|
65
|
-
90c819e chore: Add repo info to package.json files
|
|
66
|
-
b200d9d feat(davaux)!: Add OmlCacheConfig - opt-in with includes option or opt-out with excludes option; Fix OML implementation to follow OML spec - use output instead of return
|
|
67
|
-
3bce0c2 chore: Update and add READMEs to packages; update ROADMAP
|
|
68
|
-
fc27c20 fix(davaux): Remove old dist folder on new builds; fix server port per DavauxConfig
|
|
69
|
-
c760659 feat(davaux): Add minify CSS in production builds
|
|
70
|
-
c899ab8 fix(davaux): Added method JS and JSX extenstion to scanner
|
|
71
|
-
7d99f04 feat(davaux): Add support for declarative partial updates
|
|
72
|
-
3b47f37 chore: Bump package versions to 0.8.0
|
|
73
|
-
aa0460f chore: Add CHANGELOG.md
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
## Dependencies
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
### Development
|
|
80
|
-
@types/node, davaux, typescript
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
## Exported Symbols
|
|
91
|
-
|
|
92
|
-
**`src/index.ts`**
|
|
93
|
-
`swagger`, `SwaggerInfo`, `SwaggerOptions`
|
|
94
|
-
|
|
95
|
-
|
package/src/index.ts
DELETED
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs'
|
|
2
|
-
import { resolve } from 'node:path'
|
|
3
|
-
import type { MiddlewareFn } from 'davaux'
|
|
4
|
-
|
|
5
|
-
/** OpenAPI metadata used in the `info` block of the generated spec. */
|
|
6
|
-
export interface SwaggerInfo {
|
|
7
|
-
title?: string
|
|
8
|
-
version?: string
|
|
9
|
-
description?: string
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface SwaggerOptions {
|
|
13
|
-
/**
|
|
14
|
-
* Directory containing compiled route files to introspect.
|
|
15
|
-
* In production: the davaux build output directory (default: './dist').
|
|
16
|
-
* In development: './.davaux/routes' (the dev server's compiled output).
|
|
17
|
-
*/
|
|
18
|
-
routesDir: string
|
|
19
|
-
/** OpenAPI info block. */
|
|
20
|
-
info?: SwaggerInfo
|
|
21
|
-
/** URL path for the Swagger UI page. Default: '/docs' */
|
|
22
|
-
path?: string
|
|
23
|
-
/** URL path for the raw OpenAPI JSON spec. Default: '/openapi.json' */
|
|
24
|
-
specPath?: string
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const TYPE_TO_METHOD: Record<string, string | undefined> = {
|
|
28
|
-
get: 'get',
|
|
29
|
-
post: 'post',
|
|
30
|
-
put: 'put',
|
|
31
|
-
patch: 'patch',
|
|
32
|
-
delete: 'delete',
|
|
33
|
-
head: 'head',
|
|
34
|
-
options: 'options',
|
|
35
|
-
page: undefined, // HTML page routes are not API endpoints
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function toOpenApiPath(pattern: string): string {
|
|
39
|
-
return pattern
|
|
40
|
-
.replace(/:(\w+)/g, '{$1}') // :id → {id}
|
|
41
|
-
.replace(/\*(\w+)/g, '{$1}') // *slug → {slug} (catch-all)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function buildInfo(info?: SwaggerInfo): object {
|
|
45
|
-
return {
|
|
46
|
-
title: info?.title ?? 'API',
|
|
47
|
-
version: info?.version ?? '1.0.0',
|
|
48
|
-
...(info?.description ? { description: info.description } : {}),
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function escapeHtml(s: string): string {
|
|
53
|
-
return s
|
|
54
|
-
.replace(/&/g, '&')
|
|
55
|
-
.replace(/</g, '<')
|
|
56
|
-
.replace(/>/g, '>')
|
|
57
|
-
.replace(/"/g, '"')
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function swaggerHtml(specPath: string, title: string): string {
|
|
61
|
-
return `<!DOCTYPE html>
|
|
62
|
-
<html lang="en">
|
|
63
|
-
<head>
|
|
64
|
-
<meta charset="utf-8" />
|
|
65
|
-
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
66
|
-
<title>${escapeHtml(title)}</title>
|
|
67
|
-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.18.2/swagger-ui.css" integrity="sha384-rcbEi6xgdPk0iWkAQzT2F3FeBJXdG+ydrawGlfHAFIZG7wU6aKbQaRewysYpmrlW" crossorigin="anonymous" />
|
|
68
|
-
</head>
|
|
69
|
-
<body>
|
|
70
|
-
<div id="swagger-ui"></div>
|
|
71
|
-
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.18.2/swagger-ui-bundle.js" integrity="sha384-NXtFPpN61oWCuN4D42K6Zd5Rt2+uxeIT36R7kpXBuY9tLnZorzrJ4ykpqwJfgjpZ" crossorigin="anonymous"></script>
|
|
72
|
-
<script>
|
|
73
|
-
window.onload = () => {
|
|
74
|
-
SwaggerUIBundle({
|
|
75
|
-
url: ${JSON.stringify(specPath)},
|
|
76
|
-
dom_id: '#swagger-ui',
|
|
77
|
-
presets: [SwaggerUIBundle.presets.apis, SwaggerUIBundle.SwaggerUIStandalonePreset],
|
|
78
|
-
layout: 'StandaloneLayout',
|
|
79
|
-
deepLinking: true,
|
|
80
|
-
})
|
|
81
|
-
}
|
|
82
|
-
</script>
|
|
83
|
-
</body>
|
|
84
|
-
</html>`
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async function buildSpec(options: SwaggerOptions): Promise<object> {
|
|
88
|
-
const routesDir = resolve(options.routesDir)
|
|
89
|
-
|
|
90
|
-
if (!existsSync(routesDir)) {
|
|
91
|
-
console.warn(`[@davaux/swagger] routesDir not found: ${routesDir} — serving empty spec`)
|
|
92
|
-
return { openapi: '3.0.0', info: buildInfo(options.info), paths: {} }
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const { scanRoutes } = await import('davaux/scanner')
|
|
96
|
-
const { routes } = await scanRoutes(routesDir)
|
|
97
|
-
|
|
98
|
-
const paths: Record<string, Record<string, unknown>> = {}
|
|
99
|
-
|
|
100
|
-
for (const route of routes) {
|
|
101
|
-
const method = TYPE_TO_METHOD[route.type]
|
|
102
|
-
if (!method) continue
|
|
103
|
-
|
|
104
|
-
const openApiPath = toOpenApiPath(route.urlPattern)
|
|
105
|
-
if (!paths[openApiPath]) paths[openApiPath] = {}
|
|
106
|
-
|
|
107
|
-
const parameters = route.params.map((param) => ({
|
|
108
|
-
name: param,
|
|
109
|
-
in: 'path',
|
|
110
|
-
required: true,
|
|
111
|
-
schema: { type: 'string' },
|
|
112
|
-
}))
|
|
113
|
-
|
|
114
|
-
const operation: Record<string, unknown> = {
|
|
115
|
-
parameters,
|
|
116
|
-
responses: {
|
|
117
|
-
'200': { description: 'Success' },
|
|
118
|
-
'400': { description: 'Bad Request' },
|
|
119
|
-
'500': { description: 'Internal Server Error' },
|
|
120
|
-
},
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// For mutating methods, try to extract the first named *Schema export from the
|
|
124
|
-
// compiled route module and convert it to a JSON Schema request body.
|
|
125
|
-
// Requires zod-to-json-schema to be installed — silently skipped otherwise.
|
|
126
|
-
if (method !== 'get' && method !== 'head' && method !== 'options') {
|
|
127
|
-
try {
|
|
128
|
-
const mod = (await import(route.filePath)) as Record<string, unknown>
|
|
129
|
-
const schemaEntry = Object.entries(mod).find(([k]) => k.endsWith('Schema'))
|
|
130
|
-
if (schemaEntry) {
|
|
131
|
-
// Dynamic import by variable so TypeScript doesn't statically resolve
|
|
132
|
-
// the optional peer dep (zod-to-json-schema may not be installed).
|
|
133
|
-
const depId = 'zod-to-json-schema'
|
|
134
|
-
const ztjs = (await import(depId)) as {
|
|
135
|
-
zodToJsonSchema: (schema: unknown, opts?: { target: string }) => unknown
|
|
136
|
-
}
|
|
137
|
-
operation.requestBody = {
|
|
138
|
-
required: true,
|
|
139
|
-
content: {
|
|
140
|
-
'application/json': {
|
|
141
|
-
schema: ztjs.zodToJsonSchema(schemaEntry[1], { target: 'openapi3' }),
|
|
142
|
-
},
|
|
143
|
-
},
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
} catch {
|
|
147
|
-
// zod-to-json-schema not installed or module import failed — skip
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
paths[openApiPath][method] = operation
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return {
|
|
155
|
-
openapi: '3.0.0',
|
|
156
|
-
info: buildInfo(options.info),
|
|
157
|
-
paths,
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* OpenAPI/Swagger middleware. Introspects compiled route files to build an
|
|
163
|
-
* OpenAPI 3.0 spec and serves it at `specPath` (default: `/openapi.json`),
|
|
164
|
-
* plus a Swagger UI page at `path` (default: `/docs`).
|
|
165
|
-
*
|
|
166
|
-
* The spec is generated once on the first request and cached for the lifetime
|
|
167
|
-
* of the process. When `zod-to-json-schema` is installed, mutating routes that
|
|
168
|
-
* export a `*Schema` named export will have their request body documented
|
|
169
|
-
* automatically.
|
|
170
|
-
*
|
|
171
|
-
* @example
|
|
172
|
-
* import { swagger } from '@davaux/swagger'
|
|
173
|
-
* export default swagger({
|
|
174
|
-
* routesDir: './dist/routes',
|
|
175
|
-
* info: { title: 'My API', version: '1.0.0' },
|
|
176
|
-
* })
|
|
177
|
-
*/
|
|
178
|
-
export function swagger(options: SwaggerOptions): MiddlewareFn {
|
|
179
|
-
const docsPath = options.path ?? '/docs'
|
|
180
|
-
const specPath = options.specPath ?? '/openapi.json'
|
|
181
|
-
const title = options.info?.title ?? 'API Docs'
|
|
182
|
-
|
|
183
|
-
// Spec is generated once on the first request and cached for the process lifetime.
|
|
184
|
-
let specPromise: Promise<object> | null = null
|
|
185
|
-
|
|
186
|
-
return async (ctx, next) => {
|
|
187
|
-
const pathname = ctx.req.url?.split('?')[0] ?? '/'
|
|
188
|
-
|
|
189
|
-
if (pathname === specPath) {
|
|
190
|
-
if (!specPromise) specPromise = buildSpec(options)
|
|
191
|
-
const spec = await specPromise
|
|
192
|
-
ctx.res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' })
|
|
193
|
-
ctx.res.end(JSON.stringify(spec, null, 2))
|
|
194
|
-
return
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (pathname === docsPath) {
|
|
198
|
-
ctx.res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
|
|
199
|
-
ctx.res.end(swaggerHtml(specPath, title))
|
|
200
|
-
return
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return next()
|
|
204
|
-
}
|
|
205
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ESNext",
|
|
4
|
-
"module": "NodeNext",
|
|
5
|
-
"moduleResolution": "NodeNext",
|
|
6
|
-
"strict": true,
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"declarationMap": true,
|
|
9
|
-
"sourceMap": true,
|
|
10
|
-
"outDir": "./dist",
|
|
11
|
-
"rootDir": "./src",
|
|
12
|
-
"skipLibCheck": true,
|
|
13
|
-
"lib": ["ESNext"],
|
|
14
|
-
"types": ["node"]
|
|
15
|
-
},
|
|
16
|
-
"include": ["src/**/*"]
|
|
17
|
-
}
|