svelte-reflector 1.0.13 → 1.0.14

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
+ }
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;
@@ -81,9 +82,21 @@ export class Reflector {
81
82
  }
82
83
  build() {
83
84
  const treatedSchemas = this.schemas.map((s) => {
84
- return `${s.schema} ${s.type}`;
85
+ return s.schema;
85
86
  });
86
- this.schemaFile.changeData([`import z from 'zod';`, ...treatedSchemas].join("\n\n"));
87
+ const teste = `
88
+ function build<T>(params: { key?: T; example: T; required: boolean }) {
89
+ const { example, required, key } = params;
90
+
91
+ return {
92
+ value: key ?? example,
93
+ display: key ?? example,
94
+ required,
95
+ placeholder: example,
96
+ } as FieldSetup<T>
97
+ }
98
+ `;
99
+ this.schemaFile.changeData(["import type { FieldSetup } from '$repository/types';", teste, ...treatedSchemas].join("\n\n"));
87
100
  this.schemaFile.save();
88
101
  this.typesSrc.changeData("export class Behavior { onError?: (e) => void; onSuccess?: () => void }");
89
102
  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
  }
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;
@@ -21,7 +21,6 @@ export declare class Module {
21
21
  path: string;
22
22
  dir: string;
23
23
  });
24
- private buildZObject;
25
24
  private creator;
26
25
  private getPath;
27
26
  private getAdditionalMethod;
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;
@@ -57,10 +55,10 @@ export class Module {
57
55
  data: this.buildFile({ moduleAttributes, moduleTypes, moduleInit, moduleClear }),
58
56
  });
59
57
  }
