pwi-plata-type 0.4.210 → 0.4.211

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.
Files changed (99) hide show
  1. package/README.md +2 -2
  2. package/__BUILD__/libs/runtime.js +1 -1
  3. package/__BUILD__/libs/runtime.js.map +1 -1
  4. package/__BUILD__/scripts/post-install.js +1 -1
  5. package/__BUILD__/scripts/post-install.js.map +1 -1
  6. package/package.json +60 -58
  7. package/src/@types/build.d.ts +26 -26
  8. package/src/@types/cluster.actions.d.ts +27 -27
  9. package/src/@types/cluster.d.ts +55 -55
  10. package/src/@types/create-cli.d.ts +8 -8
  11. package/src/@types/exported.d.ts +34 -34
  12. package/src/@types/fs.d.ts +6 -6
  13. package/src/@types/global.d.ts +138 -138
  14. package/src/@types/knex.d.ts +13 -13
  15. package/src/@types/model.d.ts +148 -148
  16. package/src/@types/router.d.ts +108 -108
  17. package/src/@types/sql.d.ts +120 -120
  18. package/src/@types/swagger.d.ts +22 -22
  19. package/src/bin/extras/create-api-cli.ts +218 -218
  20. package/src/bin/extras/create-cluster-cli.ts +160 -160
  21. package/src/bin/extras/create-lib-cli.ts +84 -84
  22. package/src/bin/extras/plata-create-tools.ts +97 -97
  23. package/src/bin/plata-api.ts +194 -194
  24. package/src/bin/plata-build.ts +19 -19
  25. package/src/bin/plata-cluster.ts +42 -42
  26. package/src/bin/plata-cmd.ts +19 -19
  27. package/src/bin/plata-create.ts +50 -50
  28. package/src/bin/plata-runtime.ts +110 -110
  29. package/src/bin/plata-swagger.ts +104 -104
  30. package/src/index.ts +20 -20
  31. package/src/libs/axios.ts +32 -32
  32. package/src/libs/build.ts +234 -234
  33. package/src/libs/build_wasm.ts +169 -169
  34. package/src/libs/cluster/cluster-controller.ts +93 -93
  35. package/src/libs/cluster.ts +319 -319
  36. package/src/libs/custom.ts +21 -21
  37. package/src/libs/env.ts +10 -10
  38. package/src/libs/fs.ts +399 -399
  39. package/src/libs/hot-reload.ts +63 -63
  40. package/src/libs/job.ts +39 -39
  41. package/src/libs/models/index.ts +4 -4
  42. package/src/libs/models/model-template-combine.ts +93 -93
  43. package/src/libs/models/model-template-env.ts +58 -58
  44. package/src/libs/models/model-template-union.ts +87 -87
  45. package/src/libs/models/model-template.ts +111 -111
  46. package/src/libs/models/model-validations.ts +453 -453
  47. package/src/libs/models/tools.ts +196 -196
  48. package/src/libs/router.ts +184 -184
  49. package/src/libs/runtime.ts +356 -356
  50. package/src/libs/sql.ts +933 -933
  51. package/src/libs/swagger.ts +81 -81
  52. package/src/libs/tools.ts +236 -236
  53. package/src/misc/golang/wasm_exec.js +574 -574
  54. package/src/scripts/post-install/api.ts +32 -32
  55. package/src/scripts/post-install/cluster.ts +28 -28
  56. package/src/scripts/post-install/lib.ts +25 -25
  57. package/src/scripts/post-install.ts +64 -64
  58. package/src/scripts/pre-test.ts +33 -33
  59. package/src/tests/01-config.test.ts +12 -12
  60. package/src/tests/02-effect.test.ts +107 -107
  61. package/src/tests/03-model.env.test.ts +66 -66
  62. package/src/tests/04-model.test.ts +150 -150
  63. package/src/tests/05-model.union.test.ts +160 -160
  64. package/src/tests/06-router.test.ts +44 -44
  65. package/src/tests/07-switch.test.ts +75 -75
  66. package/src/tests/08-brands.test.ts +55 -55
  67. package/src/tests/09-refs.test.ts +190 -190
  68. package/src/tests/10-sql.test.ts +639 -639
  69. package/src/tests/assets/sql/example.sql +94 -94
  70. package/templates/create-cli/class.ts +13 -13
  71. package/templates/create-cli/cluster.ts +14 -14
  72. package/templates/create-cli/cmd.ts +3 -3
  73. package/templates/create-cli/config.ts +2 -2
  74. package/templates/create-cli/lib.ts +3 -3
  75. package/templates/create-cli/model.ts +14 -14
  76. package/templates/create-cli/route.ts +8 -8
  77. package/templates/create-cli/task.ts +13 -13
  78. package/templates/postinstall/api/.vscode/launch.json +20 -20
  79. package/templates/postinstall/api/Dockerfile +22 -22
  80. package/templates/postinstall/api/configs/env.ts +2 -2
  81. package/templates/postinstall/api/envs/debug.env +15 -15
  82. package/templates/postinstall/api/envs/homolog.env +15 -15
  83. package/templates/postinstall/api/envs/prod.env +15 -15
  84. package/templates/postinstall/api/tsconfig.json +24 -24
  85. package/templates/postinstall/cluster/.vscode/launch.json +20 -20
  86. package/templates/postinstall/cluster/Dockerfile +24 -24
  87. package/templates/postinstall/cluster/configs/env.ts +2 -2
  88. package/templates/postinstall/cluster/envs/debug.env +10 -10
  89. package/templates/postinstall/cluster/envs/homolog.env +10 -10
  90. package/templates/postinstall/cluster/envs/prod.env +10 -10
  91. package/templates/postinstall/cluster/tsconfig.json +24 -24
  92. package/templates/postinstall/lib/postinstall-tools.mjs +111 -111
  93. package/templates/postinstall/lib/postinstall.js +33 -33
  94. package/templates/postinstall/lib/tsconfig.json +22 -22
  95. package/templates/swagger-cli/header.json +21 -21
  96. package/templates/swagger-cli/route/body.json +14 -14
  97. package/templates/swagger-cli/route/header.json +18 -18
  98. package/templates/swagger-cli/route/response.json +53 -53
  99. package/tsconfig.json +28 -28
