svelte-reflector 1.0.32 → 1.0.34
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/array.property.js +10 -7
- package/dist/main.js +27 -2
- package/dist/method.d.ts +6 -2
- package/dist/method.js +47 -14
- package/dist/module.d.ts +4 -4
- package/dist/module.imports.d.ts +2 -0
- package/dist/module.imports.js +2 -0
- package/dist/module.js +112 -119
- package/dist/object.property.js +1 -1
- package/dist/primitive-property.js +20 -4
- package/dist/request.d.ts +1 -2
- package/package.json +1 -1
package/dist/array.property.js
CHANGED
|
@@ -16,26 +16,29 @@ export class ArrayProp {
|
|
|
16
16
|
}
|
|
17
17
|
getType(params) {
|
|
18
18
|
const { schemaObject, schemaName } = params;
|
|
19
|
-
let name = schemaName;
|
|
20
19
|
const teste = schemaObject.items;
|
|
21
20
|
if (!teste)
|
|
22
|
-
return
|
|
21
|
+
return schemaName;
|
|
22
|
+
this.isSpecial = true;
|
|
23
23
|
if ("$ref" in teste) {
|
|
24
24
|
return teste.$ref.split("/").at(-1);
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
if (teste.enum && teste.type === "string") {
|
|
27
|
+
return "string";
|
|
28
|
+
}
|
|
29
|
+
return teste.type ?? "string";
|
|
28
30
|
}
|
|
29
31
|
constructorBuild() {
|
|
30
32
|
const result = this.isSpecial ? "" : `.map((param) => new ${this.type}(param))`;
|
|
31
33
|
return `this.${this.name} = params?.${this.name}${result} ?? []`;
|
|
32
34
|
}
|
|
33
35
|
classBuild() {
|
|
34
|
-
|
|
36
|
+
const sanitizedType = this.isSpecial ? this.type : `${this.type}[]`;
|
|
37
|
+
return `${this.name}: ${sanitizedType}[]`;
|
|
35
38
|
}
|
|
36
39
|
interfaceBuild() {
|
|
37
|
-
const
|
|
38
|
-
return `${this.name}: ${
|
|
40
|
+
const sanitizedType = this.isSpecial ? this.type : `${this.type}Interface`;
|
|
41
|
+
return `${this.name}: ${sanitizedType}[]`;
|
|
39
42
|
}
|
|
40
43
|
bundleBuild() {
|
|
41
44
|
const result = this.isSpecial ? "" : ".map((obj) => obj.bundle())";
|
package/dist/main.js
CHANGED
|
@@ -101,15 +101,26 @@ export class Reflector {
|
|
|
101
101
|
});
|
|
102
102
|
this.schemaFile.changeData([
|
|
103
103
|
'import { build, BuildedInput } from "$reflector/reflector.svelte";',
|
|
104
|
+
'import { validateInputs } from "$lib/sanitizers/validateFormats";',
|
|
104
105
|
// ...Array.from(enums),
|
|
105
106
|
...treatedSchemas,
|
|
106
107
|
].join("\n\n"));
|
|
107
108
|
this.schemaFile.save();
|
|
108
109
|
const buildFunctions = `
|
|
110
|
+
import toast from "$lib/utils/toast.svelte";
|
|
111
|
+
|
|
109
112
|
type ValidatorResult = string | null;
|
|
110
113
|
type ValidatorFn<T> = (v: T) => ValidatorResult;
|
|
114
|
+
type Partial<T> = {
|
|
115
|
+
[K in Exclude<keyof T, 'bundle'>]?: BuildedInput<T[K]>;
|
|
116
|
+
} & {
|
|
117
|
+
bundle: unknown;
|
|
118
|
+
};
|
|
111
119
|
|
|
112
|
-
export class Behavior
|
|
120
|
+
export class Behavior<TSuccess = unknown, TError = unknown> {
|
|
121
|
+
onError?: (e: TError) => void;
|
|
122
|
+
onSuccess?: (v: TSuccess) => void;
|
|
123
|
+
}
|
|
113
124
|
|
|
114
125
|
export class BuildedInput<T> {
|
|
115
126
|
value = $state<T>(null as any);
|
|
@@ -135,7 +146,7 @@ export class Reflector {
|
|
|
135
146
|
|
|
136
147
|
validate(): ValidatorResult {
|
|
137
148
|
if (!this.validator) return null;
|
|
138
|
-
return this.
|
|
149
|
+
return this.validator(this.value);
|
|
139
150
|
}
|
|
140
151
|
}
|
|
141
152
|
|
|
@@ -147,6 +158,20 @@ export class Reflector {
|
|
|
147
158
|
}): BuildedInput<T> {
|
|
148
159
|
return new BuildedInput(params);
|
|
149
160
|
}
|
|
161
|
+
|
|
162
|
+
export function isFormValid<T>(schema: Partial<T>): boolean {
|
|
163
|
+
delete schema.bundle;
|
|
164
|
+
|
|
165
|
+
const arrayOfBuildedInputs = Object.values(schema) as BuildedInput<unknown>[];
|
|
166
|
+
|
|
167
|
+
const isValid = arrayOfBuildedInputs.every((a) => a.validate() === null);
|
|
168
|
+
|
|
169
|
+
if (!isValid) {
|
|
170
|
+
toast.error('Erro ao fazer a requisição', 'Um ou mais campos preenchidos estão incorretos.');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return isValid;
|
|
174
|
+
}
|
|
150
175
|
`;
|
|
151
176
|
this.typesSrc.changeData(buildFunctions);
|
|
152
177
|
this.typesSrc.save();
|
package/dist/method.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Request } from "./request.js";
|
|
2
2
|
import type { ReflectorOperation } from "./types/types.js";
|
|
3
3
|
import { PrimitiveProp } from "./primitive-property.js";
|
|
4
|
+
import { ArrayProp } from "./array.property.js";
|
|
4
5
|
export declare class Method {
|
|
5
6
|
name: string;
|
|
6
7
|
description: string | undefined;
|
|
@@ -9,8 +10,10 @@ export declare class Method {
|
|
|
9
10
|
request: Request;
|
|
10
11
|
paths: PrimitiveProp[];
|
|
11
12
|
headers: PrimitiveProp[];
|
|
12
|
-
querys: PrimitiveProp[];
|
|
13
|
+
querys: (PrimitiveProp | ArrayProp)[];
|
|
13
14
|
cookies: PrimitiveProp[];
|
|
15
|
+
private readonly additionalMethod;
|
|
16
|
+
responseTypeInterface: string;
|
|
14
17
|
constructor(params: {
|
|
15
18
|
operation: ReflectorOperation;
|
|
16
19
|
moduleName: string;
|
|
@@ -20,6 +23,7 @@ export declare class Method {
|
|
|
20
23
|
private getProps;
|
|
21
24
|
private buildCallMethod;
|
|
22
25
|
private readonly methodReturn;
|
|
23
|
-
|
|
26
|
+
private getAdditionalMethod;
|
|
24
27
|
private buildDescription;
|
|
28
|
+
build(): string;
|
|
25
29
|
}
|
package/dist/method.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Request } from "./request.js";
|
|
2
2
|
import { createDangerMessage, getFullEndpoint, treatByUppercase } from "./helpers/helpers.js";
|
|
3
3
|
import { PrimitiveProp } from "./primitive-property.js";
|
|
4
|
+
import { ArrayProp } from "./array.property.js";
|
|
4
5
|
export class Method {
|
|
5
6
|
name;
|
|
6
7
|
// zodProperties: Property[];
|
|
@@ -12,6 +13,8 @@ export class Method {
|
|
|
12
13
|
headers = [];
|
|
13
14
|
querys = [];
|
|
14
15
|
cookies = [];
|
|
16
|
+
additionalMethod;
|
|
17
|
+
responseTypeInterface;
|
|
15
18
|
constructor(params) {
|
|
16
19
|
const { operation } = params;
|
|
17
20
|
this.request = new Request(operation);
|
|
@@ -19,6 +22,10 @@ export class Method {
|
|
|
19
22
|
this.endpoint = operation.endpoint;
|
|
20
23
|
this.name = operation.operationId?.split("_")[1] ?? this.request.apiType;
|
|
21
24
|
this.buildProps(params);
|
|
25
|
+
this.additionalMethod = this.getAdditionalMethod({
|
|
26
|
+
name: this.name,
|
|
27
|
+
attributeType: this.request.attributeType,
|
|
28
|
+
});
|
|
22
29
|
}
|
|
23
30
|
buildProps(params) {
|
|
24
31
|
const { operation } = params;
|
|
@@ -34,7 +41,12 @@ export class Method {
|
|
|
34
41
|
continue;
|
|
35
42
|
const properties = { name, required: !!required, schemaObject: schema, validator: undefined };
|
|
36
43
|
if (inParam === "query") {
|
|
37
|
-
|
|
44
|
+
if (schema.type === "array") {
|
|
45
|
+
this.querys.push(new ArrayProp({ name, schemaObject: schema, schemaName: "" }));
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this.querys.push(new PrimitiveProp(properties));
|
|
49
|
+
}
|
|
38
50
|
}
|
|
39
51
|
else if (inParam === "header") {
|
|
40
52
|
this.headers.push(new PrimitiveProp(properties));
|
|
@@ -63,14 +75,12 @@ export class Method {
|
|
|
63
75
|
}
|
|
64
76
|
buildCallMethod() {
|
|
65
77
|
const beforeResponse = [];
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
};
|
|
69
|
-
const [diamond, response] = preBuild();
|
|
78
|
+
const responseType = this.request.responseType ? `${this.request.responseType}Interface` : "null";
|
|
79
|
+
this.responseTypeInterface = responseType;
|
|
70
80
|
if (this.request.attributeType === "list") {
|
|
71
81
|
beforeResponse.push(`this.list = ${this.request.responseType}.from(response.data)`);
|
|
72
82
|
const inside = `
|
|
73
|
-
|
|
83
|
+
const response = await api.get<${responseType}, unknown>({
|
|
74
84
|
endpoint,
|
|
75
85
|
queryData: { ${this.gee(this.querys)} }
|
|
76
86
|
})
|
|
@@ -85,7 +95,7 @@ export class Method {
|
|
|
85
95
|
}
|
|
86
96
|
let querys = this.querys.length > 0 ? `queryData: {${this.querys.map((q) => q.name).join(",")}}` : "";
|
|
87
97
|
const inside = `
|
|
88
|
-
|
|
98
|
+
const response = await api.get<${responseType}, unknown>({
|
|
89
99
|
endpoint,
|
|
90
100
|
${querys}
|
|
91
101
|
})
|
|
@@ -97,7 +107,7 @@ export class Method {
|
|
|
97
107
|
let data;
|
|
98
108
|
let headers;
|
|
99
109
|
if (this.request.bodyType) {
|
|
100
|
-
data = `const data = this.forms.${this.name}.bundle()
|
|
110
|
+
data = [`const data = this.forms.${this.name}.bundle()`, `if (!isFormValid(this.forms.${this.name})) return`].join(";");
|
|
101
111
|
}
|
|
102
112
|
const hasHeaders = this.request.parameters.some((p) => p.in === "header");
|
|
103
113
|
const hasData = this.request.bodyType;
|
|
@@ -106,7 +116,7 @@ export class Method {
|
|
|
106
116
|
}
|
|
107
117
|
const outside = ["this.loading = true", data, headers].join("\n");
|
|
108
118
|
const inside = `
|
|
109
|
-
|
|
119
|
+
const response = await api.${this.request.apiType}<${responseType}>({
|
|
110
120
|
endpoint,
|
|
111
121
|
${hasData ? "data," : ""}
|
|
112
122
|
${hasHeaders ? "headers," : ""}
|
|
@@ -116,7 +126,7 @@ export class Method {
|
|
|
116
126
|
}
|
|
117
127
|
else if (this.request.apiType === "delete") {
|
|
118
128
|
const inside = `
|
|
119
|
-
|
|
129
|
+
const response = await api.delete<${responseType}, unknown>({
|
|
120
130
|
endpoint,
|
|
121
131
|
})
|
|
122
132
|
`;
|
|
@@ -134,6 +144,30 @@ export class Method {
|
|
|
134
144
|
}
|
|
135
145
|
return this.request.responseType ? `new ${this.request.responseType}(response)` : "null";
|
|
136
146
|
};
|
|
147
|
+
getAdditionalMethod(params) {
|
|
148
|
+
const { attributeType, name } = params;
|
|
149
|
+
let additionalMethod = "";
|
|
150
|
+
const canAddClearMethod = attributeType === "form" || attributeType === "entity";
|
|
151
|
+
if (canAddClearMethod && attributeType === "form") {
|
|
152
|
+
additionalMethod = `
|
|
153
|
+
/** Limpa o form depois do back retornar uma resposta de sucesso */
|
|
154
|
+
async ${name}AndClear(behavior: Behavior = new Behavior()) {
|
|
155
|
+
const data = await this.${name}(behavior)
|
|
156
|
+
|
|
157
|
+
if (data) {
|
|
158
|
+
this.clearForms()
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return data
|
|
162
|
+
}
|
|
163
|
+
`;
|
|
164
|
+
}
|
|
165
|
+
// return additionalMethod;
|
|
166
|
+
return "";
|
|
167
|
+
}
|
|
168
|
+
buildDescription() {
|
|
169
|
+
return `/** ${this.description ?? ""} */`;
|
|
170
|
+
}
|
|
137
171
|
build() {
|
|
138
172
|
const { inside, outside } = this.buildCallMethod();
|
|
139
173
|
if (this.name === "list")
|
|
@@ -143,7 +177,7 @@ export class Method {
|
|
|
143
177
|
const endpoint = `${a}${getFullEndpoint(this.endpoint)}${a}`;
|
|
144
178
|
return `
|
|
145
179
|
${description}
|
|
146
|
-
async ${this.name}(behavior: Behavior = new Behavior()) {
|
|
180
|
+
async ${this.name}(behavior: Behavior<${this.responseTypeInterface}> = new Behavior()) {
|
|
147
181
|
const {onError, onSuccess} = behavior
|
|
148
182
|
${this.getProps()}
|
|
149
183
|
const endpoint = ${endpoint}
|
|
@@ -161,9 +195,8 @@ export class Method {
|
|
|
161
195
|
this.loading = false
|
|
162
196
|
}
|
|
163
197
|
}
|
|
198
|
+
|
|
199
|
+
${this.additionalMethod}
|
|
164
200
|
`;
|
|
165
201
|
}
|
|
166
|
-
buildDescription() {
|
|
167
|
-
return `/** ${this.description ?? ""} */`;
|
|
168
|
-
}
|
|
169
202
|
}
|
package/dist/module.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export declare class Module {
|
|
|
8
8
|
readonly src: Source;
|
|
9
9
|
imports: Set<string>;
|
|
10
10
|
methods: Method[];
|
|
11
|
+
reflectorImports: Set<string>;
|
|
11
12
|
moduleConstructor: string;
|
|
12
13
|
constructor(params: {
|
|
13
14
|
name: string;
|
|
@@ -16,11 +17,10 @@ export declare class Module {
|
|
|
16
17
|
path: string;
|
|
17
18
|
});
|
|
18
19
|
private buildClassProps;
|
|
19
|
-
private
|
|
20
|
-
private
|
|
20
|
+
private masterBuilder;
|
|
21
|
+
private processMethods;
|
|
22
|
+
private processParams;
|
|
21
23
|
private getPath;
|
|
22
|
-
private getAdditionalMethod;
|
|
23
|
-
private getParameters;
|
|
24
24
|
private buildClass;
|
|
25
25
|
private buildConstructor;
|
|
26
26
|
buildFile(params: {
|
package/dist/module.js
CHANGED
|
@@ -11,15 +11,13 @@ export class Module {
|
|
|
11
11
|
src;
|
|
12
12
|
imports;
|
|
13
13
|
methods;
|
|
14
|
+
reflectorImports;
|
|
14
15
|
moduleConstructor;
|
|
15
16
|
constructor(params) {
|
|
16
17
|
const { name, operations, moduleName, path } = params;
|
|
17
18
|
this.moduleName = moduleName;
|
|
18
|
-
this.imports = new Set([
|
|
19
|
-
|
|
20
|
-
'import repo from "$repository/main"',
|
|
21
|
-
'import { Behavior, build, BuildedInput } from "$reflector/reflector.svelte";',
|
|
22
|
-
]);
|
|
19
|
+
this.imports = new Set(["// AUTO GERADO. QUEM ALTERAR GOSTA DE RAPAZES!\n", 'import api from "$repository/api"']);
|
|
20
|
+
this.reflectorImports = new Set(["Behavior"]);
|
|
23
21
|
this.name = capitalizeFirstLetter(name);
|
|
24
22
|
this.path = path;
|
|
25
23
|
this.methods = operations.map((operation) => {
|
|
@@ -28,12 +26,9 @@ export class Module {
|
|
|
28
26
|
moduleName: this.name,
|
|
29
27
|
});
|
|
30
28
|
});
|
|
31
|
-
const { cookies, headers, paths, querys } = this.getParameters();
|
|
32
|
-
const allBuilded = this.
|
|
33
|
-
|
|
34
|
-
headers,
|
|
35
|
-
paths,
|
|
36
|
-
querys,
|
|
29
|
+
// const { cookies, headers, paths, querys } = this.getParameters();
|
|
30
|
+
const allBuilded = this.masterBuilder({
|
|
31
|
+
methods: this.methods,
|
|
37
32
|
});
|
|
38
33
|
this.moduleConstructor = this.buildConstructor(allBuilded.form);
|
|
39
34
|
//sempre por último
|
|
@@ -75,77 +70,23 @@ export class Module {
|
|
|
75
70
|
`;
|
|
76
71
|
return [buildedInterface, buildedClass].join(";");
|
|
77
72
|
}
|
|
78
|
-
|
|
79
|
-
const {
|
|
80
|
-
const canAddClearMethod = hasForm && hasEntity;
|
|
81
|
-
let additionalMethod = this.getAdditionalMethod({ canAddClearMethod, method });
|
|
82
|
-
return [method.build(), additionalMethod].join("\n");
|
|
83
|
-
}
|
|
84
|
-
creator(params) {
|
|
85
|
-
const { cookies, headers, paths, querys } = params;
|
|
86
|
-
const buildedModuleTypes = [];
|
|
73
|
+
masterBuilder(params) {
|
|
74
|
+
const { methods } = params;
|
|
87
75
|
const moduleAttributes = new Set().add("loading = $state<boolean>(false)");
|
|
88
76
|
const moduleInit = new Set([]);
|
|
89
77
|
const moduleClear = new Set([]);
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
{ name: "headers", props: headers },
|
|
101
|
-
{ name: "paths", props: paths },
|
|
102
|
-
{ name: "cookies", props: cookies },
|
|
103
|
-
];
|
|
104
|
-
for (const { name, props } of argEntries) {
|
|
105
|
-
if (!props.length)
|
|
106
|
-
continue;
|
|
107
|
-
getParams({ name, props });
|
|
108
|
-
}
|
|
109
|
-
const hasForm = this.methods.some((m) => m.request.attributeType === "form");
|
|
110
|
-
const hasEntity = this.methods.some((m) => m.request.attributeType === "entity");
|
|
111
|
-
const form = [];
|
|
112
|
-
const formSet = new Set();
|
|
113
|
-
const entries = new Set();
|
|
114
|
-
const methods = [];
|
|
115
|
-
for (const method of this.methods) {
|
|
116
|
-
const { bodyType, responseType, attributeType } = method.request;
|
|
117
|
-
if (method.request.bodyType === "string") {
|
|
118
|
-
createDangerMessage(`Metodo ${method.name} foi ignorado por possuir um body inválido.`);
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
if (attributeType === "form" && bodyType) {
|
|
122
|
-
const name = method.name;
|
|
123
|
-
const type = bodyType;
|
|
124
|
-
form.push({ name, type });
|
|
125
|
-
formSet.add(`${name}: new ${type}()`);
|
|
126
|
-
}
|
|
127
|
-
methods.push(this.buildMethod({ hasEntity, hasForm, method }));
|
|
128
|
-
if (attributeType === "entity") {
|
|
129
|
-
const entityName = treatByUppercase(method.request.responseType ?? "");
|
|
130
|
-
moduleAttributes.add(`${entityName} = $state<${responseType} | undefined>()`);
|
|
131
|
-
moduleInit.add(`this.clear${capitalizeFirstLetter(entityName)}()`);
|
|
132
|
-
moduleClear.add(`clear${capitalizeFirstLetter(entityName)}() { this.${entityName} = undefined }`);
|
|
133
|
-
}
|
|
134
|
-
else if (attributeType === "list") {
|
|
135
|
-
moduleAttributes.add(`list = $state<${responseType}['data']>([])`);
|
|
136
|
-
moduleInit.add("this.clearList()");
|
|
137
|
-
moduleClear.add(`clearList() { this.list = [] }`);
|
|
138
|
-
}
|
|
139
|
-
if (bodyType) {
|
|
140
|
-
entries.add(`${bodyType}`);
|
|
141
|
-
}
|
|
142
|
-
if (responseType) {
|
|
143
|
-
entries.add(`type ${responseType}Interface`);
|
|
144
|
-
entries.add(`${responseType}`);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
78
|
+
const { buildedMethods, entries, form, formSet, methodsAttributes, methodsClear, methodsInit, ...rawParams } = this.processMethods({
|
|
79
|
+
methods,
|
|
80
|
+
});
|
|
81
|
+
const { buildedParamsTypes, paramAttributes, paramClear, paramInit } = this.processParams(rawParams);
|
|
82
|
+
methodsAttributes.forEach((attr) => moduleAttributes.add(attr));
|
|
83
|
+
methodsClear.forEach((clear) => moduleClear.add(clear));
|
|
84
|
+
methodsInit.forEach((init) => moduleInit.add(init));
|
|
85
|
+
paramAttributes.forEach((attr) => moduleAttributes.add(attr));
|
|
86
|
+
paramClear.forEach((clear) => moduleClear.add(clear));
|
|
87
|
+
paramInit.forEach((init) => moduleInit.add(init));
|
|
147
88
|
const cleanEntries = Array.from(entries).filter((x) => x != "type any");
|
|
148
|
-
const classImports = `import { ${cleanEntries} } from '$reflector/schemas.svelte'
|
|
89
|
+
const classImports = cleanEntries.length > 0 ? `import { ${cleanEntries} } from '$reflector/schemas.svelte';` : "";
|
|
149
90
|
if (formSet.size > 0) {
|
|
150
91
|
moduleAttributes.add(`
|
|
151
92
|
forms = $state({
|
|
@@ -159,68 +100,120 @@ export class Module {
|
|
|
159
100
|
clearForms() { this.forms = this.buildForms(true) };
|
|
160
101
|
`);
|
|
161
102
|
}
|
|
162
|
-
// return this.methods.map((method) => {
|
|
163
|
-
// });
|
|
164
103
|
return {
|
|
165
104
|
moduleAttributes: Array.from(moduleAttributes),
|
|
166
|
-
moduleTypes:
|
|
105
|
+
moduleTypes: buildedParamsTypes,
|
|
167
106
|
moduleInit: Array.from(moduleInit),
|
|
168
107
|
moduleClear: Array.from(moduleClear),
|
|
169
108
|
form,
|
|
170
109
|
classImports,
|
|
171
|
-
buildedMethods
|
|
110
|
+
buildedMethods,
|
|
172
111
|
};
|
|
173
112
|
}
|
|
174
|
-
|
|
175
|
-
const
|
|
176
|
-
const
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
const
|
|
183
|
-
let additionalMethod = "";
|
|
184
|
-
if (canAddClearMethod && method.request.attributeType === "form") {
|
|
185
|
-
additionalMethod = `
|
|
186
|
-
/** Limpa o form depois do back retornar uma resposta de sucesso */
|
|
187
|
-
async ${method.name}AndClear(behavior: Behavior = new Behavior()) {
|
|
188
|
-
const data = await this.${method.name}(behavior)
|
|
189
|
-
|
|
190
|
-
if (data) {
|
|
191
|
-
this.clearForms()
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return data
|
|
195
|
-
}
|
|
196
|
-
`;
|
|
197
|
-
}
|
|
198
|
-
return additionalMethod;
|
|
199
|
-
}
|
|
200
|
-
// private buildMethods() {
|
|
201
|
-
// }
|
|
202
|
-
getParameters() {
|
|
113
|
+
processMethods(params) {
|
|
114
|
+
const { methods } = params;
|
|
115
|
+
const methodsAttributes = new Set();
|
|
116
|
+
const methodsInit = new Set([]);
|
|
117
|
+
const methodsClear = new Set([]);
|
|
118
|
+
const form = [];
|
|
119
|
+
const formSet = new Set();
|
|
120
|
+
const entries = new Set();
|
|
121
|
+
const buildedMethods = [];
|
|
203
122
|
const queryMap = new Map();
|
|
204
123
|
const headerMap = new Map();
|
|
205
124
|
const pathMap = new Map();
|
|
206
125
|
const cookieMap = new Map();
|
|
207
|
-
for (const method of
|
|
208
|
-
const { headers, cookies, paths, querys } = method;
|
|
126
|
+
for (const method of methods) {
|
|
127
|
+
const { request, headers, cookies, paths, querys } = method;
|
|
128
|
+
const { bodyType, responseType, attributeType } = request;
|
|
209
129
|
headers.forEach((h) => headerMap.set(h.name, h));
|
|
210
130
|
cookies.forEach((c) => cookieMap.set(c.name, c));
|
|
211
131
|
paths.forEach((p) => pathMap.set(p.name, p));
|
|
212
132
|
querys.forEach((q) => queryMap.set(q.name, q));
|
|
133
|
+
if (method.request.bodyType === "string") {
|
|
134
|
+
createDangerMessage(`Metodo ${method.name} foi ignorado por possuir um body inválido.`);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (attributeType === "form" && bodyType) {
|
|
138
|
+
const name = method.name;
|
|
139
|
+
const type = bodyType;
|
|
140
|
+
form.push({ name, type });
|
|
141
|
+
formSet.add(`${name}: new ${type}()`);
|
|
142
|
+
this.reflectorImports.add("isFormValid");
|
|
143
|
+
}
|
|
144
|
+
buildedMethods.push(method.build());
|
|
145
|
+
if (attributeType === "entity") {
|
|
146
|
+
const entityName = treatByUppercase(method.request.responseType ?? "");
|
|
147
|
+
methodsAttributes.add(`${entityName} = $state<${responseType} | undefined>()`);
|
|
148
|
+
methodsInit.add(`this.clear${capitalizeFirstLetter(entityName)}()`);
|
|
149
|
+
methodsClear.add(`clear${capitalizeFirstLetter(entityName)}() { this.${entityName} = undefined }`);
|
|
150
|
+
}
|
|
151
|
+
else if (attributeType === "list") {
|
|
152
|
+
methodsAttributes.add(`list = $state<${responseType}['data']>([])`);
|
|
153
|
+
methodsInit.add("this.clearList()");
|
|
154
|
+
methodsClear.add(`clearList() { this.list = [] }`);
|
|
155
|
+
}
|
|
156
|
+
if (bodyType) {
|
|
157
|
+
entries.add(bodyType);
|
|
158
|
+
}
|
|
159
|
+
if (responseType) {
|
|
160
|
+
entries.add(`type ${responseType}Interface`);
|
|
161
|
+
entries.add(responseType);
|
|
162
|
+
}
|
|
213
163
|
}
|
|
214
164
|
return {
|
|
165
|
+
form,
|
|
166
|
+
formSet,
|
|
167
|
+
entries,
|
|
168
|
+
buildedMethods,
|
|
169
|
+
methodsAttributes,
|
|
170
|
+
methodsInit,
|
|
171
|
+
methodsClear,
|
|
215
172
|
headers: Array.from(headerMap.values()),
|
|
216
173
|
cookies: Array.from(cookieMap.values()),
|
|
217
174
|
paths: Array.from(pathMap.values()),
|
|
218
175
|
querys: Array.from(queryMap.values()),
|
|
219
176
|
};
|
|
220
177
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
178
|
+
processParams(params) {
|
|
179
|
+
const { cookies, headers, paths, querys } = params;
|
|
180
|
+
const buildedParamsTypes = [];
|
|
181
|
+
const paramAttributes = new Set();
|
|
182
|
+
const paramInit = new Set([]);
|
|
183
|
+
const paramClear = new Set([]);
|
|
184
|
+
const getParams = (params) => {
|
|
185
|
+
const { name, props } = params;
|
|
186
|
+
const capitalizedName = capitalizeFirstLetter(name);
|
|
187
|
+
buildedParamsTypes.push(this.buildClassProps({ props, name: capitalizedName }));
|
|
188
|
+
paramAttributes.add(`${name} = $state(new ${capitalizedName}())`);
|
|
189
|
+
paramInit.add(`this.clear${capitalizeFirstLetter(capitalizedName)}()`);
|
|
190
|
+
paramClear.add(`clear${capitalizedName}() { this.${name} = new ${capitalizedName}() }`);
|
|
191
|
+
};
|
|
192
|
+
const argEntries = [
|
|
193
|
+
{ name: "querys", props: querys },
|
|
194
|
+
{ name: "headers", props: headers },
|
|
195
|
+
{ name: "paths", props: paths },
|
|
196
|
+
{ name: "cookies", props: cookies },
|
|
197
|
+
];
|
|
198
|
+
for (const { name, props } of argEntries) {
|
|
199
|
+
if (!props.length)
|
|
200
|
+
continue;
|
|
201
|
+
this.reflectorImports.add("build");
|
|
202
|
+
this.reflectorImports.add("BuildedInput");
|
|
203
|
+
getParams({ name, props });
|
|
204
|
+
}
|
|
205
|
+
if (buildedParamsTypes.length > 0) {
|
|
206
|
+
this.imports.add('import { validateInputs } from "$lib/sanitizers/validateFormats"');
|
|
207
|
+
}
|
|
208
|
+
return { buildedParamsTypes, paramAttributes, paramInit, paramClear };
|
|
209
|
+
}
|
|
210
|
+
getPath() {
|
|
211
|
+
const fileName = this.path.split("/").slice(-2).join("-");
|
|
212
|
+
const inPath = path.join(generatedDir, this.path);
|
|
213
|
+
const outPath = path.join(inPath, `${fileName.toLowerCase()}.module.svelte.ts`);
|
|
214
|
+
fs.mkdirSync(inPath, { recursive: true });
|
|
215
|
+
return outPath;
|
|
216
|
+
}
|
|
224
217
|
buildClass(params) {
|
|
225
218
|
const { moduleInit, moduleAttributes, moduleClear, buildedMethods } = params;
|
|
226
219
|
const reset = moduleAttributes.length > 1
|
|
@@ -247,7 +240,7 @@ export class Module {
|
|
|
247
240
|
return "";
|
|
248
241
|
const teste = `
|
|
249
242
|
constructor(params?: { empty: boolean }) {
|
|
250
|
-
const isEmpty = params?.empty || PUBLIC_ENVIRONMENT != 'DEV'
|
|
243
|
+
const isEmpty = params?.empty || import.meta.env.PUBLIC_ENVIRONMENT != 'DEV'
|
|
251
244
|
|
|
252
245
|
this.forms = this.buildForms(isEmpty);
|
|
253
246
|
}
|
|
@@ -264,12 +257,12 @@ export class Module {
|
|
|
264
257
|
}
|
|
265
258
|
buildFile(params) {
|
|
266
259
|
const { moduleInit, moduleTypes, moduleAttributes, moduleClear, classImports, buildedMethods } = params;
|
|
260
|
+
const reflectorImports = `import { ${Array.from(this.reflectorImports)} } from "$reflector/reflector.svelte";`;
|
|
267
261
|
return `
|
|
268
262
|
${Array.from(this.imports).join(";")}
|
|
263
|
+
${reflectorImports}
|
|
269
264
|
${classImports}
|
|
270
265
|
|
|
271
|
-
const PUBLIC_ENVIRONMENT = import.meta.env.PUBLIC_ENVIRONMENT;
|
|
272
|
-
|
|
273
266
|
${moduleTypes.join(";")}
|
|
274
267
|
|
|
275
268
|
${this.buildClass({ moduleAttributes, moduleInit, moduleClear, buildedMethods })}
|
package/dist/object.property.js
CHANGED
|
@@ -6,7 +6,7 @@ export class PrimitiveProp {
|
|
|
6
6
|
rawType;
|
|
7
7
|
buildedConst;
|
|
8
8
|
constructor(params) {
|
|
9
|
-
const { name, schemaObject, required } = params;
|
|
9
|
+
const { name, schemaObject, required, validator } = params;
|
|
10
10
|
const { example: rawExample, type: rawType } = schemaObject;
|
|
11
11
|
const type = rawType ?? "string";
|
|
12
12
|
const example = rawExample ?? this.getEmptyExample({ type, schemaObject });
|
|
@@ -16,7 +16,7 @@ export class PrimitiveProp {
|
|
|
16
16
|
this.rawType = type ?? "any";
|
|
17
17
|
this.type = `BuildedInput<${buildedType}>`;
|
|
18
18
|
this.required = required;
|
|
19
|
-
this.buildedConst = this.buildConst({ example, name: this.name, required, type });
|
|
19
|
+
this.buildedConst = this.buildConst({ example, name: this.name, required, type, validator });
|
|
20
20
|
}
|
|
21
21
|
treatName(name) {
|
|
22
22
|
let newName = name;
|
|
@@ -27,10 +27,26 @@ export class PrimitiveProp {
|
|
|
27
27
|
return newName;
|
|
28
28
|
}
|
|
29
29
|
buildConst(params) {
|
|
30
|
-
const { example, name, required, type } = params;
|
|
30
|
+
const { example, name, required, type, validator } = params;
|
|
31
|
+
const getValidator = (type) => {
|
|
32
|
+
if (type === "string") {
|
|
33
|
+
return "emptyString";
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
};
|
|
37
|
+
const buildedValidator = () => {
|
|
38
|
+
if (validator) {
|
|
39
|
+
return `validator: ${validator}`;
|
|
40
|
+
}
|
|
41
|
+
else if (required && type) {
|
|
42
|
+
const v = getValidator(type);
|
|
43
|
+
return v ? `validator: validateInputs.${v}` : "";
|
|
44
|
+
}
|
|
45
|
+
return "";
|
|
46
|
+
};
|
|
31
47
|
const sanitizedExample = type === "boolean" || type === "number" ? example : `"${example}"`;
|
|
32
48
|
return `
|
|
33
|
-
build({ key: params?.${name}, example: ${sanitizedExample}, required: ${required}})
|
|
49
|
+
build({ key: params?.${name}, example: ${sanitizedExample}, required: ${required}, ${buildedValidator()}})
|
|
34
50
|
`;
|
|
35
51
|
}
|
|
36
52
|
getEmptyExample(params) {
|
package/dist/request.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ParameterObject } from "./types/open-api-spec.interface.js";
|
|
2
2
|
import type { ApiType, ReflectorOperation } from "./types/types.js";
|
|
3
|
-
type ReflectorRequestType = "entity" | "list" | "pagination" | "form" | "other";
|
|
3
|
+
export type ReflectorRequestType = "entity" | "list" | "pagination" | "form" | "other";
|
|
4
4
|
export declare class Request {
|
|
5
5
|
readonly attributeType: ReflectorRequestType;
|
|
6
6
|
readonly apiType: ApiType;
|
|
@@ -30,4 +30,3 @@ export declare class Request {
|
|
|
30
30
|
*/
|
|
31
31
|
private typeFromProperties;
|
|
32
32
|
}
|
|
33
|
-
export {};
|