svelte-reflector 1.0.2 → 1.0.4

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.
@@ -1,4 +1,4 @@
1
1
  import "dotenv/config";
2
2
  export declare function reflector(manual?: boolean): Promise<{
3
3
  name: string;
4
- } | undefined>;
4
+ }>;
@@ -53,12 +53,12 @@ export async function reflector(manual = false) {
53
53
  const { components, paths } = data;
54
54
  if (!components) {
55
55
  console.warn("[reflector] OpenAPI sem components; abortando.");
56
- return { name: "vite-plugin-generate-doc" };
56
+ return breakReflector();
57
57
  }
58
58
  const r = new Reflector({ components, paths });
59
59
  r.build();
60
60
  r.localSave(data);
61
- breakReflector();
61
+ return breakReflector();
62
62
  }
63
63
  function breakReflector() {
64
64
  return {
@@ -1,6 +1,7 @@
1
1
  export declare function stripState(attr: string): string;
2
2
  export declare function toCamelCase(str: string): string;
3
3
  export declare function sanitizeKey(name: string): string;
4
+ export declare function sanitizeNumber(texto: string): string;
4
5
  export declare function capitalizeFirstLetter(text: string): string;
5
6
  export declare function splitByUppercase(text: string): string[];
6
7
  export declare function createDangerMessage(text: string): void;
@@ -22,6 +22,9 @@ export function sanitizeKey(name) {
22
22
  }
23
23
  return toCamelCase(name);
24
24
  }
25
+ export function sanitizeNumber(texto) {
26
+ return texto.replace(/["']/g, "");
27
+ }
25
28
  export function capitalizeFirstLetter(text) {
26
29
  if (!text)
27
30
  return "";
package/dist/main.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import path from "node:path";
2
2
  import fs from "node:fs";
3
3
  import { Source } from "./file.js";
4
- import { getEndpointAndModuleName } from "./helpers/helpers.js";
4
+ import { getEndpointAndModuleName, splitByUppercase } from "./helpers/helpers.js";
5
5
  import { Schema } from "./schema.js";
6
6
  import { Module } from "./module.js";
7
7
  const methods = ["get", "patch", "post", "put", "delete"];
@@ -12,7 +12,7 @@ export class Reflector {
12
12
  generatedDir = `${this.dir}/reflector`;
13
13
  localDoc = new Source({ path: path.resolve(process.cwd(), `${this.dir}/backup.json`) });
14
14
  src = new Source({ path: path.resolve(process.cwd(), this.generatedDir) });
15
- schemaFile = new Source({ path: path.resolve(process.cwd(), `${this.dir}/schemas.ts`) });
15
+ schemaFile = new Source({ path: path.resolve(process.cwd(), `${this.generatedDir}/schemas.ts`) });
16
16
  files;
17
17
  schemas;
18
18
  modules;
@@ -54,9 +54,11 @@ export class Reflector {
54
54
  if (!object[method])
55
55
  continue;
56
56
  operations.push({ ...object[method], apiMethod: method });
57
- const tags = object[method].tags;
58
- if (!entity && tags) {
59
- entity = tags.join("").split("/").join("");
57
+ if (!entity) {
58
+ const teste = object[method].operationId.split("_")[0];
59
+ const x = splitByUppercase(teste);
60
+ const aaa = x.filter((y) => y !== "Controller");
61
+ entity = aaa.join("");
60
62
  }
61
63
  }
62
64
  if (!entity)
package/dist/module.js CHANGED
@@ -25,10 +25,15 @@ export class Module {
25
25
  });
26
26
  // não vão entrar metodos que não tiverem uma resposta tipada
27
27
  this.methods = methods.filter((op) => {
28
- if (!op.request.responseType) {
28
+ const responseTypeOk = op.request.responseType;
29
+ const propertiesOk = op.zodProperties.length > 0;
30
+ if (!responseTypeOk) {
29
31
  createDangerMessage(`Método [ ${op.name} ] do módulo [ ${this.moduleName} ] sem tipagem na resposta.`);
30
32
  }
31
- return op.request.responseType;
33
+ else if (!propertiesOk) {
34
+ createDangerMessage(`Método [ ${op.name} ] do módulo [ ${this.moduleName} ] com tipagem incorreta.`);
35
+ }
36
+ return responseTypeOk && propertiesOk;
32
37
  });
33
38
  this.parameters = this.getParameters();
34
39
  const { moduleAtributes, moduleTypes } = this.creator();
@@ -54,12 +59,11 @@ export class Module {
54
59
  type: bodyType,
55
60
  });
56
61
  }
57
- // console.warn(method);
58
- console.warn(attributeType);
59
62
  if (attributeType === "entity") {
60
63
  moduleAtributes.add(`entity = $state<${responseType} | undefined>()`);
61
64
  }
62
65
  else if (attributeType === "list") {
66
+ this.imports.push(`import z from "zod";`);
63
67
  moduleAtributes.add(`list = $state<${responseType}[]>([])`);
64
68
  }
65
69
  }
@@ -67,11 +71,13 @@ export class Module {
67
71
  for (const f of form) {
68
72
  formSet.add(`${f.name}: repo.newForm(${f.type}Schema)`);
69
73
  }
70
- moduleAtributes.add(`
74
+ if (formSet.size > 0) {
75
+ moduleAtributes.add(`
71
76
  forms = $state({
72
77
  ${Array.from(formSet)}
73
78
  })
74
79
  `);
80
+ }
75
81
  return {
76
82
  moduleAtributes: Array.from(moduleAtributes),
77
83
  moduleTypes: buildedModuleTypes,
@@ -113,7 +119,7 @@ export class Module {
113
119
  const cleanEntries = Array.from(entries).filter((x) => x != "type any");
114
120
  if (cleanEntries.length === 0)
115
121
  return "";
116
- return `import { ${cleanEntries} } from '$api/schemas';`;
122
+ return `import { ${cleanEntries} } from '$reflector/schemas';`;
117
123
  }
118
124
  buildClass(modulesAttributes) {
119
125
  const initAssignments = modulesAttributes.map((attr) => `this.${stripState(attr)}`).join(";");
@@ -136,7 +142,6 @@ export class Module {
136
142
  buildFile(modulesAttributes, moduleTypes) {
137
143
  return `
138
144
  ${this.imports.join(";")}
139
- import z from "zod";
140
145
  ${this.buildImports()}
141
146
 
142
147
  ${moduleTypes.join(";")}
package/dist/property.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { sanitizeNumber } from "./helpers/helpers.js";
1
2
  import { ReflectorInput } from "./helpers/input.js";
2
3
  const inputs = new ReflectorInput();
3
4
  export class ZodProperty {
@@ -64,13 +65,14 @@ export class ZodProperty {
64
65
  return `${x}.default(${this.example})`;
65
66
  case "number": {
66
67
  const number = JSON.stringify(this.example) ?? 1;
67
- return `${x}.default(${number})`;
68
+ return `${x}.default(${sanitizeNumber(number)})`;
68
69
  }
69
70
  case "array": {
70
- if (!value.items || !("$ref" in value.items))
71
- return `${x}.default([])`;
71
+ if (!value.items || !("$ref" in value.items)) {
72
+ return `${this.name}: z.${this.type}(z.${value.items?.type || "string"}())${this.isNullable()}.default([])`;
73
+ }
72
74
  const dto = this.getDtoName(value.items.$ref);
73
- return `${this.name}: z.array(${dto}Schema).default(Array(10).fill(${dto}Schema.parse({})))`;
75
+ return `${this.name}: z.array(${dto}Schema).default(new Array(10).fill(${dto}Schema.parse({})))`;
74
76
  }
75
77
  case "object":
76
78
  return `${this.name}: z.any()`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-reflector",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Reflects zod types from openAPI schemas",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",