pwi-plata-type 0.2.4 → 0.2.7
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/extras/plata-create-api.ts +4 -4
- package/bin/plata.ts +15 -8
- package/index.ts +2 -0
- package/libs/model.ts +69 -14
- package/libs/routes.ts +71 -3
- package/libs/tools.ts +7 -1
- package/package.json +1 -1
- package/templates/create-cli/lib.ts +3 -1
- package/templates/create-cli/route.ts +4 -2
|
@@ -8,7 +8,7 @@ export async function h() {
|
|
|
8
8
|
|
|
9
9
|
switch (command) {
|
|
10
10
|
case 'lib':
|
|
11
|
-
const nameLib = args.shift()
|
|
11
|
+
const nameLib = `${args.shift()}`
|
|
12
12
|
promises.push(tools.files.deployFileToProject(
|
|
13
13
|
'configs',
|
|
14
14
|
'config',
|
|
@@ -24,7 +24,7 @@ export async function h() {
|
|
|
24
24
|
).finally(() => console.log(`Lib ${nameLib.toLowerCase()} criada`)))
|
|
25
25
|
break;
|
|
26
26
|
case 'config':
|
|
27
|
-
const nameConfig = args.shift()
|
|
27
|
+
const nameConfig = `${args.shift()}`
|
|
28
28
|
promises.push(tools.files.deployFileToProject(
|
|
29
29
|
'configs',
|
|
30
30
|
'config',
|
|
@@ -45,14 +45,14 @@ export async function h() {
|
|
|
45
45
|
routeFilePath,
|
|
46
46
|
'route',
|
|
47
47
|
`${routeFileName}.ts`,
|
|
48
|
-
{}
|
|
48
|
+
{ PlataName: `${PlataDirs.ProjectJson.plata_name}` }
|
|
49
49
|
).finally(() => console.log(
|
|
50
50
|
`Rota ${routeFilePath}/${routeFileName === 'index' ? '' : routeFileName} criada`
|
|
51
51
|
)))
|
|
52
52
|
|
|
53
53
|
break;
|
|
54
54
|
case 'model':
|
|
55
|
-
const modelName = args.shift()
|
|
55
|
+
const modelName = `${args.shift()}`
|
|
56
56
|
const plataName = PlataDirs.ProjectJson.plata_name
|
|
57
57
|
|
|
58
58
|
promises.push(tools.files.deployFileToProject(
|
package/bin/plata.ts
CHANGED
|
@@ -21,8 +21,8 @@ const main = async () => {
|
|
|
21
21
|
|
|
22
22
|
clusterLib.run({
|
|
23
23
|
name: '_plata_express',
|
|
24
|
-
onStart: async () =>
|
|
25
|
-
env: process.env.ENV
|
|
24
|
+
onStart: async () => {},
|
|
25
|
+
env: process.env.ENV ?? 'prod'
|
|
26
26
|
})
|
|
27
27
|
|
|
28
28
|
clusterLib.loadClusters()
|
|
@@ -34,13 +34,20 @@ const main = async () => {
|
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
if (process.env._plata_name === '_plata_express') {
|
|
37
|
-
process.on('exit', () =>
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
action: 'KILL'
|
|
37
|
+
process.on('exit', () => {
|
|
38
|
+
if (process.send === undefined) {
|
|
39
|
+
console.log('Erro ao iniciar o cluster verifique a versão do node')
|
|
40
|
+
return
|
|
42
41
|
}
|
|
43
|
-
|
|
42
|
+
|
|
43
|
+
process.send({
|
|
44
|
+
id: 0,
|
|
45
|
+
name: '_plata',
|
|
46
|
+
data: {
|
|
47
|
+
action: 'KILL'
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
})
|
|
44
51
|
|
|
45
52
|
const port = process.env.PORT ?? 3050
|
|
46
53
|
const app: Express = express()
|
package/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as PlataCluster from './libs/cluster.js'
|
|
2
2
|
import * as PlataEnv from './libs/env.js'
|
|
3
3
|
import * as PlataRoutes from './libs/routes.js'
|
|
4
|
+
import { PlataRoute } from './libs/routes.js'
|
|
4
5
|
import * as PlataTools from './libs/tools.js'
|
|
5
6
|
import * as PlataModels from './libs/model.js'
|
|
6
7
|
|
|
@@ -10,4 +11,5 @@ export {
|
|
|
10
11
|
PlataRoutes,
|
|
11
12
|
PlataTools,
|
|
12
13
|
PlataModels,
|
|
14
|
+
PlataRoute,
|
|
13
15
|
}
|
package/libs/model.ts
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
errorID: string
|
|
3
|
-
error?: any
|
|
4
|
-
msg: string
|
|
5
|
-
}
|
|
1
|
+
import { PlataError } from "./tools.js"
|
|
6
2
|
|
|
7
3
|
export interface RetornoValidacaoPrimitivo {
|
|
8
|
-
error?:
|
|
4
|
+
error?: PlataError
|
|
9
5
|
continua: boolean
|
|
10
6
|
}
|
|
11
7
|
|
|
12
8
|
export interface RetornoValicadaoModel {
|
|
13
9
|
value: any,
|
|
14
|
-
errors:
|
|
10
|
+
errors: PlataError[]
|
|
15
11
|
}
|
|
16
12
|
|
|
17
|
-
export type ModelValicacao = (value: any) => Promise<
|
|
13
|
+
export type ModelValicacao = (value: any) => Promise<PlataError | null>
|
|
18
14
|
|
|
19
15
|
export type ModelValicacaoPrimitivo = (nome: string, valor: any) => Promise<RetornoValidacaoPrimitivo>
|
|
20
16
|
|
|
@@ -36,7 +32,7 @@ export class Model {
|
|
|
36
32
|
}
|
|
37
33
|
}
|
|
38
34
|
|
|
39
|
-
export async function _validaCampo(value: any, pipeline: ModelValicacaoPrimitivo[], name: string): Promise<
|
|
35
|
+
export async function _validaCampo(value: any, pipeline: ModelValicacaoPrimitivo[], name: string): Promise<PlataError | null> {
|
|
40
36
|
for (let i = 0; i < pipeline.length; i++) {
|
|
41
37
|
const result = await pipeline[i](name, value)
|
|
42
38
|
|
|
@@ -90,7 +86,7 @@ export function _filtraModel(value: any, model: Model): any {
|
|
|
90
86
|
return value
|
|
91
87
|
}
|
|
92
88
|
|
|
93
|
-
export async function _validaModel(value: any, model: Model, name?: string): Promise<
|
|
89
|
+
export async function _validaModel(value: any, model: Model, name?: string): Promise<PlataError[]> {
|
|
94
90
|
let promises: any[] = []
|
|
95
91
|
|
|
96
92
|
for(const campo in model.template) {
|
|
@@ -139,11 +135,11 @@ export async function _validaModel(value: any, model: Model, name?: string): Pro
|
|
|
139
135
|
)
|
|
140
136
|
|
|
141
137
|
if ((Erros as any).errorID !== undefined) {
|
|
142
|
-
return Erros as
|
|
138
|
+
return Erros as PlataError[]
|
|
143
139
|
}
|
|
144
140
|
|
|
145
141
|
if ((Erros as any[]).length !== 0) {
|
|
146
|
-
return Erros as
|
|
142
|
+
return Erros as PlataError[]
|
|
147
143
|
}
|
|
148
144
|
|
|
149
145
|
promises = []
|
|
@@ -164,10 +160,10 @@ export async function _validaModel(value: any, model: Model, name?: string): Pro
|
|
|
164
160
|
)
|
|
165
161
|
|
|
166
162
|
if ((Erros as any).errorID !== undefined) {
|
|
167
|
-
return Erros as
|
|
163
|
+
return Erros as PlataError[]
|
|
168
164
|
}
|
|
169
165
|
|
|
170
|
-
return Erros as
|
|
166
|
+
return Erros as PlataError[]
|
|
171
167
|
}
|
|
172
168
|
|
|
173
169
|
export async function valida(value: any, model: Model): Promise<RetornoValicadaoModel> {
|
|
@@ -450,6 +446,65 @@ export function Telefone(): ModelValicacaoPrimitivo {
|
|
|
450
446
|
}
|
|
451
447
|
}
|
|
452
448
|
|
|
449
|
+
export function TelefoneDDI(): ModelValicacaoPrimitivo {
|
|
450
|
+
return async (nome: string, valor: any) => {
|
|
451
|
+
const [ ddi, t ] = `${valor}`.split(' ')
|
|
452
|
+
const telefone = `${t}`.split('-')
|
|
453
|
+
|
|
454
|
+
if (telefone.length !== 2) {
|
|
455
|
+
return {
|
|
456
|
+
error: {
|
|
457
|
+
errorID: "BPVTELD001",
|
|
458
|
+
msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
|
|
459
|
+
},
|
|
460
|
+
continua: false
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (telefone[0].length !== 2 || telefone[1].length > 9) {
|
|
465
|
+
return {
|
|
466
|
+
error: {
|
|
467
|
+
errorID: "BPVTELD002",
|
|
468
|
+
msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
|
|
469
|
+
},
|
|
470
|
+
continua: false
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (!+telefone[0] || !+telefone[1]) {
|
|
475
|
+
return {
|
|
476
|
+
error: {
|
|
477
|
+
errorID: "BPVTELD003",
|
|
478
|
+
msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
|
|
479
|
+
},
|
|
480
|
+
continua: false
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
if (ddi.length !== 4) {
|
|
485
|
+
return {
|
|
486
|
+
error: {
|
|
487
|
+
errorID: "BPVTELD004",
|
|
488
|
+
msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
|
|
489
|
+
},
|
|
490
|
+
continua: false
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (ddi[0] !== '+' || !+ddi) {
|
|
495
|
+
return {
|
|
496
|
+
error: {
|
|
497
|
+
errorID: "BPVTELD005",
|
|
498
|
+
msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
|
|
499
|
+
},
|
|
500
|
+
continua: false
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
return { continua: true }
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
453
508
|
export function VarChar(size: number): ModelValicacaoPrimitivo {
|
|
454
509
|
return async (nome: string, valor: any) => {
|
|
455
510
|
if (valor.length > size) {
|
package/libs/routes.ts
CHANGED
|
@@ -1,8 +1,19 @@
|
|
|
1
|
-
import { PlataDirs, PlataRequire } from "./tools.js";
|
|
2
|
-
import express, {
|
|
1
|
+
import { PlataDirs, PlataRequire, PlataError } from "./tools.js";
|
|
2
|
+
import express, { Router } from "express";
|
|
3
3
|
|
|
4
4
|
export type PlataRouter = () => Router
|
|
5
5
|
|
|
6
|
+
export type PlataRequest = express.Request & { user: any, extras: any }
|
|
7
|
+
|
|
8
|
+
export type PlataOnResponse = (req: PlataRequest, response?: any, error?: PlataError, nodeError?: any) => Promise<any>
|
|
9
|
+
export interface PlataResponse extends express.Response {
|
|
10
|
+
_onResponse: PlataOnResponse[]
|
|
11
|
+
_oldJson(body?: any): this
|
|
12
|
+
_onResponseEvent(req: PlataRequest, response?: any, error?: PlataError, nodeError?: any): void
|
|
13
|
+
error(error: PlataError, nodeError?: any): this
|
|
14
|
+
addOnResponse(callback: PlataOnResponse): void
|
|
15
|
+
}
|
|
16
|
+
|
|
6
17
|
export async function loadExpressRoutes(): Promise<Router> {
|
|
7
18
|
const promise = PlataRequire.requireFolderAsync<PlataRouter>(PlataDirs.getProjectRoutesDir())
|
|
8
19
|
|
|
@@ -12,7 +23,11 @@ export async function loadExpressRoutes(): Promise<Router> {
|
|
|
12
23
|
const routes = await promise.catch(err => { return { error: err } })
|
|
13
24
|
|
|
14
25
|
if ((routes as any).error === undefined) {
|
|
15
|
-
|
|
26
|
+
let r = (routes as PlataRequire.Required<PlataRouter>[]).sort((x,y) => {
|
|
27
|
+
if (x.name === 'index') return -1
|
|
28
|
+
|
|
29
|
+
return +(x.name > y.name)
|
|
30
|
+
})
|
|
16
31
|
|
|
17
32
|
for (let i = 0; i < r.length; i++) {
|
|
18
33
|
const element = r[i];
|
|
@@ -31,4 +46,57 @@ export async function loadExpressRoutes(): Promise<Router> {
|
|
|
31
46
|
}
|
|
32
47
|
|
|
33
48
|
return router
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type PlataRequestHandler = (req: PlataRequest, res: PlataResponse, next: express.NextFunction) => Promise<any>
|
|
52
|
+
|
|
53
|
+
export function PlataRoute(r: PlataRequestHandler): express.RequestHandler {
|
|
54
|
+
return async (req, res, next) => {
|
|
55
|
+
const aReq = req as any
|
|
56
|
+
|
|
57
|
+
if (aReq.user === undefined) {
|
|
58
|
+
aReq.user = new Object(null)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (aReq.extras === undefined) {
|
|
62
|
+
aReq.extras = new Object(null)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const aRes = res as any
|
|
66
|
+
|
|
67
|
+
if (aRes._onResponse === undefined) {
|
|
68
|
+
aRes._onResponse = []
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (aRes.addOnResponse === undefined) {
|
|
72
|
+
aRes.addOnResponse = aRes._onResponse.push
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (aRes._oldJson === undefined) {
|
|
76
|
+
aRes._oldJson = aRes.json
|
|
77
|
+
|
|
78
|
+
aRes._onResponseEvent = (...a) => {
|
|
79
|
+
aRes._onResponse.forEach(r => {
|
|
80
|
+
try {
|
|
81
|
+
r(...a)
|
|
82
|
+
} catch (e) {}
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
aRes.json = (body) => {
|
|
87
|
+
(async () => { aRes._onResponseEvent(aReq, body, undefined, undefined) })
|
|
88
|
+
|
|
89
|
+
return aRes._oldJson(body)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
aRes.error = (error, nodeError) => {
|
|
93
|
+
(async () => { aRes._onResponseEvent(aReq, undefined, error, nodeError) })
|
|
94
|
+
|
|
95
|
+
return aRes._oldJson(error)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
return r(aReq, aRes, next)
|
|
101
|
+
}
|
|
34
102
|
}
|
package/libs/tools.ts
CHANGED
|
@@ -5,6 +5,12 @@ import * as tGlob from 'glob'
|
|
|
5
5
|
|
|
6
6
|
const glob = (tGlob as any).default as typeof tGlob
|
|
7
7
|
|
|
8
|
+
export interface PlataError {
|
|
9
|
+
errorID: string,
|
|
10
|
+
msg: string,
|
|
11
|
+
error?: any
|
|
12
|
+
}
|
|
13
|
+
|
|
8
14
|
export module PlataDirs {
|
|
9
15
|
export function getProjectDir(): string {
|
|
10
16
|
return path.resolve('.')
|
|
@@ -92,7 +98,7 @@ export module PlataFiles {
|
|
|
92
98
|
export function readFileSync(file: string, callback: readLineCallbackSync): Promise<string | { err: any }> {
|
|
93
99
|
return new Promise(resolve => {
|
|
94
100
|
try {
|
|
95
|
-
const content = []
|
|
101
|
+
const content: string[] = []
|
|
96
102
|
const stream = readline.createInterface({
|
|
97
103
|
input: fs.createReadStream(file),
|
|
98
104
|
crlfDelay: Infinity
|
package/package.json
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
import * as ç__Name__çConfig from '../configs/ç__name__ç.js'
|
|
3
3
|
|
|
4
4
|
export class ç__Name__ç {
|
|
5
|
-
|
|
5
|
+
private readonly config: typeof ç__Name__çConfig
|
|
6
6
|
|
|
7
|
+
constructor(config: typeof ç__Name__çConfig.default) {
|
|
8
|
+
this.config = config
|
|
7
9
|
}
|
|
8
10
|
}
|
|
9
11
|
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
//@ts-nocheck
|
|
1
2
|
import { Router } from 'express'
|
|
3
|
+
import { PlataRoute } from 'ç__PlataName__ç'
|
|
2
4
|
|
|
3
5
|
export default (): Router => {
|
|
4
6
|
const router = Router()
|
|
5
7
|
|
|
6
|
-
router.get('/', (req, res) => {
|
|
8
|
+
router.get('/', PlataRoute(async (req, res) => {
|
|
7
9
|
return res.status(200).json({})
|
|
8
|
-
})
|
|
10
|
+
}))
|
|
9
11
|
|
|
10
12
|
return router
|
|
11
13
|
}
|