pwi-plata-type 0.4.166 → 0.4.167
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 +2 -2
- package/__BUILD__/libs/sql.js +4 -1
- package/__BUILD__/libs/sql.js.map +1 -1
- package/package.json +53 -53
- package/src/@types/build.d.ts +26 -26
- package/src/@types/cluster.actions.d.ts +27 -27
- package/src/@types/cluster.d.ts +55 -55
- package/src/@types/create-cli.d.ts +8 -8
- package/src/@types/exported.d.ts +31 -31
- package/src/@types/fs.d.ts +6 -6
- package/src/@types/global.d.ts +113 -113
- package/src/@types/knex.d.ts +13 -13
- package/src/@types/model.d.ts +143 -143
- package/src/@types/router.d.ts +107 -107
- package/src/@types/sql.d.ts +75 -75
- package/src/@types/swagger.d.ts +22 -22
- package/src/bin/extras/create-api-cli.ts +218 -218
- package/src/bin/extras/create-cluster-cli.ts +160 -160
- package/src/bin/extras/create-lib-cli.ts +84 -84
- package/src/bin/extras/plata-create-tools.ts +97 -97
- package/src/bin/plata-api.ts +189 -189
- package/src/bin/plata-build.ts +17 -17
- package/src/bin/plata-cluster.ts +42 -42
- package/src/bin/plata-cmd.ts +18 -18
- package/src/bin/plata-create.ts +49 -49
- package/src/bin/plata-runtime.ts +131 -131
- package/src/bin/plata-swagger.ts +99 -99
- package/src/bin/runtime/_setupRuntime.ts +48 -48
- package/src/bin/runtime/_types.d.ts +7 -7
- package/src/bin/runtime/class.ts +20 -20
- package/src/bin/runtime/dirs.ts +25 -25
- package/src/bin/runtime/effect.ts +60 -60
- package/src/bin/runtime/fs.ts +10 -10
- package/src/bin/runtime/promises.ts +18 -18
- package/src/bin/runtime/require.ts +113 -113
- package/src/bin/runtime/sql.ts +12 -12
- package/src/bin/runtime/switch.ts +60 -60
- package/src/index.ts +19 -19
- package/src/libs/axios.ts +51 -51
- package/src/libs/build.ts +162 -162
- package/src/libs/cluster/cluster-controller.ts +93 -93
- package/src/libs/cluster.ts +330 -330
- package/src/libs/custom.ts +15 -15
- package/src/libs/env.ts +10 -10
- package/src/libs/fs.ts +399 -399
- package/src/libs/hot-reload.ts +63 -63
- package/src/libs/models/index.ts +4 -4
- package/src/libs/models/model-template-combine.ts +93 -93
- package/src/libs/models/model-template-env.ts +58 -58
- package/src/libs/models/model-template-union.ts +83 -83
- package/src/libs/models/model-template.ts +107 -107
- package/src/libs/models/model-validations.ts +443 -443
- package/src/libs/models/tools.ts +194 -194
- package/src/libs/router.ts +185 -185
- package/src/libs/sql.ts +792 -788
- package/src/libs/swagger.ts +81 -81
- package/src/libs/tools.ts +240 -240
- package/src/scripts/post-install/api.ts +32 -32
- package/src/scripts/post-install/cluster.ts +28 -28
- package/src/scripts/post-install/lib.ts +25 -25
- package/src/scripts/post-install.ts +64 -64
- package/src/scripts/pre-test.ts +33 -33
- package/src/tests/assets/sql/example.sql +76 -76
- package/src/tests/config.test.ts +12 -12
- package/src/tests/effect.test.ts +62 -62
- package/src/tests/model.env.test.ts +66 -66
- package/src/tests/model.test.ts +141 -141
- package/src/tests/model.union.test.ts +160 -160
- package/src/tests/router.test.ts +44 -44
- package/src/tests/sql.test.ts +502 -502
- package/src/tests/switch.test.ts +76 -76
- package/templates/create-cli/class.ts +13 -13
- package/templates/create-cli/cluster.ts +14 -14
- package/templates/create-cli/cmd.ts +3 -3
- package/templates/create-cli/config.ts +2 -2
- package/templates/create-cli/lib.ts +3 -3
- package/templates/create-cli/model.ts +14 -14
- package/templates/create-cli/route.ts +8 -8
- package/templates/create-cli/task.ts +13 -13
- package/templates/postinstall/api/.vscode/launch.json +20 -20
- package/templates/postinstall/api/Dockerfile +24 -24
- package/templates/postinstall/api/configs/env.ts +2 -2
- package/templates/postinstall/api/envs/debug.env +15 -15
- package/templates/postinstall/api/envs/homolog.env +15 -15
- package/templates/postinstall/api/envs/prod.env +15 -15
- package/templates/postinstall/api/tsconfig.json +24 -24
- package/templates/postinstall/cluster/.vscode/launch.json +20 -20
- package/templates/postinstall/cluster/Dockerfile +24 -24
- package/templates/postinstall/cluster/configs/env.ts +2 -2
- package/templates/postinstall/cluster/envs/debug.env +10 -10
- package/templates/postinstall/cluster/envs/homolog.env +10 -10
- package/templates/postinstall/cluster/envs/prod.env +10 -10
- package/templates/postinstall/cluster/tsconfig.json +24 -24
- package/templates/postinstall/lib/postinstall-tools.mjs +111 -111
- package/templates/postinstall/lib/postinstall.js +33 -33
- package/templates/postinstall/lib/tsconfig.json +22 -22
- package/templates/swagger-cli/header.json +21 -21
- package/templates/swagger-cli/route/body.json +14 -14
- package/templates/swagger-cli/route/header.json +18 -18
- package/templates/swagger-cli/route/response.json +53 -53
- package/tsconfig.json +28 -28
package/src/libs/router.ts
CHANGED
|
@@ -1,186 +1,186 @@
|
|
|
1
|
-
import path from 'node:path'
|
|
2
|
-
import express from 'express'
|
|
3
|
-
import { getCustomExpress } from './custom'
|
|
4
|
-
|
|
5
|
-
export namespace Internals {
|
|
6
|
-
export function buildRouterObject(fileRoute: string): Router.Router {
|
|
7
|
-
const router: Router.RouterConfig = {
|
|
8
|
-
routes: [],
|
|
9
|
-
swaggerHide: false,
|
|
10
|
-
httpRoute: fileRoute,
|
|
11
|
-
mark: Object.create(null)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return new Proxy(router, {
|
|
15
|
-
get: (targetRouter, prop) => {
|
|
16
|
-
if (targetRouter[prop] !== undefined) {
|
|
17
|
-
return targetRouter[prop]
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (prop === 'then') {
|
|
21
|
-
return targetRouter
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return (httpPathOrHandler: string | Router.RequestHandler, ...handlers: Router.RequestHandler[]) => {
|
|
25
|
-
let httpPath: string | undefined = undefined
|
|
26
|
-
|
|
27
|
-
if (typeof httpPathOrHandler === 'string') {
|
|
28
|
-
httpPath = httpPathOrHandler
|
|
29
|
-
} else {
|
|
30
|
-
handlers = [ httpPathOrHandler, ...handlers ]
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
targetRouter.routes.push({
|
|
34
|
-
method: prop.toString(),
|
|
35
|
-
handlers,
|
|
36
|
-
path: httpPath
|
|
37
|
-
})
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
|
|
41
|
-
set: (targetRouter, prop, value) => {
|
|
42
|
-
if (targetRouter[prop] !== undefined) {
|
|
43
|
-
targetRouter[prop] = value
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return true
|
|
47
|
-
}
|
|
48
|
-
}) as Router.Router
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export const ResolveOnResponseEventQueue = async(aReq: Router.Request, aRes: Router.Response, body?: any, error?: PlataError | PlataError[]) => {
|
|
52
|
-
if (aReq._onResponseEvent.length === 0) {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
aReq._onResponseEvent.forEach((r) => {
|
|
58
|
-
Plata.FastPromise(() => r(aReq, aRes, body, error)).catch(() => undefined)
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function buildRequestHandler(r: Router.RequestHandler | Router.RequestHandler[]): express.RequestHandler | express.RequestHandler[] {
|
|
64
|
-
if (Array.isArray(r)) {
|
|
65
|
-
return r.map(h => buildRequestHandler(h)) as express.RequestHandler[]
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return async (req, res, next) => {
|
|
69
|
-
const aReq = req as Router.Request
|
|
70
|
-
const aRes = res as Router.Response
|
|
71
|
-
|
|
72
|
-
if (aReq.user === undefined) {
|
|
73
|
-
aReq.user = Object.create(null)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (aReq.extras === undefined) {
|
|
77
|
-
aReq.extras = Object.create(null)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (aReq._onResponseEvent === undefined) {
|
|
81
|
-
aReq._onResponseEvent = []
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (aRes.addOnResponseEvent === undefined) {
|
|
85
|
-
aRes.addOnResponseEvent = (callback) => {
|
|
86
|
-
aReq._onResponseEvent.push(callback)
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (aRes.oldJson === undefined) {
|
|
91
|
-
aRes.oldJson = aRes.json
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
aRes.json = (body?: any) => {
|
|
95
|
-
Plata.FastPromise(async () => { ResolveOnResponseEventQueue(aReq, aRes, body, undefined) })
|
|
96
|
-
return aRes.oldJson(body)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
aRes.error = error => {
|
|
100
|
-
Plata.FastPromise(async () => { ResolveOnResponseEventQueue(aReq, aRes, undefined, error)})
|
|
101
|
-
return aRes.oldJson(error)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return r(aReq, aRes, next)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export function processRoutes(routes: RequiredInterface<{default: Router.RouterBuilder}>[]): Router.RequiredRoute[] {
|
|
110
|
-
const indexRoutes: Router.RequiredRoute[] = []
|
|
111
|
-
const normalRoutes: Router.RequiredRoute[] = []
|
|
112
|
-
|
|
113
|
-
for (let i = routes.length - 1; i >= 0; i--) {
|
|
114
|
-
const route = routes[i]
|
|
115
|
-
|
|
116
|
-
const r: Router.RequiredRoute = {
|
|
117
|
-
...route,
|
|
118
|
-
exports: route.exports.default,
|
|
119
|
-
httpRoute: path.normalize(route.filePath
|
|
120
|
-
.replace('__BUILD__', '')
|
|
121
|
-
.replace(Plata.ProjectDir.replace(/\\/g, '/'), '')
|
|
122
|
-
.replace('routes', '')
|
|
123
|
-
.replace(/(\.ts|\.js)$/, '')
|
|
124
|
-
.replace(/index/, '')
|
|
125
|
-
.replace(/\{(.*?)\}/g, ':$1')
|
|
126
|
-
.replace(/\[(.*?)\]/g, ':$1?')
|
|
127
|
-
).replace(/\\/g, '/'),
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (r.name === 'index') {
|
|
131
|
-
indexRoutes.push(r)
|
|
132
|
-
continue
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
normalRoutes.push(r)
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
normalRoutes.unshift(...indexRoutes.sort((x, y) => x.httpRoute.length - y.httpRoute.length))
|
|
139
|
-
|
|
140
|
-
return normalRoutes
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export async function loadRoutesFromProject(): PlataPromise<Router.RequiredRoute[]> {
|
|
144
|
-
const routes: RequiredFolder<{default: Router.RouterBuilder}> = await require('@@/routes/*.*')
|
|
145
|
-
|
|
146
|
-
if (routes.errorID !== undefined) {
|
|
147
|
-
return routes
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return processRoutes(routes)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export async function buildExpressRouter(requiredRoutes: Router.RequiredRoute[]): PlataPromise<Router.Router> {
|
|
154
|
-
const router = await getCustomExpress() as any as Router.Router
|
|
155
|
-
|
|
156
|
-
router.routes = []
|
|
157
|
-
router.swaggerHide = false
|
|
158
|
-
|
|
159
|
-
for (const r of requiredRoutes) {
|
|
160
|
-
const v = await r.exports(Internals.buildRouterObject(r.httpRoute))
|
|
161
|
-
|
|
162
|
-
const routes = v.routes
|
|
163
|
-
const swaggerHide = v.swaggerHide
|
|
164
|
-
|
|
165
|
-
for (let i = 0; i < routes.length; i++) {
|
|
166
|
-
const route = routes[i]
|
|
167
|
-
|
|
168
|
-
let httpPath = r.httpRoute
|
|
169
|
-
|
|
170
|
-
if (route.path !== undefined) {
|
|
171
|
-
httpPath = path.normalize(`${httpPath}/${route.path}`).replace(/\\/g, '/')
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
router[route.method](httpPath, ...route.handlers.map(Internals.buildRequestHandler))
|
|
175
|
-
|
|
176
|
-
router.routes.push({
|
|
177
|
-
path: httpPath,
|
|
178
|
-
method: route.method,
|
|
179
|
-
handlers: [],
|
|
180
|
-
swaggerHide: swaggerHide
|
|
181
|
-
})
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return router
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import express from 'express'
|
|
3
|
+
import { getCustomExpress } from './custom'
|
|
4
|
+
|
|
5
|
+
export namespace Internals {
|
|
6
|
+
export function buildRouterObject(fileRoute: string): Router.Router {
|
|
7
|
+
const router: Router.RouterConfig = {
|
|
8
|
+
routes: [],
|
|
9
|
+
swaggerHide: false,
|
|
10
|
+
httpRoute: fileRoute,
|
|
11
|
+
mark: Object.create(null)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return new Proxy(router, {
|
|
15
|
+
get: (targetRouter, prop) => {
|
|
16
|
+
if (targetRouter[prop] !== undefined) {
|
|
17
|
+
return targetRouter[prop]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (prop === 'then') {
|
|
21
|
+
return targetRouter
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return (httpPathOrHandler: string | Router.RequestHandler, ...handlers: Router.RequestHandler[]) => {
|
|
25
|
+
let httpPath: string | undefined = undefined
|
|
26
|
+
|
|
27
|
+
if (typeof httpPathOrHandler === 'string') {
|
|
28
|
+
httpPath = httpPathOrHandler
|
|
29
|
+
} else {
|
|
30
|
+
handlers = [ httpPathOrHandler, ...handlers ]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
targetRouter.routes.push({
|
|
34
|
+
method: prop.toString(),
|
|
35
|
+
handlers,
|
|
36
|
+
path: httpPath
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
set: (targetRouter, prop, value) => {
|
|
42
|
+
if (targetRouter[prop] !== undefined) {
|
|
43
|
+
targetRouter[prop] = value
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return true
|
|
47
|
+
}
|
|
48
|
+
}) as Router.Router
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const ResolveOnResponseEventQueue = async(aReq: Router.Request, aRes: Router.Response, body?: any, error?: PlataError | PlataError[]) => {
|
|
52
|
+
if (aReq._onResponseEvent.length === 0) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
aReq._onResponseEvent.forEach((r) => {
|
|
58
|
+
Plata.FastPromise(() => r(aReq, aRes, body, error)).catch(() => undefined)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function buildRequestHandler(r: Router.RequestHandler | Router.RequestHandler[]): express.RequestHandler | express.RequestHandler[] {
|
|
64
|
+
if (Array.isArray(r)) {
|
|
65
|
+
return r.map(h => buildRequestHandler(h)) as express.RequestHandler[]
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return async (req, res, next) => {
|
|
69
|
+
const aReq = req as Router.Request
|
|
70
|
+
const aRes = res as Router.Response
|
|
71
|
+
|
|
72
|
+
if (aReq.user === undefined) {
|
|
73
|
+
aReq.user = Object.create(null)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (aReq.extras === undefined) {
|
|
77
|
+
aReq.extras = Object.create(null)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (aReq._onResponseEvent === undefined) {
|
|
81
|
+
aReq._onResponseEvent = []
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (aRes.addOnResponseEvent === undefined) {
|
|
85
|
+
aRes.addOnResponseEvent = (callback) => {
|
|
86
|
+
aReq._onResponseEvent.push(callback)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (aRes.oldJson === undefined) {
|
|
91
|
+
aRes.oldJson = aRes.json
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
aRes.json = (body?: any) => {
|
|
95
|
+
Plata.FastPromise(async () => { ResolveOnResponseEventQueue(aReq, aRes, body, undefined) })
|
|
96
|
+
return aRes.oldJson(body)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
aRes.error = error => {
|
|
100
|
+
Plata.FastPromise(async () => { ResolveOnResponseEventQueue(aReq, aRes, undefined, error)})
|
|
101
|
+
return aRes.oldJson(error)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return r(aReq, aRes, next)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function processRoutes(routes: RequiredInterface<{default: Router.RouterBuilder}>[]): Router.RequiredRoute[] {
|
|
110
|
+
const indexRoutes: Router.RequiredRoute[] = []
|
|
111
|
+
const normalRoutes: Router.RequiredRoute[] = []
|
|
112
|
+
|
|
113
|
+
for (let i = routes.length - 1; i >= 0; i--) {
|
|
114
|
+
const route = routes[i]
|
|
115
|
+
|
|
116
|
+
const r: Router.RequiredRoute = {
|
|
117
|
+
...route,
|
|
118
|
+
exports: route.exports.default,
|
|
119
|
+
httpRoute: path.normalize(route.filePath
|
|
120
|
+
.replace('__BUILD__', '')
|
|
121
|
+
.replace(Plata.ProjectDir.replace(/\\/g, '/'), '')
|
|
122
|
+
.replace('routes', '')
|
|
123
|
+
.replace(/(\.ts|\.js)$/, '')
|
|
124
|
+
.replace(/index/, '')
|
|
125
|
+
.replace(/\{(.*?)\}/g, ':$1')
|
|
126
|
+
.replace(/\[(.*?)\]/g, ':$1?')
|
|
127
|
+
).replace(/\\/g, '/'),
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (r.name === 'index') {
|
|
131
|
+
indexRoutes.push(r)
|
|
132
|
+
continue
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
normalRoutes.push(r)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
normalRoutes.unshift(...indexRoutes.sort((x, y) => x.httpRoute.length - y.httpRoute.length))
|
|
139
|
+
|
|
140
|
+
return normalRoutes
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export async function loadRoutesFromProject(): PlataPromise<Router.RequiredRoute[]> {
|
|
144
|
+
const routes: RequiredFolder<{default: Router.RouterBuilder}> = await require('@@/routes/*.*')
|
|
145
|
+
|
|
146
|
+
if (routes.errorID !== undefined) {
|
|
147
|
+
return routes
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return processRoutes(routes)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export async function buildExpressRouter(requiredRoutes: Router.RequiredRoute[]): PlataPromise<Router.Router> {
|
|
154
|
+
const router = await getCustomExpress() as any as Router.Router
|
|
155
|
+
|
|
156
|
+
router.routes = []
|
|
157
|
+
router.swaggerHide = false
|
|
158
|
+
|
|
159
|
+
for (const r of requiredRoutes) {
|
|
160
|
+
const v = await r.exports(Internals.buildRouterObject(r.httpRoute))
|
|
161
|
+
|
|
162
|
+
const routes = v.routes
|
|
163
|
+
const swaggerHide = v.swaggerHide
|
|
164
|
+
|
|
165
|
+
for (let i = 0; i < routes.length; i++) {
|
|
166
|
+
const route = routes[i]
|
|
167
|
+
|
|
168
|
+
let httpPath = r.httpRoute
|
|
169
|
+
|
|
170
|
+
if (route.path !== undefined) {
|
|
171
|
+
httpPath = path.normalize(`${httpPath}/${route.path}`).replace(/\\/g, '/')
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
router[route.method](httpPath, ...route.handlers.map(Internals.buildRequestHandler))
|
|
175
|
+
|
|
176
|
+
router.routes.push({
|
|
177
|
+
path: httpPath,
|
|
178
|
+
method: route.method,
|
|
179
|
+
handlers: [],
|
|
180
|
+
swaggerHide: swaggerHide
|
|
181
|
+
})
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return router
|
|
186
186
|
}
|