svelte-reflector 1.1.7 → 1.1.9

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,14 +1,16 @@
1
1
  import type { SchemaObject } from "../types/open-api-spec.interface.js";
2
- type AbstractType = string | boolean | Date | number | undefined;
2
+ import type { ReflectorParamType } from "../types/types.js";
3
+ type AbstractType = string | boolean | number | undefined;
3
4
  export declare class PrimitiveProp {
4
5
  name: string;
5
6
  type: AbstractType;
6
7
  isSpecial: boolean;
7
8
  isParam: boolean;
8
9
  private readonly required;
9
- readonly rawType: string;
10
+ readonly rawType: ReflectorParamType;
10
11
  private readonly buildedConst;
11
- private readonly emptyExample;
12
+ private readonly example;
13
+ private readonly fallbackExample;
12
14
  constructor(params: {
13
15
  name: string;
14
16
  schemaObject: SchemaObject;
@@ -18,6 +20,7 @@ export declare class PrimitiveProp {
18
20
  });
19
21
  private treatName;
20
22
  private getEmptyExample;
23
+ private getExampleAndFallback;
21
24
  private buildConst;
22
25
  private thisDot;
23
26
  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,9 @@ 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
+ const example = this.rawType === "string" ? this.fallbackExample : this.example;
117
+ console.log(example);
118
+ return `readonly ${this.name} = $derived(new QueryBuilder({ key: '${this.name}', value: ${example} }))`;
108
119
  }
109
120
  updateQueryBuild() {
110
121
  return `${this.name}: (event: SvelteEvent) => changeParam({ key: '${this.name}', event })`;
@@ -2,7 +2,7 @@ import type { ArrayProp } from "../props/array.property.js";
2
2
  import type { EnumProp } from "../props/enum.property.js";
3
3
  import type { PrimitiveProp } from "../props/primitive.property.js";
4
4
  import type { OperationObject } from "./open-api-spec.interface.js";
5
- export type ReflectorParamType = "string" | "boolean" | "number" | "array" | "object" | "enum";
5
+ export type ReflectorParamType = "string" | "boolean" | "number" | "array" | "object" | "enum" | "any";
6
6
  export type ApiType = "get" | "post" | "delete" | "patch" | "put";
7
7
  export type ReflectorOperation = OperationObject & {
8
8
  apiMethod: ApiType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-reflector",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
4
4
  "description": "Reflects zod types from openAPI schemas",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",