svelte-reflector 1.0.13 → 1.0.15

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.
@@ -0,0 +1,2 @@
1
+ export declare class SchemaConstructor {
2
+ }
@@ -0,0 +1,2 @@
1
+ export class SchemaConstructor {
2
+ }
@@ -4,6 +4,7 @@ export declare function sanitizeNumber(texto: string): string;
4
4
  export declare function capitalizeFirstLetter(text: string): string;
5
5
  export declare function splitByUppercase(text: string): string[];
6
6
  export declare function treatByUppercase(text?: string): string;
7
+ export declare function treatAndUpper(text: string): string;
7
8
  export declare function createDangerMessage(text: string): void;
8
9
  export declare function getFilteredEntities(rawEndpoint: string): string[];
9
10
  export declare function getEndpointAndModuleName(rawEndpoint: string): {
@@ -7,7 +7,19 @@
7
7
  // if (!lhs) return "";
8
8
  // return `${lhs.trim()} = ${cleaned}`;
9
9
  // }
10
- const trashWords = new Set(["Kyc", "Get", "Customer", "Response", "Res", "Self", "Admin"]);
10
+ const trashWords = new Set([
11
+ "Kyc",
12
+ "Get",
13
+ "Customer",
14
+ "Response",
15
+ "Res",
16
+ "Self",
17
+ "Admin",
18
+ "Default",
19
+ "Owner",
20
+ "Member",
21
+ "Public",
22
+ ]);
11
23
  export function toCamelCase(str) {
12
24
  return str
13
25
  .split("-")
@@ -58,6 +70,9 @@ export function treatByUppercase(text) {
58
70
  }
59
71
  return out.length > 0 ? out : "entity";
60
72
  }
73
+ export function treatAndUpper(text) {
74
+ return capitalizeFirstLetter(treatByUppercase(text));
75
+ }
61
76
  export function createDangerMessage(text) {
62
77
  console.log("\x1b[31m%s\x1b[0m", `[!] ${text}`);
63
78
  }
package/dist/main.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import * as path from "node:path";
2
2
  import * as fs from "node:fs";
3
3
  import { Source } from "./file.js";
4
- import { getEndpoint, splitByUppercase, testeEndpoint } from "./helpers/helpers.js";
4
+ import { getEndpoint, splitByUppercase } from "./helpers/helpers.js";
5
5
  import { Schema } from "./schema.js";
6
6
  import { Module } from "./module.js";
7
+ // import { Module } from "./module.js";
7
8
  export class Reflector {
8
9
  components;
9
10
  paths;
@@ -12,7 +13,7 @@ export class Reflector {
12
13
  localDoc = new Source({ path: path.resolve(process.cwd(), `${this.dir}/backup.json`) });
13
14
  src = new Source({ path: path.resolve(process.cwd(), `${this.generatedDir}/controllers`) });
14
15
  typesSrc = new Source({ path: path.resolve(process.cwd(), `${this.generatedDir}/reflector.types.ts`) });
15
- schemaFile = new Source({ path: path.resolve(process.cwd(), `${this.generatedDir}/schemas.ts`) });
16
+ schemaFile = new Source({ path: path.resolve(process.cwd(), `${this.generatedDir}/schemas.svelte.ts`) });
16
17
  files;
17
18
  schemas;
18
19
  modules;
@@ -39,7 +40,7 @@ export class Reflector {
39
40
  name: key,
40
41
  requireds: object.required || [],
41
42
  };
42
- schemas.push(new Schema({ ...schema, isEmpty: false }), new Schema({ ...schema, isEmpty: true }));
43
+ schemas.push(new Schema({ ...schema, isEmpty: false }));
43
44
  }
44
45
  console.log(`${schemas.length} schemas gerados com sucesso.`);
45
46
  return schemas;
@@ -80,10 +81,29 @@ export class Reflector {
80
81
  return modules;
81
82
  }
82
83
  build() {
84
+ const enums = new Set();
83
85
  const treatedSchemas = this.schemas.map((s) => {
84
- return `${s.schema} ${s.type}`;
86
+ s.enums.forEach((en) => enums.add(en));
87
+ return s.schema;
85
88
  });
86
- this.schemaFile.changeData([`import z from 'zod';`, ...treatedSchemas].join("\n\n"));
89
+ const buildFunction = `
90
+ function build<T>(params: { key?: T; example: T; required: boolean }) {
91
+ const { example, required, key } = params;
92
+
93
+ return {
94
+ value: key ?? example,
95
+ display: key ?? example,
96
+ required,
97
+ placeholder: example,
98
+ } as FieldSetup<T>
99
+ }
100
+ `;
101
+ this.schemaFile.changeData([
102
+ "import type { FieldSetup } from '$repository/types';",
103
+ buildFunction,
104
+ // ...Array.from(enums),
105
+ ...treatedSchemas,
106
+ ].join("\n\n"));
87
107
  this.schemaFile.save();
88
108
  this.typesSrc.changeData("export class Behavior { onError?: (e) => void; onSuccess?: () => void }");
89
109
  this.typesSrc.save();
package/dist/method.d.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  import { Request } from "./request.js";
2
- import { ZodProperty } from "./zodProperty.js";
3
2
  import type { ReflectorOperation } from "./types/types.js";
3
+ import { SchemaProp } from "./property.js";
4
4
  export declare class Method {
5
5
  name: string;
6
6
  description: string | undefined;
7
7
  endpoint: string;
8
8
  request: Request;
9
- paths: ZodProperty[];
10
- headers: ZodProperty[];
11
- querys: ZodProperty[];
12
- cookies: ZodProperty[];
9
+ paths: SchemaProp[];
10
+ headers: SchemaProp[];
11
+ querys: SchemaProp[];
12
+ cookies: SchemaProp[];
13
13
  constructor(params: {
14
14
  operation: ReflectorOperation;
15
15
  moduleName: string;
package/dist/method.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import { Request } from "./request.js";
2
- import { ZodProperty } from "./zodProperty.js";
3
- import { createDangerMessage, getEndpoint, testeEndpoint, treatByUppercase } from "./helpers/helpers.js";
2
+ import { createDangerMessage, testeEndpoint, treatByUppercase } from "./helpers/helpers.js";
3
+ import { SchemaProp } from "./property.js";
4
4
  export class Method {
5
5
  name;
6
- // zodProperties: ZodProperty[];
6
+ // zodProperties: Property[];
7
7
  description;
8
8
  endpoint;
9
9
  request;
@@ -42,16 +42,16 @@ export class Method {
42
42
  inParam,
43
43
  };
44
44
  if (inParam === "query") {
45
- this.querys.push(new ZodProperty(zodPropertie));
45
+ this.querys.push(new SchemaProp(zodPropertie));
46
46
  }
47
47
  else if (inParam === "header") {
48
- this.headers.push(new ZodProperty(zodPropertie));
48
+ this.headers.push(new SchemaProp(zodPropertie));
49
49
  }
50
50
  else if (inParam === "path") {
51
- this.paths.push(new ZodProperty(zodPropertie));
51
+ this.paths.push(new SchemaProp(zodPropertie));
52
52
  }
53
53
  else if (inParam === "cookie") {
54
- this.paths.push(new ZodProperty(zodPropertie));
54
+ this.paths.push(new SchemaProp(zodPropertie));
55
55
  }
56
56
  }
57
57
  }
@@ -74,7 +74,7 @@ export class Method {
74
74
  // const props = this.getProps();
75
75
  if (this.request.apiType === "get") {
76
76
  if (this.request.attributeType === "list") {
77
- beforeResponse.push(`const {data: { data, ...params }} = response`, "\n\n", `this.list = data`, "repo.intercept.rebuild(this.querys, params)");
77
+ beforeResponse.push(`const {data: { data }} = response`, "\n\n", `this.list = data`);
78
78
  const inside = `
79
79
  const response = await repo.api.get<{data: ${this.request.responseType}}, unknown>({
80
80
  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 "./zodProperty.js";
4
+ import type { SchemaProp } from "./property.js";
5
5
  export declare class Module {
6
6
  readonly name: string;
7
7
  readonly path: string;
@@ -9,10 +9,10 @@ export declare class Module {
9
9
  readonly src: Source;
10
10
  imports: Set<string>;
11
11
  methods: Method[];
12
- querys: ZodProperty[];
13
- paths: ZodProperty[];
14
- headers: ZodProperty[];
15
- cookies: ZodProperty[];
12
+ querys: SchemaProp[];
13
+ paths: SchemaProp[];
14
+ headers: SchemaProp[];
15
+ cookies: SchemaProp[];
16
16
  moduleConstructor: string;
17
17
  constructor(params: {
18
18
  name: string;
package/dist/module.js CHANGED
@@ -3,7 +3,6 @@ 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 "./zodProperty.js";
7
6
  export class Module {
8
7
  name;
9
8
  path;
@@ -24,7 +23,6 @@ export class Module {
24
23
  'import repo from "$repository/main"',
25
24
  'import { Behavior } from "$reflector/reflector.types";',
26
25
  'import { PUBLIC_ENVIRONMENT } from "$env/static/public";',
27
- 'import z from "zod";',
28
26
  ]);
29
27
  this.name = capitalizeFirstLetter(name);
30
28
  this.path = path;
@@ -58,7 +56,11 @@ export class Module {
58
56
  });
59
57
  }
60
58
  buildZObject(props) {
61
- const teste = `z.object({${props.map((p) => p.buildedProp)}})`;
59
+ const teste = props
60
+ .map((prop) => {
61
+ return `${prop.name} = $state(${prop.emptyExample})`;
62
+ })
63
+ .join(";");
62
64
  return teste;
63
65
  }
64
66
  creator() {
@@ -66,25 +68,25 @@ export class Module {
66
68
  const moduleAttributes = new Set().add("loading = $state<boolean>(false)");
67
69
  const moduleInit = new Set([]);
68
70
  const moduleClear = new Set([]);
69
- const getXablau = (params) => {
71
+ const getParams = (params) => {
70
72
  const { name, objets } = params;
71
73
  const capitalizedName = capitalizeFirstLetter(name);
72
- buildedModuleTypes.push(`const ${capitalizedName}Schema = ${this.buildZObject(objets)}`);
73
- moduleAttributes.add(`${name} = $state(repo.newForm(${capitalizedName}Schema))`);
74
+ buildedModuleTypes.push(`class ${capitalizedName}Schema { ${this.buildZObject(objets)} }`);
75
+ moduleAttributes.add(`${name} = $state(new ${capitalizedName}Schema())`);
74
76
  moduleInit.add(`this.clear${capitalizeFirstLetter(capitalizedName)}()`);
75
- moduleClear.add(`clear${capitalizedName}() { this.${name} = repo.newForm(${capitalizedName}Schema) }`);
77
+ moduleClear.add(`clear${capitalizedName}() { this.${name} = new ${capitalizedName}Schema() }`);
76
78
  };
77
79
  if (this.querys.length > 0) {
78
- getXablau({ name: "querys", objets: this.querys });
80
+ getParams({ name: "querys", objets: this.querys });
79
81
  }
80
82
  if (this.headers.length > 0) {
81
- getXablau({ name: "headers", objets: this.headers });
83
+ getParams({ name: "headers", objets: this.headers });
82
84
  }
83
85
  if (this.paths.length > 0) {
84
- getXablau({ name: "paths", objets: this.paths });
86
+ getParams({ name: "paths", objets: this.paths });
85
87
  }
86
88
  if (this.cookies.length > 0) {
87
- getXablau({ name: "cookies", objets: this.cookies });
89
+ getParams({ name: "cookies", objets: this.cookies });
88
90
  }
89
91
  const form = [];
90
92
  for (const method of this.methods) {
@@ -106,13 +108,10 @@ export class Module {
106
108
  moduleInit.add("this.clearList()");
107
109
  moduleClear.add(`clearList() { this.list = [] }`);
108
110
  }
109
- if (attributeType === "list" || this.querys.length > 0 || this.headers.length > 0) {
110
- this.imports.add(`import z from "zod";`);
111
- }
112
111
  }
113
112
  const formSet = new Set();
114
113
  for (const f of form) {
115
- formSet.add(`${f.name}: repo.newForm(Empty${f.type}Schema)`);
114
+ formSet.add(`${f.name}: new ${f.type}()`);
116
115
  }
117
116
  if (formSet.size > 0) {
118
117
  moduleAttributes.add(`
@@ -194,20 +193,16 @@ export class Module {
194
193
  for (const method of this.methods) {
195
194
  const { bodyType, responseType, apiType } = method.request;
196
195
  if (bodyType) {
197
- entries.add(`${bodyType}Schema`);
198
- entries.add(`Empty${bodyType}Schema`);
196
+ entries.add(`${bodyType}`);
199
197
  }
200
198
  if (responseType) {
201
- if (apiType === "delete") {
202
- entries.add(`${responseType}Schema`);
203
- }
204
- entries.add(`type ${responseType}`);
199
+ entries.add(`${responseType}`);
205
200
  }
206
201
  }
207
202
  const cleanEntries = Array.from(entries).filter((x) => x != "type any");
208
203
  if (cleanEntries.length === 0)
209
204
  return "";
210
- return `import { ${cleanEntries} } from '$reflector/schemas';`;
205
+ return `import { ${cleanEntries} } from '$reflector/schemas.svelte';`;
211
206
  }
212
207
  buildClass(params) {
213
208
  const { moduleInit, moduleAttributes, moduleClear } = params;
@@ -220,7 +215,7 @@ export class Module {
220
215
  ${this.buildMethods().join("\n")}
221
216
 
222
217
  ${moduleClear.join("\n\n")}
223
-
218
+
224
219
  reset() {
225
220
  ${moduleInit.join(";")}
226
221
  }
@@ -230,7 +225,7 @@ export class Module {
230
225
  buildConstructor(form) {
231
226
  if (form.length === 0)
232
227
  return "";
233
- const teste = `
228
+ const teste = `
234
229
  constructor(params?: { empty: boolean }) {
235
230
  const isEmpty = params?.empty || PUBLIC_ENVIRONMENT != 'DEV'
236
231
 
@@ -241,7 +236,7 @@ export class Module {
241
236
  if(isEmpty) return this.forms
242
237
 
243
238
  return {
244
- ${form.map((f) => `${f.name}: repo.newForm(${f.type}Schema)`)}
239
+ ${form.map((f) => `${f.name}: new ${f.type}()`)}
245
240
  }
246
241
  }
247
242
  `;
@@ -1,14 +1,15 @@
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
- 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;
3
+ export declare class SchemaProp {
11
4
  inParam: ParameterLocation;
5
+ example: string;
6
+ emptyExample: string;
7
+ name: string;
8
+ bType: string;
9
+ isRequired: boolean;
10
+ reflectorType: ReflectorParamType;
11
+ buildedValue: string;
12
+ enums?: string;
12
13
  constructor(params: {
13
14
  schemaName?: string;
14
15
  name: string;
@@ -20,11 +21,13 @@ export declare class ZodProperty {
20
21
  isEmpty: boolean;
21
22
  inParam: ParameterLocation;
22
23
  });
23
- private getDtoName;
24
- private getEmptyExample;
24
+ private getBuildedValue;
25
+ getEmptyExample(params: {
26
+ type: ReflectorParamType;
27
+ schemaObject: SchemaObject;
28
+ }): string | false | 0;
25
29
  private getExample;
26
30
  private deepValidator;
27
- private isNullable;
28
31
  private treatName;
29
- private build;
32
+ private getType;
30
33
  }
package/dist/property.js CHANGED
@@ -1,116 +1,140 @@
1
- import { sanitizeNumber, splitByUppercase } from "./helpers/helpers.js";
2
1
  import { ReflectorInput } from "./helpers/input.js";
3
2
  import {} from "./types/open-api-spec.interface.js";
4
3
  import {} from "./types/types.js";
5
4
  const inputs = new ReflectorInput();
6
- export class ZodProperty {
7
- schemaName;
8
- name;
9
- example;
10
- type;
11
- buildedProp;
12
- description;
13
- required;
5
+ export class SchemaProp {
6
+ // schemaName?: string;
7
+ // name: string;
8
+ // example: Example | undefined;
9
+ // type: ReflectorParamType;
10
+ // buildedProp: string;
11
+ // description?: string;
12
+ // required: boolean;
14
13
  inParam;
14
+ // isEnum: boolean = false;
15
+ // enums: string[] = [];
16
+ example;
17
+ emptyExample;
18
+ name;
19
+ bType;
20
+ isRequired;
21
+ reflectorType;
22
+ buildedValue;
23
+ enums;
15
24
  constructor(params) {
16
25
  const { schemaName, name, schemaObject, type, example, required, description, isEmpty, inParam } = params;
26
+ if (schemaObject.enum) {
27
+ this.enums = schemaObject.enum.map((e) => `'${e}'`).join("|");
28
+ }
29
+ // this.inParam = inParam;
30
+ // this.isEnum = false;
17
31
  this.inParam = inParam;
18
- if (schemaName) {
19
- this.schemaName = schemaName;
32
+ this.reflectorType = schemaObject.enum ? "enum" : type;
33
+ this.isRequired = required;
34
+ this.name = this.treatName(name);
35
+ this.bType = this.getType({ type, schemaName, schemaObject });
36
+ this.example = `${this.getExample({ example, type, schemaObject })}`;
37
+ this.emptyExample = `${this.getEmptyExample({ schemaObject, type: this.reflectorType })}`;
38
+ this.buildedValue = this.getBuildedValue({
39
+ example: this.example,
40
+ isRequired: this.isRequired,
41
+ type,
42
+ bType: this.bType,
43
+ enums: this.enums,
44
+ schemaObject,
45
+ });
46
+ }
47
+ getBuildedValue(params) {
48
+ const { example, isRequired, type, bType, enums, schemaObject } = params;
49
+ let content = "";
50
+ const diamondType = enums ? `<${enums}>` : "";
51
+ if (type === "number" || type === "string" || type === "boolean") {
52
+ content = `build${diamondType}({key: ${this.getEmptyExample({ type, schemaObject })}, example: ${example}, required: ${isRequired}})`;
20
53
  }
21
- if (name.split("-").length > 1) {
22
- this.name = `['${name}']`;
54
+ else if (type === "object") {
55
+ content = `new ${bType}()`;
23
56
  }
24
- else {
25
- this.name = name;
57
+ else if (type === "array") {
58
+ content = "[]";
26
59
  }
27
- this.required = required;
28
- this.type = type;
29
- this.example = isEmpty ? this.getEmptyExample() : this.getExample(example);
30
- this.buildedProp = this.build(schemaObject);
31
- if (description)
32
- this.description = description;
33
- }
34
- getDtoName(ref) {
35
- const cavalos = ref.split("/");
36
- const dto = cavalos[cavalos.length - 1];
37
- return dto;
60
+ return content;
38
61
  }
39
- getEmptyExample() {
40
- switch (this.type) {
41
- case "number":
42
- return 0;
43
- case "boolean":
44
- return false;
45
- case "string":
46
- return "";
47
- default:
48
- return "";
62
+ getEmptyExample(params) {
63
+ const { schemaObject, type } = params;
64
+ if (type === "number") {
65
+ return 0;
66
+ }
67
+ else if (type === "boolean") {
68
+ return false;
69
+ }
70
+ else if (schemaObject.enum) {
71
+ return `'${schemaObject.enum[0]}'`;
72
+ }
73
+ else {
74
+ return "''";
49
75
  }
50
76
  }
51
- getExample(example) {
52
- if (example)
53
- return example;
54
- return this.getEmptyExample();
77
+ getExample(params) {
78
+ const { example, type, schemaObject } = params;
79
+ // if (schemaObject.enum) {
80
+ // return `'${schemaObject.enum[0]}'`;
81
+ // }
82
+ const sanitizedExample = type === "boolean" || type === "number" ? example : `"${example}"`;
83
+ return example ? sanitizedExample : this.getEmptyExample({ schemaObject, type });
55
84
  }
56
- deepValidator() {
57
- if (this.name === "email") {
58
- return `z.email().default('${this.example}')`;
85
+ // private getExample(params: { example: Example | undefined; type: ReflectorParamType; schemaObject: SchemaObject }) {
86
+ // const { example, type, schemaObject } = params;
87
+ // const sanitizedExample = () => {
88
+ // if (!example || type === "enum") {
89
+ // return this.getEmptyExample({ schemaObject, type });
90
+ // } else if (type === "boolean" || type === "number") {
91
+ // return example;
92
+ // } else {
93
+ // return `"${example}"`;
94
+ // }
95
+ // };
96
+ // console.log(sanitizedExample());
97
+ // return sanitizedExample();
98
+ // }
99
+ deepValidator(params) {
100
+ const { name } = params;
101
+ if (name === "email") {
102
+ // return `z.email().default('${this.example}')`;
103
+ return "Email";
59
104
  }
60
- else if (this.name === "password") {
61
- return inputs.password;
105
+ else if (name === "password") {
106
+ return "Password";
62
107
  }
108
+ // else if (this.isEnum) {
109
+ // return `${treatenEnum(this.enums)}.default('${this.enums[0]}')`;
110
+ // }
63
111
  else {
64
112
  return false;
65
113
  }
66
114
  }
67
- isNullable() {
68
- return this.required ? "" : ".nullable()";
69
- }
70
115
  treatName(name) {
71
- return name;
116
+ let newName = name;
117
+ if (name.split("-").length > 1) {
118
+ newName = `['${name}']`;
119
+ }
120
+ return newName;
72
121
  }
73
- build(schemaObject) {
74
- const name = this.treatName(this.name);
75
- const x = `${name}: z.${this.type}()${this.isNullable()}`;
76
- switch (this.type) {
77
- case "string": {
78
- // if (name === "role") {
79
- // console.log(schemaObject.enum);
80
- // }
81
- console.log(schemaObject.enum);
82
- const deepValidation = this.deepValidator();
83
- return deepValidation ? `${name}: ${deepValidation}` : `${x}.default('${this.example}')`;
84
- }
85
- case "boolean":
86
- return `${x}.default(${this.example})`;
87
- case "number": {
88
- const number = JSON.stringify(this.example) ?? 0;
89
- return `${x}.default(${sanitizeNumber(number)})`;
90
- }
91
- case "array": {
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([])`;
106
- }
107
- const dto = this.getDtoName(schemaObject.items.$ref);
108
- return `${name}: z.array(${dto}Schema).default(new Array(10).fill(${dto}Schema.parse({})))`;
122
+ getType(params) {
123
+ const { type, schemaName, schemaObject } = params;
124
+ if (type === "object") {
125
+ return `${schemaName}`;
126
+ }
127
+ else if (type === "array") {
128
+ let name = schemaName;
129
+ const teste = schemaObject.items;
130
+ if (teste && "$ref" in teste) {
131
+ const a = teste.$ref;
132
+ name = a.split("/").at(-1);
109
133
  }
110
- case "object":
111
- return `${name}: z.any()`;
112
- default:
113
- return `${name}: z.any()`;
134
+ return `${name}[]`;
135
+ }
136
+ else {
137
+ return type;
114
138
  }
115
139
  }
116
140
  }
package/dist/schema.d.ts CHANGED
@@ -1,9 +1,8 @@
1
- import { ZodProperty } from "./zodProperty.js";
1
+ import { SchemaProp } from "./property.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
- properties: ZodProperty[];
6
- type: string;
5
+ properties: SchemaProp[];
7
6
  schema: string;
8
7
  enums: Set<string>;
9
8
  objects: Map<string, string>;
package/dist/schema.js CHANGED
@@ -1,8 +1,9 @@
1
- import { ZodProperty } from "./zodProperty.js";
1
+ import { capitalizeFirstLetter, splitByUppercase, treatAndUpper, treatByUppercase } from "./helpers/helpers.js";
2
+ import { SchemaProp } from "./property.js";
2
3
  export class Schema {
3
4
  name;
4
5
  properties = [];
5
- type;
6
+ // type: string;
6
7
  schema;
7
8
  enums = new Set();
8
9
  objects = new Map();
@@ -14,19 +15,20 @@ export class Schema {
14
15
  if ("$ref" in value) {
15
16
  const teste = value.$ref;
16
17
  const object = teste.split("/").at(-1);
17
- this.objects.set(key, `${object}Schema`);
18
+ this.objects.set(key, `${object}`);
18
19
  }
19
20
  continue;
20
21
  }
21
22
  const required = requireds.includes(key);
22
23
  const teste = value.items;
24
+ // const enumName = `Enum${treatAndUpper(name)}${capitalizeFirstLetter(key)}`;
23
25
  if (teste && !("$ref" in teste) && teste.enum) {
24
26
  this.enums.add(this.getEnumConst({ enums: teste.enum, schemaName: key }));
25
27
  }
26
28
  else if (value.enum) {
27
29
  this.enums.add(this.getEnumConst({ enums: value.enum, schemaName: key }));
28
30
  }
29
- this.properties.push(new ZodProperty({
31
+ this.properties.push(new SchemaProp({
30
32
  schemaName: this.name,
31
33
  name: key,
32
34
  schemaObject: value,
@@ -37,20 +39,36 @@ export class Schema {
37
39
  inParam: "path",
38
40
  }));
39
41
  }
40
- this.type = `export type ${this.name} = z.infer<typeof ${this.name}Schema>;`;
41
- this.schema = `export const ${this.name}Schema = z.object({
42
- ${this.properties.map((p) => {
43
- return p.buildedProp;
44
- })}
45
- ${this.properties.length > 0 ? "," : ""}
46
- ${Array.from(this.objects).map(([k, v]) => {
47
- return `${k}: z.object(${v})`;
48
- })}
49
- });`;
42
+ const keys = this.properties
43
+ .map((p) => {
44
+ const keyName = `${p.name}${p.isRequired ? "" : "?"}`;
45
+ let state;
46
+ if (p.reflectorType === "object") {
47
+ state = `$state<${p.bType}>()`;
48
+ }
49
+ else if (p.reflectorType === "array") {
50
+ state = `$state<${p.bType}>([])`;
51
+ }
52
+ else {
53
+ state = `$state(${p.buildedValue})`;
54
+ }
55
+ return `${keyName} = ${state}`;
56
+ })
57
+ .join(";\n");
58
+ const buildedObjects = Array.from(this.objects)
59
+ .map(([k, v]) => {
60
+ return `${k} = $state(new ${v}())`;
61
+ })
62
+ .join(";\n");
63
+ this.schema = `export class ${this.name} {
64
+ ${keys}
65
+ ${this.properties.length > 0 ? ";" : ""}
66
+ ${buildedObjects}
67
+ };`;
50
68
  }
51
69
  getEnumConst(params) {
52
70
  const { enums, schemaName } = params;
53
- const enumList = enums.map((en) => `'${en}'`);
54
- return `export const ${schemaName}EnumSchema = z.enum([${enumList}])`;
71
+ const enumList = enums.map((en) => `'${en}'`).join("|");
72
+ return `export type Enum${capitalizeFirstLetter(schemaName)} = ${enumList}`;
55
73
  }
56
74
  }
@@ -1,5 +1,5 @@
1
1
  import type { OperationObject } from "./open-api-spec.interface.js";
2
- export type ReflectorParamType = "string" | "boolean" | "number" | "array" | "object";
2
+ export type ReflectorParamType = "string" | "boolean" | "number" | "array" | "object" | "enum";
3
3
  export type ApiType = "get" | "post" | "delete" | "patch" | "put";
4
4
  export type ReflectorOperation = OperationObject & {
5
5
  apiMethod: ApiType;
package/package.json CHANGED
@@ -1,32 +1,32 @@
1
- {
2
- "name": "svelte-reflector",
3
- "version": "1.0.13",
4
- "description": "Reflects zod types from openAPI schemas",
5
- "type": "module",
6
- "main": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
8
- "exports": {
9
- ".": {
10
- "import": "./dist/index.js",
11
- "types": "./dist/index.d.ts"
12
- }
13
- },
14
- "bin": {
15
- "reflect": "./dist/cli.js"
16
- },
17
- "files": [
18
- "dist"
19
- ],
20
- "scripts": {
21
- "build": "tsc"
22
- },
23
- "dependencies": {
24
- "axios": "^1.12.2",
25
- "dotenv": "^16.4.5"
26
- },
27
- "devDependencies": {
28
- "@types/node": "^24.8.1",
29
- "prettier": "^3.6.2",
30
- "typescript": "^5.9.3"
31
- }
32
- }
1
+ {
2
+ "name": "svelte-reflector",
3
+ "version": "1.0.15",
4
+ "description": "Reflects zod types from openAPI schemas",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "bin": {
15
+ "reflect": "./dist/cli.js"
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsc"
22
+ },
23
+ "dependencies": {
24
+ "axios": "^1.12.2",
25
+ "dotenv": "^16.4.5"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "^24.8.1",
29
+ "prettier": "^3.6.2",
30
+ "typescript": "^5.9.3"
31
+ }
32
+ }