svelte-reflector 1.0.14 → 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.
@@ -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
@@ -81,10 +81,12 @@ export class Reflector {
81
81
  return modules;
82
82
  }
83
83
  build() {
84
+ const enums = new Set();
84
85
  const treatedSchemas = this.schemas.map((s) => {
86
+ s.enums.forEach((en) => enums.add(en));
85
87
  return s.schema;
86
88
  });
87
- const teste = `
89
+ const buildFunction = `
88
90
  function build<T>(params: { key?: T; example: T; required: boolean }) {
89
91
  const { example, required, key } = params;
90
92
 
@@ -96,7 +98,12 @@ export class Reflector {
96
98
  } as FieldSetup<T>
97
99
  }
98
100
  `;
99
- this.schemaFile.changeData(["import type { FieldSetup } from '$repository/types';", teste, ...treatedSchemas].join("\n\n"));
101
+ this.schemaFile.changeData([
102
+ "import type { FieldSetup } from '$repository/types';",
103
+ buildFunction,
104
+ // ...Array.from(enums),
105
+ ...treatedSchemas,
106
+ ].join("\n\n"));
100
107
  this.schemaFile.save();
101
108
  this.typesSrc.changeData("export class Behavior { onError?: (e) => void; onSuccess?: () => void }");
102
109
  this.typesSrc.save();
package/dist/method.js CHANGED
@@ -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
@@ -21,6 +21,7 @@ export declare class Module {
21
21
  path: string;
22
22
  dir: string;
23
23
  });
24
+ private buildZObject;
24
25
  private creator;
25
26
  private getPath;
26
27
  private getAdditionalMethod;
package/dist/module.js CHANGED
@@ -55,34 +55,38 @@ export class Module {
55
55
  data: this.buildFile({ moduleAttributes, moduleTypes, moduleInit, moduleClear }),
56
56
  });
57
57
  }
58
- // private buildZObject(props: SchemaProp[]) {
59
- // const teste = `z.object({${props.map((p) => p.buildedProp)}})`;
60
- // return teste;
61
- // }
58
+ buildZObject(props) {
59
+ const teste = props
60
+ .map((prop) => {
61
+ return `${prop.name} = $state(${prop.emptyExample})`;
62
+ })
63
+ .join(";");
64
+ return teste;
65
+ }
62
66
  creator() {
63
67
  const buildedModuleTypes = [];
64
68
  const moduleAttributes = new Set().add("loading = $state<boolean>(false)");
65
69
  const moduleInit = new Set([]);
66
70
  const moduleClear = new Set([]);
67
- const getXablau = (params) => {
71
+ const getParams = (params) => {
68
72
  const { name, objets } = params;
69
73
  const capitalizedName = capitalizeFirstLetter(name);
70
- // buildedModuleTypes.push(`const ${capitalizedName}Schema = ${this.buildZObject(objets)}`);
71
- 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())`);
72
76
  moduleInit.add(`this.clear${capitalizeFirstLetter(capitalizedName)}()`);
73
77
  moduleClear.add(`clear${capitalizedName}() { this.${name} = new ${capitalizedName}Schema() }`);
74
78
  };
75
79
  if (this.querys.length > 0) {
76
- getXablau({ name: "querys", objets: this.querys });
80
+ getParams({ name: "querys", objets: this.querys });
77
81
  }
78
82
  if (this.headers.length > 0) {
79
- getXablau({ name: "headers", objets: this.headers });
83
+ getParams({ name: "headers", objets: this.headers });
80
84
  }
81
85
  if (this.paths.length > 0) {
82
- getXablau({ name: "paths", objets: this.paths });
86
+ getParams({ name: "paths", objets: this.paths });
83
87
  }
84
88
  if (this.cookies.length > 0) {
85
- getXablau({ name: "cookies", objets: this.cookies });
89
+ getParams({ name: "cookies", objets: this.cookies });
86
90
  }
87
91
  const form = [];
