svelte-reflector 1.0.5 → 1.0.7
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/dist/helpers/helpers.js +1 -1
- package/dist/main.d.ts +1 -0
- package/dist/main.js +3 -0
- package/dist/method.d.ts +1 -1
- package/dist/method.js +39 -17
- package/dist/module.d.ts +6 -1
- package/dist/module.js +47 -29
- package/dist/property.js +2 -6
- package/package.json +1 -1
package/dist/helpers/helpers.js
CHANGED
package/dist/main.d.ts
CHANGED
package/dist/main.js
CHANGED
|
@@ -12,6 +12,7 @@ export class Reflector {
|
|
|
12
12
|
generatedDir = `${this.dir}/reflector`;
|
|
13
13
|
localDoc = new Source({ path: path.resolve(process.cwd(), `${this.dir}/backup.json`) });
|
|
14
14
|
src = new Source({ path: path.resolve(process.cwd(), this.generatedDir) });
|
|
15
|
+
typesSrc = new Source({ path: path.resolve(process.cwd(), `${this.generatedDir}/reflector.types.ts`) });
|
|
15
16
|
schemaFile = new Source({ path: path.resolve(process.cwd(), `${this.generatedDir}/schemas.ts`) });
|
|
16
17
|
files;
|
|
17
18
|
schemas;
|
|
@@ -83,6 +84,8 @@ export class Reflector {
|
|
|
83
84
|
build() {
|
|
84
85
|
this.schemaFile.changeData([`import z from 'zod';`, ...this.schemas.map((s) => `${s.schema} ${s.type}`)].join("\n\n"));
|
|
85
86
|
this.schemaFile.save();
|
|
87
|
+
this.typesSrc.changeData("export class Behavior { onError?: () => void; onSuccess?: () => void }");
|
|
88
|
+
this.typesSrc.save();
|
|
86
89
|
for (const module of this.modules) {
|
|
87
90
|
if (module.methods.length === 0)
|
|
88
91
|
continue;
|
package/dist/method.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { ReflectorOperation } from "./types/types.js";
|
|
|
4
4
|
export declare class Method {
|
|
5
5
|
name: string;
|
|
6
6
|
zodProperties: ZodProperty[];
|
|
7
|
-
description
|
|
7
|
+
description?: string;
|
|
8
8
|
request: Request;
|
|
9
9
|
constructor(params: {
|
|
10
10
|
operation: ReflectorOperation;
|
package/dist/method.js
CHANGED
|
@@ -9,7 +9,7 @@ export class Method {
|
|
|
9
9
|
constructor(params) {
|
|
10
10
|
const { operation } = params;
|
|
11
11
|
this.request = new Request(operation);
|
|
12
|
-
this.description = operation.description;
|
|
12
|
+
this.description = operation.description ?? operation.summary;
|
|
13
13
|
this.name = operation.operationId?.split("_")[1] ?? this.request.apiType;
|
|
14
14
|
const { parameters } = this.getParams(params);
|
|
15
15
|
this.zodProperties = parameters;
|
|
@@ -56,15 +56,13 @@ export class Method {
|
|
|
56
56
|
beforeResponse.push(`\n`);
|
|
57
57
|
}
|
|
58
58
|
if (this.request.attributeType === "list") {
|
|
59
|
-
beforeResponse.push(`const {data, ...params} = response`, "\n\n", `this.list = data`, `repo.intercept.rebuild(this.parameters, params)`);
|
|
59
|
+
beforeResponse.push(`const {data: { data }, ...params} = response`, "\n\n", `this.list = data`, `repo.intercept.rebuild(this.parameters, params)`);
|
|
60
60
|
return `
|
|
61
61
|
${afterResponse.join(";")}
|
|
62
|
-
const response = await repo.api.get<{data: ${this.request.responseType}
|
|
62
|
+
const response = await repo.api.get<{data: ${this.request.responseType}}, unknown>({
|
|
63
63
|
endpoint: this.endpoint, ${query}
|
|
64
64
|
})
|
|
65
65
|
${beforeResponse.join(";")}
|
|
66
|
-
|
|
67
|
-
return response
|
|
68
66
|
`;
|
|
69
67
|
}
|
|
70
68
|
else if (this.request.attributeType === "entity") {
|
|
@@ -75,12 +73,10 @@ export class Method {
|
|
|
75
73
|
endpoint: this.endpoint, ${query}
|
|
76
74
|
})
|
|
77
75
|
${beforeResponse.join(";")}
|
|
78
|
-
|
|
79
|
-
return response
|
|
80
76
|
`;
|
|
81
77
|
}
|
|
82
78
|
}
|
|
83
|
-
else if (this.request.apiType === "post" || this.request.apiType === "put") {
|
|
79
|
+
else if (this.request.apiType === "post" || this.request.apiType === "put" || this.request.apiType === "patch") {
|
|
84
80
|
let data = "";
|
|
85
81
|
if (this.request.bodyType) {
|
|
86
82
|
data = `const data = repo.intercept.bundle(this.forms.${this.name})`;
|
|
@@ -92,21 +88,19 @@ export class Method {
|
|
|
92
88
|
endpoint: this.endpoint,
|
|
93
89
|
${data ? "data" : ""}
|
|
94
90
|
})
|
|
95
|
-
|
|
96
|
-
return response
|
|
97
91
|
`;
|
|
98
92
|
}
|
|
99
93
|
else if (this.request.apiType === "delete") {
|
|
100
94
|
const props = this.zodProperties.map((x) => x.name).join(",");
|
|
95
|
+
const propsString = props.length > 0 ? `const {${props}} = this.parameters` : "";
|
|
101
96
|
return `
|
|
102
|
-
|
|
97
|
+
${propsString}
|
|
103
98
|
|
|
104
|
-
const response = await repo.api.delete<${this.request.responseType}, unknown>({
|
|
99
|
+
const response = await repo.api.delete<${this.request.responseType ?? "null"}, unknown>({
|
|
105
100
|
endpoint: this.endpoint, ${query}
|
|
106
101
|
})
|
|
107
102
|
|
|
108
|
-
this.
|
|
109
|
-
return response
|
|
103
|
+
this.clearEntity()
|
|
110
104
|
`;
|
|
111
105
|
}
|
|
112
106
|
return "";
|
|
@@ -124,11 +118,39 @@ export class Method {
|
|
|
124
118
|
if (!hasProprierties && this.request.apiType === "delete") {
|
|
125
119
|
createDangerMessage(`${this.name} não vai funcionar, pois não aceita parâmetros na requisição.`);
|
|
126
120
|
}
|
|
121
|
+
let additionalMethod = "";
|
|
122
|
+
const description = this.buildDescription();
|
|
123
|
+
if (this.request.apiType === "post") {
|
|
124
|
+
additionalMethod = `
|
|
125
|
+
/** Limpa a entity depois de ser criada com sucesso */
|
|
126
|
+
async ${this.name}AndClear(behavior: Behavior = new Behavior()) {
|
|
127
|
+
const data = await this.${this.name}(behavior)
|
|
128
|
+
|
|
129
|
+
if(data) {
|
|
130
|
+
this.clearEntity()
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return data
|
|
134
|
+
}
|
|
135
|
+
`;
|
|
136
|
+
}
|
|
127
137
|
return `
|
|
128
|
-
${
|
|
129
|
-
async ${this.name}() {
|
|
130
|
-
|
|
138
|
+
${description}
|
|
139
|
+
async ${this.name}(behavior: Behavior = new Behavior()) {
|
|
140
|
+
const {onError, onSuccess} = behavior
|
|
141
|
+
|
|
142
|
+
try{
|
|
143
|
+
${content}
|
|
144
|
+
onSuccess?.()
|
|
145
|
+
|
|
146
|
+
return response
|
|
147
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
148
|
+
} catch(e) {
|
|
149
|
+
onError?.()
|
|
150
|
+
}
|
|
131
151
|
}
|
|
152
|
+
|
|
153
|
+
${additionalMethod}
|
|
132
154
|
`;
|
|
133
155
|
}
|
|
134
156
|
}
|
package/dist/module.d.ts
CHANGED
|
@@ -22,5 +22,10 @@ export declare class Module {
|
|
|
22
22
|
private getParameters;
|
|
23
23
|
private buildImports;
|
|
24
24
|
private buildClass;
|
|
25
|
-
buildFile(
|
|
25
|
+
buildFile(params: {
|
|
26
|
+
moduleAttributes: string[];
|
|
27
|
+
moduleTypes: string[];
|
|
28
|
+
moduleInit: string[];
|
|
29
|
+
moduleClear: string[];
|
|
30
|
+
}): string;
|
|
26
31
|
}
|
package/dist/module.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import { Source } from "./file.js";
|
|
4
|
-
import { capitalizeFirstLetter, createDangerMessage
|
|
4
|
+
import { capitalizeFirstLetter, createDangerMessage } from "./helpers/helpers.js";
|
|
5
5
|
import { Method } from "./method.js";
|
|
6
6
|
export class Module {
|
|
7
7
|
name;
|
|
@@ -14,7 +14,11 @@ export class Module {
|
|
|
14
14
|
constructor(params) {
|
|
15
15
|
const { name, operations, endpoint, dir, moduleName } = params;
|
|
16
16
|
this.moduleName = moduleName;
|
|
17
|
-
this.imports = new Set([
|
|
17
|
+
this.imports = new Set([
|
|
18
|
+
"// AUTO GERADO. QUEM ALTERAR GOSTA DE RAPAZES!\n",
|
|
19
|
+
'import repo from "$repository/main"',
|
|
20
|
+
'import { Behavior } from "$reflector/reflector.types";',
|
|
21
|
+
]);
|
|
18
22
|
this.name = capitalizeFirstLetter(name);
|
|
19
23
|
this.endpoint = endpoint;
|
|
20
24
|
const methods = operations.map((operation) => {
|
|
@@ -27,28 +31,31 @@ export class Module {
|
|
|
27
31
|
this.methods = methods.filter((op) => {
|
|
28
32
|
const responseTypeOk = op.request.responseType;
|
|
29
33
|
const propertiesOk = op.zodProperties.length > 0;
|
|
34
|
+
if (op.request.apiType === "delete")
|
|
35
|
+
return true;
|
|
30
36
|
if (!responseTypeOk) {
|
|
31
37
|
createDangerMessage(`Método [ ${op.name} ] do módulo [ ${this.moduleName} ] sem tipagem na resposta.`);
|
|
32
38
|
}
|
|
33
|
-
|
|
34
|
-
createDangerMessage(`Método [ ${op.name} ] do módulo [ ${this.moduleName} ] com tipagem incorreta.`);
|
|
35
|
-
}
|
|
36
|
-
return responseTypeOk && propertiesOk;
|
|
39
|
+
return responseTypeOk;
|
|
37
40
|
});
|
|
38
41
|
this.parameters = this.getParameters();
|
|
39
|
-
const {
|
|
42
|
+
const { moduleAttributes, moduleTypes, moduleInit, moduleClear } = this.creator();
|
|
40
43
|
//sempre por último
|
|
41
44
|
this.src = new Source({
|
|
42
45
|
path: this.getPath(dir),
|
|
43
|
-
data: this.buildFile(
|
|
46
|
+
data: this.buildFile({ moduleAttributes, moduleTypes, moduleInit, moduleClear }),
|
|
44
47
|
});
|
|
45
48
|
}
|
|
46
49
|
creator() {
|
|
47
50
|
const buildedModuleTypes = [];
|
|
48
|
-
const
|
|
51
|
+
const moduleAttributes = new Set([`endpoint = '${this.endpoint}'`]);
|
|
52
|
+
const moduleInit = new Set([]);
|
|
53
|
+
const moduleClear = new Set([]);
|
|
49
54
|
if (this.parameters.length > 0) {
|
|
50
55
|
buildedModuleTypes.push(`const ParametersSchema = z.object({${this.parameters}})`);
|
|
51
|
-
|
|
56
|
+
moduleAttributes.add(`parameters = $state(repo.newForm(ParametersSchema))`);
|
|
57
|
+
moduleInit.add(`this.clearParameters()`);
|
|
58
|
+
moduleClear.add(`clearParameters() { this.parameters = repo.newForm(ParametersSchema) }`);
|
|
52
59
|
}
|
|
53
60
|
const form = [];
|
|
54
61
|
for (const method of this.methods) {
|
|
@@ -60,10 +67,14 @@ export class Module {
|
|
|
60
67
|
});
|
|
61
68
|
}
|
|
62
69
|
if (attributeType === "entity") {
|
|
63
|
-
|
|
70
|
+
moduleAttributes.add(`entity = $state<${responseType} | undefined>()`);
|
|
71
|
+
moduleInit.add("this.clearEntity()");
|
|
72
|
+
moduleClear.add(`clearEntity() { this.entity = undefined }`);
|
|
64
73
|
}
|
|
65
74
|
else if (attributeType === "list") {
|
|
66
|
-
|
|
75
|
+
moduleAttributes.add(`list = $state<${responseType}['data']>([])`);
|
|
76
|
+
moduleInit.add("this.clearList()");
|
|
77
|
+
moduleClear.add(`clearList() { this.list = [] }`);
|
|
67
78
|
}
|
|
68
79
|
if (attributeType === "list" || this.parameters.length > 0) {
|
|
69
80
|
this.imports.add(`import z from "zod";`);
|
|
@@ -74,15 +85,23 @@ export class Module {
|
|
|
74
85
|
formSet.add(`${f.name}: repo.newForm(${f.type}Schema)`);
|
|
75
86
|
}
|
|
76
87
|
if (formSet.size > 0) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
88
|
+
moduleAttributes.add(`
|
|
89
|
+
forms = $state({
|
|
90
|
+
${Array.from(formSet)}
|
|
91
|
+
})
|
|
92
|
+
`);
|
|
93
|
+
moduleInit.add(`
|
|
94
|
+
this.clearForms()
|
|
95
|
+
`);
|
|
96
|
+
moduleClear.add(`
|
|
97
|
+
clearForms() { this.forms = { ${Array.from(formSet)} } }
|
|
98
|
+
`);
|
|
82
99
|
}
|
|
83
100
|
return {
|
|
84
|
-
|
|
101
|
+
moduleAttributes: Array.from(moduleAttributes),
|
|
85
102
|
moduleTypes: buildedModuleTypes,
|
|
103
|
+
moduleInit: Array.from(moduleInit),
|
|
104
|
+
moduleClear: Array.from(moduleClear),
|
|
86
105
|
};
|
|
87
106
|
}
|
|
88
107
|
getPath(dir) {
|
|
@@ -123,32 +142,31 @@ export class Module {
|
|
|
123
142
|
return "";
|
|
124
143
|
return `import { ${cleanEntries} } from '$reflector/schemas';`;
|
|
125
144
|
}
|
|
126
|
-
buildClass(
|
|
127
|
-
const
|
|
145
|
+
buildClass(params) {
|
|
146
|
+
const { moduleInit, moduleAttributes, moduleClear } = params;
|
|
128
147
|
return `
|
|
129
148
|
export class ${this.moduleName}Module {
|
|
130
|
-
${
|
|
149
|
+
${moduleAttributes.join(";")}
|
|
131
150
|
|
|
132
151
|
${this.buildMethods().join("\n")}
|
|
133
152
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
clear() {
|
|
139
|
-
this.init()
|
|
153
|
+
${moduleClear.join("\n\n")}
|
|
154
|
+
|
|
155
|
+
clearAll() {
|
|
156
|
+
${moduleInit.join(";")}
|
|
140
157
|
}
|
|
141
158
|
}
|
|
142
159
|
`;
|
|
143
160
|
}
|
|
144
|
-
buildFile(
|
|
161
|
+
buildFile(params) {
|
|
162
|
+
const { moduleInit, moduleTypes, moduleAttributes, moduleClear } = params;
|
|
145
163
|
return `
|
|
146
164
|
${Array.from(this.imports).join(";")}
|
|
147
165
|
${this.buildImports()}
|
|
148
166
|
|
|
149
167
|
${moduleTypes.join(";")}
|
|
150
168
|
|
|
151
|
-
${this.buildClass(
|
|
169
|
+
${this.buildClass({ moduleAttributes, moduleInit, moduleClear })}
|
|
152
170
|
`;
|
|
153
171
|
}
|
|
154
172
|
}
|
package/dist/property.js
CHANGED
|
@@ -10,10 +10,11 @@ export class ZodProperty {
|
|
|
10
10
|
required;
|
|
11
11
|
constructor(params) {
|
|
12
12
|
const { name, schemaObject, type, example, required } = params;
|
|
13
|
+
const realExample = example ?? schemaObject.example;
|
|
13
14
|
this.required = required;
|
|
14
15
|
this.name = name;
|
|
15
16
|
this.type = type;
|
|
16
|
-
this.example = this.getExample(
|
|
17
|
+
this.example = this.getExample(realExample);
|
|
17
18
|
this.buildedProp = this.build(schemaObject);
|
|
18
19
|
}
|
|
19
20
|
getDtoName(ref) {
|
|
@@ -35,11 +36,6 @@ export class ZodProperty {
|
|
|
35
36
|
return "lorem ipsum";
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
|
-
// fiscalNumber = cpf | cnpj | fiscalNumber
|
|
39
|
-
// cep
|
|
40
|
-
// telefone
|
|
41
|
-
// CUID
|
|
42
|
-
// url
|
|
43
39
|
deepValidator() {
|
|
44
40
|
if (this.name === "email") {
|
|
45
41
|
return `z.email().default('${this.example}')`;
|