pwi-plata-type 0.2.3 → 0.2.6

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.
@@ -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 () => 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/index.ts CHANGED
@@ -1,13 +1,17 @@
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
 
8
+
9
+
7
10
  export {
8
11
  PlataCluster,
9
12
  PlataEnv,
10
13
  PlataRoutes,
11
14
  PlataTools,
12
15
  PlataModels,
16
+ PlataRoute,
13
17
  }
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";
2
- import express, { Express, Router } from "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
 
@@ -31,4 +42,57 @@ export async function loadExpressRoutes(): Promise<Router> {
31
42
  }
32
43
 
33
44
  return router
45
+ }
46
+
47
+ export type PlataRequestHandler = (req: PlataRequest, res: PlataResponse, next: express.NextFunction) => Promise<any>
48
+
49
+ export function PlataRoute(r: PlataRequestHandler): express.RequestHandler {
50
+ return async (req, res, next) => {
51
+ const aReq = req as any
52
+
53
+ if (aReq.user === undefined) {
54
+ aReq.user = new Object(null)
55
+ }
56
+
57
+ if (aReq.extras === undefined) {
58
+ aReq.extras = new Object(null)
59
+ }
60
+
61
+ const aRes = res as any
62
+
63
+ if (aRes._onResponse === undefined) {
64
+ aRes._onResponse = []
65
+ }
66
+
67
+ if (aRes.addOnResponse === undefined) {
68
+ aRes.addOnResponse = aRes._onResponse.push
69
+ }
70
+
71
+ if (aRes._oldJson === undefined) {
72
+ aRes._oldJson = aRes.json
73
+
74
+ aRes._onResponseEvent = (...a) => {
75
+ aRes._onResponse.forEach(r => {
76
+ try {
77
+ r(...a)
78
+ } catch (e) {}
79
+ })
80
+ }
81
+
82
+ aRes.json = (body) => {
83
+ (async () => { aRes._onResponseEvent(aReq, body, undefined, undefined) })
84
+
85
+ return aRes._oldJson(body)
86
+ }
87
+
88
+ aRes.error = (error, nodeError) => {
89
+ (async () => { aRes._onResponseEvent(aReq, undefined, error, nodeError) })
90
+
91
+ return aRes._oldJson(error)
92
+ }
93
+ }
94
+
95
+
96
+ return r(aReq, aRes, next)
97
+ }
34
98
  }
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pwi-plata-type",
3
- "version": "0.2.3",
3
+ "version": "0.2.6",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "bin": {
@@ -1,10 +1,11 @@
1
1
  //@ts-nocheck
2
- import * as ç__Name__çConfigImport from '../configs/ç__name__ç.js'
3
- const ç__Name__çConfig = ç__Name__çConfigImport.default as unknown as typeof ç__Name__çConfigImport
2
+ import * as ç__Name__çConfig from '../configs/ç__name__ç.js'
4
3
 
5
4
  export class ç__Name__ç {
6
- constructor(config: typeof ç__Name__çConfig) {
5
+ private readonly config: typeof ç__Name__çConfig
7
6
 
7
+ constructor(config: typeof ç__Name__çConfig.default) {
8
+ this.config = config
8
9
  }
9
10
  }
10
11
 
@@ -12,7 +13,7 @@ export function getç__Name__ç(): ç__Name__ç {
12
13
  const p = process as any
13
14
 
14
15
  if (p._plata._libç__Name__ç === undefined) {
15
- p._plata._libç__Name__ç = new ç__Name__ç(ç__Name__çConfig)
16
+ p._plata._libç__Name__ç = new ç__Name__ç(ç__Name__çConfig.default)
16
17
  }
17
18
 
18
19
  return p._plata._libç__Name__ç
@@ -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
  }