88
92
  for (const method of this.methods) {
@@ -192,10 +196,7 @@ export class Module {
192
196
  entries.add(`${bodyType}`);
193
197
  }
194
198
  if (responseType) {
195
- if (apiType === "delete") {
196
- entries.add(`${responseType}`);
197
- }
198
- entries.add(`type ${responseType}`);
199
+ entries.add(`${responseType}`);
199
200
  }
200
201
  }
201
202
  const cleanEntries = Array.from(entries).filter((x) => x != "type any");
@@ -1,13 +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
3
  export declare class SchemaProp {
4
+ inParam: ParameterLocation;
4
5
  example: string;
5
6
  emptyExample: string;
6
7
  name: string;
7
8
  bType: string;
8
9
  isRequired: boolean;
9
- paramType: ReflectorParamType;
10
+ reflectorType: ReflectorParamType;
10
11
  buildedValue: string;
12
+ enums?: string;
11
13
  constructor(params: {
12
14
  schemaName?: string;
13
15
  name: string;
@@ -20,10 +22,12 @@ export declare class SchemaProp {
20
22
  inParam: ParameterLocation;
21
23
  });
22
24
  private getBuildedValue;
23
- getEmptyExample(type: ReflectorParamType): false | 0 | "''";
25
+ getEmptyExample(params: {
26
+ type: ReflectorParamType;
27
+ schemaObject: SchemaObject;
28
+ }): string | false | 0;
24
29
  private getExample;
25
30
  private deepValidator;
26
- private isNullable;
27
31
  private treatName;
28
32
  private getType;
29
33
  }
package/dist/property.js CHANGED
@@ -1,4 +1,3 @@
1
- import { sanitizeNumber, treatenEnum } 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";
@@ -11,7 +10,7 @@ export class SchemaProp {
11
10
  // buildedProp: string;
12
11
  // description?: string;
13
12
  // required: boolean;
14
- // inParam: ParameterLocation;
13
+ inParam;
15
14
  // isEnum: boolean = false;
16
15
  // enums: string[] = [];
17
16
  example;
@@ -19,64 +18,84 @@ export class SchemaProp {
19
18
  name;
20
19
  bType;
21
20
  isRequired;
22
- paramType;
21
+ reflectorType;
23
22
  buildedValue;
23
+ enums;
24
24
  constructor(params) {
25
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
+ }
26
29
  // this.inParam = inParam;
27
30
  // this.isEnum = false;
28
- this.paramType = type;
31
+ this.inParam = inParam;
32
+ this.reflectorType = schemaObject.enum ? "enum" : type;
29
33
  this.isRequired = required;
30
34
  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)}`;
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 })}`;
34
38
  this.buildedValue = this.getBuildedValue({
35
39
  example: this.example,
36
40
  isRequired: this.isRequired,
37
- name: this.name,
38
41
  type,
39
42
  bType: this.bType,
43
+ enums: this.enums,
44
+ schemaObject,
40
45
  });
41
46
  }
42
47
  getBuildedValue(params) {
43
- const { example, isRequired, name, type, bType } = params;
48
+ const { example, isRequired, type, bType, enums, schemaObject } = params;
44
49
  let content = "";
45
- // let emptyContent: string = "";
50
+ const diamondType = enums ? `<${enums}>` : "";
46
51
  if (type === "number" || type === "string" || type === "boolean") {
47
- content = `build({key: ${this.getEmptyExample(type)}, example: ${example}, required: ${isRequired}})`;
52
+ content = `build${diamondType}({key: ${this.getEmptyExample({ type, schemaObject })}, example: ${example}, required: ${isRequired}})`;
48
53
  }
49
54
  else if (type === "object") {
50
55
  content = `new ${bType}()`;
51
- // emptyContent = `new ${bType}()`;
52
56
  }
53
57
  else if (type === "array") {
54
58
  content = "[]";
55
- // emptyContent = "[]";
56
59
  }
57
60
  return content;
58
61
  }
