pwi-plata-type 0.2.17 → 0.2.20

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.
@@ -36,7 +36,11 @@ export async function h() {
36
36
  console.log('CLUSTER AINDA NÃO CRIANDO AINDA')
37
37
  break;
38
38
  case 'route':
39
- const routeArg = `${args.shift()}`.split('/')
39
+ const r = `${args.shift()}/`.replace(/:(.*?)(?:\/|$)/g, (_, parm) => {
40
+ return parm[parm.length - 1] !== '?' ? `{${parm}}/` : `[${parm.slice(0, -1)}]/`
41
+ })
42
+
43
+ const routeArg = r.slice(0, -1).split('/')
40
44
 
41
45
  const routeFileName = routeArg[routeArg.length - 1] === '' ? 'index' : routeArg.pop()
42
46
  const routeFilePath = path.join('routes', ...routeArg.filter(v => v !== ''))
package/libs/model.ts CHANGED
@@ -12,6 +12,8 @@ export interface RetornoValicadaoModel {
12
12
 
13
13
  export type ModelValicacao = (value: any) => Promise<PlataError | null>
14
14
 
15
+ export type ModelTratamento<T> = (value: T) => T
16
+
15
17
  export type ModelValicacaoPrimitivo = (nome: string, valor: any) => Promise<RetornoValidacaoPrimitivo>
16
18
 
17
19
  export interface ModelType {
@@ -21,6 +23,7 @@ export interface ModelType {
21
23
  export class Model {
22
24
  public readonly template: ModelType
23
25
  public readonly validacoes: Map<string, ModelValicacao>
26
+ public readonly tratamentos: Map<string, ModelTratamento<any>>
24
27
 
25
28
  constructor(template: ModelType) {
26
29
  this.template = template
@@ -30,6 +33,10 @@ export class Model {
30
33
  public addValidacao(nome: string, callback: ModelValicacao) {
31
34
  this.validacoes.set(nome, callback)
32
35
  }
36
+
37
+ public addTratamento<T>(nome: string, callback: ModelTratamento<T>) {
38
+ this.tratamentos.set(nome, callback)
39
+ }
33
40
  }
34
41
 
35
42
  export async function _validaCampo(value: any, pipelines: ModelValicacaoPrimitivo[], name: string): Promise<PlataError | null> {
@@ -48,6 +55,14 @@ export async function _validaCampo(value: any, pipelines: ModelValicacaoPrimitiv
48
55
  return null
49
56
  }
50
57
 
58
+ export function _trataModel<T>(value: T, model: Model): T {
59
+ model.tratamentos.forEach(tratamento => {
60
+ value = tratamento(value)
61
+ })
62
+
63
+ return value
64
+ }
65
+
51
66
  export function _filtraModel(value: any, model: Model): any {
52
67
  for (const campo in model.template) {
53
68
  const t = model.template[campo]
@@ -169,6 +184,8 @@ export async function _validaModel(value: any, model: Model, name?: string): Pro
169
184
  export async function valida(value: any, model: Model): Promise<RetornoValicadaoModel> {
170
185
  value = _filtraModel(value, model)
171
186
 
187
+ value = _trataModel(value, model)
188
+
172
189
  return {
173
190
  value,
174
191
  errors: await _validaModel(value, model),
package/libs/routes.ts CHANGED
@@ -43,6 +43,9 @@ export async function loadExpressRoutes(): Promise<PlataRequiredRoute[]> {
43
43
  .replace(/.(ts|js)$/, '')
44
44
  .replace(/index/, '')
45
45
  .replace(/\\/g, '/')
46
+ .replace(/\{(.*?)\}/g, ':$1')
47
+ .replace(/\[(.*?)\]/g, ':$1?')
48
+ ;
46
49
 
47
50
  return route
48
51
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pwi-plata-type",
3
- "version": "0.2.17",
3
+ "version": "0.2.20",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "bin": {
@@ -15,7 +15,7 @@ export async function install({ dirs, projectPackageJson }) {
15
15
  promises.push(t.copyFolderToProject('envs'))
16
16
  promises.push(t.syncFolderProject('.vscode'))
17
17
 
18
- promises.push(t.syncFileToProject('Dockerfile'))
18
+ promises.push(t.copyFileIfNotExists('Dockerfile'))
19
19
  promises.push(t.syncFileToProject('tsconfig.json'))
20
20
 
21
21
  projectPackageJson.type = "module"