60
- buildZObject(props) {
61
- const teste = `z.object({${props.map((p) => p.buildedProp)}})`;
62
- return teste;
63
- }
58
+ // private buildZObject(props: SchemaProp[]) {
59
+ // const teste = `z.object({${props.map((p) => p.buildedProp)}})`;
60
+ // return teste;
61
+ // }
64
62
  creator() {
65
63
  const buildedModuleTypes = [];
66
64
  const moduleAttributes = new Set().add("loading = $state<boolean>(false)");
@@ -69,10 +67,10 @@ export class Module {
69
67
  const getXablau = (params) => {
70
68
  const { name, objets } = params;
71
69
  const capitalizedName = capitalizeFirstLetter(name);
72
- buildedModuleTypes.push(`const ${capitalizedName}Schema = ${this.buildZObject(objets)}`);
70
+ // buildedModuleTypes.push(`const ${capitalizedName}Schema = ${this.buildZObject(objets)}`);
73
71
  moduleAttributes.add(`${name} = $state(repo.newForm(${capitalizedName}Schema))`);
74
72
  moduleInit.add(`this.clear${capitalizeFirstLetter(capitalizedName)}()`);
75
- moduleClear.add(`clear${capitalizedName}() { this.${name} = repo.newForm(${capitalizedName}Schema) }`);
73
+ moduleClear.add(`clear${capitalizedName}() { this.${name} = new ${capitalizedName}Schema() }`);
76
74
  };
77
75
  if (this.querys.length > 0) {
78
76
  getXablau({ name: "querys", objets: this.querys });
@@ -106,13 +104,10 @@ export class Module {
106
104
  moduleInit.add("this.clearList()");
107
105
  moduleClear.add(`clearList() { this.list = [] }`);
108
106
  }
109
- if (attributeType === "list" || this.querys.length > 0 || this.headers.length > 0) {
110
- this.imports.add(`import z from "zod";`);
111
- }
112
107
  }
113
108
  const formSet = new Set();
114
109
  for (const f of form) {
115
- formSet.add(`${f.name}: repo.newForm(Empty${f.type}Schema)`);
110
+ formSet.add(`${f.name}: new ${f.type}()`);
116
111
  }
117
112
  if (formSet.size > 0) {
118
113
  moduleAttributes.add(`
@@ -194,12 +189,11 @@ export class Module {
194
189
  for (const method of this.methods) {
195
190
  const { bodyType, responseType, apiType } = method.request;
196
191
  if (bodyType) {
197
- entries.add(`${bodyType}Schema`);
198
- entries.add(`Empty${bodyType}Schema`);
192
+ entries.add(`${bodyType}`);
199
193
  }
200
194
  if (responseType) {
201
195
  if (apiType === "delete") {
202
- entries.add(`${responseType}Schema`);
196
+ entries.add(`${responseType}`);
203
197
  }
204
198
  entries.add(`type ${responseType}`);
205
199
  }
@@ -207,7 +201,7 @@ export class Module {
207
201
  const cleanEntries = Array.from(entries).filter((x) => x != "type any");
208
202
  if (cleanEntries.length === 0)
209
203
  return "";
210
- return `import { ${cleanEntries} } from '$reflector/schemas';`;
204
+ return `import { ${cleanEntries} } from '$reflector/schemas.svelte';`;
211
205
  }
212
206
  buildClass(params) {
213
207
  const { moduleInit, moduleAttributes, moduleClear } = params;
@@ -220,7 +214,7 @@ export class Module {
220
214
  ${this.buildMethods().join("\n")}
221
215
 
222
216
  ${moduleClear.join("\n\n")}
223
-
217
+
224
218
  reset() {
225
219
  ${moduleInit.join(";")}
226
220
  }
@@ -230,7 +224,7 @@ export class Module {
230
224
  buildConstructor(form) {
231
225
  if (form.length === 0)
232
226
  return "";
233
- const teste = `
227
+ const teste = `
234
228
  constructor(params?: { empty: boolean }) {
235
229
  const isEmpty = params?.empty || PUBLIC_ENVIRONMENT != 'DEV'
236
230
 
@@ -241,7 +235,7 @@ export class Module {
241
235
  if(isEmpty) return this.forms
242
236
 
243
237
  return {
244
- ${form.map((f) => `${f.name}: repo.newForm(${f.type}Schema)`)}
238
+ ${form.map((f) => `${f.name}: new ${f.type}()`)}
245
239
  }
246
240
  }
247
241
  `;
@@ -1,14 +1,13 @@
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;
3
+ export declare class SchemaProp {
4
+ example: string;
5
+ emptyExample: string;
5
6
  name: string;
6
- example: Example | undefined;
7
- type: ReflectorParamType;
8
- buildedProp: string;
9
- description?: string;
10
- required: boolean;
11
- inParam: ParameterLocation;
7
+ bType: string;
8
+ isRequired: boolean;
9
+ paramType: ReflectorParamType;
10
+ buildedValue: string;
12
11
  constructor(params: {
13
12
  schemaName?: string;
14
13
  name: string;
@@ -20,11 +19,11 @@ export declare class ZodProperty {
20
19
  isEmpty: boolean;
21
20
  inParam: ParameterLocation;
22
21
  });
23
- private getDtoName;
24
- private getEmptyExample;
22
+ private getBuildedValue;
23
+ getEmptyExample(type: ReflectorParamType): false | 0 | "''";
25
24
  private getExample;
26
25
  private deepValidator;
27
26
  private isNullable;
28
27
  private treatName;
29
- private build;
28
+ private getType;
30
29
  }
package/dist/property.js CHANGED
@@ -1,116 +1,114 @@
1
- import { sanitizeNumber, splitByUppercase } from "./helpers/helpers.js";
1
+ import { sanitizeNumber, treatenEnum } 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
- export class ZodProperty {
7
- schemaName;
8
- name;
6
+ export class SchemaProp {
7
+ // schemaName?: string;
8
+ // name: string;
9
+ // example: Example | undefined;
10
+ // type: ReflectorParamType;
11
+ // buildedProp: string;
12
+ // description?: string;
13
+ // required: boolean;
14
+ // inParam: ParameterLocation;
15
+ // isEnum: boolean = false;
16
+ // enums: string[] = [];
9
17
  example;
10
- type;
11
- buildedProp;
12
- description;
13
- required;
14
- inParam;
18
+ emptyExample;
19
+ name;
20
+ bType;
21
+ isRequired;
22
+ paramType;
23
+ buildedValue;
15
24
  constructor(params) {
16
25
  const { schemaName, name, schemaObject, type, example, required, description, isEmpty, inParam } = params;
17
- this.inParam = inParam;
18
- if (schemaName) {
19
- this.schemaName = schemaName;
26
+ // this.inParam = inParam;
27
+ // this.isEnum = false;
28
+ this.paramType = type;
29
+ this.isRequired = required;
30
+ this.name = this.treatName(name);
31
+ this.bType = this.getType({ type, schemaName });
32
+ this.example = `${this.getExample({ example, type })}`;
33
+ this.emptyExample = `${this.getEmptyExample(type)}`;
34
+ this.buildedValue = this.getBuildedValue({
35
+ example: this.example,
36
+ isRequired: this.isRequired,
37
+ name: this.name,
38
+ type,
39
+ bType: this.bType,
40
+ });
41
+ }
42
+ getBuildedValue(params) {
43
+ const { example, isRequired, name, type, bType } = params;
44
+ let content = "";
45
+ // let emptyContent: string = "";
46
+ if (type === "number" || type === "string" || type === "boolean") {
47
+ content = `build({key: ${this.getEmptyExample(type)}, example: ${example}, required: ${isRequired}})`;
20
48
  }
21
- if (name.split("-").length > 1) {
22
- this.name = `['${name}']`;
49
+ else if (type === "object") {
50
+ content = `new ${bType}()`;
51
+ // emptyContent = `new ${bType}()`;
23
52
  }
24
- else {
25
- this.name = name;
53
+ else if (type === "array") {
54
+ content = "[]";
55
+ // emptyContent = "[]";
26
56
  }
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;
57
+ return content;
38
58
  }
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 "";
59
+ // private getDtoName(ref: string) {
60
+ // const splittedRef = ref.split("/");
61
+ // const dto = splittedRef[splittedRef.length - 1];
62
+ // return dto;
63
+ // }
64
+ getEmptyExample(type) {
65
+ if (type === "number") {
66
+ return 0;
67
+ }
68
+ else if (type === "boolean") {
69
+ return false;
70
+ }
71
+ else {
72
+ return "''";
49
73
  }
50
74
  }
51
- getExample(example) {
52
- if (example)
53
- return example;
54
- return this.getEmptyExample();
75
+ getExample(params) {
76
+ const { example, type } = params;
77
+ const sanitizedExample = type === "boolean" || type === "number" ? example : `"${example}"`;
78
+ return example ? sanitizedExample : this.getEmptyExample(type);
55
79
  }
56
- deepValidator() {
57
- if (this.name === "email") {
58
- return `z.email().default('${this.example}')`;
80
+ deepValidator(params) {
81
+ const { name } = params;
82
+ if (name === "email") {
83
+ // return `z.email().default('${this.example}')`;
84
+ return "Email";
59
85
  }
60
- else if (this.name === "password") {
61
- return inputs.password;
86
+ else if (name === "password") {
87
+ return "Password";
62
88
  }
89
+ // else if (this.isEnum) {
90
+ // return `${treatenEnum(this.enums)}.default('${this.enums[0]}')`;
91
+ // }
63
92
  else {
64
93
  return false;
65
94
  }
66
95
  }
67
- isNullable() {
68
- return this.required ? "" : ".nullable()";
96
+ isNullable(isRequired) {
97
+ return isRequired ? "" : " | null";
69
98
  }
70
99
  treatName(name) {
71
100
  return name;
72
101
  }
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({})))`;
109
- }
110
- case "object":
111
- return `${name}: z.any()`;
112
- default:
113
- return `${name}: z.any()`;
102
+ getType(params) {
103
+ const { type, schemaName } = params;
104
+ if (type === "object") {
105
+ return `${schemaName}`;
106
+ }
107
+ else if (type === "array") {
108
+ return `${schemaName}[]`;
109
+ }
110
+ else {
111
+ return type;
114
112
  }
115
113
  }
116
114
  }
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,8 @@
1
- import { ZodProperty } from "./zodProperty.js";
1
+ import { SchemaProp } from "./property.js";
2
2
  export class Schema {
3
3
  name;
4
4
  properties = [];
5
- type;
5
+ // type: string;
6
6
  schema;
7
7
  enums = new Set();
8
8
  objects = new Map();
@@ -14,7 +14,7 @@ export class Schema {
14
14
  if ("$ref" in value) {
15
15
  const teste = value.$ref;
16
16
  const object = teste.split("/").at(-1);
17
- this.objects.set(key, `${object}Schema`);
17
+ this.objects.set(key, `${object}`);
18
18
  }
19
19
  continue;
20
20
  }
@@ -26,7 +26,7 @@ export class Schema {
26
26
  else if (value.enum) {
27
27
  this.enums.add(this.getEnumConst({ enums: value.enum, schemaName: key }));
28
28
  }
29
- this.properties.push(new ZodProperty({
29
+ this.properties.push(new SchemaProp({
30
30
  schemaName: this.name,
31
31
  name: key,
32
32
  schemaObject: value,
@@ -37,16 +37,36 @@ export class Schema {
37
37
  inParam: "path",
38
38
  }));
39
39
  }
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
- });`;
40
+ const keys = this.properties
41
+ .map((p) => {
42
+ const keyName = `${p.name}${p.isRequired ? "" : "?"}`;
43
+ let state;
44
+ if (p.paramType === "object") {
45
+ state = `$state<${p.bType}>()`;
46
+ }
47
+ else if (p.paramType === "array") {
48
+ state = `$state<${p.bType}>([])`;
49
+ }
50
+ else {
51
+ state = `$state(${p.buildedValue})`;
52
+ }
53
+ return `${keyName} = ${state}`;
54
+ })
55
+ .join(";\n");
56
+ const buildedObjects = Array.from(this.objects)
57
+ .map(([k, v]) => {
58
+ return `${k} = $state(new ${v}())`;
59
+ })
60
+ .join(";\n");
61
+ // const builded = this.properties.map((p) => ).join(";\n\n\n");
62
+ if (this.name === "OwnerFinishSignUpDataDto") {
63
+ console.log(properties);
64
+ }
65
+ this.schema = `export class ${this.name} {
66
+ ${keys}
67
+ ${this.properties.length > 0 ? ";" : ""}
68
+ ${buildedObjects}
69
+ };`;
50
70
  }
51
71
  getEnumConst(params) {
52
72
  const { enums, schemaName } = params;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-reflector",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "Reflects zod types from openAPI schemas",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",