@@ -1,139 +1,139 @@
1
- interface RequiredInterface<T> {
2
- filePath: string,
3
- name: string,
4
- exports: T
5
- }
6
-
7
- type RequireAsyncReturn<T> = PlataPromise<RequiredInterface<T>>
8
- type RequireFolderReturn<T> = PlataPromise<RequiredInterface<T>[]>
9
- type RequiredFolder<T> = Awaited<RequireFolderReturn<T>>
10
-
11
- type StringKeyOfObject<T extends Object> = Exclude<{
12
- [name in keyof T]:
13
- name extends string ? name
14
- : name extends number ? `${name}`
15
- : undefined
16
- }[keyof T], undefined>;
17
-
18
- type AnyRecord<T> = {
19
- [P in string]: T;
20
- }
21
- type LooseAutocomplete<T extends string> = T | (string & {})
22
-
23
- type Replace<T, O, U> = T extends O ? Exclude<T, O> | U : T
24
-
25
- type PlataEnvDefaults = "prod" | "homolog" | "debug"
26
- type PlataEnv = LooseAutocomplete<PlataEnvDefaults>
27
-
28
- type PlataFunctionWrapperReturn<F extends (...a: any) => any, T> = (...a: Parameters<F>) => T
29
-
30
- type RemovePlataResult<T> = T extends PlataResult<infer U> ? Exclude<U, PlataError> : T
31
-
32
- type RemovePlataResultOnArray<T> =
33
- T extends [ infer U ] ? [ RemovePlataResult<Awaited<U>> ]
34
- : T extends [ infer U, ...infer V ] ? [ RemovePlataResult<Awaited<U>>, ...RemovePlataResultOnArray<V> ]
35
- : []
36
- //: T extends Promise<infer U>[] ? RemovePlataResult<U>[]
37
- //: never
38
-
39
- ;
40
-
41
- type RemovePlataResultOnPromiseAll<T> =
42
- RemovePlataResultOnArray<T>
43
- // extends [ infer U ] ? RemovePlataResultOnArray<T>
44
- // : T extends Promise<infer U>[] ? 'RemovePlataResult<U>'
45
- // : 'never'
46
- ;
47
-
48
- interface PlataProcess {
49
- _plata_cache?: Record<string, any>
50
- }
51
-
52
- type Prettify<T> = {
53
- [K in keyof T]: T[K];
54
- } & {};
55
-
56
- interface PlataEnvAutoComplete extends NodeJS.ProcessEnv {
57
- ENV?: PlataEnv
58
- PORT?: string
59
- _PLATA_API_ROOT?: string
60
- _PLATA_API_SSL_KEY?: string
61
- _PLATA_API_SSL_CERT?: string
62
- _PLATA_API_SSL_PASS?: string
63
- _PLATA_API_JSON_SIZE?: string
64
- _PLATA_WORKER_NAME?: string
65
- _PLATA_WORKER_TYPE?: 'WORKER' | 'CONTROLLER'
66
- _PLATA_SQL_CACHETABLE?: string
67
- _PLATA_SQL_CACHETABLETIME?: string
68
- }
69
-
70
- interface PlataRuntime {
71
-
72
- }
73
-
74
- interface Plata extends PlataRuntime {
75
- }
76
-
77
- declare const Plata: Plata
78
-
79
- declare const process: NodeJS.Process & PlataProcess
80
-
81
- interface EnvConfig {
82
- env: PlataEnv
83
- }
84
-
85
- interface PlataError {
86
- errorID: string
87
- msg: string,
88
- error?: any
89
- }
90
-
91
- type MaybePlataError = null | PlataError
92
-
93
- type PlataResult<T> = (T & { errorID?: undefined }) | PlataError
94
-
95
- type PlataPromise<T> = Promise<PlataResult<T>>
96
-
97
- interface PlataRef<T> {
98
- readonly value: T
99
- applyTo<T extends object, K extends keyof T = keyof T>(key: K, target: Omit<T, K>): Readonly<T>
100
- }
101
-
102
- type Dictionary<T> = {
103
- [key: string]: T
104
- }
105
-
106
- type DictionaryRecord<K extends string | number, T> = {
107
- [key in K]: T
108
- }
109
-
110
- interface PlataEnvConfig {
111
- env: PlataEnv
112
- }
113
-
114
- type DeepWritable<T extends {}> = Prettify<{
115
- -readonly [K in keyof T]: T[K] extends Dictionary<any> ?
116
- T[K] extends Date ? Date
117
- : T[K] extends Brand<infer R> ? Brand<R>
118
- : DeepWritable<T[K]>
119
- : T[K]
120
- }>
121
-
122
- type ArrayCombine<T extends readonly any[]> =
123
- T extends readonly [ infer U extends T[number] ] ? U
124
- : T extends readonly [ infer U extends T[number], ...infer R] ? U & ArrayCombine<R>
125
- : never
126
- ;
127
-
128
- interface Brands {
129
-
130
- }
131
-
132
- type Brand<K extends keyof Brands> = Brands[K] & { z__brand: K }
133
-
134
- interface PType {
135
- checkType<T>(a: T): unknown
136
- testType<R, T>(a: T, result: T extends R ? true : false)
137
- }
138
-
1
+ interface RequiredInterface<T> {
2
+ filePath: string,
3
+ name: string,
4
+ exports: T
5
+ }
6
+
7
+ type RequireAsyncReturn<T> = PlataPromise<RequiredInterface<T>>
8
+ type RequireFolderReturn<T> = PlataPromise<RequiredInterface<T>[]>
9
+ type RequiredFolder<T> = Awaited<RequireFolderReturn<T>>
10
+
11
+ type StringKeyOfObject<T extends Object> = Exclude<{
12
+ [name in keyof T]:
13
+ name extends string ? name
14
+ : name extends number ? `${name}`
15
+ : undefined
16
+ }[keyof T], undefined>;
17
+
18
+ type AnyRecord<T> = {
19
+ [P in string]: T;
20
+ }
21
+ type LooseAutocomplete<T extends string> = T | (string & {})
22
+
23
+ type Replace<T, O, U> = T extends O ? Exclude<T, O> | U : T
24
+
25
+ type PlataEnvDefaults = "prod" | "homolog" | "debug"
26
+ type PlataEnv = LooseAutocomplete<PlataEnvDefaults>
27
+
28
+ type PlataFunctionWrapperReturn<F extends (...a: any) => any, T> = (...a: Parameters<F>) => T
29
+
30
+ type RemovePlataResult<T> = T extends PlataResult<infer U> ? Exclude<U, PlataError> : T
31
+
32
+ type RemovePlataResultOnArray<T> =
33
+ T extends [ infer U ] ? [ RemovePlataResult<Awaited<U>> ]
34
+ : T extends [ infer U, ...infer V ] ? [ RemovePlataResult<Awaited<U>>, ...RemovePlataResultOnArray<V> ]
35
+ : []
36
+ //: T extends Promise<infer U>[] ? RemovePlataResult<U>[]
37
+ //: never
38
+
39
+ ;
40
+
41
+ type RemovePlataResultOnPromiseAll<T> =
42
+ RemovePlataResultOnArray<T>
43
+ // extends [ infer U ] ? RemovePlataResultOnArray<T>
44
+ // : T extends Promise<infer U>[] ? 'RemovePlataResult<U>'
45
+ // : 'never'
46
+ ;
47
+
48
+ interface PlataProcess {
49
+ _plata_cache?: Record<string, any>
50
+ }
51
+
52
+ type Prettify<T> = {
53
+ [K in keyof T]: T[K];
54
+ } & {};
55
+
56
+ interface PlataEnvAutoComplete extends NodeJS.ProcessEnv {
57
+ ENV?: PlataEnv
58
+ PORT?: string
59
+ _PLATA_API_ROOT?: string
60
+ _PLATA_API_SSL_KEY?: string
61
+ _PLATA_API_SSL_CERT?: string
62
+ _PLATA_API_SSL_PASS?: string
63
+ _PLATA_API_JSON_SIZE?: string
64
+ _PLATA_WORKER_NAME?: string
65
+ _PLATA_WORKER_TYPE?: 'WORKER' | 'CONTROLLER'
66
+ _PLATA_SQL_CACHETABLE?: string
67
+ _PLATA_SQL_CACHETABLETIME?: string
68
+ }
69
+
70
+ interface PlataRuntime {
71
+
72
+ }
73
+
74
+ interface Plata extends PlataRuntime {
75
+ }
76
+
77
+ declare const Plata: Plata
78
+
79
+ declare const process: NodeJS.Process & PlataProcess
80
+
81
+ interface EnvConfig {
82
+ env: PlataEnv
83
+ }
84
+
85
+ interface PlataError {
86
+ errorID: string
87
+ msg: string,
88
+ error?: any
89
+ }
90
+
91
+ type MaybePlataError = null | PlataError
92
+
93
+ type PlataResult<T> = (T & { errorID?: undefined }) | PlataError
94
+
95
+ type PlataPromise<T> = Promise<PlataResult<T>>
96
+
97
+ interface PlataRef<T> {
98
+ readonly value: T
99
+ applyTo<T extends object, K extends keyof T = keyof T>(key: K, target: Omit<T, K>): Readonly<T>
100
+ }
101
+
102
+ type Dictionary<T> = {
103
+ [key: string]: T
104
+ }
105
+
106
+ type DictionaryRecord<K extends string | number, T> = {
107
+ [key in K]: T
108
+ }
109
+
110
+ interface PlataEnvConfig {
111
+ env: PlataEnv
112
+ }
113
+
114
+ type DeepWritable<T extends {}> = Prettify<{
115
+ -readonly [K in keyof T]: T[K] extends Dictionary<any> ?
116
+ T[K] extends Date ? Date
117
+ : T[K] extends Brand<infer R> ? Brand<R>
118
+ : DeepWritable<T[K]>
119
+ : T[K]
120
+ }>
121
+
122
+ type ArrayCombine<T extends readonly any[]> =
123
+ T extends readonly [ infer U extends T[number] ] ? U
124
+ : T extends readonly [ infer U extends T[number], ...infer R] ? U & ArrayCombine<R>
125
+ : never
126
+ ;
127
+
128
+ interface Brands {
129
+
130
+ }
131
+
132
+ type Brand<K extends keyof Brands> = Brands[K] & { z__brand: K }
133
+
134
+ interface PType {
135
+ checkType<T>(a: T): unknown
136
+ testType<R, T>(a: T, result: T extends R ? true : false)
137
+ }
138
+
139
139
  declare const PType: PType
