pwi-plata-type 0.4.165 → 0.4.166

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 (101) hide show
  1. package/README.md +2 -2
  2. package/__BUILD__/libs/sql.js +11 -4
  3. package/__BUILD__/libs/sql.js.map +1 -1
  4. package/package.json +53 -53
  5. package/src/@types/build.d.ts +26 -26
  6. package/src/@types/cluster.actions.d.ts +27 -27
  7. package/src/@types/cluster.d.ts +55 -55
  8. package/src/@types/create-cli.d.ts +8 -8
  9. package/src/@types/exported.d.ts +31 -31
  10. package/src/@types/fs.d.ts +6 -6
  11. package/src/@types/global.d.ts +113 -113
  12. package/src/@types/knex.d.ts +13 -13
  13. package/src/@types/model.d.ts +143 -143
  14. package/src/@types/router.d.ts +107 -107
  15. package/src/@types/sql.d.ts +75 -75
  16. package/src/@types/swagger.d.ts +22 -22
  17. package/src/bin/extras/create-api-cli.ts +218 -218
  18. package/src/bin/extras/create-cluster-cli.ts +160 -160
  19. package/src/bin/extras/create-lib-cli.ts +84 -84
  20. package/src/bin/extras/plata-create-tools.ts +97 -97
  21. package/src/bin/plata-api.ts +189 -189
  22. package/src/bin/plata-build.ts +17 -17
  23. package/src/bin/plata-cluster.ts +42 -42
  24. package/src/bin/plata-cmd.ts +18 -18
  25. package/src/bin/plata-create.ts +49 -49
  26. package/src/bin/plata-runtime.ts +131 -131
  27. package/src/bin/plata-swagger.ts +99 -99
  28. package/src/bin/runtime/_setupRuntime.ts +48 -48
  29. package/src/bin/runtime/_types.d.ts +7 -7
  30. package/src/bin/runtime/class.ts +20 -20
  31. package/src/bin/runtime/dirs.ts +25 -25
  32. package/src/bin/runtime/effect.ts +60 -60
  33. package/src/bin/runtime/fs.ts +10 -10
  34. package/src/bin/runtime/promises.ts +18 -18
  35. package/src/bin/runtime/require.ts +113 -113
  36. package/src/bin/runtime/sql.ts +12 -12
  37. package/src/bin/runtime/switch.ts +60 -60
  38. package/src/index.ts +19 -19
  39. package/src/libs/axios.ts +51 -51
  40. package/src/libs/build.ts +162 -162
  41. package/src/libs/cluster/cluster-controller.ts +93 -93
  42. package/src/libs/cluster.ts +330 -330
  43. package/src/libs/custom.ts +15 -15
  44. package/src/libs/env.ts +10 -10
  45. package/src/libs/fs.ts +399 -399
  46. package/src/libs/hot-reload.ts +63 -63
  47. package/src/libs/models/index.ts +4 -4
  48. package/src/libs/models/model-template-combine.ts +93 -93
  49. package/src/libs/models/model-template-env.ts +58 -58
  50. package/src/libs/models/model-template-union.ts +83 -83
  51. package/src/libs/models/model-template.ts +107 -107
  52. package/src/libs/models/model-validations.ts +443 -443
  53. package/src/libs/models/tools.ts +194 -194
  54. package/src/libs/router.ts +185 -185
  55. package/src/libs/sql.ts +788 -778
  56. package/src/libs/swagger.ts +81 -81
  57. package/src/libs/tools.ts +240 -240
  58. package/src/scripts/post-install/api.ts +32 -32
  59. package/src/scripts/post-install/cluster.ts +28 -28
  60. package/src/scripts/post-install/lib.ts +25 -25
  61. package/src/scripts/post-install.ts +64 -64
  62. package/src/scripts/pre-test.ts +33 -33
  63. package/src/tests/assets/sql/example.sql +76 -76
  64. package/src/tests/config.test.ts +12 -12
  65. package/src/tests/effect.test.ts +62 -62
  66. package/src/tests/model.env.test.ts +66 -66
  67. package/src/tests/model.test.ts +141 -141
  68. package/src/tests/model.union.test.ts +160 -160
  69. package/src/tests/router.test.ts +44 -44
  70. package/src/tests/sql.test.ts +502 -502
  71. package/src/tests/switch.test.ts +76 -76
  72. package/templates/create-cli/class.ts +13 -13
  73. package/templates/create-cli/cluster.ts +14 -14
  74. package/templates/create-cli/cmd.ts +3 -3
  75. package/templates/create-cli/config.ts +2 -2
  76. package/templates/create-cli/lib.ts +3 -3
  77. package/templates/create-cli/model.ts +14 -14
  78. package/templates/create-cli/route.ts +8 -8
  79. package/templates/create-cli/task.ts +13 -13
  80. package/templates/postinstall/api/.vscode/launch.json +20 -20
  81. package/templates/postinstall/api/Dockerfile +24 -24
  82. package/templates/postinstall/api/configs/env.ts +2 -2
  83. package/templates/postinstall/api/envs/debug.env +15 -15
  84. package/templates/postinstall/api/envs/homolog.env +15 -15
  85. package/templates/postinstall/api/envs/prod.env +15 -15
  86. package/templates/postinstall/api/tsconfig.json +24 -24
  87. package/templates/postinstall/cluster/.vscode/launch.json +20 -20
  88. package/templates/postinstall/cluster/Dockerfile +24 -24
  89. package/templates/postinstall/cluster/configs/env.ts +2 -2
  90. package/templates/postinstall/cluster/envs/debug.env +10 -10
  91. package/templates/postinstall/cluster/envs/homolog.env +10 -10
  92. package/templates/postinstall/cluster/envs/prod.env +10 -10
  93. package/templates/postinstall/cluster/tsconfig.json +24 -24
  94. package/templates/postinstall/lib/postinstall-tools.mjs +111 -111
  95. package/templates/postinstall/lib/postinstall.js +33 -33
  96. package/templates/postinstall/lib/tsconfig.json +22 -22
  97. package/templates/swagger-cli/header.json +21 -21
  98. package/templates/swagger-cli/route/body.json +14 -14
  99. package/templates/swagger-cli/route/header.json +18 -18
  100. package/templates/swagger-cli/route/response.json +53 -53
  101. package/tsconfig.json +28 -28
