pwi-plata-type 0.3.3 → 0.3.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/bin/plata-swagger-gen.ts +24 -29
- package/libs/routes.ts +18 -10
- package/libs/swagger.ts +1 -2
- package/package.json +1 -1
package/bin/plata-swagger-gen.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
2
|
import fs from 'node:fs'
|
|
3
|
-
import {
|
|
3
|
+
import { buildExpressRouter } from '../libs/routes.js'
|
|
4
4
|
import { PlataResultado, PlataDirs, PlataFiles, PlataError } from '../libs/tools.js'
|
|
5
5
|
|
|
6
6
|
interface dumpParameter {
|
|
@@ -106,41 +106,36 @@ function loadSwaggerFiles(): swaggerFiles {
|
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
async function dumpRotas(): Promise<dumpRoute[]> {
|
|
109
|
-
const rotas = await
|
|
109
|
+
const rotas = await buildExpressRouter()
|
|
110
110
|
const rotasMetodos: dumpRoute[] = []
|
|
111
111
|
const regex = /:(.*?)\??\//g
|
|
112
|
-
for (const r of rotas) {
|
|
113
|
-
const expressRouter = await r.exports()
|
|
114
112
|
|
|
115
|
-
|
|
113
|
+
if (rotas._routes === undefined) return []
|
|
114
|
+
|
|
115
|
+
for (const r of rotas._routes) {
|
|
116
|
+
if (r.swaggerHide) {
|
|
116
117
|
continue
|
|
117
118
|
}
|
|
118
119
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
required: p[p.length - 2] !== '?',
|
|
132
|
-
in: 'path',
|
|
133
|
-
type: 'string'
|
|
134
|
-
})
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
rotasMetodos.push({
|
|
138
|
-
httpRoute: httpLayerRoute.replace(regex, '{$1}/'),
|
|
139
|
-
params: routeParams,
|
|
140
|
-
metodo,
|
|
141
|
-
})
|
|
142
|
-
}
|
|
120
|
+
r.path = path.normalize(`${r.path ?? '/'}/`).replace(/\\/g, '/')
|
|
121
|
+
|
|
122
|
+
const routeParams: dumpParameter[] = []
|
|
123
|
+
const urlParms = r.path.match(regex) ?? []
|
|
124
|
+
|
|
125
|
+
for (const p of urlParms) {
|
|
126
|
+
routeParams.push({
|
|
127
|
+
name: p.replace(regex, '$1'),
|
|
128
|
+
required: p[p.length - 2] !== '?',
|
|
129
|
+
in: 'path',
|
|
130
|
+
type: 'string'
|
|
131
|
+
})
|
|
143
132
|
}
|
|
133
|
+
|
|
134
|
+
rotasMetodos.push({
|
|
135
|
+
httpRoute: r.path.replace(regex, '{$1}/'),
|
|
136
|
+
metodo: r.method,
|
|
137
|
+
params: routeParams,
|
|
138
|
+
})
|
|
144
139
|
}
|
|
145
140
|
|
|
146
141
|
return rotasMetodos
|
package/libs/routes.ts
CHANGED
|
@@ -69,7 +69,8 @@ export type PlataRouterMethods = (
|
|
|
69
69
|
export interface _PlataRoute {
|
|
70
70
|
path?: string,
|
|
71
71
|
method: HttpMethods
|
|
72
|
-
handlers: PlataRequestHandler[]
|
|
72
|
+
handlers: PlataRequestHandler[],
|
|
73
|
+
swaggerHide?: boolean
|
|
73
74
|
}
|
|
74
75
|
export interface PlataRouterExtras {
|
|
75
76
|
swaggerHide?: boolean
|
|
@@ -235,8 +236,9 @@ export async function loadExpressRoutes(): Promise<PlataRequiredRoute[]> {
|
|
|
235
236
|
}
|
|
236
237
|
}
|
|
237
238
|
|
|
238
|
-
export async function buildExpressRouter(): Promise<
|
|
239
|
-
const router = Router()
|
|
239
|
+
export async function buildExpressRouter(): Promise<_Router> {
|
|
240
|
+
const router = Router() as _Router
|
|
241
|
+
router._routes = []
|
|
240
242
|
router.use(express.json())
|
|
241
243
|
|
|
242
244
|
const routes = await loadExpressRoutes()
|
|
@@ -249,14 +251,20 @@ export async function buildExpressRouter(): Promise<Router> {
|
|
|
249
251
|
for (let i = 0; i < pr._routes.length; i++) {
|
|
250
252
|
const prr = pr._routes[i];
|
|
251
253
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
path.normalize(`${r.httpRoute}/${prr.path}`).replace(/\\/g, '/'),
|
|
257
|
-
...prr.handlers.map(PlataRoute)
|
|
258
|
-
)
|
|
254
|
+
let httpPath = r.httpRoute
|
|
255
|
+
|
|
256
|
+
if (prr.path !== undefined) {
|
|
257
|
+
httpPath = path.normalize(`${r.httpRoute}/${prr.path}`).replace(/\\/g, '/')
|
|
259
258
|
}
|
|
259
|
+
|
|
260
|
+
router[prr.method](httpPath, ...prr.handlers.map(PlataRoute))
|
|
261
|
+
|
|
262
|
+
router._routes.push({
|
|
263
|
+
path: httpPath,
|
|
264
|
+
method: prr.method,
|
|
265
|
+
handlers: [],
|
|
266
|
+
swaggerHide: prr.swaggerHide
|
|
267
|
+
})
|
|
260
268
|
}
|
|
261
269
|
}
|
|
262
270
|
|
package/libs/swagger.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import swaggerUi from 'swagger-ui-express'
|
|
2
|
-
import { Router } from 'express'
|
|
3
2
|
import path from 'node:path'
|
|
4
3
|
import { PlataRouterBuilder, PlataRequestHandler } from './routes.js';
|
|
5
4
|
import { PlataFiles, PlataDirs } from "./tools.js";
|
|
@@ -35,7 +34,7 @@ export namespace PlataSwagger {
|
|
|
35
34
|
const metodo = path.normalize(path.basename(docPath))
|
|
36
35
|
const httpPath = path.normalize(
|
|
37
36
|
path.dirname(docPath.replace(PlataDirs.getProjectDirSwaggerRotas(), ''))
|
|
38
|
-
)
|
|
37
|
+
).replace(/\\/g, '/')
|
|
39
38
|
|
|
40
39
|
const header = PlataFiles.readJsonSync(path.join(docPath, 'header.json'))
|
|
41
40
|
const body = PlataFiles.readJsonSync(path.join(docPath, 'body.json'))
|