svelte-reflector 1.0.22 → 1.0.24

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,16 @@
1
+ import type { SchemaObject } from "./types/open-api-spec.interface.js";
2
+ export declare class ArrayProp {
3
+ name: string;
4
+ type: string;
5
+ constructor(params: {
6
+ name: string;
7
+ schemaObject: SchemaObject;
8
+ schemaName: string;
9
+ });
10
+ private treatName;
11
+ private getType;
12
+ constructorBuild(): string;
13
+ classBuild(): string;
14
+ interfaceBuild(): string;
15
+ bundleBuild(): string;
16
+ }
@@ -0,0 +1,38 @@
1
+ export class ArrayProp {
2
+ name;
3
+ type;
4
+ constructor(params) {
5
+ const { name, schemaObject, schemaName } = params;
6
+ this.name = this.treatName(name);
7
+ this.type = this.getType({ schemaObject, schemaName });
8
+ }
9
+ treatName(name) {
10
+ let newName = name;
11
+ if (name.split("-").length > 1) {
12
+ newName = `['${name}']`;
13
+ }
14
+ return newName;
15
+ }
16
+ getType(params) {
17
+ const { schemaObject, schemaName } = params;
18
+ let name = schemaName;
19
+ const teste = schemaObject.items;
20
+ if (teste && "$ref" in teste) {
21
+ const a = teste.$ref;
22
+ name = a.split("/").at(-1);
23
+ }
24
+ return name;
25
+ }
26
+ constructorBuild() {
27
+ return `this.${this.name} = params?.${this.name}.map((param) => new ${this.type}(param)) ?? []`;
28
+ }
29
+ classBuild() {
30
+ return `${this.name}: ${this.type}[]`;
31
+ }
32
+ interfaceBuild() {
33
+ return `${this.name}: ${this.type}Interface[]`;
34
+ }
35
+ bundleBuild() {
36
+ return `${this.name}: this.${this.name}.map((obj) => obj.bundle())`;
37
+ }
38
+ }
@@ -1,5 +1,6 @@
1
+ import type { ArrayProp } from "./array.property.js";
2
+ import type { ObjectProp } from "./object.property.js";
1
3
  import type { PrimitiveProp } from "./primitive-property.js";
