svelte-reflector 1.3.0 → 1.3.2

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.
package/dist/file.js CHANGED
@@ -1,4 +1,5 @@
1
- import { writeFileSync } from "node:fs";
1
+ import { mkdirSync, writeFileSync } from "node:fs";
2
+ import { dirname } from "node:path";
2
3
  import prettier from "prettier";
3
4
  export class Source {
4
5
  path;
@@ -9,11 +10,13 @@ export class Source {
9
10
  this.data = data ?? "";
10
11
  }
11
12
  async save() {
13
+ mkdirSync(dirname(this.path), { recursive: true });
12
14
  const options = await prettier.resolveConfig(process.cwd());
13
15
  const formatted = await prettier.format(this.data, { ...options, filepath: this.path });
14
16
  writeFileSync(this.path, formatted, "utf8");
15
17
  }
16
18
  async safeSave() {
19
+ mkdirSync(dirname(this.path), { recursive: true });
17
20
  writeFileSync(this.path, this.data, "utf8");
18
21
  }
19
22
  changeData(data) {
@@ -15,6 +15,8 @@ export declare class PrimitiveProp {
15
15
  private readonly example;
16
16
  private readonly fallbackExample;
17
17
  private get effectiveType();
18
+ /** Non-required fields become nullable (| null) instead of optional (?) */
19
+ private get isEffectivelyNullable();
18
20
  constructor(params: {
19
21
  name: string;
20
22
  schemaObject: SchemaObject;
@@ -14,6 +14,10 @@ export class PrimitiveProp {
14
14
  get effectiveType() {
15
15
  return this.customType ?? this.rawType;
16
16
  }
17
+ /** Non-required fields become nullable (| null) instead of optional (?) */
18
+ get isEffectivelyNullable() {
19
+ return this.isNullable || !this.required;
20
+ }
17
21
  constructor(params) {
18
22
  const { name, schemaObject, required, validator, customType, isParam, isNullable } = params;
19
23
  const { type: rawType } = schemaObject;
@@ -94,8 +98,15 @@ export class PrimitiveProp {
94
98
  return "";
95
99
  };
96
100
  const buildedExample = `params?.empty || isEmpty ? ${this.fallbackExample} : ${this.example}`;
97
- const keyExpr = this.isNullable ? `params?.data?.${name} ?? null` : `params?.data?.${name}`;
98
- const typeParam = this.isNullable ? `<${this.effectiveType} | null>` : "";
101
+ const effectivelyNullable = this.isEffectivelyNullable;
102
+ const keyExpr = effectivelyNullable ? `params?.data?.${name} ?? null` : `params?.data?.${name}`;
103
+ let typeParam = "";
104
+ if (effectivelyNullable) {
105
+ typeParam = `<${this.effectiveType} | null>`;
106
+ }
107
+ else if (this.customType) {
108
+ typeParam = `<${this.effectiveType}>`;
109
+ }
99
110
  return `
100
111
  build${typeParam}({ key: ${keyExpr}, placeholder: ${this.example}, example: ${buildedExample}, required: ${required}, ${buildedValidator()}})
101
112
  `;
@@ -107,13 +118,13 @@ export class PrimitiveProp {
107
118
  return `${this.thisDot()}${this.name} = ${this.buildedConst}`;
108
119
  }
109
120
  classBuild() {
110
- const req = (this.required || this.isNullable) ? "" : "?";
111
- const nullable = this.isNullable ? " | null" : "";
121
+ const req = (this.required || this.isEffectivelyNullable) ? "" : "?";
122
+ const nullable = this.isEffectivelyNullable ? " | null" : "";
112
123
  return `${this.name}${req}: BuildedInput<${this.effectiveType}${nullable}>`;
113
124
  }
114
125
  interfaceBuild() {
115
- const req = this.required ? "" : "?";
116
- const nullable = this.isNullable ? " | null" : "";
126
+ const req = (this.required || this.isEffectivelyNullable) ? "" : "?";
127
+ const nullable = this.isEffectivelyNullable ? " | null" : "";
117
128
  return `${this.name}${req}: ${this.effectiveType}${nullable}`;
118
129
  }
119
130
  patchBuild() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-reflector",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Reflects zod types from openAPI schemas",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",