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/hot-reload.ts
CHANGED
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
import watch from 'node-watch'
|
|
2
|
-
import { PlataCluster } from '..'
|
|
3
|
-
import { PlataBuild } from './build'
|
|
4
|
-
|
|
5
|
-
export namespace HotReload {
|
|
6
|
-
export function canReload(filename: string): boolean {
|
|
7
|
-
if (filename.includes('__BUILD__'))
|
|
8
|
-
return false
|
|
9
|
-
;
|
|
10
|
-
|
|
11
|
-
if (filename.includes('__RELEASE__'))
|
|
12
|
-
return false
|
|
13
|
-
;
|
|
14
|
-
|
|
15
|
-
if (filename.includes('node_modules'))
|
|
16
|
-
return false
|
|
17
|
-
;
|
|
18
|
-
|
|
19
|
-
if (Plata.ProjectJson['ignore-hot'])
|
|
20
|
-
for (let i = 0; i < Plata.ProjectJson['ignore-hot'].length; i++) {
|
|
21
|
-
const ignore = Plata.ProjectJson['ignore-hot'][i];
|
|
22
|
-
|
|
23
|
-
if(filename.includes(ignore))
|
|
24
|
-
return false
|
|
25
|
-
;
|
|
26
|
-
}
|
|
27
|
-
;
|
|
28
|
-
|
|
29
|
-
return /(ts|env|json)$/.test(filename)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export async function realod() {
|
|
33
|
-
await PlataBuild.compile({
|
|
34
|
-
basePath: Plata.ProjectDir,
|
|
35
|
-
mapFiles: true,
|
|
36
|
-
packageJsonSetup: false,
|
|
37
|
-
otimizar: false,
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
const cluster = new PlataCluster.PlataClusterManager()
|
|
41
|
-
|
|
42
|
-
cluster._sendMsgToAllWorkers({
|
|
43
|
-
action: 'RELOAD',
|
|
44
|
-
msg: true
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export async function setupHotReloadPrimary() {
|
|
49
|
-
console.log('🔥 Hot Realod ativo')
|
|
50
|
-
watch(Plata.ProjectDir, {
|
|
51
|
-
recursive: true,
|
|
52
|
-
delay: 500,
|
|
53
|
-
filter: HotReload.canReload
|
|
54
|
-
}, HotReload.realod)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export async function setupHotReloadWorker() {
|
|
58
|
-
process.on('message', (msg: Cluster.Actions) => {
|
|
59
|
-
if (msg.action === 'RELOAD')
|
|
60
|
-
process.exit(0)
|
|
61
|
-
;
|
|
62
|
-
})
|
|
63
|
-
}
|
|
1
|
+
import watch from 'node-watch'
|
|
2
|
+
import { PlataCluster } from '..'
|
|
3
|
+
import { PlataBuild } from './build'
|
|
4
|
+
|
|
5
|
+
export namespace HotReload {
|
|
6
|
+
export function canReload(filename: string): boolean {
|
|
7
|
+
if (filename.includes('__BUILD__'))
|
|
8
|
+
return false
|
|
9
|
+
;
|
|
10
|
+
|
|
11
|
+
if (filename.includes('__RELEASE__'))
|
|
12
|
+
return false
|
|
13
|
+
;
|
|
14
|
+
|
|
15
|
+
if (filename.includes('node_modules'))
|
|
16
|
+
return false
|
|
17
|
+
;
|
|
18
|
+
|
|
19
|
+
if (Plata.ProjectJson['ignore-hot'])
|
|
20
|
+
for (let i = 0; i < Plata.ProjectJson['ignore-hot'].length; i++) {
|
|
21
|
+
const ignore = Plata.ProjectJson['ignore-hot'][i];
|
|
22
|
+
|
|
23
|
+
if(filename.includes(ignore))
|
|
24
|
+
return false
|
|
25
|
+
;
|
|
26
|
+
}
|
|
27
|
+
;
|
|
28
|
+
|
|
29
|
+
return /(ts|env|json)$/.test(filename)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function realod() {
|
|
33
|
+
await PlataBuild.compile({
|
|
34
|
+
basePath: Plata.ProjectDir,
|
|
35
|
+
mapFiles: true,
|
|
36
|
+
packageJsonSetup: false,
|
|
37
|
+
otimizar: false,
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const cluster = new PlataCluster.PlataClusterManager()
|
|
41
|
+
|
|
42
|
+
cluster._sendMsgToAllWorkers({
|
|
43
|
+
action: 'RELOAD',
|
|
44
|
+
msg: true
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function setupHotReloadPrimary() {
|
|
49
|
+
console.log('🔥 Hot Realod ativo')
|
|
50
|
+
watch(Plata.ProjectDir, {
|
|
51
|
+
recursive: true,
|
|
52
|
+
delay: 500,
|
|
53
|
+
filter: HotReload.canReload
|
|
54
|
+
}, HotReload.realod)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function setupHotReloadWorker() {
|
|
58
|
+
process.on('message', (msg: Cluster.Actions) => {
|
|
59
|
+
if (msg.action === 'RELOAD')
|
|
60
|
+
process.exit(0)
|
|
61
|
+
;
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
64
|
}
|
package/src/libs/models/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from './model-validations'
|
|
2
|
-
export * from './model-template'
|
|
3
|
-
export * from './model-template-env'
|
|
4
|
-
export * from './model-template-union'
|
|
1
|
+
export * from './model-validations'
|
|
2
|
+
export * from './model-template'
|
|
3
|
+
export * from './model-template-env'
|
|
4
|
+
export * from './model-template-union'
|
|
5
5
|
export * from './model-template-combine'
|
|
@@ -1,94 +1,94 @@
|
|
|
1
|
-
export class ModelTemplateCombine<const T extends readonly Model.ModelClass[]> {
|
|
2
|
-
readonly type: ArrayCombine<T>['type']
|
|
3
|
-
readonly models: T
|
|
4
|
-
public readonly validations: Set<(value: typeof this.type) => Promise<Model.ValidateReturn>>
|
|
5
|
-
public readonly filters: Set<(value: Model.RemoveTypesForFilter<typeof this.type>) => ArrayCombine<T>['type']>
|
|
6
|
-
|
|
7
|
-
constructor(models: T) {
|
|
8
|
-
this.models = models
|
|
9
|
-
this.validations = new Set()
|
|
10
|
-
this.filters = new Set()
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public addValidation(callback: (value: typeof this.type) => Promise<Model.ValidateReturn>) {
|
|
14
|
-
this.validations.add(callback)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public addFilters(callback: (value: Model.RemoveTypesForFilter<typeof this.type>) => Model.RemoveTypesForFilter<typeof this.type>) {
|
|
18
|
-
this.filters.add(callback)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public filter(obj: any): any {
|
|
22
|
-
let value = obj
|
|
23
|
-
for (let i = 0; i < this.models.length; i++) {
|
|
24
|
-
const model = this.models[i]
|
|
25
|
-
|
|
26
|
-
if (model.filter !== undefined) {
|
|
27
|
-
value = model.filter(value)
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
this.filters.forEach(f => { value = f(value)})
|
|
32
|
-
|
|
33
|
-
return value
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public async validate(obj: any, skipFilter?: true): Promise<Model.ModelValidateReturnHelper<typeof this.type, undefined>> {
|
|
37
|
-
let v = obj
|
|
38
|
-
|
|
39
|
-
if (!skipFilter) {
|
|
40
|
-
v = this.filter(v)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
{ // Valida as models
|
|
44
|
-
const promises: Promise<{ errors?: PlataError[] }>[] = []
|
|
45
|
-
|
|
46
|
-
for (let i = 0; i < this.models.length; i++) {
|
|
47
|
-
const model = this.models[i];
|
|
48
|
-
|
|
49
|
-
promises.push(Plata.FastPromise(() => model.validate(v, true)))
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const results = await Promise.all(promises)
|
|
53
|
-
|
|
54
|
-
const errors = results
|
|
55
|
-
.filter((r): r is { errors: PlataError[] } => r.errors !== undefined)
|
|
56
|
-
.flatMap(r => r.errors)
|
|
57
|
-
;
|
|
58
|
-
|
|
59
|
-
if (errors.length !== 0) {
|
|
60
|
-
return {
|
|
61
|
-
value: undefined,
|
|
62
|
-
errors
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
{ // Validacoes adicionais
|
|
69
|
-
const promises: Promise<Model.ValidateReturn>[] = []
|
|
70
|
-
|
|
71
|
-
this.validations.forEach(va =>
|
|
72
|
-
promises.push(
|
|
73
|
-
Plata.FastPromise(() => va(v))
|
|
74
|
-
)
|
|
75
|
-
)
|
|
76
|
-
|
|
77
|
-
const result = await Promise.all(promises)
|
|
78
|
-
|
|
79
|
-
const errors = result.filter(Boolean)
|
|
80
|
-
|
|
81
|
-
if (errors.length !== 0) {
|
|
82
|
-
return {
|
|
83
|
-
errors,
|
|
84
|
-
value: undefined
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return {
|
|
90
|
-
value: v,
|
|
91
|
-
errors: undefined
|
|
92
|
-
}
|
|
93
|
-
}
|
|
1
|
+
export class ModelTemplateCombine<const T extends readonly Model.ModelClass[]> {
|
|
2
|
+
readonly type: ArrayCombine<T>['type']
|
|
3
|
+
readonly models: T
|
|
4
|
+
public readonly validations: Set<(value: typeof this.type) => Promise<Model.ValidateReturn>>
|
|
5
|
+
public readonly filters: Set<(value: Model.RemoveTypesForFilter<typeof this.type>) => ArrayCombine<T>['type']>
|
|
6
|
+
|
|
7
|
+
constructor(models: T) {
|
|
8
|
+
this.models = models
|
|
9
|
+
this.validations = new Set()
|
|
10
|
+
this.filters = new Set()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public addValidation(callback: (value: typeof this.type) => Promise<Model.ValidateReturn>) {
|
|
14
|
+
this.validations.add(callback)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public addFilters(callback: (value: Model.RemoveTypesForFilter<typeof this.type>) => Model.RemoveTypesForFilter<typeof this.type>) {
|
|
18
|
+
this.filters.add(callback)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public filter(obj: any): any {
|
|
22
|
+
let value = obj
|
|
23
|
+
for (let i = 0; i < this.models.length; i++) {
|
|
24
|
+
const model = this.models[i]
|
|
25
|
+
|
|
26
|
+
if (model.filter !== undefined) {
|
|
27
|
+
value = model.filter(value)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
this.filters.forEach(f => { value = f(value)})
|
|
32
|
+
|
|
33
|
+
return value
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public async validate(obj: any, skipFilter?: true): Promise<Model.ModelValidateReturnHelper<typeof this.type, undefined>> {
|
|
37
|
+
let v = obj
|
|
38
|
+
|
|
39
|
+
if (!skipFilter) {
|
|
40
|
+
v = this.filter(v)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
{ // Valida as models
|
|
44
|
+
const promises: Promise<{ errors?: PlataError[] }>[] = []
|
|
45
|
+
|
|
46
|
+
for (let i = 0; i < this.models.length; i++) {
|
|
47
|
+
const model = this.models[i];
|
|
48
|
+
|
|
49
|
+
promises.push(Plata.FastPromise(() => model.validate(v, true)))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const results = await Promise.all(promises)
|
|
53
|
+
|
|
54
|
+
const errors = results
|
|
55
|
+
.filter((r): r is { errors: PlataError[] } => r.errors !== undefined)
|
|
56
|
+
.flatMap(r => r.errors)
|
|
57
|
+
;
|
|
58
|
+
|
|
59
|
+
if (errors.length !== 0) {
|
|
60
|
+
return {
|
|
61
|
+
value: undefined,
|
|
62
|
+
errors
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
{ // Validacoes adicionais
|
|
69
|
+
const promises: Promise<Model.ValidateReturn>[] = []
|
|
70
|
+
|
|
71
|
+
this.validations.forEach(va =>
|
|
72
|
+
promises.push(
|
|
73
|
+
Plata.FastPromise(() => va(v))
|
|
74
|
+
)
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
const result = await Promise.all(promises)
|
|
78
|
+
|
|
79
|
+
const errors = result.filter(Boolean)
|
|
80
|
+
|
|
81
|
+
if (errors.length !== 0) {
|
|
82
|
+
return {
|
|
83
|
+
errors,
|
|
84
|
+
value: undefined
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
value: v,
|
|
91
|
+
errors: undefined
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
94
|
}
|
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import { PlataError } from "../tools"
|
|
2
|
-
import { ModelsInternals } from "./tools"
|
|
3
|
-
|
|
4
|
-
export class ModelTemplateEnv<const T extends Model.TemplateEnv> {
|
|
5
|
-
public readonly template: T
|
|
6
|
-
public readonly validations: Model.ModelTemplateEnvValidation<T>[] = []
|
|
7
|
-
public readonly type: Model.ExtractTemplateEnvType<T>
|
|
8
|
-
|
|
9
|
-
constructor(template: T) {
|
|
10
|
-
this.template = template
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public addValidation(callback: Model.ModelTemplateEnvValidation<T>) {
|
|
14
|
-
this.validations.push(callback)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public async validate(value: Record<string, string | undefined>): Promise<Model.ModelValidateEnvReturn<T>> {
|
|
18
|
-
let errors = await ModelsInternals.validateTemplateEnv(value, this.template)
|
|
19
|
-
|
|
20
|
-
if (errors.errorID !== undefined) {
|
|
21
|
-
return {
|
|
22
|
-
errors: [errors],
|
|
23
|
-
value: undefined
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (errors.length !== 0) {
|
|
28
|
-
return {
|
|
29
|
-
errors,
|
|
30
|
-
value: undefined
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const promises: Promise<null | PlataError>[] = []
|
|
35
|
-
|
|
36
|
-
for (let i = this.validations.length - 1; i >= 0; i--) {
|
|
37
|
-
const validation = this.validations[i];
|
|
38
|
-
|
|
39
|
-
promises.push(Plata.FastPromise(() => validation(value as any)))
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
errors = (await Promise.all(promises)).filter(Boolean)
|
|
43
|
-
|
|
44
|
-
if (errors.length !== 0) {
|
|
45
|
-
return {
|
|
46
|
-
errors,
|
|
47
|
-
value: undefined
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
value: value as any,
|
|
53
|
-
errors: undefined
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
}
|
|
1
|
+
import { PlataError } from "../tools"
|
|
2
|
+
import { ModelsInternals } from "./tools"
|
|
3
|
+
|
|
4
|
+
export class ModelTemplateEnv<const T extends Model.TemplateEnv> {
|
|
5
|
+
public readonly template: T
|
|
6
|
+
public readonly validations: Model.ModelTemplateEnvValidation<T>[] = []
|
|
7
|
+
public readonly type: Model.ExtractTemplateEnvType<T>
|
|
8
|
+
|
|
9
|
+
constructor(template: T) {
|
|
10
|
+
this.template = template
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public addValidation(callback: Model.ModelTemplateEnvValidation<T>) {
|
|
14
|
+
this.validations.push(callback)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public async validate(value: Record<string, string | undefined>): Promise<Model.ModelValidateEnvReturn<T>> {
|
|
18
|
+
let errors = await ModelsInternals.validateTemplateEnv(value, this.template)
|
|
19
|
+
|
|
20
|
+
if (errors.errorID !== undefined) {
|
|
21
|
+
return {
|
|
22
|
+
errors: [errors],
|
|
23
|
+
value: undefined
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (errors.length !== 0) {
|
|
28
|
+
return {
|
|
29
|
+
errors,
|
|
30
|
+
value: undefined
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const promises: Promise<null | PlataError>[] = []
|
|
35
|
+
|
|
36
|
+
for (let i = this.validations.length - 1; i >= 0; i--) {
|
|
37
|
+
const validation = this.validations[i];
|
|
38
|
+
|
|
39
|
+
promises.push(Plata.FastPromise(() => validation(value as any)))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
errors = (await Promise.all(promises)).filter(Boolean)
|
|
43
|
+
|
|
44
|
+
if (errors.length !== 0) {
|
|
45
|
+
return {
|
|
46
|
+
errors,
|
|
47
|
+
value: undefined
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
value: value as any,
|
|
53
|
+
errors: undefined
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
}
|
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
export class ModelTemplateUnion<const C extends Model.TemplateUnionConfig, const R extends Model.TemplateRecord> {
|
|
2
|
-
public readonly config: C
|
|
3
|
-
public readonly templates: R
|
|
4
|
-
public readonly type: Model.ExtractModelUnionType<C, R>
|
|
5
|
-
public readonly filters: Set<(x: Model.ExtractModelUnionTypeFilter<C, R>) => Model.ExtractModelUnionTypeFilter<C, R>>
|
|
6
|
-
|
|
7
|
-
constructor(config: C, templates: R) {
|
|
8
|
-
this.config = config
|
|
9
|
-
this.templates = templates
|
|
10
|
-
this.filters = new Set()
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public addFilter(filter: (x: Model.ExtractModelUnionTypeFilter<C, R>) => Model.ExtractModelUnionTypeFilter<C, R>) {
|
|
14
|
-
this.filters.add(filter)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public getExtractFromUnion(value: any): PlataResult<Model.ExtractModelUnionReturn<C, R>> {
|
|
18
|
-
let type = `${value[this.config.key] ?? null}`
|
|
19
|
-
value[this.config.key] = value[this.config.key] ?? null
|
|
20
|
-
|
|
21
|
-
const template = this.templates[type]
|
|
22
|
-
|
|
23
|
-
if (template === undefined) {
|
|
24
|
-
return {
|
|
25
|
-
errorID: 'PLMODUNEXTFRUN001',
|
|
26
|
-
msg: `O Valor de ${this.config.key} tem que ser ${Object.keys(this.templates).join(',')}`,
|
|
27
|
-
error: type
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return {
|
|
32
|
-
model: template,
|
|
33
|
-
value: value,
|
|
34
|
-
} as any
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Essa função so é para ser usada dentro da plataforma, considera que logo depois será rodado o validate
|
|
39
|
-
*/
|
|
40
|
-
public filter(value: any): any {
|
|
41
|
-
const extraced = this.getExtractFromUnion(value)
|
|
42
|
-
|
|
43
|
-
if (extraced.errorID !== undefined) {
|
|
44
|
-
return value
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (extraced.model.filter !== undefined) {
|
|
48
|
-
value = extraced.model.filter(value)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
this.filters.forEach(f => { value = f(value)})
|
|
52
|
-
|
|
53
|
-
return value
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public async validate(value: any, skipFilter?: true): Promise<Model.ModelValidateReturnHelper<typeof this.type, undefined>> {
|
|
57
|
-
const extraced = this.getExtractFromUnion(value)
|
|
58
|
-
|
|
59
|
-
if (extraced.errorID !== undefined) {
|
|
60
|
-
return {
|
|
61
|
-
errors: [extraced],
|
|
62
|
-
value: undefined
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (!skipFilter) {
|
|
67
|
-
this.filters.forEach(f => { value = f(value) })
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const result = await extraced.model.validate(extraced.value, skipFilter)
|
|
71
|
-
|
|
72
|
-
if (result.errors !== undefined) {
|
|
73
|
-
return {
|
|
74
|
-
errors: result.errors,
|
|
75
|
-
value: undefined
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
errors: undefined,
|
|
81
|
-
value: result.value
|
|
82
|
-
}
|
|
83
|
-
}
|
|
1
|
+
export class ModelTemplateUnion<const C extends Model.TemplateUnionConfig, const R extends Model.TemplateRecord> {
|
|
2
|
+
public readonly config: C
|
|
3
|
+
public readonly templates: R
|
|
4
|
+
public readonly type: Model.ExtractModelUnionType<C, R>
|
|
5
|
+
public readonly filters: Set<(x: Model.ExtractModelUnionTypeFilter<C, R>) => Model.ExtractModelUnionTypeFilter<C, R>>
|
|
6
|
+
|
|
7
|
+
constructor(config: C, templates: R) {
|
|
8
|
+
this.config = config
|
|
9
|
+
this.templates = templates
|
|
10
|
+
this.filters = new Set()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public addFilter(filter: (x: Model.ExtractModelUnionTypeFilter<C, R>) => Model.ExtractModelUnionTypeFilter<C, R>) {
|
|
14
|
+
this.filters.add(filter)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public getExtractFromUnion(value: any): PlataResult<Model.ExtractModelUnionReturn<C, R>> {
|
|
18
|
+
let type = `${value[this.config.key] ?? null}`
|
|
19
|
+
value[this.config.key] = value[this.config.key] ?? null
|
|
20
|
+
|
|
21
|
+
const template = this.templates[type]
|
|
22
|
+
|
|
23
|
+
if (template === undefined) {
|
|
24
|
+
return {
|
|
25
|
+
errorID: 'PLMODUNEXTFRUN001',
|
|
26
|
+
msg: `O Valor de ${this.config.key} tem que ser ${Object.keys(this.templates).join(',')}`,
|
|
27
|
+
error: type
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
model: template,
|
|
33
|
+
value: value,
|
|
34
|
+
} as any
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Essa função so é para ser usada dentro da plataforma, considera que logo depois será rodado o validate
|
|
39
|
+
*/
|
|
40
|
+
public filter(value: any): any {
|
|
41
|
+
const extraced = this.getExtractFromUnion(value)
|
|
42
|
+
|
|
43
|
+
if (extraced.errorID !== undefined) {
|
|
44
|
+
return value
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (extraced.model.filter !== undefined) {
|
|
48
|
+
value = extraced.model.filter(value)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
this.filters.forEach(f => { value = f(value)})
|
|
52
|
+
|
|
53
|
+
return value
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public async validate(value: any, skipFilter?: true): Promise<Model.ModelValidateReturnHelper<typeof this.type, undefined>> {
|
|
57
|
+
const extraced = this.getExtractFromUnion(value)
|
|
58
|
+
|
|
59
|
+
if (extraced.errorID !== undefined) {
|
|
60
|
+
return {
|
|
61
|
+
errors: [extraced],
|
|
62
|
+
value: undefined
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!skipFilter) {
|
|
67
|
+
this.filters.forEach(f => { value = f(value) })
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const result = await extraced.model.validate(extraced.value, skipFilter)
|
|
71
|
+
|
|
72
|
+
if (result.errors !== undefined) {
|
|
73
|
+
return {
|
|
74
|
+
errors: result.errors,
|
|
75
|
+
value: undefined
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
errors: undefined,
|
|
81
|
+
value: result.value
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
84
|
}
|