svelte-reflector 1.1.7 → 1.1.8

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,5 +1,5 @@
1
1
  import type { SchemaObject } from "../types/open-api-spec.interface.js";
2
- type AbstractType = string | boolean | Date | number | undefined;
2
+ type AbstractType = string | boolean | number | undefined;
3
3
  export declare class PrimitiveProp {
4
4
  name: string;
5
5
  type: AbstractType;
@@ -8,7 +8,8 @@ export declare class PrimitiveProp {
8
8
  private readonly required;
9
9
  readonly rawType: string;
10
10
  private readonly buildedConst;
11
- private readonly emptyExample;
11
+ private readonly example;
12
+ private readonly fallbackExample;
12
13
  constructor(params: {
13
14
  name: string;
14
15
  schemaObject: SchemaObject;
@@ -18,6 +19,7 @@ export declare class PrimitiveProp {
18
19
  });
19
20
  private treatName;
20
21
  private getEmptyExample;
22
+ private getExampleAndFallback;
21
23
  private buildConst;
22
24
  private thisDot;
23
25
  constructorBuild(): string;
@@ -1,3 +1,4 @@
1
+ // type FallbackExample = string | false | number;
1
2
  export class PrimitiveProp {
2
3
  name;
3
4
  type;
@@ -6,25 +7,28 @@ export class PrimitiveProp {
6
7
  required;
7
8
  rawType;
8
9
  buildedConst;
9
- emptyExample;
10
+ example;
11
+ fallbackExample;
10
12
  constructor(params) {
11
13
  const { name, schemaObject, required, validator, isParam } = params;
12
14
  const { type: rawType } = schemaObject;
13
15
  const type = rawType ?? "string";
14
- this.emptyExample = this.getEmptyExample({ type, schemaObject });
15
- const example = schemaObject.example ?? schemaObject.default;
16
- if (isParam) {
17
- if (name === "limit") {
18
- console.log(example);
19
- }
20
- }
16
+ const { emptyExample, example } = this.getExampleAndFallback({ schemaObject, type });
17
+ this.example = example;
18
+ this.fallbackExample = emptyExample;
21
19
  const buildedType = type;
20
+ // if (name === "limit") {
21
+ // console.log(example);
22
+ // }
23
+ // if (isParam) {
24
+ // console.log(name, example);
25
+ // }
22
26
  this.name = this.treatName(name);
23
27
  this.rawType = type ?? "any";
24
28
  this.type = `BuildedInput<${buildedType}>`;
25
29
  this.required = required;
26
30
  this.isParam = !!isParam;
27
- this.buildedConst = this.buildConst({ example, name: this.name, required, type, validator, emptyExample: this.emptyExample });
31
+ this.buildedConst = this.buildConst({ example, name: this.name, required, type, validator, emptyExample: this.example });
28
32
  }
29
33
  treatName(name) {
30
34
  let newName = name;
@@ -49,8 +53,27 @@ export class PrimitiveProp {
49
53
  return "''";
50
54
  }
51
55
  }
56
+ getExampleAndFallback(params) {
57
+ const { schemaObject, type } = params;
58
+ const example = schemaObject.example ?? schemaObject.default;
59
+ const emptyExample = this.getEmptyExample({ type, schemaObject });
60
+ if (!example)
61
+ return {
62
+ example: emptyExample,
63
+ emptyExample,
64
+ };
65
+ if (type === "string")
66
+ return {
67
+ example: `"${example}"`,
68
+ emptyExample,
69
+ };
70
+ return {
71
+ example,
72
+ emptyExample,
73
+ };
74
+ }
52
75
  buildConst(params) {
53
- const { example, name, required, type, validator, emptyExample } = params;
76
+ const { name, required, type, validator } = params;
54
77
  const getValidator = (type) => {
55
78
  if (type === "string") {
56
79
  return "emptyString";
@@ -67,23 +90,9 @@ export class PrimitiveProp {
67
90
  }
68
91
  return "";
69
92
  };
70
- const sanitizedExample = () => {
71
- if (this.isParam || !example) {
72
- return emptyExample;
73
- }
74
- if (type === "string") {
75
- return `"${example}"`;
76
- }
77
- return example;
78
- };
79
- const buildedExample = () => {
80
- if (this.emptyExample === sanitizedExample()) {
81
- return sanitizedExample();
82
- }
83
- return `params?.empty || isEmpty ? ${this.emptyExample} : ${sanitizedExample()}`;
84
- };
93
+ const buildedExample = `params?.empty || isEmpty ? ${this.example} : ${this.fallbackExample}`;
85
94
  return `
86
- build({ key: params?.data?.${name}, placeholder: ${sanitizedExample()}, example: ${buildedExample()}, required: ${required}, ${buildedValidator()}})
95
+ build({ key: params?.data?.${name}, placeholder: ${this.fallbackExample}, example: ${buildedExample}, required: ${required}, ${buildedValidator()}})
87
96
  `;
88
97
  }
89
98
  thisDot() {
@@ -104,7 +113,7 @@ export class PrimitiveProp {
104
113
  return `readonly ${this.name} = $derived.by(() => '${this.name}' in page.params ? page.params.${this.name} : mockedParams.${this.name}) as string | null;`;
105
114
  }
106
115
  queryBuild() {
107
- return `readonly ${this.name} = $derived(new QueryBuilder({ key: '${this.name}', value: ${this.emptyExample} }))`;
116
+ return `readonly ${this.name} = $derived(new QueryBuilder({ key: '${this.name}', value: ${this.example} }))`;
108
117
  }
109
118
  updateQueryBuild() {
110
119
  return `${this.name}: (event: SvelteEvent) => changeParam({ key: '${this.name}', event })`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-reflector",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "description": "Reflects zod types from openAPI schemas",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",