59
- // private getDtoName(ref: string) {
60
- // const splittedRef = ref.split("/");
61
- // const dto = splittedRef[splittedRef.length - 1];
62
- // return dto;
63
- // }
64
- getEmptyExample(type) {
62
+ getEmptyExample(params) {
63
+ const { schemaObject, type } = params;
65
64
  if (type === "number") {
66
65
  return 0;
67
66
  }
68
67
  else if (type === "boolean") {
69
68
  return false;
70
69
  }
70
+ else if (schemaObject.enum) {
71
+ return `'${schemaObject.enum[0]}'`;
72
+ }
71
73
  else {
72
74
  return "''";
73
75
  }
74
76
  }
75
77
  getExample(params) {
76
- const { example, type } = params;
78
+ const { example, type, schemaObject } = params;
79
+ // if (schemaObject.enum) {
80
+ // return `'${schemaObject.enum[0]}'`;
81
+ // }
77
82
  const sanitizedExample = type === "boolean" || type === "number" ? example : `"${example}"`;
78
- return example ? sanitizedExample : this.getEmptyExample(type);
83
+ return example ? sanitizedExample : this.getEmptyExample({ schemaObject, type });
79
84
  }
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
+ // }
80
99
  deepValidator(params) {
81
100
  const { name } = params;
82
101
  if (name === "email") {
@@ -93,19 +112,26 @@ export class SchemaProp {
93
112
  return false;
94
113
  }
95
114
  }
96
- isNullable(isRequired) {
97
- return isRequired ? "" : " | null";
98
- }
99
115
  treatName(name) {
100
- return name;
116
+ let newName = name;
117
+ if (name.split("-").length > 1) {
118
+ newName = `['${name}']`;
119
+ }
120
+ return newName;
101
121
  }
102
122
  getType(params) {
103
- const { type, schemaName } = params;
123
+ const { type, schemaName, schemaObject } = params;
104
124
  if (type === "object") {
105
125
  return `${schemaName}`;
106
126
  }
107
127
  else if (type === "array") {
108
- return `${schemaName}[]`;
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);
133
+ }
134
+ return `${name}[]`;
109
135
  }
110
136
  else {
111
137
  return type;
package/dist/schema.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { capitalizeFirstLetter, splitByUppercase, treatAndUpper, treatByUppercase } from "./helpers/helpers.js";
1
2
  import { SchemaProp } from "./property.js";
2
3
  export class Schema {
3
4
  name;
@@ -20,6 +21,7 @@ export class Schema {
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
  }
@@ -41,10 +43,10 @@ export class Schema {
41
43
  .map((p) => {
42
44
  const keyName = `${p.name}${p.isRequired ? "" : "?"}`;
43
45
  let state;
44
- if (p.paramType === "object") {
46
+ if (p.reflectorType === "object") {
45
47
  state = `$state<${p.bType}>()`;
46
48
  }
47
- else if (p.paramType === "array") {
49
+ else if (p.reflectorType === "array") {
48
50
  state = `$state<${p.bType}>([])`;
49
51
  }
50
52
  else {
@@ -58,10 +60,6 @@ export class Schema {
58
60
  return `${k} = $state(new ${v}())`;
59
61
  })
60
62
  .join(";\n");
61
- // const builded = this.properties.map((p) => ).join(";\n\n\n");
62
- if (this.name === "OwnerFinishSignUpDataDto") {
63
- console.log(properties);
64
- }
65
63
  this.schema = `export class ${this.name} {
66
64
  ${keys}
67
65
  ${this.properties.length > 0 ? ";" : ""}
@@ -70,7 +68,7 @@ export class Schema {
70
68
  }
71
69
  getEnumConst(params) {
72
70
  const { enums, schemaName } = params;
73
- const enumList = enums.map((en) => `'${en}'`);
74
- return `export const ${schemaName}EnumSchema = z.enum([${enumList}])`;
71
+ const enumList = enums.map((en) => `'${en}'`).join("|");
72
+ return `export type Enum${capitalizeFirstLetter(schemaName)} = ${enumList}`;
75
73
  }
76
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.14",
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
+ }