svelte-reflector 1.0.21 → 1.0.22

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.
@@ -12,5 +12,5 @@ export declare function getEndpointAndModuleName(rawEndpoint: string): {
12
12
  moduleName: string;
13
13
  };
14
14
  export declare function getEndpoint(rawEndpoint: string): string;
15
- export declare function testeEndpoint(rawEndpoint: string): string;
15
+ export declare function getFullEndpoint(rawEndpoint: string): string;
16
16
  export declare function treatenEnum(enums: string[]): string;
@@ -90,7 +90,7 @@ export function getEndpoint(rawEndpoint) {
90
90
  const filteredEntitys = getFilteredEntities(rawEndpoint);
91
91
  return filteredEntitys.join("/");
92
92
  }
93
- export function testeEndpoint(rawEndpoint) {
93
+ export function getFullEndpoint(rawEndpoint) {
94
94
  const teste = rawEndpoint.split("/");
95
95
  const a = teste
96
96
  .filter((t) => t !== "")
@@ -0,0 +1,11 @@
1
+ import type { PrimitiveProp } from "./primitive-property.js";
2
+ import type { ArrayProp, ObjectProp } from "./property.js";
3
+ export declare class ReflectorInterface {
4
+ builded: string;
5
+ constructor(params: {
6
+ primitiveProps: PrimitiveProp[];
7
+ arrayProps: ArrayProp[];
8
+ name: string;
9
+ objectProps: ObjectProp[];
10
+ });
11
+ }
@@ -0,0 +1,21 @@
1
+ export class ReflectorInterface {
2
+ builded;
3
+ constructor(params) {
4
+ const { name, arrayProps, primitiveProps, objectProps } = params;
5
+ const buildedProps = [];
6
+ arrayProps.forEach((prop) => {
7
+ buildedProps.push(prop.interfaceBuild());
8
+ });
9
+ primitiveProps.forEach((prop) => {
10
+ buildedProps.push(prop.interfaceBuild());
11
+ });
12
+ objectProps.forEach((prop) => {
13
+ buildedProps.push(prop.interfaceBuild());
14
+ });
15
+ this.builded = `
16
+ export interface ${name}Interface {
17
+ ${buildedProps}
18
+ }
19
+ `;
20
+ }
21
+ }
package/dist/main.js CHANGED
@@ -93,10 +93,14 @@ export class Reflector {
93
93
  const enums = new Set();
94
94
  const treatedSchemas = this.schemas.map((s) => {
95
95
  s.enums.forEach((en) => enums.add(en));
96
- return s.schema;
96
+ return `
97
+ ${s.interface};
98
+
99
+ ${s.schema};
100
+ `;
97
101
  });
98
102
  this.schemaFile.changeData([
99
- 'import { build } from "$reflector/reflector.svelte";',
103
+ 'import { build, BuildedInput } from "$reflector/reflector.svelte";',
100
104
  'import { validateInputs } from "$lib/sanitizers/validateFormats";',
101
105
  // ...Array.from(enums),
102
106
  ...treatedSchemas,
package/dist/method.d.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  import { Request } from "./request.js";
2
2
  import type { ReflectorOperation } from "./types/types.js";
3
- import { SchemaProp } from "./property.js";
3
+ import { PrimitiveProp } from "./primitive-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: SchemaProp[];
10
- headers: SchemaProp[];
11
- querys: SchemaProp[];
12
- cookies: SchemaProp[];
9
+ paths: PrimitiveProp[];
10
+ headers: PrimitiveProp[];
11
+ querys: PrimitiveProp[];
12
+ cookies: PrimitiveProp[];
13
13
  constructor(params: {
14
14
  operation: ReflectorOperation;
15
15
  moduleName: string;
package/dist/method.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Request } from "./request.js";
2
- import { createDangerMessage, testeEndpoint, treatByUppercase } from "./helpers/helpers.js";
3
- import { SchemaProp } from "./property.js";
2
+ import { createDangerMessage, getFullEndpoint, treatByUppercase } from "./helpers/helpers.js";
3
+ import { PrimitiveProp } from "./primitive-property.js";
4
4
  export class Method {
5
5
  name;
6
6
  // zodProperties: Property[];
@@ -31,27 +31,18 @@ export class Method {
31
31
  const { required, name, description, schema, in: inParam } = object;
32
32
  if ("$ref" in schema)
33
33
  continue;
34
- const zodPropertie = {
35
- name,
36
- example: schema.default,
37
- schemaObject: schema,
38
- type: schema.type,
39
- description: description ?? "",
40
- required: required || true,
41
- isEmpty: false,
42
- inParam,
43
- };
34
+ const properties = { name, required: !!required, schemaObject: schema, validator: undefined };
44
35
  if (inParam === "query") {
45
- this.querys.push(new SchemaProp(zodPropertie));
36
+ this.querys.push(new PrimitiveProp(properties));
46
37
  }
47
38
  else if (inParam === "header") {
48
- this.headers.push(new SchemaProp(zodPropertie));
39
+ this.headers.push(new PrimitiveProp(properties));
49
40
  }
50
41
  else if (inParam === "path") {
51
- this.paths.push(new SchemaProp(zodPropertie));
42
+ this.paths.push(new PrimitiveProp(properties));
52
43
  }
53
44
  else if (inParam === "cookie") {
54
- this.paths.push(new SchemaProp(zodPropertie));
45
+ this.paths.push(new PrimitiveProp(properties));
55
46
  }
56
47
  }
57
48
  }
@@ -76,7 +67,7 @@ export class Method {
76
67
  if (this.request.attributeType === "list") {
77
68
  beforeResponse.push(`const {data: { data }} = response`, "\n\n", `this.list = data`);
78
69
  const inside = `
79
- const response = await repo.api.get<{data: ${this.request.responseType}}, unknown>({
70
+ const response = await repo.api.get<{data: ${this.request.responseType}Interface}, unknown>({
80
71
  endpoint,
81
72
  queryData: { ${this.gee(this.querys)} }
82
73
  })
@@ -86,9 +77,9 @@ export class Method {
86
77
  }
87
78
  else if (this.request.attributeType === "entity") {
88
79
  const entityName = treatByUppercase(this.request.responseType);
89
- beforeResponse.push(`this.${entityName} = response`);
80
+ beforeResponse.push(`this.${entityName} = new ${this.request.responseType}(response)`);
90
81
  const inside = `
91
- const response = await repo.api.get<${this.request.responseType}, unknown>({
82
+ const response = await repo.api.get<${this.request.responseType}Interface, unknown>({
92
83
  endpoint,
93
84
  })
94
85
  ${beforeResponse.join(";")}
@@ -109,7 +100,7 @@ export class Method {
109
100
  }
110
101
  const outside = ["this.loading = true", data, headers].join("\n");
111
102
  const inside = `
112
- const response = await repo.api.${this.request.apiType}<${this.request.responseType}>({
103
+ const response = await repo.api.${this.request.apiType}<${this.request.responseType}Interface>({
113
104
  endpoint,
114
105
  ${hasData ? "data," : ""}
115
106
  ${hasHeaders ? "headers," : ""}
@@ -118,8 +109,9 @@ export class Method {
118
109
  return { outside, inside };
119
110
  }
120
111
  else if (this.request.apiType === "delete") {
112
+ const diamond = this.request.responseType ? `${this.request.responseType}Interface` : "null";
121
113
  const inside = `
122
- const response = await repo.api.delete<${this.request.responseType ?? "null"}, unknown>({
114
+ const response = await repo.api.delete<${diamond}, unknown>({
123
115
  endpoint,
124
116
  })
125
117
  `;
@@ -143,7 +135,7 @@ export class Method {
143
135
  async ${this.name}(behavior: Behavior = new Behavior()) {
144
136
  const {onError, onSuccess} = behavior
145
137
  ${this.getProps()}
146
- const endpoint = ${a}${testeEndpoint(this.endpoint)}${a}
138
+ const endpoint = ${a}${getFullEndpoint(this.endpoint)}${a}
147
139
 
148
140
  ${outside}
149
141
 
@@ -151,7 +143,7 @@ export class Method {
151
143
  ${inside}
152
144
  onSuccess?.()
153
145
 
154
- return response
146
+ return new ${this.request.responseType}(response)
155
147
  } catch(e) {
156
148
  onError?.(e)
157
149
  } finally {
package/dist/module.d.ts CHANGED
@@ -15,7 +15,7 @@ export declare class Module {
15
15
  operations: ReflectorOperation[];
16
16
  path: string;
17
17
  });
18
- private buildZObject;
18
+ private buildClassProps;
19
19
  private creator;
20
20
  private getPath;
21
21
  private getAdditionalMethod;
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 { baseDir, generatedDir } from "./vars.global.js";
6
+ import { generatedDir } from "./vars.global.js";
7
7
  export class Module {
8
8
  name;
9
9
  path;
@@ -11,10 +11,6 @@ export class Module {
11
11
  src;
12
12
  imports;
13
13
  methods;
14
- // querys: SchemaProp[] = [];
15
- // paths: SchemaProp[] = [];
16
- // headers: SchemaProp[] = [];
17
- // cookies: SchemaProp[] = [];
18
14
  moduleConstructor;
19
15
  constructor(params) {
20
16
  const { name, operations, moduleName, path } = params;
@@ -22,7 +18,7 @@ export class Module {
22
18
  this.imports = new Set([
23
19
  "// AUTO GERADO. QUEM ALTERAR GOSTA DE RAPAZES!\n",
24
20
  'import repo from "$repository/main"',
25
- 'import { Behavior, build } from "$reflector/reflector.svelte";',
21
+ 'import { Behavior, build, BuildedInput } from "$reflector/reflector.svelte";',
26
22
  'import { PUBLIC_ENVIRONMENT } from "$env/static/public";',
27
23
  ]);
28
24
  this.name = capitalizeFirstLetter(name);
@@ -52,19 +48,38 @@ export class Module {
52
48
  data: this.buildFile({ moduleAttributes, moduleTypes, moduleInit, moduleClear }),
53
49
  });
54
50
  }
55
- buildZObject(props) {
56
- const bundle = new Set();
57
- const teste = props
58
- .map((prop) => {
59
- bundle.add(`${prop.name}: this${prop.isSpecial ? "" : "."}${prop.name}.value`);
60
- return `${prop.name} = ${prop.buildedValue}`;
61
- })
62
- .join(";");
63
- return `
64
- ${teste};
65
-
66
- bundle() { return { ${Array.from(bundle)} } }
51
+ buildClassProps(params) {
52
+ const { name, props } = params;
53
+ const bundle = [];
54
+ const attributes = [];
55
+ const constructorThis = [];
56
+ const interfaceBuild = [];
57
+ props.forEach((prop) => {
58
+ constructorThis.push(prop.constructorBuild());
59
+ bundle.push(prop.bundleBuild());
60
+ attributes.push(prop.classBuild());
61
+ interfaceBuild.push(prop.interfaceBuild());
62
+ });
63
+ const buildedInterface = `
64
+ interface ${name}Interface {
65
+ ${interfaceBuild.join(";")}
66
+ }
67
+ `;
68
+ const buildedClass = `
69
+ class ${name} {
70
+ ${attributes.join(";")}
71
+
72
+ constructor(params?: ${name}Interface){
73
+ ${constructorThis.join(";")}
74
+ }
75
+
76
+ bundle(){
77
+ return { ${bundle.join(",")} }
78
+ }
79
+ }
80
+
67
81
  `;
82
+ return [buildedInterface, buildedClass].join(";");
68
83
  }
69
84
  creator(params) {
70
85
  const { cookies, headers, paths, querys } = params;
@@ -73,24 +88,24 @@ export class Module {
73
88
  const moduleInit = new Set([]);
74
89
  const moduleClear = new Set([]);
75
90
  const getParams = (params) => {
76
- const { name, objets } = params;
91
+ const { name, props } = params;
77
92
  const capitalizedName = capitalizeFirstLetter(name);
78
- buildedModuleTypes.push(`class ${capitalizedName}Schema { ${this.buildZObject(objets)} }`);
79
- moduleAttributes.add(`${name} = $state(new ${capitalizedName}Schema())`);
93
+ buildedModuleTypes.push(this.buildClassProps({ props, name: capitalizedName }));
94
+ moduleAttributes.add(`${name} = $state(new ${capitalizedName}())`);
80
95
  moduleInit.add(`this.clear${capitalizeFirstLetter(capitalizedName)}()`);
81
- moduleClear.add(`clear${capitalizedName}() { this.${name} = new ${capitalizedName}Schema() }`);
96
+ moduleClear.add(`clear${capitalizedName}() { this.${name} = new ${capitalizedName}() }`);
82
97
  };
83
98
  if (querys.length > 0) {
84
- getParams({ name: "querys", objets: querys });
99
+ getParams({ name: "querys", props: querys });
85
100
  }
86
101
  if (headers.length > 0) {
87
- getParams({ name: "headers", objets: headers });
102
+ getParams({ name: "headers", props: headers });
88
103
  }
89
104
  if (paths.length > 0) {
90
- getParams({ name: "paths", objets: paths });
105
+ getParams({ name: "paths", props: paths });
91
106
  }
92
107
  if (cookies.length > 0) {
93
- getParams({ name: "cookies", objets: cookies });
108
+ getParams({ name: "cookies", props: cookies });
94
109
  }
95
110
  const form = [];
96
111
  for (const method of this.methods) {
@@ -200,6 +215,7 @@ export class Module {
200
215
  entries.add(`${bodyType}`);
201
216
  }
202
217
  if (responseType) {
218
+ entries.add(`type ${responseType}Interface`);
203
219
  entries.add(`${responseType}`);
204
220
  }
205
221
  }
@@ -0,0 +1,21 @@
1
+ import type { SchemaObject } from "./types/open-api-spec.interface.js";
2
+ export declare class PrimitiveProp {
3
+ name: string;
4
+ type: string;
5
+ isSpecial: boolean;
6
+ private readonly required;
7
+ private readonly rawType;
8
+ private readonly buildedConst;
9
+ constructor(params: {
10
+ name: string;
11
+ schemaObject: SchemaObject;
12
+ required: boolean;
13
+ validator: string | undefined;
14
+ });
15
+ private treatName;
16
+ private buildConst;
17
+ constructorBuild(): string;
18
+ classBuild(): string;
19
+ interfaceBuild(): string;
20
+ bundleBuild(): string;
21
+ }
@@ -0,0 +1,46 @@
1
+ export class PrimitiveProp {
2
+ name;
3
+ type;
4
+ isSpecial = false;
5
+ required;
6
+ rawType;
7
+ buildedConst;
8
+ constructor(params) {
9
+ const { name, schemaObject, required } = params;
10
+ const { example, type } = schemaObject;
11
+ this.name = this.treatName(name);
12
+ this.rawType = type ?? "any";
13
+ this.type = `BuildedInput<${type}>`;
14
+ this.required = required;
15
+ this.buildedConst = this.buildConst({ example, name, required, type });
16
+ }
17
+ treatName(name) {
18
+ let newName = name;
19
+ if (name.split("-").length > 1) {
20
+ this.isSpecial = true;
21
+ newName = `['${name}']`;
22
+ }
23
+ return newName;
24
+ }
25
+ buildConst(params) {
26
+ const { example, name, required, type } = params;
27
+ const sanitizedExample = type === "boolean" || type === "number" ? example : `"${example}"`;
28
+ return `
29
+ build({ key: params?.${name}, example: ${sanitizedExample}, required: ${required}})
30
+ `;
31
+ }
32
+ constructorBuild() {
33
+ return `this.${this.name} = ${this.buildedConst}`;
34
+ }
35
+ classBuild() {
36
+ const req = this.required ? "" : "?";
37
+ return `${this.name}${req}: ${this.type}`;
38
+ }
39
+ interfaceBuild() {
40
+ const req = this.required ? "" : "?";
41
+ return `${this.name}${req}: ${this.rawType}`;
42
+ }
43
+ bundleBuild() {
44
+ return `${this.name}: this.${this.name}?.value`;
45
+ }
46
+ }
@@ -1,35 +1,28 @@
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 SchemaProp {
4
- inParam: ParameterLocation;
5
- isSpecial: boolean;
6
- example: string;
7
- emptyExample: string;
1
+ import { type ReferenceObject, type SchemaObject } from "./types/open-api-spec.interface.js";
2
+ export declare class ArrayProp {
8
3
  name: string;
9
- bType: string;
10
- isRequired: boolean;
11
- reflectorType: ReflectorParamType;
12
- buildedValue: string;
13
- enums?: string;
4
+ type: string;
14
5
  constructor(params: {
15
- schemaName?: string;
16
6
  name: string;
17
7
  schemaObject: SchemaObject;
18
- type: ReflectorParamType;
19
- example: Example | undefined;
20
- required: boolean;
21
- description?: string;
22
- isEmpty: boolean;
23
- inParam: ParameterLocation;
24
- validator?: string | undefined;
8
+ schemaName: string;
25
9
  });
26
- private getBuildedValue;
27
- getEmptyExample(params: {
28
- type: ReflectorParamType;
29
- schemaObject: SchemaObject;
30
- }): string | false | 0;
31
- private getExample;
32
- private deepValidator;
33
10
  private treatName;
34
11
  private getType;
12
+ constructorBuild(): string;
13
+ classBuild(): string;
14
+ interfaceBuild(): string;
15
+ bundleBuild(): string;
16
+ }
17
+ export declare class ObjectProp {
18
+ name: string;
19
+ type: string;
20
+ private readonly required;
21
+ constructor(params: {
22
+ referenceObject: ReferenceObject;
23
+ name: string;
24
+ });
25
+ constructorBuild(): string;
26
+ classBuild(): string;
27
+ interfaceBuild(): string;
35
28
  }
package/dist/property.js CHANGED
@@ -1,113 +1,175 @@
1
- import { ReflectorInput } from "./helpers/input.js";
2
1
  import {} from "./types/open-api-spec.interface.js";
3
2
  import {} from "./types/types.js";
4
- const inputs = new ReflectorInput();
5
- export class SchemaProp {
6
- inParam;
7
- isSpecial = false;
8
- example;
9
- emptyExample;
3
+ // export class SchemaProp {
4
+ // inParam?: ParameterLocation | undefined;
5
+ // reflectorType: ReflectorParamType;
6
+ // example: string;
7
+ // emptyExample: string;
8
+ // name: string;
9
+ // buildedValue: string;
10
+ // buildedType: string;
11
+ // enums?: string;
12
+ // isRequired: boolean;
13
+ // isSpecial: boolean = false;
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
+ // validator?: string | undefined;
25
+ // }) {
26
+ // const { schemaName, name, schemaObject, type, example, required, description, isEmpty, inParam, validator } = params;
27
+ // if (schemaObject.enum) {
28
+ // this.enums = schemaObject.enum.map((e) => `'${e}'`).join("|");
29
+ // }
30
+ // this.inParam = inParam;
31
+ // this.reflectorType = schemaObject.enum ? "enum" : type;
32
+ // this.isRequired = required;
33
+ // this.name = this.treatName(name);
34
+ // this.buildedType = this.getType({ type, schemaName, schemaObject });
35
+ // this.example = `${this.getExample({ example, type, schemaObject })}`;
36
+ // this.emptyExample = `${this.getEmptyExample({ schemaObject, type: this.reflectorType })}`;
37
+ // this.buildedValue = this.getBuildedValue({
38
+ // example: this.example,
39
+ // isRequired: this.isRequired,
40
+ // type,
41
+ // bType: this.buildedType,
42
+ // enums: this.enums,
43
+ // schemaObject,
44
+ // validator,
45
+ // inParam,
46
+ // });
47
+ // }
48
+ // private getBuildedValue(params: {
49
+ // type: ReflectorParamType;
50
+ // isRequired: boolean;
51
+ // example: string;
52
+ // bType: string;
53
+ // enums: string | undefined;
54
+ // schemaObject: SchemaObject;
55
+ // validator: string | undefined;
56
+ // inParam?: ParameterLocation | undefined;
57
+ // }) {
58
+ // const { example, isRequired, type, bType, schemaObject, validator, inParam } = params;
59
+ // let content: string = "";
60
+ // const buildedValidator = validator ? `validator: ${validator}` : "";
61
+ // const buildedKey = () => {
62
+ // const example = this.getEmptyExample({ type, schemaObject });
63
+ // return inParam ? example : `params?.${this.name} ?? ${example}`;
64
+ // };
65
+ // if (type === "number" || type === "string" || type === "boolean") {
66
+ // content = `build({key: ${buildedKey()}, example: ${example}, required: ${isRequired}, ${buildedValidator}})`;
67
+ // } else if (type === "object") {
68
+ // content = `new ${bType}()`;
69
+ // } else if (type === "array") {
70
+ // content = `${this.name}Interface[]`;
71
+ // }
72
+ // return content;
73
+ // }
74
+ // private getExample(params: { example: Example | undefined; type: ReflectorParamType; schemaObject: SchemaObject }) {
75
+ // const { example, type, schemaObject } = params;
76
+ // const sanitizedExample = type === "boolean" || type === "number" ? example : `"${example}"`;
77
+ // return example ? sanitizedExample : this.getEmptyExample({ schemaObject, type });
78
+ // }
79
+ // private treatName(name: string) {
80
+ // let newName = name;
81
+ // if (name.split("-").length > 1) {
82
+ // this.isSpecial = true;
83
+ // newName = `['${name}']`;
84
+ // }
85
+ // return newName;
86
+ // }
87
+ // private getType(params: { type: ReflectorParamType; schemaName: string | undefined; schemaObject: SchemaObject }) {
88
+ // const { type, schemaName, schemaObject } = params;
89
+ // if (type === "object") {
90
+ // return `${schemaName}`;
91
+ // } else if (type === "array") {
92
+ // let name = schemaName;
93
+ // const teste = schemaObject.items;
94
+ // if (teste && "$ref" in teste) {
95
+ // const a = teste.$ref;
96
+ // name = a.split("/").at(-1);
97
+ // }
98
+ // return `${name}[]`;
99
+ // } else {
100
+ // return type as string;
101
+ // }
102
+ // }
103
+ // getEmptyExample(params: { type: ReflectorParamType; schemaObject: SchemaObject }) {
104
+ // const { schemaObject, type } = params;
105
+ // if (type === "number") {
106
+ // return 0;
107
+ // } else if (type === "boolean") {
108
+ // return false;
109
+ // } else if (schemaObject.enum) {
110
+ // return `'${schemaObject.enum[0]}'`;
111
+ // } else {
112
+ // return "''";
113
+ // }
114
+ // }
115
+ // }
116
+ export class ArrayProp {
10
117
  name;
11
- bType;
12
- isRequired;
13
- reflectorType;
14
- buildedValue;
15
- enums;
118
+ type;
16
119
  constructor(params) {
17
- const { schemaName, name, schemaObject, type, example, required, description, isEmpty, inParam, validator } = params;
18
- if (schemaObject.enum) {
19
- this.enums = schemaObject.enum.map((e) => `'${e}'`).join("|");
20
- }
21
- this.inParam = inParam;
22
- this.reflectorType = schemaObject.enum ? "enum" : type;
23
- this.isRequired = required;
120
+ const { name, schemaObject, schemaName } = params;
24
121
  this.name = this.treatName(name);
25
- this.bType = this.getType({ type, schemaName, schemaObject });
26
- this.example = `${this.getExample({ example, type, schemaObject })}`;
27
- this.emptyExample = `${this.getEmptyExample({ schemaObject, type: this.reflectorType })}`;
28
- this.buildedValue = this.getBuildedValue({
29
- example: this.example,
30
- isRequired: this.isRequired,
31
- type,
32
- bType: this.bType,
33
- enums: this.enums,
34
- schemaObject,
35
- validator,
36
- });
37
- }
38
- getBuildedValue(params) {
39
- const { example, isRequired, type, bType, enums, schemaObject, validator } = params;
40
- let content = "";
41
- const diamondType = enums ? `<${enums}>` : "";
42
- const buildedValidator = validator ? `validator: ${validator}` : "";
43
- if (type === "number" || type === "string" || type === "boolean") {
44
- content = `build${diamondType}({key: ${this.getEmptyExample({ type, schemaObject })}, example: ${example}, required: ${isRequired}, ${buildedValidator}})`;
45
- }
46
- else if (type === "object") {
47
- content = `new ${bType}()`;
48
- }
49
- else if (type === "array") {
50
- content = "[]";
51
- }
52
- return content;
53
- }
54
- getEmptyExample(params) {
55
- const { schemaObject, type } = params;
56
- if (type === "number") {
57
- return 0;
58
- }
59
- else if (type === "boolean") {
60
- return false;
61
- }
62
- else if (schemaObject.enum) {
63
- return `'${schemaObject.enum[0]}'`;
64
- }
65
- else {
66
- return "''";
67
- }
68
- }
69
- getExample(params) {
70
- const { example, type, schemaObject } = params;
71
- const sanitizedExample = type === "boolean" || type === "number" ? example : `"${example}"`;
72
- return example ? sanitizedExample : this.getEmptyExample({ schemaObject, type });
73
- }
74
- deepValidator(params) {
75
- const { name } = params;
76
- if (name === "email") {
77
- // return `z.email().default('${this.example}')`;
78
- return "Email";
79
- }
80
- else if (name === "password") {
81
- return "Password";
82
- }
83
- else {
84
- return false;
85
- }
122
+ this.type = this.getType({ schemaObject, schemaName });
86
123
  }
87
124
  treatName(name) {
88
125
  let newName = name;
89
126
  if (name.split("-").length > 1) {
90
- this.isSpecial = true;
91
127
  newName = `['${name}']`;
92
128
  }
93
129
  return newName;
94
130
  }
95
131
  getType(params) {
96
- const { type, schemaName, schemaObject } = params;
97
- if (type === "object") {
98
- return `${schemaName}`;
99
- }
100
- else if (type === "array") {
101
- let name = schemaName;
102
- const teste = schemaObject.items;
103
- if (teste && "$ref" in teste) {
104
- const a = teste.$ref;
105
- name = a.split("/").at(-1);
106
- }
107
- return `${name}[]`;
108
- }
109
- else {
110
- return type;
132
+ const { schemaObject, schemaName } = params;
133
+ let name = schemaName;
134
+ const teste = schemaObject.items;
135
+ if (teste && "$ref" in teste) {
136
+ const a = teste.$ref;
137
+ name = a.split("/").at(-1);
111
138
  }
139
+ return name;
140
+ }
141
+ constructorBuild() {
142
+ return `this.${this.name} = params?.${this.name}.map((param) => new ${this.type}(param)) ?? []`;
143
+ }
144
+ classBuild() {
145
+ return `${this.name}: ${this.type}[]`;
146
+ }
147
+ interfaceBuild() {
148
+ return `${this.name}: ${this.type}Interface[]`;
149
+ }
150
+ bundleBuild() {
151
+ return `${this.name}: this.${this.name}.map((obj) => obj.bundle())`;
152
+ }
153
+ }
154
+ export class ObjectProp {
155
+ name;
156
+ type;
157
+ required;
158
+ constructor(params) {
159
+ const { referenceObject, name } = params;
160
+ this.name = name;
161
+ this.type = referenceObject.$ref.split("/").at(-1) ?? "";
162
+ this.required = true; // tem que ver isso daí
163
+ }
164
+ constructorBuild() {
165
+ return `this.${this.name} = new ${this.type}(params?.${this.name})`;
166
+ }
167
+ classBuild() {
168
+ const req = this.required ? "" : "?";
169
+ return `${this.name}${req}: ${this.type}`;
170
+ }
171
+ interfaceBuild() {
172
+ const req = this.required ? "" : "?";
173
+ return `${this.name}${req}: ${this.type}Interface`;
112
174
  }
113
175
  }
package/dist/schema.d.ts CHANGED
@@ -1,12 +1,16 @@
1
- import { SchemaProp } from "./property.js";
1
+ import { PrimitiveProp } from "./primitive-property.js";
2
+ import { ArrayProp, ObjectProp } from "./property.js";
2
3
  import type { SchemaObject, ReferenceObject } from "./types/open-api-spec.interface.js";
3
4
  import type { FieldValidators } from "./types/types.js";
4
5
  export declare class Schema {
5
6
  name: string;
6
- properties: SchemaProp[];
7
+ primitiveProps: PrimitiveProp[];
8
+ arrayProps: ArrayProp[];
9
+ objectProps: ObjectProp[];
7
10
  schema: string;
8
11
  enums: Set<string>;
9
12
  objects: Map<string, string>;
13
+ interface: string;
10
14
  constructor(params: {
11
15
  properties: Record<string, SchemaObject | ReferenceObject>;
12
16
  name: string;
package/dist/schema.js CHANGED
@@ -1,23 +1,23 @@
1
- import { capitalizeFirstLetter, splitByUppercase, treatAndUpper, treatByUppercase } from "./helpers/helpers.js";
2
- import { SchemaProp } from "./property.js";
1
+ import { capitalizeFirstLetter } from "./helpers/helpers.js";
2
+ import { ReflectorInterface } from "./interface.js";
3
+ import { PrimitiveProp } from "./primitive-property.js";
4
+ import { ArrayProp, ObjectProp } from "./property.js";
3
5
  export class Schema {
4
6
  name;
5
- properties = [];
6
- // type: string;
7
+ primitiveProps = [];
8
+ arrayProps = [];
9
+ objectProps = [];
7
10
  schema;
8
11
  enums = new Set();
9
12
  objects = new Map();
13
+ interface;
10
14
  constructor(params) {
11
15
  const { name, properties, requireds, isEmpty, validators } = params;
12
16
  this.name = `${isEmpty ? "Empty" : ""}${name}`;
13
- const bundleParams = new Set();
14
17
  for (const [key, value] of Object.entries(properties)) {
15
18
  if ("$ref" in value || !value?.type) {
16
19
  if ("$ref" in value) {
17
- const teste = value.$ref;
18
- const object = teste.split("/").at(-1);
19
- this.objects.set(key, `${object}`);
20
- bundleParams.add(`${key}: this.${key}?.bundle()`);
20
+ this.objectProps.push(new ObjectProp({ name: key, referenceObject: value }));
21
21
  }
22
22
  continue;
23
23
  }
@@ -31,46 +31,51 @@ export class Schema {
31
31
  this.enums.add(this.getEnumConst({ enums: value.enum, schemaName: key }));
32
32
  }
33
33
  const validator = validators.get(key);
34
- bundleParams.add(`${key}: this.${key}?.value`);
35
- this.properties.push(new SchemaProp({
36
- schemaName: this.name,
37
- name: key,
38
- schemaObject: value,
39
- type: value.type,
40
- example: value.example,
41
- required,
42
- isEmpty,
43
- inParam: "path",
44
- validator,
45
- }));
46
- }
47
- const keys = this.properties
48
- .map((p) => {
49
- const keyName = `${p.name}${p.isRequired ? "" : "?"}`;
50
- let state;
51
- if (p.reflectorType === "object") {
52
- state = `$state<${p.bType}>()`;
53
- }
54
- else if (p.reflectorType === "array") {
55
- state = `$state<${p.bType}>([])`;
34
+ const type = value.type;
35
+ if (type === "object")
36
+ continue;
37
+ if (type === "array") {
38
+ this.arrayProps.push(new ArrayProp({ schemaObject: value, schemaName: this.name, name: key }));
56
39
  }
57
40
  else {
58
- state = `$state(${p.buildedValue})`;
41
+ this.primitiveProps.push(new PrimitiveProp({ name: key, schemaObject: value, required, validator }));
59
42
  }
60
- return `${keyName} = ${state}`;
61
- })
62
- .join(";\n");
63
- const buildedObjects = Array.from(this.objects)
64
- .map(([k, v]) => {
65
- return `${k} = $state(new ${v}())`;
66
- })
67
- .join(";\n");
68
- this.schema = `export class ${this.name} {
69
- ${keys}
70
- ${this.properties.length > 0 ? ";" : ""}
71
- ${buildedObjects}
43
+ }
44
+ const reflectorInterface = new ReflectorInterface({
45
+ name: this.name,
46
+ arrayProps: this.arrayProps,
47
+ primitiveProps: this.primitiveProps,
48
+ objectProps: this.objectProps,
49
+ });
50
+ this.interface = reflectorInterface.builded;
51
+ const constructorThis = [];
52
+ const keys = [];
53
+ const bundleParams = [];
54
+ this.primitiveProps.forEach((prop) => {
55
+ constructorThis.push(prop.constructorBuild());
56
+ bundleParams.push(prop.bundleBuild());
57
+ keys.push(prop.classBuild());
58
+ });
59
+ this.arrayProps.forEach((prop) => {
60
+ constructorThis.push(prop.constructorBuild());
61
+ keys.push(prop.classBuild());
62
+ bundleParams.push(prop.bundleBuild());
63
+ });
64
+ this.objectProps.forEach((prop) => {
65
+ constructorThis.push(prop.constructorBuild());
66
+ keys.push(prop.classBuild());
67
+ // bundleParams.push(prop.bundleBuild());
68
+ });
69
+ this.schema = `
70
+ export class ${this.name} {
71
+ ${keys.join(";")}
72
+
73
+ constructor(params?: ${this.name}Interface) {
74
+ ${constructorThis.join(";\n")}
75
+ }
76
+
72
77
  bundle(){
73
- return { ${Array.from(bundleParams).join(",")} }
78
+ return { ${bundleParams.join(",")} }
74
79
  }
75
80
  };`;
76
81
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-reflector",
3
- "version": "1.0.21",
3
+ "version": "1.0.22",
4
4
  "description": "Reflects zod types from openAPI schemas",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",