svelte-reflector 1.0.11 → 1.0.13
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.d.ts +1 -0
- package/dist/helpers/helpers.js +4 -0
- package/dist/main.js +5 -8
- package/dist/method.d.ts +1 -1
- package/dist/method.js +1 -3
- package/dist/module.d.ts +1 -1
- package/dist/module.js +4 -4
- package/dist/property.d.ts +2 -0
- package/dist/property.js +26 -6
- package/dist/schema.d.ts +4 -1
- package/dist/schema.js +27 -2
- package/dist/zodProperty.d.ts +32 -0
- package/dist/zodProperty.js +123 -0
- package/package.json +1 -1
|
@@ -12,3 +12,4 @@ export declare function getEndpointAndModuleName(rawEndpoint: string): {
|
|
|
12
12
|
};
|
|
13
13
|
export declare function getEndpoint(rawEndpoint: string): string;
|
|
14
14
|
export declare function testeEndpoint(rawEndpoint: string): string;
|
|
15
|
+
export declare function treatenEnum(enums: string[]): string;
|
package/dist/helpers/helpers.js
CHANGED
package/dist/main.js
CHANGED
|
@@ -39,13 +39,7 @@ export class Reflector {
|
|
|
39
39
|
name: key,
|
|
40
40
|
requireds: object.required || [],
|
|
41
41
|
};
|
|
42
|
-
schemas.push(new Schema({
|
|
43
|
-
...schema,
|
|
44
|
-
isEmpty: false,
|
|
45
|
-
}), new Schema({
|
|
46
|
-
...schema,
|
|
47
|
-
isEmpty: true,
|
|
48
|
-
}));
|
|
42
|
+
schemas.push(new Schema({ ...schema, isEmpty: false }), new Schema({ ...schema, isEmpty: true }));
|
|
49
43
|
}
|
|
50
44
|
console.log(`${schemas.length} schemas gerados com sucesso.`);
|
|
51
45
|
return schemas;
|
|
@@ -86,7 +80,10 @@ export class Reflector {
|
|
|
86
80
|
return modules;
|
|
87
81
|
}
|
|
88
82
|
build() {
|
|
89
|
-
|
|
83
|
+
const treatedSchemas = this.schemas.map((s) => {
|
|
84
|
+
return `${s.schema} ${s.type}`;
|
|
85
|
+
});
|
|
86
|
+
this.schemaFile.changeData([`import z from 'zod';`, ...treatedSchemas].join("\n\n"));
|
|
90
87
|
this.schemaFile.save();
|
|
91
88
|
this.typesSrc.changeData("export class Behavior { onError?: (e) => void; onSuccess?: () => void }");
|
|
92
89
|
this.typesSrc.save();
|
package/dist/method.d.ts
CHANGED
package/dist/method.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request } from "./request.js";
|
|
2
|
-
import { ZodProperty } from "./
|
|
2
|
+
import { ZodProperty } from "./zodProperty.js";
|
|
3
3
|
import { createDangerMessage, getEndpoint, testeEndpoint, treatByUppercase } from "./helpers/helpers.js";
|
|
4
4
|
export class Method {
|
|
5
5
|
name;
|
|
@@ -118,8 +118,6 @@ export class Method {
|
|
|
118
118
|
return { outside, inside };
|
|
119
119
|
}
|
|
120
120
|
else if (this.request.apiType === "delete") {
|
|
121
|
-
// const props = this.zodProperties.map((x) => x.name).join(",");
|
|
122
|
-
// const propsString = props.length > 0 ? `const {${props}} = this.parameters` : "";
|
|
123
121
|
const inside = `
|
|
124
122
|
const response = await repo.api.delete<${this.request.responseType ?? "null"}, unknown>({
|
|
125
123
|
endpoint,
|
package/dist/module.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Source } from "./file.js";
|
|
2
2
|
import { Method } from "./method.js";
|
|
3
3
|
import type { ReflectorOperation } from "./types/types.js";
|
|
4
|
-
import { ZodProperty } from "./
|
|
4
|
+
import { ZodProperty } from "./zodProperty.js";
|
|
5
5
|
export declare class Module {
|
|
6
6
|
readonly name: string;
|
|
7
7
|
readonly path: string;
|
package/dist/module.js
CHANGED
|
@@ -3,7 +3,7 @@ import * as fs from "node:fs";
|
|
|
3
3
|
import { Source } from "./file.js";
|
|
4
4
|
import { capitalizeFirstLetter, createDangerMessage, treatByUppercase } from "./helpers/helpers.js";
|
|
5
5
|
import { Method } from "./method.js";
|
|
6
|
-
import { ZodProperty } from "./
|
|
6
|
+
import { ZodProperty } from "./zodProperty.js";
|
|
7
7
|
export class Module {
|
|
8
8
|
name;
|
|
9
9
|
path;
|
|
@@ -71,7 +71,7 @@ export class Module {
|
|
|
71
71
|
const capitalizedName = capitalizeFirstLetter(name);
|
|
72
72
|
buildedModuleTypes.push(`const ${capitalizedName}Schema = ${this.buildZObject(objets)}`);
|
|
73
73
|
moduleAttributes.add(`${name} = $state(repo.newForm(${capitalizedName}Schema))`);
|
|
74
|
-
moduleInit.add(`this.clear${capitalizedName}()`);
|
|
74
|
+
moduleInit.add(`this.clear${capitalizeFirstLetter(capitalizedName)}()`);
|
|
75
75
|
moduleClear.add(`clear${capitalizedName}() { this.${name} = repo.newForm(${capitalizedName}Schema) }`);
|
|
76
76
|
};
|
|
77
77
|
if (this.querys.length > 0) {
|
|
@@ -98,8 +98,8 @@ export class Module {
|
|
|
98
98
|
if (attributeType === "entity") {
|
|
99
99
|
const entityName = treatByUppercase(method.request.responseType);
|
|
100
100
|
moduleAttributes.add(`${entityName} = $state<${responseType} | undefined>()`);
|
|
101
|
-
moduleInit.add(`this.clear${entityName}()`);
|
|
102
|
-
moduleClear.add(`clear${entityName}() { this.${entityName} = undefined }`);
|
|
101
|
+
moduleInit.add(`this.clear${capitalizeFirstLetter(entityName)}()`);
|
|
102
|
+
moduleClear.add(`clear${capitalizeFirstLetter(entityName)}() { this.${entityName} = undefined }`);
|
|
103
103
|
}
|
|
104
104
|
else if (attributeType === "list") {
|
|
105
105
|
moduleAttributes.add(`list = $state<${responseType}['data']>([])`);
|
package/dist/property.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type ParameterLocation, type SchemaObject } from "./types/open-api-spec.interface.js";
|
|
2
2
|
import { type Example, type ReflectorParamType } from "./types/types.js";
|
|
3
3
|
export declare class ZodProperty {
|
|
4
|
+
schemaName?: string;
|
|
4
5
|
name: string;
|
|
5
6
|
example: Example | undefined;
|
|
6
7
|
type: ReflectorParamType;
|
|
@@ -9,6 +10,7 @@ export declare class ZodProperty {
|
|
|
9
10
|
required: boolean;
|
|
10
11
|
inParam: ParameterLocation;
|
|
11
12
|
constructor(params: {
|
|
13
|
+
schemaName?: string;
|
|
12
14
|
name: string;
|
|
13
15
|
schemaObject: SchemaObject;
|
|
14
16
|
type: ReflectorParamType;
|
package/dist/property.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { sanitizeNumber } from "./helpers/helpers.js";
|
|
1
|
+
import { sanitizeNumber, splitByUppercase } from "./helpers/helpers.js";
|
|
2
2
|
import { ReflectorInput } from "./helpers/input.js";
|
|
3
3
|
import {} from "./types/open-api-spec.interface.js";
|
|
4
4
|
import {} from "./types/types.js";
|
|
5
5
|
const inputs = new ReflectorInput();
|
|
6
6
|
export class ZodProperty {
|
|
7
|
+
schemaName;
|
|
7
8
|
name;
|
|
8
9
|
example;
|
|
9
10
|
type;
|
|
@@ -12,8 +13,11 @@ export class ZodProperty {
|
|
|
12
13
|
required;
|
|
13
14
|
inParam;
|
|
14
15
|
constructor(params) {
|
|
15
|
-
const { name, schemaObject, type, example, required, description, isEmpty, inParam } = params;
|
|
16
|
+
const { schemaName, name, schemaObject, type, example, required, description, isEmpty, inParam } = params;
|
|
16
17
|
this.inParam = inParam;
|
|
18
|
+
if (schemaName) {
|
|
19
|
+
this.schemaName = schemaName;
|
|
20
|
+
}
|
|
17
21
|
if (name.split("-").length > 1) {
|
|
18
22
|
this.name = `['${name}']`;
|
|
19
23
|
}
|
|
@@ -66,11 +70,15 @@ export class ZodProperty {
|
|
|
66
70
|
treatName(name) {
|
|
67
71
|
return name;
|
|
68
72
|
}
|
|
69
|
-
build(
|
|
73
|
+
build(schemaObject) {
|
|
70
74
|
const name = this.treatName(this.name);
|
|
71
75
|
const x = `${name}: z.${this.type}()${this.isNullable()}`;
|
|
72
76
|
switch (this.type) {
|
|
73
77
|
case "string": {
|
|
78
|
+
// if (name === "role") {
|
|
79
|
+
// console.log(schemaObject.enum);
|
|
80
|
+
// }
|
|
81
|
+
console.log(schemaObject.enum);
|
|
74
82
|
const deepValidation = this.deepValidator();
|
|
75
83
|
return deepValidation ? `${name}: ${deepValidation}` : `${x}.default('${this.example}')`;
|
|
76
84
|
}
|
|
@@ -81,10 +89,22 @@ export class ZodProperty {
|
|
|
81
89
|
return `${x}.default(${sanitizeNumber(number)})`;
|
|
82
90
|
}
|
|
83
91
|
case "array": {
|
|
84
|
-
if (!
|
|
85
|
-
|
|
92
|
+
if (!schemaObject.items || !("$ref" in schemaObject.items)) {
|
|
93
|
+
let zodType = "z.string()";
|
|
94
|
+
if (schemaObject.items?.enum) {
|
|
95
|
+
if (this.schemaName) {
|
|
96
|
+
const teste = splitByUppercase(this.schemaName)
|
|
97
|
+
.filter((name) => name !== "Empty")
|
|
98
|
+
.join("");
|
|
99
|
+
zodType = `${teste}EnumSchema`;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else if (schemaObject.items?.type) {
|
|
103
|
+
zodType = `z.${schemaObject.items.type}()`;
|
|
104
|
+
}
|
|
105
|
+
return `${name}: z.${this.type}(${zodType})${this.isNullable()}.default([])`;
|
|
86
106
|
}
|
|
87
|
-
const dto = this.getDtoName(
|
|
107
|
+
const dto = this.getDtoName(schemaObject.items.$ref);
|
|
88
108
|
return `${name}: z.array(${dto}Schema).default(new Array(10).fill(${dto}Schema.parse({})))`;
|
|
89
109
|
}
|
|
90
110
|
case "object":
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import { ZodProperty } from "./
|
|
1
|
+
import { ZodProperty } from "./zodProperty.js";
|
|
2
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[];
|
|
6
6
|
type: string;
|
|
7
7
|
schema: string;
|
|
8
|
+
enums: Set<string>;
|
|
9
|
+
objects: Map<string, string>;
|
|
8
10
|
constructor(params: {
|
|
9
11
|
properties: Record<string, SchemaObject | ReferenceObject>;
|
|
10
12
|
name: string;
|
|
11
13
|
requireds: string[];
|
|
12
14
|
isEmpty: boolean;
|
|
13
15
|
});
|
|
16
|
+
private getEnumConst;
|
|
14
17
|
}
|
package/dist/schema.js
CHANGED
|
@@ -1,17 +1,33 @@
|
|
|
1
|
-
import { ZodProperty } from "./
|
|
1
|
+
import { ZodProperty } from "./zodProperty.js";
|
|
2
2
|
export class Schema {
|
|
3
3
|
name;
|
|
4
4
|
properties = [];
|
|
5
5
|
type;
|
|
6
6
|
schema;
|
|
7
|
+
enums = new Set();
|
|
8
|
+
objects = new Map();
|
|
7
9
|
constructor(params) {
|
|
8
10
|
const { name, properties, requireds, isEmpty } = params;
|
|
9
11
|
this.name = `${isEmpty ? "Empty" : ""}${name}`;
|
|
10
12
|
for (const [key, value] of Object.entries(properties)) {
|
|
11
|
-
if ("$ref" in value || !value?.type)
|
|
13
|
+
if ("$ref" in value || !value?.type) {
|
|
14
|
+
if ("$ref" in value) {
|
|
15
|
+
const teste = value.$ref;
|
|
16
|
+
const object = teste.split("/").at(-1);
|
|
17
|
+
this.objects.set(key, `${object}Schema`);
|
|
18
|
+
}
|
|
12
19
|
continue;
|
|
20
|
+
}
|
|
13
21
|
const required = requireds.includes(key);
|
|
22
|
+
const teste = value.items;
|
|
23
|
+
if (teste && !("$ref" in teste) && teste.enum) {
|
|
24
|
+
this.enums.add(this.getEnumConst({ enums: teste.enum, schemaName: key }));
|
|
25
|
+
}
|
|
26
|
+
else if (value.enum) {
|
|
27
|
+
this.enums.add(this.getEnumConst({ enums: value.enum, schemaName: key }));
|
|
28
|
+
}
|
|
14
29
|
this.properties.push(new ZodProperty({
|
|
30
|
+
schemaName: this.name,
|
|
15
31
|
name: key,
|
|
16
32
|
schemaObject: value,
|
|
17
33
|
type: value.type,
|
|
@@ -26,6 +42,15 @@ export class Schema {
|
|
|
26
42
|
${this.properties.map((p) => {
|
|
27
43
|
return p.buildedProp;
|
|
28
44
|
})}
|
|
45
|
+
${this.properties.length > 0 ? "," : ""}
|
|
46
|
+
${Array.from(this.objects).map(([k, v]) => {
|
|
47
|
+
return `${k}: z.object(${v})`;
|
|
48
|
+
})}
|
|
29
49
|
});`;
|
|
30
50
|
}
|
|
51
|
+
getEnumConst(params) {
|
|
52
|
+
const { enums, schemaName } = params;
|
|
53
|
+
const enumList = enums.map((en) => `'${en}'`);
|
|
54
|
+
return `export const ${schemaName}EnumSchema = z.enum([${enumList}])`;
|
|
55
|
+
}
|
|
31
56
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type ParameterLocation, type SchemaObject } from "./types/open-api-spec.interface.js";
|
|
2
|
+
import { type Example, type ReflectorParamType } from "./types/types.js";
|
|
3
|
+
export declare class ZodProperty {
|
|
4
|
+
schemaName?: string;
|
|
5
|
+
name: string;
|
|
6
|
+
example: Example | undefined;
|
|
7
|
+
type: ReflectorParamType;
|
|
8
|
+
buildedProp: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
required: boolean;
|
|
11
|
+
inParam: ParameterLocation;
|
|
12
|
+
isEnum: boolean;
|
|
13
|
+
enums: string[];
|
|
14
|
+
constructor(params: {
|
|
15
|
+
schemaName?: string;
|
|
16
|
+
name: string;
|
|
17
|
+
schemaObject: SchemaObject;
|
|
18
|
+
type: ReflectorParamType;
|
|
19
|
+
example: Example | undefined;
|
|
20
|
+
required: boolean;
|
|
21
|
+
description?: string;
|
|
22
|
+
isEmpty: boolean;
|
|
23
|
+
inParam: ParameterLocation;
|
|
24
|
+
});
|
|
25
|
+
private getDtoName;
|
|
26
|
+
private getEmptyExample;
|
|
27
|
+
private getExample;
|
|
28
|
+
private deepValidator;
|
|
29
|
+
private isNullable;
|
|
30
|
+
private treatName;
|
|
31
|
+
private build;
|
|
32
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { sanitizeNumber, treatenEnum } from "./helpers/helpers.js";
|
|
2
|
+
import { ReflectorInput } from "./helpers/input.js";
|
|
3
|
+
import {} from "./types/open-api-spec.interface.js";
|
|
4
|
+
import {} from "./types/types.js";
|
|
5
|
+
const inputs = new ReflectorInput();
|
|
6
|
+
export class ZodProperty {
|
|
7
|
+
schemaName;
|
|
8
|
+
name;
|
|
9
|
+
example;
|
|
10
|
+
type;
|
|
11
|
+
buildedProp;
|
|
12
|
+
description;
|
|
13
|
+
required;
|
|
14
|
+
inParam;
|
|
15
|
+
isEnum = false;
|
|
16
|
+
enums = [];
|
|
17
|
+
constructor(params) {
|
|
18
|
+
const { schemaName, name, schemaObject, type, example, required, description, isEmpty, inParam } = params;
|
|
19
|
+
this.inParam = inParam;
|
|
20
|
+
this.isEnum = false;
|
|
21
|
+
const items = schemaObject.items;
|
|
22
|
+
if (items && !("$ref" in items) && items.enum) {
|
|
23
|
+
this.isEnum = true;
|
|
24
|
+
this.enums = items.enum;
|
|
25
|
+
}
|
|
26
|
+
else if (schemaObject.enum) {
|
|
27
|
+
this.isEnum = true;
|
|
28
|
+
this.enums = schemaObject.enum;
|
|
29
|
+
}
|
|
30
|
+
if (schemaName) {
|
|
31
|
+
this.schemaName = schemaName;
|
|
32
|
+
}
|
|
33
|
+
if (name.split("-").length > 1) {
|
|
34
|
+
this.name = `['${name}']`;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
this.name = name;
|
|
38
|
+
}
|
|
39
|
+
this.required = required;
|
|
40
|
+
this.type = type;
|
|
41
|
+
this.example = isEmpty ? this.getEmptyExample() : this.getExample(example);
|
|
42
|
+
this.buildedProp = this.build(schemaObject);
|
|
43
|
+
if (description)
|
|
44
|
+
this.description = description;
|
|
45
|
+
}
|
|
46
|
+
getDtoName(ref) {
|
|
47
|
+
const cavalos = ref.split("/");
|
|
48
|
+
const dto = cavalos[cavalos.length - 1];
|
|
49
|
+
return dto;
|
|
50
|
+
}
|
|
51
|
+
getEmptyExample() {
|
|
52
|
+
switch (this.type) {
|
|
53
|
+
case "number":
|
|
54
|
+
return 0;
|
|
55
|
+
case "boolean":
|
|
56
|
+
return false;
|
|
57
|
+
case "string":
|
|
58
|
+
return "";
|
|
59
|
+
default:
|
|
60
|
+
return "";
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
getExample(example) {
|
|
64
|
+
if (example)
|
|
65
|
+
return example;
|
|
66
|
+
return this.getEmptyExample();
|
|
67
|
+
}
|
|
68
|
+
deepValidator() {
|
|
69
|
+
if (this.name === "email") {
|
|
70
|
+
return `z.email().default('${this.example}')`;
|
|
71
|
+
}
|
|
72
|
+
else if (this.name === "password") {
|
|
73
|
+
return inputs.password;
|
|
74
|
+
}
|
|
75
|
+
else if (this.isEnum) {
|
|
76
|
+
return `${treatenEnum(this.enums)}.default('${this.enums[0]}')`;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
isNullable() {
|
|
83
|
+
return this.required ? "" : ".nullable()";
|
|
84
|
+
}
|
|
85
|
+
treatName(name) {
|
|
86
|
+
return name;
|
|
87
|
+
}
|
|
88
|
+
build(schemaObject) {
|
|
89
|
+
const name = this.treatName(this.name);
|
|
90
|
+
const x = `${name}: z.${this.type}()${this.isNullable()}`;
|
|
91
|
+
switch (this.type) {
|
|
92
|
+
case "string": {
|
|
93
|
+
// console.log(schemaObject.enum);
|
|
94
|
+
const deepValidation = this.deepValidator();
|
|
95
|
+
return deepValidation ? `${name}: ${deepValidation}` : `${x}.default('${this.example}')`;
|
|
96
|
+
}
|
|
97
|
+
case "boolean":
|
|
98
|
+
return `${x}.default(${this.example})`;
|
|
99
|
+
case "number": {
|
|
100
|
+
const number = JSON.stringify(this.example) ?? 0;
|
|
101
|
+
return `${x}.default(${sanitizeNumber(number)})`;
|
|
102
|
+
}
|
|
103
|
+
case "array": {
|
|
104
|
+
if (!schemaObject.items || !("$ref" in schemaObject.items)) {
|
|
105
|
+
let zodType = "z.string()";
|
|
106
|
+
if (schemaObject.items?.enum) {
|
|
107
|
+
zodType = treatenEnum(schemaObject.items.enum);
|
|
108
|
+
}
|
|
109
|
+
else if (schemaObject.items?.type) {
|
|
110
|
+
zodType = `z.${schemaObject.items.type}()`;
|
|
111
|
+
}
|
|
112
|
+
return `${name}: z.${this.type}(${zodType})${this.isNullable()}.default([])`;
|
|
113
|
+
}
|
|
114
|
+
const dto = this.getDtoName(schemaObject.items.$ref);
|
|
115
|
+
return `${name}: z.array(${dto}Schema).default(new Array(10).fill(${dto}Schema.parse({})))`;
|
|
116
|
+
}
|
|
117
|
+
case "object":
|
|
118
|
+
return `${name}: z.any()`;
|
|
119
|
+
default:
|
|
120
|
+
return `${name}: z.any()`;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|