@@ -1,114 +1,114 @@
1
- import glob from 'glob'
2
- import path from 'node:path'
3
-
4
- declare global {
5
- interface PlataRuntime extends PlataRuntimeEType {}
6
- }
7
-
8
- type PlataRuntimeEType = typeof PlataRuntimeE
9
-
10
- export namespace PlataRuntimeE {
11
-
12
- export async function RequireAsync<T = any>(p: string): RequireAsyncReturn<T> {
13
- try {
14
- return {
15
- filePath: p,
16
- name: path.basename(p).replace('.ts', '').replace('.js', ''),
17
- exports: require(p) as T
18
- }
19
- } catch(err) {
20
- return {
21
- errorID: 'PLTRF0001',
22
- msg: `Error while importing file ${p}`,
23
- error: {
24
- message: err?.toString(),
25
- stack: err?.stack ?? new Error().stack
26
- }
27
- }
28
- }
29
- }
30
-
31
- export async function RequireFolder<T = any>(p: string): RequireFolderReturn<T> {
32
- const promises: PlataPromise<RequiredInterface<T>>[] = []
33
-
34
- glob.sync(`${p.replace(/\\/g, '/')}/**/*.+(js|ts)`)
35
- .filter(file => !file.endsWith('.d.ts'))
36
- .forEach(file => {
37
- file = file.replace(/(\.ts|\.js)$/, '')
38
-
39
- promises.push(Plata.FastPromise(() => Plata.RequireAsync(file)))
40
- })
41
- ;
42
-
43
- return Promise.all(promises).then(
44
- rs => {
45
- const errors: PlataError[] = []
46
- const result: RequiredInterface<T>[] = []
47
-
48
- rs.forEach(r => {
49
- if (r.errorID !== undefined) {
50
- errors.push(r)
51
- } else {
52
- result.push(r)
53
- }
54
- })
55
-
56
- if (errors.length !== 0) {
57
- return {
58
- errorID: 'PLTRF0003',
59
- msg: `Errors while importing folder: ${p}`,
60
- error: errors
61
- }
62
- }
63
-
64
- return result
65
- },
66
- err => {
67
- return {
68
- errorID: 'PLTRF0002',
69
- msg: `Error while importing folder: ${p}`,
70
- error: {
71
- message: err?.toString(),
72
- stack: err?.stack ?? new Error().stack
73
- }
74
- }
75
- }
76
-
77
- )
78
- }
79
-
80
- export async function ReadEnvFileToPromise(p: string) {
81
- const envFile = Object.create(null)
82
-
83
- if (await Plata.files.access(p.replace('__BUILD__/', ''))) {
84
- p = p.replace('__BUILD__/', '')
85
- }
86
-
87
- const result = await Plata.files.ReadFileAsync(p, async line => {
88
- if (line[0] === '#') return
89
-
90
- if (line !== '') {
91
- const env = line.split('=')
92
-
93
- if (env.length >= 2) {
94
- const [ key, ...v ] = env
95
- let value = v.join('=')
96
-
97
- if (value[0] === '{' && value.slice(-1) === '}')
98
- value = process.env[value.slice(1, -1)] ?? ''
99
- ;
100
-
101
- if (value !== '')
102
- envFile[key] = value
103
- ;
104
- }
105
- }
106
- })
107
-
108
- if (result !== null) {
109
- return result
110
- }
111
-
112
- return envFile
113
- }
1
+ import glob from 'glob'
2
+ import path from 'node:path'
3
+
4
+ declare global {
5
+ interface PlataRuntime extends PlataRuntimeEType {}
6
+ }
7
+
8
+ type PlataRuntimeEType = typeof PlataRuntimeE
9
+
10
+ export namespace PlataRuntimeE {
11
+
12
+ export async function RequireAsync<T = any>(p: string): RequireAsyncReturn<T> {
13
+ try {
14
+ return {
15
+ filePath: p,
16
+ name: path.basename(p).replace('.ts', '').replace('.js', ''),
17
+ exports: require(p) as T
18
+ }
19
+ } catch(err) {
20
+ return {
21
+ errorID: 'PLTRF0001',
22
+ msg: `Error while importing file ${p}`,
23
+ error: {
24
+ message: err?.toString(),
25
+ stack: err?.stack ?? new Error().stack
26
+ }
27
+ }
28
+ }
29
+ }
30
+
31
+ export async function RequireFolder<T = any>(p: string): RequireFolderReturn<T> {
32
+ const promises: PlataPromise<RequiredInterface<T>>[] = []
33
+
34
+ glob.sync(`${p.replace(/\\/g, '/')}/**/*.+(js|ts)`)
35
+ .filter(file => !file.endsWith('.d.ts'))
36
+ .forEach(file => {
37
+ file = file.replace(/(\.ts|\.js)$/, '')
38
+
39
+ promises.push(Plata.FastPromise(() => Plata.RequireAsync(file)))
40
+ })
41
+ ;
42
+
43
+ return Promise.all(promises).then(
44
+ rs => {
45
+ const errors: PlataError[] = []
46
+ const result: RequiredInterface<T>[] = []
47
+
48
+ rs.forEach(r => {
49
+ if (r.errorID !== undefined) {
50
+ errors.push(r)
51
+ } else {
52
+ result.push(r)
53
+ }
54
+ })
55
+
56
+ if (errors.length !== 0) {
57
+ return {
58
+ errorID: 'PLTRF0003',
59
+ msg: `Errors while importing folder: ${p}`,
60
+ error: errors
61
+ }
62
+ }
63
+
64
+ return result
65
+ },
66
+ err => {
67
+ return {
68
+ errorID: 'PLTRF0002',
69
+ msg: `Error while importing folder: ${p}`,
70
+ error: {
71
+ message: err?.toString(),
72
+ stack: err?.stack ?? new Error().stack
73
+ }
74
+ }
75
+ }
76
+
77
+ )
78
+ }
79
+
80
+ export async function ReadEnvFileToPromise(p: string) {
81
+ const envFile = Object.create(null)
82
+
83
+ if (await Plata.files.access(p.replace('__BUILD__/', ''))) {
84
+ p = p.replace('__BUILD__/', '')
85
+ }
86
+
87
+ const result = await Plata.files.ReadFileAsync(p, async line => {
88
+ if (line[0] === '#') return
89
+
90
+ if (line !== '') {
91
+ const env = line.split('=')
92
+
93
+ if (env.length >= 2) {
94
+ const [ key, ...v ] = env
95
+ let value = v.join('=')
96
+
97
+ if (value[0] === '{' && value.slice(-1) === '}')
98
+ value = process.env[value.slice(1, -1)] ?? ''
99
+ ;
100
+
101
+ if (value !== '')
102
+ envFile[key] = value
103
+ ;
104
+ }
105
+ }
106
+ })
107
+
108
+ if (result !== null) {
109
+ return result
110
+ }
111
+
112
+ return envFile
113
+ }
114
114
  }
@@ -1,13 +1,13 @@
1
- declare global {
2
- interface PlataRuntime extends PlataRuntimeEType {}
3
- }
4
-
5
- type PlataRuntimeEType = typeof PlataRuntimeE
6
-
7
- export namespace PlataRuntimeE {
8
- export function TimeZoneDate(d: Date): Sql.TimeZoneDate {
9
- return new Date(d.getTime() - (d.getTimezoneOffset() * 1000 * 60)) as any
10
- }
11
-
12
- export let _knexExtended: boolean = false
1
+ declare global {
2
+ interface PlataRuntime extends PlataRuntimeEType {}
3
+ }
4
+
5
+ type PlataRuntimeEType = typeof PlataRuntimeE
6
+
7
+ export namespace PlataRuntimeE {
8
+ export function TimeZoneDate(d: Date): Sql.TimeZoneDate {
9
+ return new Date(d.getTime() - (d.getTimezoneOffset() * 1000 * 60)) as any
10
+ }
11
+
12
+ export let _knexExtended: boolean = false
13
13
  }
@@ -1,61 +1,61 @@
1
- declare global {
2
- interface PlataRuntime extends PlataRuntimeEType {}
3
- }
4
-
5
- type PlataRuntimeEType = typeof PlataRuntimeE
6
-
7
- type SwitchUnionObjectFunctionCases<V extends DictionaryRecord<string | number, any>, K extends keyof V> = {
8
- [type in V[K] | '_default']:
9
- type extends '_default' ? (v: V) => Promise<unknown>
10
- : V extends { [P in K]: type } ? (v: V) => Promise<unknown>
11
- : never
12
- }
13
-
14
- type SwitchUnionFunctionCases<V extends string | number> = {
15
- [T in (V | '_default')]: T extends V ?
16
- (v: T) => Promise<unknown>
17
- : (v: unknown) => Promise<unknown>
18
- }
19
-
20
- type SwitchUnionFunction =
21
- <V extends string | number, C extends SwitchUnionFunctionCases<V>>
22
- (
23
- values: V,
24
- cases: C,
25
- ) => PlataPromise<Awaited<ReturnType<C[V | '_default']>>>
26
- ;
27
-
28
- type SwitchUnionObjectFunction =
29
- <const V extends DictionaryRecord<string, any>, K extends keyof V, C extends SwitchUnionObjectFunctionCases<V, K> = SwitchUnionObjectFunctionCases<V, K>>
30
- (
31
- object: V,
32
- chave: K,
33
- cases: C
34
- ) => PlataPromise<Awaited<ReturnType<C[V[K] | '_default']>>>
35
- ;
36
-
37
- export namespace PlataRuntimeE {
38
- export const switchUnion: SwitchUnionFunction = (value, cases) => {
39
- const f = cases[value] as any
40
-
41
- if (f === undefined)
42
- return cases['_default'](value as any) as any
43
- ;
44
-
45
- return f(value as any) as any
46
- }
47
-
48
- export const switchUnionObject: SwitchUnionObjectFunction = (v, chave, cases) => {
49
- if (v === undefined || v === null)
50
- return cases['_default'](v) as any
51
- ;
52
-
53
- const f = cases[v[chave]]
54
-
55
- if (f === undefined)
56
- return cases['_default'](v) as any
57
- ;
58
-
59
- return f(v) as any
60
- }
1
+ declare global {
2
+ interface PlataRuntime extends PlataRuntimeEType {}
3
+ }
4
+
5
+ type PlataRuntimeEType = typeof PlataRuntimeE
6
+
7
+ type SwitchUnionObjectFunctionCases<V extends DictionaryRecord<string | number, any>, K extends keyof V> = {
8
+ [type in V[K] | '_default']:
9
+ type extends '_default' ? (v: V) => Promise<unknown>
10
+ : V extends { [P in K]: type } ? (v: V) => Promise<unknown>
11
+ : never
12
+ }
13
+
14
+ type SwitchUnionFunctionCases<V extends string | number> = {
15
+ [T in (V | '_default')]: T extends V ?
16
+ (v: T) => Promise<unknown>
17
+ : (v: unknown) => Promise<unknown>
18
+ }
19
+
20
+ type SwitchUnionFunction =
21
+ <V extends string | number, C extends SwitchUnionFunctionCases<V>>
22
+ (
23
+ values: V,
24
+ cases: C,
25
+ ) => PlataPromise<Awaited<ReturnType<C[V | '_default']>>>
26
+ ;
27
+
28
+ type SwitchUnionObjectFunction =
29
+ <const V extends DictionaryRecord<string, any>, K extends keyof V, C extends SwitchUnionObjectFunctionCases<V, K> = SwitchUnionObjectFunctionCases<V, K>>
30
+ (
31
+ object: V,
32
+ chave: K,
33
+ cases: C
34
+ ) => PlataPromise<Awaited<ReturnType<C[V[K] | '_default']>>>
35
+ ;
36
+
37
+ export namespace PlataRuntimeE {
38
+ export const switchUnion: SwitchUnionFunction = (value, cases) => {
39
+ const f = cases[value] as any
40
+
41
+ if (f === undefined)
42
+ return cases['_default'](value as any) as any
43
+ ;
44
+
45
+ return f(value as any) as any
46
+ }
47
+
48
+ export const switchUnionObject: SwitchUnionObjectFunction = (v, chave, cases) => {
49
+ if (v === undefined || v === null)
50
+ return cases['_default'](v) as any
51
+ ;
52
+
53
+ const f = cases[v[chave]]
54
+
55
+ if (f === undefined)
56
+ return cases['_default'](v) as any
57
+ ;
58
+
59
+ return f(v) as any
60
+ }
61
61
  }
package/src/index.ts CHANGED
@@ -1,20 +1,20 @@
1
- import * as PlataCluster from './libs/cluster'
2
- import * as PlataRoutes from './libs/router'
3
- import * as PlataTools from './libs/tools'
4
- import * as PlataModels from './libs/models'
5
- import * as PlataSwagger from './libs/swagger'
6
- import * as PlataSql from './libs/sql'
7
- import { PlataFs } from './libs/fs'
8
- import { PlataAxios, PlataAxiosUnsafe } from './libs/axios'
9
-
10
- export {
11
- PlataCluster,
12
- PlataRoutes,
13
- PlataTools,
14
- PlataModels,
15
- PlataSwagger,
16
- PlataAxios,
17
- PlataAxiosUnsafe,
18
- PlataSql,
19
- PlataFs
1
+ import * as PlataCluster from './libs/cluster'
2
+ import * as PlataRoutes from './libs/router'
3
+ import * as PlataTools from './libs/tools'
4
+ import * as PlataModels from './libs/models'
5
+ import * as PlataSwagger from './libs/swagger'
6
+ import * as PlataSql from './libs/sql'
7
+ import { PlataFs } from './libs/fs'
8
+ import { PlataAxios, PlataAxiosUnsafe } from './libs/axios'
9
+
10
+ export {
11
+ PlataCluster,
12
+ PlataRoutes,
13
+ PlataTools,
14
+ PlataModels,
15
+ PlataSwagger,
16
+ PlataAxios,
17
+ PlataAxiosUnsafe,
18
+ PlataSql,
19
+ PlataFs
20
20
  }
package/src/libs/axios.ts CHANGED
@@ -1,52 +1,52 @@
1
- import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
2
- import { PlataExtra, PlataResultado } from './tools';
3
-
4
- export async function PlataAxios<T = any>(config: AxiosRequestConfig): Promise<PlataResultado<AxiosResponse<T>>> {
5
- config.timeout = config.timeout ?? 1000
6
-
7
- if (config.timeout === 0) {
8
- return PlataAxiosUnsafe(config)
9
- }
10
-
11
- const axiosPromise = PlataAxiosUnsafe(config)
12
- const timeoutPromise = PlataExtra.timeout(config.timeout, {
13
- errorID: 'PLAX0003',
14
- msg: 'API externa não respondeu',
15
- error: {
16
- stack: new Error().stack
17
- }
18
- })
19
-
20
- return Promise.race([axiosPromise, timeoutPromise])
21
- }
22
-
23
- export async function PlataAxiosUnsafe<T = any>(config: AxiosRequestConfig): Promise<PlataResultado<AxiosResponse<T>>> {
24
- return axios(config).then(
25
- resp => resp,
26
- err => {
27
- if (err.response) {
28
- return err.response
29
- }
30
-
31
- if (err.request) {
32
- return {
33
- errorID: 'PLAX0001',
34
- msg: 'Erro ao se comunicar com API externa',
35
- error: {
36
- message: err?.toString(),
37
- stack: err?.stack ?? new Error().stack
38
- }
39
- }
40
- }
41
-
42
- return {
43
- errorID: 'PLAX0002',
44
- msg: 'Erro inesperado ao se comunicar com API externa',
45
- error: {
46
- message: err?.toString(),
47
- stack: err?.stack ?? new Error().stack
48
- }
49
- }
50
- }
51
- )
1
+ import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
2
+ import { PlataExtra, PlataResultado } from './tools';
3
+
4
+ export async function PlataAxios<T = any>(config: AxiosRequestConfig): Promise<PlataResultado<AxiosResponse<T>>> {
5
+ config.timeout = config.timeout ?? 1000
6
+
7
+ if (config.timeout === 0) {
8
+ return PlataAxiosUnsafe(config)
9
+ }
10
+
11
+ const axiosPromise = PlataAxiosUnsafe(config)
12
+ const timeoutPromise = PlataExtra.timeout(config.timeout, {
13
+ errorID: 'PLAX0003',
14
+ msg: 'API externa não respondeu',
15
+ error: {
16
+ stack: new Error().stack
17
+ }
18
+ })
19
+
20
+ return Promise.race([axiosPromise, timeoutPromise])
21
+ }
22
+
23
+ export async function PlataAxiosUnsafe<T = any>(config: AxiosRequestConfig): Promise<PlataResultado<AxiosResponse<T>>> {
24
+ return axios(config).then(
25
+ resp => resp,
26
+ err => {
27
+ if (err.response) {
28
+ return err.response
29
+ }
30
+
31
+ if (err.request) {
32
+ return {
33
+ errorID: 'PLAX0001',
34
+ msg: 'Erro ao se comunicar com API externa',
35
+ error: {
36
+ message: err?.toString(),
37
+ stack: err?.stack ?? new Error().stack
38
+ }
39
+ }
40
+ }
41
+
42
+ return {
43
+ errorID: 'PLAX0002',
44
+ msg: 'Erro inesperado ao se comunicar com API externa',
45
+ error: {
46
+ message: err?.toString(),
47
+ stack: err?.stack ?? new Error().stack
48
+ }
49
+ }
50
+ }
51
+ )
52
52
  }