svelte-reflector 1.0.6 → 1.0.8
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/file.d.ts +1 -1
- package/dist/generate-doc.js +2 -2
- package/dist/helpers/helpers.d.ts +2 -2
- package/dist/helpers/helpers.js +24 -15
- package/dist/main.d.ts +2 -1
- package/dist/main.js +31 -30
- package/dist/method.d.ts +1 -0
- package/dist/method.js +37 -24
- package/dist/module.d.ts +10 -4
- package/dist/module.js +77 -36
- package/dist/property.d.ts +4 -4
- package/dist/property.js +8 -8
- package/dist/request.d.ts +1 -1
- package/dist/request.js +8 -4
- package/dist/schema.d.ts +1 -1
- package/dist/types/types.d.ts +3 -2
- package/package.json +1 -1
package/dist/file.d.ts
CHANGED
package/dist/generate-doc.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// src/generate-doc.ts
|
|
2
2
|
import "dotenv/config"; // carrega .env a partir de process.cwd()
|
|
3
3
|
import axios from "axios";
|
|
4
|
-
import
|
|
5
|
-
import
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import * as fs from "node:fs";
|
|
6
6
|
import { Reflector } from "./main.js";
|
|
7
7
|
/** ajuda a pegar a 1ª env definida dentre várias chaves possíveis */
|
|
8
8
|
function pickEnv(...keys) {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export declare function stripState(attr: string): string;
|
|
2
1
|
export declare function toCamelCase(str: string): string;
|
|
3
2
|
export declare function sanitizeKey(name: string): string;
|
|
4
3
|
export declare function sanitizeNumber(texto: string): string;
|
|
@@ -6,6 +5,7 @@ export declare function capitalizeFirstLetter(text: string): string;
|
|
|
6
5
|
export declare function splitByUppercase(text: string): string[];
|
|
7
6
|
export declare function createDangerMessage(text: string): void;
|
|
8
7
|
export declare function getEndpointAndModuleName(rawEndpoint: string): {
|
|
9
|
-
|
|
8
|
+
baseEndpoint: string;
|
|
10
9
|
moduleName: string;
|
|
11
10
|
};
|
|
11
|
+
export declare function getEndpoint(rawEndpoint: string): string;
|
package/dist/helpers/helpers.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
export function stripState(attr) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
1
|
+
// export function stripState(attr: string): string {
|
|
2
|
+
// // Ex.: "form = $state(newForm(DefaultCreateUserDtoSchema))"
|
|
3
|
+
// const [lhs, rhsRaw = ""] = attr.split("=");
|
|
4
|
+
// const rhs = rhsRaw.trim();
|
|
5
|
+
// // remove apenas UM wrapper $state( ... ) do início ao fim
|
|
6
|
+
// const cleaned = rhs.startsWith("$state(") && rhs.endsWith(")") ? rhs.slice("$state(".length, -1).trim() : rhs;
|
|
7
|
+
// if (!lhs) return "";
|
|
8
|
+
// return `${lhs.trim()} = ${cleaned}`;
|
|
9
|
+
// }
|
|
9
10
|
export function toCamelCase(str) {
|
|
10
11
|
return str
|
|
11
12
|
.split("-")
|
|
12
|
-
.map((chunk, i) => (i === 0 ? chunk : chunk
|
|
13
|
+
.map((chunk, i) => (i === 0 ? chunk : chunk.charAt(0).toUpperCase() + chunk.slice(1)))
|
|
13
14
|
.join("");
|
|
14
15
|
}
|
|
15
16
|
export function sanitizeKey(name) {
|
|
16
17
|
const match = /^\[id(.+)\]$|^\{(.+)\}$/.exec(name);
|
|
17
18
|
if (match) {
|
|
18
|
-
const raw = match[1] || match[2]; // pega o conteúdo entre [] ou {}
|
|
19
|
+
const raw = (match[1] || match[2]) ?? ""; // pega o conteúdo entre [] ou {}
|
|
19
20
|
const camel = toCamelCase(raw);
|
|
20
21
|
// Garante que a primeira letra fique minúscula
|
|
21
22
|
return camel.charAt(0).toLowerCase() + camel.slice(1);
|
|
@@ -23,7 +24,7 @@ export function sanitizeKey(name) {
|
|
|
23
24
|
return toCamelCase(name);
|
|
24
25
|
}
|
|
25
26
|
export function sanitizeNumber(texto) {
|
|
26
|
-
return texto.
|
|
27
|
+
return texto.replaceAll(/["']/g, "");
|
|
27
28
|
}
|
|
28
29
|
export function capitalizeFirstLetter(text) {
|
|
29
30
|
if (!text)
|
|
@@ -36,10 +37,18 @@ export function splitByUppercase(text) {
|
|
|
36
37
|
export function createDangerMessage(text) {
|
|
37
38
|
console.log("\x1b[31m%s\x1b[0m", `[!] ${text}`);
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
+
function getFilteredEntities(rawEndpoint) {
|
|
40
41
|
const splittedEntitys = rawEndpoint.split("/");
|
|
41
|
-
|
|
42
|
+
return splittedEntitys.filter((item) => item !== "" && !item.includes("{"));
|
|
43
|
+
}
|
|
44
|
+
export function getEndpointAndModuleName(rawEndpoint) {
|
|
45
|
+
const filteredEntitys = getFilteredEntities(rawEndpoint);
|
|
46
|
+
// console.log(filteredEntitys);
|
|
42
47
|
const moduleName = filteredEntitys.map((x) => sanitizeKey(capitalizeFirstLetter(x))).join("");
|
|
43
|
-
const
|
|
44
|
-
return {
|
|
48
|
+
const baseEndpoint = filteredEntitys.join("/");
|
|
49
|
+
return { baseEndpoint: getEndpoint(baseEndpoint), moduleName };
|
|
50
|
+
}
|
|
51
|
+
export function getEndpoint(rawEndpoint) {
|
|
52
|
+
const filteredEntitys = getFilteredEntities(rawEndpoint);
|
|
53
|
+
return filteredEntitys.join("/");
|
|
45
54
|
}
|
package/dist/main.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Source } from "./file.js";
|
|
2
2
|
import { Schema } from "./schema.js";
|
|
3
|
-
import { ComponentsObject, PathsObject, OpenAPIObject } from "./types/open-api-spec.interface.js";
|
|
3
|
+
import type { ComponentsObject, PathsObject, OpenAPIObject } from "./types/open-api-spec.interface.js";
|
|
4
4
|
import { Module } from "./module.js";
|
|
5
5
|
export declare class Reflector {
|
|
6
6
|
readonly components: ComponentsObject;
|
|
@@ -9,6 +9,7 @@ export declare class Reflector {
|
|
|
9
9
|
readonly generatedDir: string;
|
|
10
10
|
readonly localDoc: Source;
|
|
11
11
|
readonly src: Source;
|
|
12
|
+
readonly typesSrc: Source;
|
|
12
13
|
readonly schemaFile: Source;
|
|
13
14
|
files: Source[];
|
|
14
15
|
schemas: Schema[];
|
package/dist/main.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import fs from "node:fs";
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
3
|
import { Source } from "./file.js";
|
|
4
|
-
import { getEndpointAndModuleName, splitByUppercase } from "./helpers/helpers.js";
|
|
4
|
+
import { getEndpoint, getEndpointAndModuleName, splitByUppercase } from "./helpers/helpers.js";
|
|
5
5
|
import { Schema } from "./schema.js";
|
|
6
6
|
import { Module } from "./module.js";
|
|
7
|
-
const
|
|
7
|
+
// const defaultMethods = ["get", "patch", "post", "put", "delete"] as const;
|
|
8
8
|
export class Reflector {
|
|
9
9
|
components;
|
|
10
10
|
paths;
|
|
@@ -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;
|
|
@@ -45,44 +46,44 @@ export class Reflector {
|
|
|
45
46
|
}
|
|
46
47
|
getModules() {
|
|
47
48
|
const methodsMap = new Map();
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (existingOps) {
|
|
68
|
-
existingOps.operations.push(...operations);
|
|
49
|
+
for (const [path, methods] of Object.entries(this.paths)) {
|
|
50
|
+
const rawName = Object.values(methods)[0];
|
|
51
|
+
const a = rawName.operationId?.split("_")[0] ?? "";
|
|
52
|
+
const teste = splitByUppercase(a).filter((x) => x !== "Controller");
|
|
53
|
+
const moduleName = teste.join("");
|
|
54
|
+
const baseEndpoint = getEndpoint(path);
|
|
55
|
+
const operations = Object.entries(methods).map(([apiMethod, attributes]) => {
|
|
56
|
+
return {
|
|
57
|
+
apiMethod,
|
|
58
|
+
endpoint: path,
|
|
59
|
+
...attributes,
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
const existentModule = methodsMap.get(moduleName);
|
|
63
|
+
if (existentModule) {
|
|
64
|
+
methodsMap.set(moduleName, {
|
|
65
|
+
...existentModule,
|
|
66
|
+
operations: [...existentModule.operations, ...operations],
|
|
67
|
+
});
|
|
69
68
|
}
|
|
70
69
|
else {
|
|
71
|
-
methodsMap.set(
|
|
70
|
+
methodsMap.set(moduleName, { operations, path: baseEndpoint, moduleName });
|
|
72
71
|
}
|
|
73
72
|
}
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
const modules = Array.from(methodsMap).map(([name, info]) => {
|
|
74
|
+
return new Module({
|
|
76
75
|
name,
|
|
77
76
|
...info,
|
|
78
77
|
dir: this.generatedDir,
|
|
79
|
-
})
|
|
80
|
-
}
|
|
78
|
+
});
|
|
79
|
+
});
|
|
81
80
|
return modules;
|
|
82
81
|
}
|
|
83
82
|
build() {
|
|
84
83
|
this.schemaFile.changeData([`import z from 'zod';`, ...this.schemas.map((s) => `${s.schema} ${s.type}`)].join("\n\n"));
|
|
85
84
|
this.schemaFile.save();
|
|
85
|
+
this.typesSrc.changeData("export class Behavior { onError?: () => void; onSuccess?: () => void }");
|
|
86
|
+
this.typesSrc.save();
|
|
86
87
|
for (const module of this.modules) {
|
|
87
88
|
if (module.methods.length === 0)
|
|
88
89
|
continue;
|
package/dist/method.d.ts
CHANGED
package/dist/method.js
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { Request } from "./request.js";
|
|
2
2
|
import { ZodProperty } from "./property.js";
|
|
3
|
-
import { createDangerMessage } from "./helpers/helpers.js";
|
|
3
|
+
import { createDangerMessage, getEndpoint } from "./helpers/helpers.js";
|
|
4
4
|
export class Method {
|
|
5
5
|
name;
|
|
6
6
|
zodProperties;
|
|
7
7
|
description;
|
|
8
|
+
endpoint;
|
|
8
9
|
request;
|
|
9
10
|
constructor(params) {
|
|
10
11
|
const { operation } = params;
|
|
11
12
|
this.request = new Request(operation);
|
|
12
|
-
this.description = operation.description;
|
|
13
|
+
this.description = operation.description ?? operation.summary;
|
|
14
|
+
this.endpoint = operation.endpoint;
|
|
13
15
|
this.name = operation.operationId?.split("_")[1] ?? this.request.apiType;
|
|
14
16
|
const { parameters } = this.getParams(params);
|
|
15
17
|
this.zodProperties = parameters;
|
|
@@ -33,7 +35,7 @@ export class Method {
|
|
|
33
35
|
example: schema.default,
|
|
34
36
|
schemaObject: schema,
|
|
35
37
|
type: schema.type,
|
|
36
|
-
description,
|
|
38
|
+
description: description ?? "",
|
|
37
39
|
required: required || true,
|
|
38
40
|
}));
|
|
39
41
|
}
|
|
@@ -56,15 +58,14 @@ export class Method {
|
|
|
56
58
|
beforeResponse.push(`\n`);
|
|
57
59
|
}
|
|
58
60
|
if (this.request.attributeType === "list") {
|
|
59
|
-
beforeResponse.push(`const {data, ...params} = response`, "\n\n", `this.list = data`, `repo.intercept.rebuild(this.parameters, params)`);
|
|
61
|
+
beforeResponse.push(`const {data: { data }, ...params} = response`, "\n\n", `this.list = data`, `repo.intercept.rebuild(this.parameters, params)`);
|
|
60
62
|
return `
|
|
61
63
|
${afterResponse.join(";")}
|
|
62
|
-
const response = await repo.api.get<{data: ${this.request.responseType}
|
|
63
|
-
endpoint
|
|
64
|
+
const response = await repo.api.get<{data: ${this.request.responseType}}, unknown>({
|
|
65
|
+
endpoint,
|
|
66
|
+
${query}
|
|
64
67
|
})
|
|
65
68
|
${beforeResponse.join(";")}
|
|
66
|
-
|
|
67
|
-
return response
|
|
68
69
|
`;
|
|
69
70
|
}
|
|
70
71
|
else if (this.request.attributeType === "entity") {
|
|
@@ -72,15 +73,14 @@ export class Method {
|
|
|
72
73
|
return `
|
|
73
74
|
${afterResponse.join(";")}
|
|
74
75
|
const response = await repo.api.get<${this.request.responseType}, unknown>({
|
|
75
|
-
endpoint
|
|
76
|
+
endpoint,
|
|
77
|
+
${query}
|
|
76
78
|
})
|
|
77
79
|
${beforeResponse.join(";")}
|
|
78
|
-
|
|
79
|
-
return response
|
|
80
80
|
`;
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
else if (this.request.apiType === "post" || this.request.apiType === "put") {
|
|
83
|
+
else if (this.request.apiType === "post" || this.request.apiType === "put" || this.request.apiType === "patch") {
|
|
84
84
|
let data = "";
|
|
85
85
|
if (this.request.bodyType) {
|
|
86
86
|
data = `const data = repo.intercept.bundle(this.forms.${this.name})`;
|
|
@@ -89,24 +89,23 @@ export class Method {
|
|
|
89
89
|
${data}
|
|
90
90
|
|
|
91
91
|
const response = await repo.api.post<${this.request.responseType}>({
|
|
92
|
-
endpoint
|
|
92
|
+
endpoint,
|
|
93
93
|
${data ? "data" : ""}
|
|
94
94
|
})
|
|
95
|
-
|
|
96
|
-
return response
|
|
97
95
|
`;
|
|
98
96
|
}
|
|
99
97
|
else if (this.request.apiType === "delete") {
|
|
100
98
|
const props = this.zodProperties.map((x) => x.name).join(",");
|
|
99
|
+
const propsString = props.length > 0 ? `const {${props}} = this.parameters` : "";
|
|
101
100
|
return `
|
|
102
|
-
|
|
101
|
+
${propsString}
|
|
103
102
|
|
|
104
|
-
const response = await repo.api.delete<${this.request.responseType}, unknown>({
|
|
105
|
-
endpoint
|
|
103
|
+
const response = await repo.api.delete<${this.request.responseType ?? "null"}, unknown>({
|
|
104
|
+
endpoint,
|
|
105
|
+
${query}
|
|
106
106
|
})
|
|
107
107
|
|
|
108
|
-
this.
|
|
109
|
-
return response
|
|
108
|
+
this.clearEntity()
|
|
110
109
|
`;
|
|
111
110
|
}
|
|
112
111
|
return "";
|
|
@@ -116,18 +115,32 @@ export class Method {
|
|
|
116
115
|
}
|
|
117
116
|
build() {
|
|
118
117
|
const content = this.buildCallMethod();
|
|
119
|
-
if (!content)
|
|
118
|
+
if (!content) {
|
|
119
|
+
createDangerMessage(`Método ${this.name} (${this.request.apiType}) não foi gerado: buildCallMethod vazio`);
|
|
120
120
|
return;
|
|
121
|
+
}
|
|
121
122
|
if (this.name === "list")
|
|
122
123
|
this.name = "listAll";
|
|
123
124
|
const hasProprierties = this.zodProperties.length > 0;
|
|
124
125
|
if (!hasProprierties && this.request.apiType === "delete") {
|
|
125
126
|
createDangerMessage(`${this.name} não vai funcionar, pois não aceita parâmetros na requisição.`);
|
|
126
127
|
}
|
|
128
|
+
const description = this.buildDescription();
|
|
127
129
|
return `
|
|
128
|
-
${
|
|
129
|
-
async ${this.name}() {
|
|
130
|
-
|
|
130
|
+
${description}
|
|
131
|
+
async ${this.name}(behavior: Behavior = new Behavior()) {
|
|
132
|
+
const {onError, onSuccess} = behavior
|
|
133
|
+
const endpoint = "${getEndpoint(this.endpoint)}"
|
|
134
|
+
|
|
135
|
+
try{
|
|
136
|
+
${content}
|
|
137
|
+
onSuccess?.()
|
|
138
|
+
|
|
139
|
+
return response
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
141
|
+
} catch(e) {
|
|
142
|
+
onError?.()
|
|
143
|
+
}
|
|
131
144
|
}
|
|
132
145
|
`;
|
|
133
146
|
}
|
package/dist/module.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Source } from "./file.js";
|
|
2
2
|
import { Method } from "./method.js";
|
|
3
|
-
import { ReflectorOperation } from "./types/types.js";
|
|
3
|
+
import type { ReflectorOperation } from "./types/types.js";
|
|
4
4
|
export declare class Module {
|
|
5
5
|
readonly name: string;
|
|
6
|
-
readonly
|
|
6
|
+
readonly path: string;
|
|
7
7
|
readonly moduleName: string;
|
|
8
8
|
readonly src: Source;
|
|
9
9
|
imports: Set<string>;
|
|
@@ -13,14 +13,20 @@ export declare class Module {
|
|
|
13
13
|
name: string;
|
|
14
14
|
moduleName: string;
|
|
15
15
|
operations: ReflectorOperation[];
|
|
16
|
-
|
|
16
|
+
path: string;
|
|
17
17
|
dir: string;
|
|
18
18
|
});
|
|
19
19
|
private creator;
|
|
20
20
|
private getPath;
|
|
21
|
+
private getAdditionalMethod;
|
|
21
22
|
private buildMethods;
|
|
22
23
|
private getParameters;
|
|
23
24
|
private buildImports;
|
|
24
25
|
private buildClass;
|
|
25
|
-
buildFile(
|
|
26
|
+
buildFile(params: {
|
|
27
|
+
moduleAttributes: string[];
|
|
28
|
+
moduleTypes: string[];
|
|
29
|
+
moduleInit: string[];
|
|
30
|
+
moduleClear: string[];
|
|
31
|
+
}): string;
|
|
26
32
|
}
|
package/dist/module.js
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import fs from "node:fs";
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
import * as 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;
|
|
8
|
-
|
|
8
|
+
path;
|
|
9
9
|
moduleName;
|
|
10
10
|
src;
|
|
11
11
|
imports;
|
|
12
12
|
parameters;
|
|
13
13
|
methods;
|
|
14
14
|
constructor(params) {
|
|
15
|
-
const { name, operations,
|
|
15
|
+
const { name, operations, path, 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
|
-
this.
|
|
23
|
+
this.path = path;
|
|
20
24
|
const methods = operations.map((operation) => {
|
|
21
25
|
return new Method({
|
|
22
26
|
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
|
-
// else if (!propertiesOk) {
|
|
34
|
-
// createDangerMessage(`Método [ ${op.name} ] do módulo [ ${this.moduleName} ] com tipagem incorreta.`);
|
|
35
|
-
// }
|
|
36
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();
|
|
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,27 +85,58 @@ 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) {
|
|
89
|
-
const fileName = this.
|
|
90
|
-
const inPath = path.join(dir, this.
|
|
108
|
+
const fileName = this.path.split("/").slice(-2).join("-");
|
|
109
|
+
const inPath = path.join(dir, this.path);
|
|
91
110
|
const outPath = path.join(inPath, `${fileName.toLowerCase()}.module.svelte.ts`);
|
|
92
111
|
fs.mkdirSync(inPath, { recursive: true });
|
|
93
112
|
return outPath;
|
|
94
113
|
}
|
|
114
|
+
getAdditionalMethod(params) {
|
|
115
|
+
const { method, canAddClearMethod } = params;
|
|
116
|
+
let additionalMethod = "";
|
|
117
|
+
if (canAddClearMethod && method.request.attributeType === "form") {
|
|
118
|
+
additionalMethod = `
|
|
119
|
+
/** Limpa o form depois do back retornar uma resposta de sucesso */
|
|
120
|
+
async ${method.name}AndClear(behavior: Behavior = new Behavior()) {
|
|
121
|
+
const data = await this.${method.name}(behavior)
|
|
122
|
+
|
|
123
|
+
if (data) {
|
|
124
|
+
this.clearForms()
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return data
|
|
128
|
+
}
|
|
129
|
+
`;
|
|
130
|
+
}
|
|
131
|
+
return additionalMethod;
|
|
132
|
+
}
|
|
95
133
|
buildMethods() {
|
|
134
|
+
const hasForm = this.methods.some((m) => m.request.attributeType === "form");
|
|
135
|
+
const hasEntity = this.methods.some((m) => m.request.attributeType === "entity");
|
|
136
|
+
const canAddClearMethod = hasForm && hasEntity;
|
|
96
137
|
return this.methods.map((method) => {
|
|
97
|
-
|
|
138
|
+
let additionalMethod = this.getAdditionalMethod({ canAddClearMethod, method });
|
|
139
|
+
return [method.build(), additionalMethod].join("\n");
|
|
98
140
|
});
|
|
99
141
|
}
|
|
100
142
|
getParameters() {
|
|
@@ -123,32 +165,31 @@ export class Module {
|
|
|
123
165
|
return "";
|
|
124
166
|
return `import { ${cleanEntries} } from '$reflector/schemas';`;
|
|
125
167
|
}
|
|
126
|
-
buildClass(
|
|
127
|
-
const
|
|
168
|
+
buildClass(params) {
|
|
169
|
+
const { moduleInit, moduleAttributes, moduleClear } = params;
|
|
128
170
|
return `
|
|
129
171
|
export class ${this.moduleName}Module {
|
|
130
|
-
${
|
|
172
|
+
${moduleAttributes.join(";")}
|
|
131
173
|
|
|
132
174
|
${this.buildMethods().join("\n")}
|
|
133
175
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
clear() {
|
|
139
|
-
this.init()
|
|
176
|
+
${moduleClear.join("\n\n")}
|
|
177
|
+
|
|
178
|
+
clearAll() {
|
|
179
|
+
${moduleInit.join(";")}
|
|
140
180
|
}
|
|
141
181
|
}
|
|
142
182
|
`;
|
|
143
183
|
}
|
|
144
|
-
buildFile(
|
|
184
|
+
buildFile(params) {
|
|
185
|
+
const { moduleInit, moduleTypes, moduleAttributes, moduleClear } = params;
|
|
145
186
|
return `
|
|
146
187
|
${Array.from(this.imports).join(";")}
|
|
147
188
|
${this.buildImports()}
|
|
148
189
|
|
|
149
190
|
${moduleTypes.join(";")}
|
|
150
191
|
|
|
151
|
-
${this.buildClass(
|
|
192
|
+
${this.buildClass({ moduleAttributes, moduleInit, moduleClear })}
|
|
152
193
|
`;
|
|
153
194
|
}
|
|
154
195
|
}
|
package/dist/property.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { SchemaObject } from "./types/open-api-spec.interface.js";
|
|
2
|
-
import { Example, ReflectorParamType } from "./types/types.js";
|
|
1
|
+
import { type SchemaObject } from "./types/open-api-spec.interface.js";
|
|
2
|
+
import { type Example, type ReflectorParamType } from "./types/types.js";
|
|
3
3
|
export declare class ZodProperty {
|
|
4
4
|
name: string;
|
|
5
|
-
example: Example;
|
|
5
|
+
example: Example | undefined;
|
|
6
6
|
type: ReflectorParamType;
|
|
7
7
|
buildedProp: string;
|
|
8
|
-
description
|
|
8
|
+
description?: string;
|
|
9
9
|
required: boolean;
|
|
10
10
|
constructor(params: {
|
|
11
11
|
name: string;
|
package/dist/property.js
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
import { sanitizeNumber } from "./helpers/helpers.js";
|
|
2
2
|
import { ReflectorInput } from "./helpers/input.js";
|
|
3
|
+
import {} from "./types/open-api-spec.interface.js";
|
|
4
|
+
import {} from "./types/types.js";
|
|
3
5
|
const inputs = new ReflectorInput();
|
|
4
6
|
export class ZodProperty {
|
|
5
7
|
name;
|
|
6
8
|
example;
|
|
7
9
|
type;
|
|
8
10
|
buildedProp;
|
|
9
|
-
description
|
|
11
|
+
description;
|
|
10
12
|
required;
|
|
11
13
|
constructor(params) {
|
|
12
|
-
const { name, schemaObject, type, example, required } = params;
|
|
14
|
+
const { name, schemaObject, type, example, required, description } = params;
|
|
15
|
+
const realExample = example ?? schemaObject.example;
|
|
13
16
|
this.required = required;
|
|
14
17
|
this.name = name;
|
|
15
18
|
this.type = type;
|
|
16
|
-
this.example = this.getExample(
|
|
19
|
+
this.example = this.getExample(realExample);
|
|
17
20
|
this.buildedProp = this.build(schemaObject);
|
|
21
|
+
if (description)
|
|
22
|
+
this.description = description;
|
|
18
23
|
}
|
|
19
24
|
getDtoName(ref) {
|
|
20
25
|
const cavalos = ref.split("/");
|
|
@@ -35,11 +40,6 @@ export class ZodProperty {
|
|
|
35
40
|
return "lorem ipsum";
|
|
36
41
|
}
|
|
37
42
|
}
|
|
38
|
-
// fiscalNumber = cpf | cnpj | fiscalNumber
|
|
39
|
-
// cep
|
|
40
|
-
// telefone
|
|
41
|
-
// CUID
|
|
42
|
-
// url
|
|
43
43
|
deepValidator() {
|
|
44
44
|
if (this.name === "email") {
|
|
45
45
|
return `z.email().default('${this.example}')`;
|
package/dist/request.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ApiType, ReflectorOperation } from "./types/types.js";
|
|
1
|
+
import type { ApiType, ReflectorOperation } from "./types/types.js";
|
|
2
2
|
type ReflectorRequestType = "entity" | "list" | "pagination" | "form" | "other";
|
|
3
3
|
export declare class Request {
|
|
4
4
|
readonly attributeType: ReflectorRequestType;
|
package/dist/request.js
CHANGED
|
@@ -8,8 +8,12 @@ export class Request {
|
|
|
8
8
|
responseType;
|
|
9
9
|
constructor(operation) {
|
|
10
10
|
this.apiType = operation.apiMethod;
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const body = this.getTypeFromRequestBody(operation.requestBody);
|
|
12
|
+
if (body)
|
|
13
|
+
this.bodyType = body;
|
|
14
|
+
const response = this.getTypeFromResponses(operation.responses);
|
|
15
|
+
if (response)
|
|
16
|
+
this.responseType = response;
|
|
13
17
|
this.attributeType = this.inferAttributeType(operation) ?? "other";
|
|
14
18
|
}
|
|
15
19
|
// ============= Derivações principais =======================================
|
|
@@ -102,9 +106,9 @@ export class Request {
|
|
|
102
106
|
* Se `data` não existir ou não tiver tipo, retorna undefined.
|
|
103
107
|
*/
|
|
104
108
|
typeFromProperties(properties) {
|
|
105
|
-
if (!properties)
|
|
109
|
+
if (!properties?.["data"])
|
|
106
110
|
return undefined;
|
|
107
|
-
const data = properties
|
|
111
|
+
const data = properties["data"];
|
|
108
112
|
if (isRef(data))
|
|
109
113
|
return this.componentName(data);
|
|
110
114
|
if (data.type === "any")
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ZodProperty } from "./property.js";
|
|
2
|
-
import { SchemaObject, ReferenceObject } from "./types/open-api-spec.interface.js";
|
|
2
|
+
import type { SchemaObject, ReferenceObject } from "./types/open-api-spec.interface.js";
|
|
3
3
|
export declare class Schema {
|
|
4
4
|
name: string;
|
|
5
5
|
properties: ZodProperty[];
|
package/dist/types/types.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { OperationObject } from "./open-api-spec.interface.js";
|
|
1
|
+
import type { OperationObject } from "./open-api-spec.interface.js";
|
|
2
2
|
export type ReflectorParamType = "string" | "boolean" | "number" | "array" | "object";
|
|
3
3
|
export type ApiType = "get" | "post" | "delete" | "patch" | "put";
|
|
4
4
|
export type ReflectorOperation = OperationObject & {
|
|
5
5
|
apiMethod: ApiType;
|
|
6
|
+
endpoint: string;
|
|
6
7
|
};
|
|
7
8
|
export type Info = {
|
|
8
|
-
|
|
9
|
+
path: string;
|
|
9
10
|
operations: ReflectorOperation[];
|
|
10
11
|
moduleName: string;
|
|
11
12
|
};
|