@@ -1,14 +1,14 @@
1
- import { Knex as KnexOriginal } from 'knex';
2
- import { ModelTemplate } from '++/libs/models'
3
-
4
- declare module 'knex' {
5
- namespace Knex {
6
- interface QueryBuilder {
7
- includeOne<TRecord extends {}, TResult>(table: keyof TRecord, type: Sql.Joins,s: KnexOriginal.JoinCallback): KnexOriginal.QueryBuilder<TRecord, TResult>;
8
- context<TRecord extends {}, TResult>(context: any): KnexOriginal.QueryBuilder<TRecord, TResult>;
9
- context<T>(): T;
10
- getConnTable<TRecord extends {}, TResult>(table: string, comments?: Record<string, string>): KnexOriginal.QueryBuilder<TRecord, TResult>
11
- comment?: <TRecord extends {}, TResult>(comment: string) => KnexOriginal.QueryBuilder<TRecord, TResult>
12
- }
13
- }
1
+ import { Knex as KnexOriginal } from 'knex';
2
+ import { ModelTemplate } from '++/libs/models'
3
+
4
+ declare module 'knex' {
5
+ namespace Knex {
6
+ interface QueryBuilder {
7
+ includeOne<TRecord extends {}, TResult>(table: keyof TRecord, type: Sql.Joins,s: KnexOriginal.JoinCallback): KnexOriginal.QueryBuilder<TRecord, TResult>;
8
+ context<TRecord extends {}, TResult>(context: any): KnexOriginal.QueryBuilder<TRecord, TResult>;
9
+ context<T>(): T;
10
+ getConnTable<TRecord extends {}, TResult>(table: string, comments?: Record<string, string>): KnexOriginal.QueryBuilder<TRecord, TResult>
11
+ comment?: <TRecord extends {}, TResult>(comment: string) => KnexOriginal.QueryBuilder<TRecord, TResult>
12
+ }
13
+ }
14
14
  }
@@ -1,149 +1,149 @@
1
- declare namespace Model {
2
-
3
- type ValidateReturn = PlataError | null
4
-
5
- interface PrimitiveValidationTypeReturn {
6
- error?: PlataError
7
- continua: boolean
8
- }
9
-
10
- type PrimitiveValidationTypeFunction<T = any, D = undefined> =
11
- (nome: string, valor: T | D) => Promise<PrimitiveValidationTypeReturn>
12
- ;
13
-
14
-
15
- interface PrimitiveValidationType<T = any, D = undefined> extends PrimitiveValidationTypeFunction<T, D>{
16
- filter?: (value: any) => any
17
- }
18
-
19
- interface Template {
20
- readonly [name: string]:
21
- readonly PrimitiveValidationType<unknown, unknown>[] |
22
- readonly Template[] |
23
- Template |
24
- readonly (readonly PrimitiveValidationType<unknown, unknown>[])[]
25
- }
26
-
27
- interface TemplateEnv {
28
- readonly [name: string]: readonly PrimitiveValidationType<unknown, unknown>[]
29
- }
30
-
31
- type GetTemplateParamType<T> =
32
- T extends readonly [ PrimitiveValidationType<infer G, infer H> ] ? G | H :
33
- T extends readonly [ PrimitiveValidationType<any, infer H>, PrimitiveValidationType<infer G, any> ] ? G | H :
34
- T extends readonly [ PrimitiveValidationType<any, infer H>, ...any[], PrimitiveValidationType<infer G, any> ] ? G | H :
35
- never
36
- ;
37
-
38
- type HandleBrandTemplateEnvParamType<G extends unknown> =
39
- G extends Brand<infer B> ? `${Brands[B]}` & { z__brand: B }
40
- : `${G}`
41
- ;
42
-
43
- type GetTemplateEnvParamType<const T extends PrimitiveValidationType<unknown, unknown>[]> =
44
- T extends readonly [ PrimitiveValidationType<infer G, infer H> ] ? HandleBrandTemplateEnvParamType<G> | H :
45
- T extends readonly [ PrimitiveValidationType<any, infer H>, PrimitiveValidationType<infer G, any> ] ? HandleBrandTemplateEnvParamType<G> | H :
46
- T extends readonly [ PrimitiveValidationType<any, infer H>, ...any[], PrimitiveValidationType<infer G, any> ] ? HandleBrandTemplateEnvParamType<G> | H :
47
- never
48
- ;
49
-
50
- type ExtractTemplateParamType<T> = Exclude<GetTemplateParamType<T>, undefined>
51
-
52
- type ExtractTemplateEnvParamType<const T> = Replace<Exclude<GetTemplateEnvParamType<T>, undefined>, null, undefined>
53
-
54
- type ExtractTemplateType<T extends Template> = {
55
- [P in keyof T]: T[P] extends Template ? ExtractTemplateType<T[P]> :
56
- T[P] extends readonly Template[] ? ExtractTemplateType<T[P][0]>[] :
57
- T[P] extends readonly (readonly PrimitiveValidationType<unknown, unknown>[])[] ? ExtractTemplateParamType<T[P][0]>[] :
58
- T[P] extends readonly PrimitiveValidationType<unknown, unknown>[] ? ExtractTemplateParamType<T[P]> :
59
- number | boolean | string | null
60
- }
61
-
62
-
63
- type ExtractTemplateEnvType<const T extends TemplateEnv> = {
64
- [P in keyof T]: ExtractTemplateEnvParamType<T[P]>
65
- }
66
-
67
- type ExtractTemplateTypeNotValid<T extends Template> = {
68
- [P in keyof T]: T[P] extends Template ? ExtractTemplateTypeNotValid<T[P]> :
69
- T[P] extends readonly Template[] ? ExtractTemplateTypeNotValid<T[P][0]>[] :
70
- T[P] extends readonly (readonly PrimitiveValidationType[])[] ? (number | boolean | string | null)[] :
71
- number | boolean | string | null
72
- }
73
-
74
- type ParcialTemplateType<T extends Template> = Partial<ExtractTemplateType<T>>
75
-
76
- type ModelTemplateValidation<T extends Template> = (value: ExtractTemplateType<T>) => Promise<PlataError | null>
77
-
78
- type ModelTemplateFilter<T extends Template> = (value: DeepWritable<ExtractTemplateTypeNotValid<T>>) => DeepWritable<ExtractTemplateTypeNotValid<T>>
79
-
80
- type ModelTemplateEnvValidation<const T extends TemplateEnv> = (value: ExtractTemplateEnvType<T>) => Promise<PlataError | null>
81
-
82
- type ModelValidateReturn<T extends Template> =
83
- ModelValidateReturnHelper<ExtractTemplateType<T>, ExtractTemplateTypeNotValid<T>>
84
- ;
85
-
86
- type ModelValidateEnvReturn<const T extends TemplateEnv> =
87
- ModelValidateReturnHelper<ExtractTemplateEnvType<T>, undefined>
88
- ;
89
-
90
- type ModelValidateReturnHelper<const OKAY, const ERROR> =
91
- | {
92
- errors: undefined,
93
- value: OKAY
94
- }
95
- | {
96
- errors: PlataError[]
97
- value: ERROR
98
- }
99
- ;
100
-
101
- type ConvertFunction<T extends Template, O> = (x: ExtractTemplateType<T>) => O
102
-
103
- type Converters<T extends Template> = (AnyRecord<Model.ConvertFunction<T, any>>)
104
-
105
- type GetConverterType<T extends Template, F> = F extends ConvertFunction<T, infer G> ? G : never
106
-
107
- type ModelConvertFuntion<T extends Template, C extends Converters<T>> =
108
- <K extends keyof C>(value: ExtractTemplateType<T>, to: K) => PlataResult<GetConverterType<T, C[K]>>
109
- ;
110
-
111
- interface TemplateUnionConfig {
112
- key: string
113
- }
114
-
115
- interface ModelClass {
116
- readonly type: Record<string | number, any>
117
- filter?: (value: any) => any
118
- validate(value: any, skipFilter?: true): Promise<Model.ModelValidateReturnHelper<any, any>>
119
-
120
- }
121
-
122
- type TemplateRecord =
123
- Record<string | `${number}` | `${boolean}` | 'null', ModelClass>
124
- ;
125
-
126
- type ExtractModelUnionType<const C extends TemplateUnionConfig, const R extends TemplateRecord> = {
127
- [K in keyof R]: Record<C['key'], K> & R[K]['type']
128
- }[keyof R]
129
-
130
- type RemoveTypesForFilter<T extends Record<string, any>> = {
131
- -readonly [K in keyof T]:
132
- T[K] extends Record<string, any> ? RemoveTypesForFilter<T[K]>
133
- : number | boolean | string | null
134
-
135
- }
136
-
137
- type ExtractModelUnionTypeFilter<const C extends TemplateUnionConfig, const R extends TemplateRecord> = {
138
- [K in keyof R]: Record<C['key'], K> & RemoveTypesForFilter<R[K]['type']>
139
- }[keyof R]
140
-
141
- type ExtractModelUnionReturn<const C extends TemplateUnionConfig, const R extends TemplateRecord> =
142
- {
143
- [K in keyof R]: {
144
- model: R[K],
145
- value: R[K]['type'] & Record<C['key'], K>
146
- }
147
- }[keyof R]
148
- ;
1
+ declare namespace Model {
2
+
3
+ type ValidateReturn = PlataError | null
4
+
5
+ interface PrimitiveValidationTypeReturn {
6
+ error?: PlataError
7
+ continua: boolean
8
+ }
9
+
10
+ type PrimitiveValidationTypeFunction<T = any, D = undefined> =
11
+ (nome: string, valor: T | D) => Promise<PrimitiveValidationTypeReturn>
12
+ ;
13
+
14
+
15
+ interface PrimitiveValidationType<T = any, D = undefined> extends PrimitiveValidationTypeFunction<T, D>{
16
+ filter?: (value: any) => any
17
+ }
18
+
19
+ interface Template {
20
+ readonly [name: string]:
21
+ readonly PrimitiveValidationType<unknown, unknown>[] |
22
+ readonly Template[] |
23
+ Template |
24
+ readonly (readonly PrimitiveValidationType<unknown, unknown>[])[]
25
+ }
26
+
27
+ interface TemplateEnv {
28
+ readonly [name: string]: readonly PrimitiveValidationType<unknown, unknown>[]
29
+ }
30
+
31
+ type GetTemplateParamType<T> =
32
+ T extends readonly [ PrimitiveValidationType<infer G, infer H> ] ? G | H :
33
+ T extends readonly [ PrimitiveValidationType<any, infer H>, PrimitiveValidationType<infer G, any> ] ? G | H :
34
+ T extends readonly [ PrimitiveValidationType<any, infer H>, ...any[], PrimitiveValidationType<infer G, any> ] ? G | H :
35
+ never
36
+ ;
37
+
38
+ type HandleBrandTemplateEnvParamType<G extends unknown> =
39
+ G extends Brand<infer B> ? `${Brands[B]}` & { z__brand: B }
40
+ : `${G}`
41
+ ;
42
+
43
+ type GetTemplateEnvParamType<const T extends PrimitiveValidationType<unknown, unknown>[]> =
44
+ T extends readonly [ PrimitiveValidationType<infer G, infer H> ] ? HandleBrandTemplateEnvParamType<G> | H :
45
+ T extends readonly [ PrimitiveValidationType<any, infer H>, PrimitiveValidationType<infer G, any> ] ? HandleBrandTemplateEnvParamType<G> | H :
46
+ T extends readonly [ PrimitiveValidationType<any, infer H>, ...any[], PrimitiveValidationType<infer G, any> ] ? HandleBrandTemplateEnvParamType<G> | H :
47
+ never
48
+ ;
49
+
50
+ type ExtractTemplateParamType<T> = Exclude<GetTemplateParamType<T>, undefined>
51
+
52
+ type ExtractTemplateEnvParamType<const T> = Replace<Exclude<GetTemplateEnvParamType<T>, undefined>, null, undefined>
53
+
54
+ type ExtractTemplateType<T extends Template> = {
55
+ [P in keyof T]: T[P] extends Template ? ExtractTemplateType<T[P]> :
56
+ T[P] extends readonly Template[] ? ExtractTemplateType<T[P][0]>[] :
57
+ T[P] extends readonly (readonly PrimitiveValidationType<unknown, unknown>[])[] ? ExtractTemplateParamType<T[P][0]>[] :
58
+ T[P] extends readonly PrimitiveValidationType<unknown, unknown>[] ? ExtractTemplateParamType<T[P]> :
59
+ number | boolean | string | null
60
+ }
61
+
62
+
63
+ type ExtractTemplateEnvType<const T extends TemplateEnv> = {
64
+ [P in keyof T]: ExtractTemplateEnvParamType<T[P]>
65
+ }
66
+
67
+ type ExtractTemplateTypeNotValid<T extends Template> = {
68
+ [P in keyof T]: T[P] extends Template ? ExtractTemplateTypeNotValid<T[P]> :
69
+ T[P] extends readonly Template[] ? ExtractTemplateTypeNotValid<T[P][0]>[] :
70
+ T[P] extends readonly (readonly PrimitiveValidationType[])[] ? (number | boolean | string | null)[] :
71
+ number | boolean | string | null
72
+ }
73
+
74
+ type ParcialTemplateType<T extends Template> = Partial<ExtractTemplateType<T>>
75
+
76
+ type ModelTemplateValidation<T extends Template> = (value: ExtractTemplateType<T>) => Promise<PlataError | null>
77
+
78
+ type ModelTemplateFilter<T extends Template> = (value: DeepWritable<ExtractTemplateTypeNotValid<T>>) => DeepWritable<ExtractTemplateTypeNotValid<T>>
79
+
80
+ type ModelTemplateEnvValidation<const T extends TemplateEnv> = (value: ExtractTemplateEnvType<T>) => Promise<PlataError | null>
81
+
82
+ type ModelValidateReturn<T extends Template> =
83
+ ModelValidateReturnHelper<ExtractTemplateType<T>, ExtractTemplateTypeNotValid<T>>
84
+ ;
85
+
86
+ type ModelValidateEnvReturn<const T extends TemplateEnv> =
87
+ ModelValidateReturnHelper<ExtractTemplateEnvType<T>, undefined>
88
+ ;
89
+
90
+ type ModelValidateReturnHelper<const OKAY, const ERROR> =
91
+ | {
92
+ errors: undefined,
93
+ value: OKAY
94
+ }
95
+ | {
96
+ errors: PlataError[]
97
+ value: ERROR
98
+ }
99
+ ;
100
+
101
+ type ConvertFunction<T extends Template, O> = (x: ExtractTemplateType<T>) => O
102
+
103
+ type Converters<T extends Template> = (AnyRecord<Model.ConvertFunction<T, any>>)
104
+
105
+ type GetConverterType<T extends Template, F> = F extends ConvertFunction<T, infer G> ? G : never
106
+
107
+ type ModelConvertFuntion<T extends Template, C extends Converters<T>> =
108
+ <K extends keyof C>(value: ExtractTemplateType<T>, to: K) => PlataResult<GetConverterType<T, C[K]>>
109
+ ;
110
+
111
+ interface TemplateUnionConfig {
112
+ key: string
113
+ }
114
+
115
+ interface ModelClass {
116
+ readonly type: Record<string | number, any>
117
+ filter?: (value: any) => any
118
+ validate(value: any, skipFilter?: true): Promise<Model.ModelValidateReturnHelper<any, any>>
119
+
120
+ }
121
+
122
+ type TemplateRecord =
123
+ Record<string | `${number}` | `${boolean}` | 'null', ModelClass>
124
+ ;
125
+
126
+ type ExtractModelUnionType<const C extends TemplateUnionConfig, const R extends TemplateRecord> = {
127
+ [K in keyof R]: Record<C['key'], K> & R[K]['type']
128
+ }[keyof R]
129
+
130
+ type RemoveTypesForFilter<T extends Record<string, any>> = {
131
+ -readonly [K in keyof T]:
132
+ T[K] extends Record<string, any> ? RemoveTypesForFilter<T[K]>
133
+ : number | boolean | string | null
134
+
135
+ }
136
+
137
+ type ExtractModelUnionTypeFilter<const C extends TemplateUnionConfig, const R extends TemplateRecord> = {
138
+ [K in keyof R]: Record<C['key'], K> & RemoveTypesForFilter<R[K]['type']>
139
+ }[keyof R]
140
+
141
+ type ExtractModelUnionReturn<const C extends TemplateUnionConfig, const R extends TemplateRecord> =
142
+ {
143
+ [K in keyof R]: {
144
+ model: R[K],
145
+ value: R[K]['type'] & Record<C['key'], K>
146
+ }
147
+ }[keyof R]
148
+ ;
149
149
  }