pwi-plata-type 0.2.5 → 0.2.8

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.ts CHANGED
@@ -21,8 +21,8 @@ const main = async () => {
21
21
 
22
22
  clusterLib.run({
23
23
  name: '_plata_express',
24
- onStart: async () => null,
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', () => process.send({
38
- id: 0,
39
- name: '_plata',
40
- data: {
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/libs/model.ts CHANGED
@@ -1,20 +1,16 @@
1
- export interface ErroValidacao {
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?: ErroValidacao
4
+ error?: PlataError
9
5
  continua: boolean
10
6
  }
11
7
 
12
8
  export interface RetornoValicadaoModel {
13
9
  value: any,
14
- errors: ErroValidacao[]
10
+ errors: PlataError[]
15
11
  }
16
12
 
17
- export type ModelValicacao = (value: any) => Promise<ErroValidacao | null>
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<ErroValidacao | null> {
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<ErroValidacao[]> {
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 ErroValidacao[]
138
+ return Erros as PlataError[]
143
139
  }
144
140
 
145
141
  if ((Erros as any[]).length !== 0) {
146
- return Erros as ErroValidacao[]
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 ErroValidacao[]
163
+ return Erros as PlataError[]
168
164
  }
169
165
 
170
- return Erros as ErroValidacao[]
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";
1
+ import { PlataDirs, PlataRequire, PlataError } from "./tools.js";
2
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
- const r = routes as PlataRequire.Required<PlataRouter>[]
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];
@@ -33,9 +48,7 @@ export async function loadExpressRoutes(): Promise<Router> {
33
48
  return router
34
49
  }
35
50
 
36
- export type PlataRequest = express.Request & { user: any, extras: any }
37
-
38
- export type PlataRequestHandler = (req: PlataRequest, res: express.Response, next: express.NextFunction) => Promise<any>
51
+ export type PlataRequestHandler = (req: PlataRequest, res: PlataResponse, next: express.NextFunction) => Promise<any>
39
52
 
40
53
  export function PlataRoute(r: PlataRequestHandler): express.RequestHandler {
41
54
  return async (req, res, next) => {
@@ -49,6 +62,41 @@ export function PlataRoute(r: PlataRequestHandler): express.RequestHandler {
49
62
  aReq.extras = new Object(null)
50
63
  }
51
64
 
52
- return r(aReq, res, next)
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)
53
101
  }
54
102
  }
package/libs/tools.ts CHANGED
@@ -5,6 +5,14 @@ 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
+
14
+ export type PlataResultado<T> = (T & { errorID: undefined }) | PlataError
15
+
8
16
  export module PlataDirs {
9
17
  export function getProjectDir(): string {
10
18
  return path.resolve('.')
@@ -92,7 +100,7 @@ export module PlataFiles {
92
100
  export function readFileSync(file: string, callback: readLineCallbackSync): Promise<string | { err: any }> {
93
101
  return new Promise(resolve => {
94
102
  try {
95
- const content = []
103
+ const content: string[] = []
96
104
  const stream = readline.createInterface({
97
105
  input: fs.createReadStream(file),
98
106
  crlfDelay: Infinity
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pwi-plata-type",
3
- "version": "0.2.5",
3
+ "version": "0.2.8",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "bin": {
@@ -2,8 +2,10 @@
2
2
  import * as ç__Name__çConfig from '../configs/ç__name__ç.js'
3
3
 
4
4
  export class ç__Name__ç {
5
- constructor(config: typeof ç__Name__çConfig.default) {
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