2
- import type { ArrayProp, ObjectProp } from "./property.js";
3
4
  export declare class ReflectorInterface {
4
5
  builded: string;
5
6
  constructor(params: {
package/dist/method.js CHANGED
@@ -63,11 +63,12 @@ export class Method {
63
63
  buildCallMethod() {
64
64
  const beforeResponse = [];
65
65
  // const props = this.getProps();
66
+ const diamond = this.request.responseType ? `${this.request.responseType}Interface` : "null";
66
67
  if (this.request.apiType === "get") {
67
68
  if (this.request.attributeType === "list") {
68
69
  beforeResponse.push(`const {data: { data }} = response`, "\n\n", `this.list = data`);
69
70
  const inside = `
70
- const response = await repo.api.get<{data: ${this.request.responseType}Interface}, unknown>({
71
+ const response = await repo.api.get<{data: ${diamond}}, unknown>({
71
72
  endpoint,
72
73
  queryData: { ${this.gee(this.querys)} }
73
74
  })
@@ -76,10 +77,10 @@ export class Method {
76
77
  return { inside, outside: "" };
77
78
  }
78
79
  else if (this.request.attributeType === "entity") {
79
- const entityName = treatByUppercase(this.request.responseType);
80
+ const entityName = treatByUppercase(this.request.responseType ?? "");
80
81
  beforeResponse.push(`this.${entityName} = new ${this.request.responseType}(response)`);
81
82
  const inside = `
82
- const response = await repo.api.get<${this.request.responseType}Interface, unknown>({
83
+ const response = await repo.api.get<${diamond}, unknown>({
83
84
  endpoint,
84
85
  })
85
86
  ${beforeResponse.join(";")}
@@ -100,7 +101,7 @@ export class Method {
100
101
  }
101
102
  const outside = ["this.loading = true", data, headers].join("\n");
102
103
  const inside = `
103
- const response = await repo.api.${this.request.apiType}<${this.request.responseType}Interface>({
104
+ const response = await repo.api.${this.request.apiType}<${diamond}>({
104
105
  endpoint,
105
106
  ${hasData ? "data," : ""}
106
107
  ${hasHeaders ? "headers," : ""}
@@ -109,7 +110,6 @@ export class Method {
109
110
  return { outside, inside };
110
111
  }
111
112
  else if (this.request.apiType === "delete") {
112
- const diamond = this.request.responseType ? `${this.request.responseType}Interface` : "null";
113
113
  const inside = `
114
114
  const response = await repo.api.delete<${diamond}, unknown>({
115
115
  endpoint,
@@ -124,12 +124,13 @@ export class Method {
124
124
  const { inside, outside } = this.buildCallMethod();
125
125
  if (this.name === "list")
126
126
  this.name = "listAll";
127
- const hasProprierties = this.querys.length > 0;
128
- if (!hasProprierties && this.request.apiType === "delete") {
129
- createDangerMessage(`${this.name} não vai funcionar, pois não aceita parâmetros na requisição.`);
130
- }
127
+ // const hasProprierties = this.querys.length > 0;
128
+ // if (!hasProprierties && this.request.apiType === "delete") {
129
+ // createDangerMessage(`${this.name} não vai funcionar, pois não aceita parâmetros na requisição.`);
130
+ // }
131
131
  const description = this.buildDescription();
132
132
  const a = "`";
133
+ const methodReturn = this.request.responseType ? `new ${this.request.responseType}(response)` : "null";
133
134
  return `
134
135
  ${description}
135
136
  async ${this.name}(behavior: Behavior = new Behavior()) {
@@ -143,7 +144,7 @@ export class Method {
143
144
  ${inside}
144
145
  onSuccess?.()
145
146
 
146
- return new ${this.request.responseType}(response)
147
+ return ${methodReturn}
147
148
  } catch(e) {
148
149
  onError?.(e)
149
150
  } finally {
package/dist/module.js CHANGED
@@ -23,22 +23,21 @@ export class Module {
23
23
  ]);
24
24
  this.name = capitalizeFirstLetter(name);
25
25
  this.path = path;
26
- const methods = operations.map((operation) => {
26
+ this.methods = operations.map((operation) => {
27
27
  return new Method({
28
28
  operation,
29
29
  moduleName: this.name,
30
30
  });
31
31
  });
32
- // não vão entrar metodos que não tiverem uma resposta tipada
33
- this.methods = methods.filter((op) => {
34
- const responseTypeOk = op.request.responseType;
35
- if (op.request.apiType === "delete")
36
- return true;
37
- if (!responseTypeOk) {
38
- createDangerMessage(`Método [ ${op.name} ] do módulo [ ${this.moduleName} ] sem tipagem na resposta.`);
39
- }
40
- return responseTypeOk;
41
- });
32
+ // // não vão entrar metodos que não tiverem uma resposta tipada
33
+ // this.methods = methods.filter((op) => {
34
+ // const responseTypeOk = op.request.responseType;
35
+ // if (op.request.apiType === "delete") return true;
36
+ // if (!responseTypeOk) {
37
+ // createDangerMessage(`Método [ ${op.name} ] do módulo [ ${this.moduleName} ] sem tipagem na resposta.`);
38
+ // }
39
+ // return responseTypeOk;
40
+ // });
42
41
  const { cookies, headers, paths, querys } = this.getParameters();
43
42
  const { moduleAttributes, moduleTypes, moduleInit, moduleClear, form } = this.creator({ cookies, headers, paths, querys });
44
43
  this.moduleConstructor = this.buildConstructor(form);
@@ -117,7 +116,7 @@ export class Module {
117
116
  });
118
117
  }
119
118
  if (attributeType === "entity") {
120
- const entityName = treatByUppercase(method.request.responseType);
119
+ const entityName = treatByUppercase(method.request.responseType ?? "");
121
120
  moduleAttributes.add(`${entityName} = $state<${responseType} | undefined>()`);
122
121
  moduleInit.add(`this.clear${capitalizeFirstLetter(entityName)}()`);
123
122
  moduleClear.add(`clear${capitalizeFirstLetter(entityName)}() { this.${entityName} = undefined }`);
@@ -0,0 +1,14 @@
1
+ import type { ReferenceObject } from "./types/open-api-spec.interface.js";
2
+ export declare class ObjectProp {
3
+ name: string;
4
+ type: string;
5
+ private readonly required;
6
+ constructor(params: {
7
+ referenceObject: ReferenceObject;
8
+ name: string;
9
+ });
10
+ constructorBuild(): string;
11
+ classBuild(): string;
12
+ interfaceBuild(): string;
13
+ bundleBuild(): string;
14
+ }
@@ -0,0 +1,25 @@
1
+ export class ObjectProp {
2
+ name;
3
+ type;
4
+ required;
5
+ constructor(params) {
6
+ const { referenceObject, name } = params;
7
+ this.name = name;
8
+ this.type = referenceObject.$ref.split("/").at(-1) ?? "";
9
+ this.required = true; // tem que ver isso daí
10
+ }
11
+ constructorBuild() {
12
+ return `this.${this.name} = new ${this.type}(params?.${this.name})`;
13
+ }
14
+ classBuild() {
15
+ const req = this.required ? "" : "?";
16
+ return `${this.name}${req}: ${this.type}`;
17
+ }
18
+ interfaceBuild() {
19
+ const req = this.required ? "" : "?";
20
+ return `${this.name}${req}: ${this.type}Interface`;
21
+ }
22
+ bundleBuild() {
23
+ return `${this.name}: this.${this.name}.bundle()`;
24
+ }
25
+ }
@@ -14,6 +14,8 @@ export declare class PrimitiveProp {
14
14
  });
15
15
  private treatName;
16
16
  private buildConst;
17
+ private getEmptyExample;
18
+ private thisDot;
17
19
  constructorBuild(): string;
18
20
  classBuild(): string;
19
21
  interfaceBuild(): string;
@@ -12,7 +12,7 @@ export class PrimitiveProp {
12
12
  this.rawType = type ?? "any";
13
13
  this.type = `BuildedInput<${type}>`;
14
14
  this.required = required;
15
- this.buildedConst = this.buildConst({ example, name, required, type });
15
+ this.buildedConst = this.buildConst({ example, name: this.name, required, type });
16
16
  }
17
17
  treatName(name) {
18
18
  let newName = name;
@@ -29,8 +29,26 @@ export class PrimitiveProp {
29
29
  build({ key: params?.${name}, example: ${sanitizedExample}, required: ${required}})
30
30
  `;
31
31
  }
32
+ getEmptyExample(params) {
33
+ const { schemaObject, type } = params;
34
+ if (type === "number") {
35
+ return 0;
36
+ }
37
+ else if (type === "boolean") {
38
+ return false;
39
+ }
40
+ else if (schemaObject.enum) {
41
+ return `'${schemaObject.enum[0]}'`;
42
+ }
43
+ else {
44
+ return "''";
45
+ }
46
+ }
47
+ thisDot() {
48
+ return `this${this.isSpecial ? "" : "."}`;
49
+ }
32
50
  constructorBuild() {
33
- return `this.${this.name} = ${this.buildedConst}`;
51
+ return `${this.thisDot()}${this.name} = ${this.buildedConst}`;
34
52
  }
35
53
  classBuild() {
36
54
  const req = this.required ? "" : "?";
@@ -41,6 +59,15 @@ export class PrimitiveProp {
41
59
  return `${this.name}${req}: ${this.rawType}`;
42
60
  }
43
61
  bundleBuild() {
44
- return `${this.name}: this.${this.name}?.value`;
62
+ return `${this.name}: ${this.thisDot()}${this.name}?.value`;
45
63
  }
46
64
  }
65
+ // class Teste {
66
+ // ["x-two-factor-code"]: string;
67
+ // constructor() {
68
+ // this["x-two-factor-code"] = "aaaaaaa";
69
+ // }
70
+ // bundle() {
71
+ // return { ["x-two-factor-code"]: this["x-two-factor-code"] };
72
+ // }
73
+ // }
@@ -1,28 +1 @@
1
- import { type ReferenceObject, type SchemaObject } from "./types/open-api-spec.interface.js";
2
- export declare class ArrayProp {
3
- name: string;
4
- type: string;
5
- constructor(params: {
6
- name: string;
7
- schemaObject: SchemaObject;
8
- schemaName: string;
9
- });
10
- private treatName;
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;
28
- }
1
+ export {};
package/dist/property.js CHANGED
@@ -113,63 +113,3 @@ import {} from "./types/types.js";
113
113
  // }
114
114
  // }
115
115
  // }
116
- export class ArrayProp {
117
- name;
118
- type;
119
- constructor(params) {
120
- const { name, schemaObject, schemaName } = params;
121
- this.name = this.treatName(name);
122
- this.type = this.getType({ schemaObject, schemaName });
123
- }
124
- treatName(name) {
125
- let newName = name;
126
- if (name.split("-").length > 1) {
127
- newName = `['${name}']`;
128
- }
129
- return newName;
130
- }
131
- getType(params) {
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);
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`;
174
- }
175
- }
package/dist/request.d.ts CHANGED
@@ -5,7 +5,7 @@ export declare class Request {
5
5
  readonly attributeType: ReflectorRequestType;
6
6
  readonly apiType: ApiType;
7
7
  bodyType?: string;
8
- responseType?: string;
8
+ responseType: string | null;
9
9
  parameters: ParameterObject[];
10
10
  constructor(operation: ReflectorOperation);
11
11
  private getTypeFromRequestBody;
package/dist/request.js CHANGED
@@ -20,8 +20,7 @@ export class Request {
20
20
  if (body)
21
21
  this.bodyType = body;
22
22
  const response = this.getTypeFromResponses(operation.responses);
23
- if (response)
24
- this.responseType = response;
23
+ this.responseType = response ?? null;
25
24
  this.attributeType = this.inferAttributeType(operation) ?? "other";
26
25
  }
27
26
  // ============= Derivações principais =======================================
package/dist/schema.d.ts CHANGED
@@ -1,5 +1,6 @@
1
+ import { ArrayProp } from "./array.property.js";
2
+ import { ObjectProp } from "./object.property.js";
1
3
  import { PrimitiveProp } from "./primitive-property.js";
2
- import { ArrayProp, ObjectProp } from "./property.js";
3
4
  import type { SchemaObject, ReferenceObject } from "./types/open-api-spec.interface.js";
4
5
  import type { FieldValidators } from "./types/types.js";
5
6
  export declare class Schema {
package/dist/schema.js CHANGED
@@ -1,7 +1,8 @@
1
+ import { ArrayProp } from "./array.property.js";
1
2
  import { capitalizeFirstLetter } from "./helpers/helpers.js";
2
3
  import { ReflectorInterface } from "./interface.js";
4
+ import { ObjectProp } from "./object.property.js";
3
5
  import { PrimitiveProp } from "./primitive-property.js";
4
- import { ArrayProp, ObjectProp } from "./property.js";
5
6
  export class Schema {
6
7
  name;
7
8
  primitiveProps = [];
@@ -64,7 +65,7 @@ export class Schema {
64
65
  this.objectProps.forEach((prop) => {
65
66
  constructorThis.push(prop.constructorBuild());
66
67
  keys.push(prop.classBuild());
67
- // bundleParams.push(prop.bundleBuild());
68
+ bundleParams.push(prop.bundleBuild());
68
69
  });
69
70
  this.schema = `
70
71
  export class ${this.name} {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-reflector",
3
- "version": "1.0.22",
3
+ "version": "1.0.24",
4
4
  "description": "Reflects zod types from openAPI schemas",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",