svelte-reflector 1.1.3 → 1.1.5

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.
@@ -47,11 +47,6 @@ export class MethodApiCallBuilder {
47
47
  const outside = [];
48
48
  if (hasData) {
49
49
  outside.push(`const data = this.forms.${method.name}.bundle()`);
50
- // outside.push(` try{
51
- // isFormValid(this.forms.create)
52
- // } finally {
53
- // this.loading = false
54
- // }`);
55
50
  }
56
51
  if (hasHeaders) {
57
52
  outside.push(`const headers = this.headers.bundle()`);
@@ -4,6 +4,7 @@ export declare class MethodGenerator {
4
4
  private readonly apiCallBuilder;
5
5
  private readonly propsBuilder;
6
6
  generate(method: Method): string;
7
+ private buildParamsType;
7
8
  private buildDescription;
8
9
  private buildMethodReturn;
9
10
  private buildAdditionalMethod;
@@ -11,13 +11,17 @@ export class MethodGenerator {
11
11
  const { inside, outside } = this.apiCallBuilder.build(method);
12
12
  const props = this.propsBuilder.build(method);
13
13
  const methodReturn = this.buildMethodReturn(method);
14
- const additionalMethod = this.buildAdditionalMethod(method);
14
+ // const additionalMethod = this.buildAdditionalMethod(method);
15
+ const pathsInfo = this.propsBuilder.getPaths(method);
16
+ const paramsType = this.buildParamsType(method, pathsInfo);
15
17
  return `
16
18
  ${description}
17
- async ${method.name}(behavior: Behavior<${method.responseTypeInterface}, ApiErrorResponse> = new Behavior()) {
18
- const {onError, onSuccess} = behavior
19
+ async ${method.name}(params?: ${paramsType}) {
19
20
 
20
- this.loading = true
21
+ const behavior = params?.behavior ?? new Behavior();
22
+ const { onError, onSuccess } = behavior;
23
+
24
+ this.loading = true;
21
25
  ${props}
22
26
  const endpoint = ${endpoint}
23
27
 
@@ -25,19 +29,29 @@ export class MethodGenerator {
25
29
 
26
30
  try {
27
31
  ${inside}
28
- onSuccess?.(response)
32
+ await onSuccess?.(response);
29
33
 
30
- return ${methodReturn}
31
- } catch(e) {
34
+ return ${methodReturn};
35
+ } catch (e) {
32
36
  const parsedError = JSON.parse((e as Error).message) as ApiErrorResponse;
33
- return onError?.(parsedError);
37
+ return await onError?.(parsedError);
34
38
  } finally {
35
- this.loading = false
39
+ this.loading = false;
36
40
  }
37
41
  }
38
42
 
39
- ${additionalMethod}
40
- `.trim();
43
+ `;
44
+ }
45
+ buildParamsType(method, paramsPaths) {
46
+ const behaviorType = `Behavior<${method.responseTypeInterface}, ApiErrorResponse>`;
47
+ if (paramsPaths) {
48
+ return `{
49
+ behavior?: ${behaviorType};${paramsPaths}
50
+ }`;
51
+ }
52
+ return `{
53
+ behavior?: ${behaviorType};
54
+ }`;
41
55
  }
42
56
  buildDescription(method) {
43
57
  return `/** ${method.description ?? ""} */`;
@@ -62,7 +76,7 @@ export class MethodGenerator {
62
76
  }
63
77
  return `
64
78
  async ${method.name}AndClear(behavior: Behavior = new Behavior()) {
65
- const data = await this.${method.name}(behavior)
79
+ const data = await this.${method.name}({behavior})
66
80
 
67
81
  if (data) {
68
82
  this.clearForms()
@@ -1,5 +1,6 @@
1
1
  import type { Method } from "../Method.js";
2
2
  export declare class MethodPropsBuilder {
3
3
  build(method: Method): string;
4
+ getPaths(method: Method): string | undefined;
4
5
  private joinNames;
5
6
  }
@@ -6,13 +6,28 @@ export class MethodPropsBuilder {
6
6
  lines.push(`const { ${this.joinNames(querys)} } = this.querys.bundle()`);
7
7
  }
8
8
  if (paths.length > 0) {
9
- lines.push(`const { ${this.joinNames(paths)} } = this.paths`);
9
+ lines.push(`const { ${this.joinNames(paths)} } = params?.paths ?? this.paths`);
10
10
  }
11
11
  if (cookies.length > 0) {
12
12
  lines.push(`const cookies = this.cookies`);
13
13
  }
14
14
  return lines.join("\n");
15
15
  }
16
+ getPaths(method) {
17
+ const { paths } = method.analyzers.props;
18
+ if (paths.length === 0)
19
+ return;
20
+ const paramsPaths = `
21
+ paths?: {
22
+ ${paths
23
+ .map((path) => {
24
+ const type = path.rawType ?? path.type;
25
+ return `${path.name}: ${type}`;
26
+ })
27
+ .join("\n")}
28
+ }`;
29
+ return paramsPaths;
30
+ }
16
31
  joinNames(props) {
17
32
  return props.map((x) => x.name).join(", ");
18
33
  }
@@ -6,7 +6,7 @@ export declare class PrimitiveProp {
6
6
  isSpecial: boolean;
7
7
  isParam: boolean;
8
8
  private readonly required;
9
- private readonly rawType;
9
+ readonly rawType: string;
10
10
  private readonly buildedConst;
11
11
  private readonly emptyExample;
12
12
  constructor(params: {
package/dist/reflector.js CHANGED
@@ -8,11 +8,12 @@ export class ReflectorFile {
8
8
  "type ValidatorResult = string | null",
9
9
  "type ValidatorFn<T> = (v: T) => ValidatorResult",
10
10
  "type BundleResult<T> = T extends { bundle: () => infer R } ? R : T;",
11
- `type Partial<T> = {
12
- [K in Exclude<keyof T, "bundle">]?: BuildedInput<T[K]>;
11
+ `type PartialBuildedInput<T> = {
12
+ [K in Exclude<keyof T, 'bundle'>]?: BuildedInput<T[K]>;
13
13
  } & {
14
14
  bundle: unknown;
15
- }`,
15
+ }
16
+ `,
16
17
  `export interface QueryContract {
17
18
  event: SvelteEvent;
18
19
  key: string;
@@ -27,14 +28,15 @@ export class ReflectorFile {
27
28
  message: string;
28
29
  }`,
29
30
  `export class Behavior<TSuccess = unknown, TError = unknown> {
30
- onError?: (e: TError) => void;
31
- onSuccess?: (v: TSuccess) => void;
31
+ onError?: (e: TError) => Promise<void> | void;
32
+ onSuccess?: (v: TSuccess) => Promise<void> | void;
32
33
  }`,
33
34
  `export class BuildedInput<T> {
34
35
  value = $state<T>(null as any);
35
36
  display = $state<T>(null as any);
36
37
  required: boolean;
37
38
  placeholder: T;
39
+ readonly kind = 'builded';
38
40
  readonly validator?: ValidatorFn<T>;
39
41
 
40
42
  constructor(params: {
@@ -102,7 +104,7 @@ export class ReflectorFile {
102
104
  }): BuildedInput<T> {
103
105
  return new BuildedInput(params);
104
106
  }`,
105
- `export function isFormValid<T>(schema: Partial<T>): boolean {
107
+ `export function isFormValid<T>(schema: PartialBuildedInput<T>): boolean {
106
108
  delete schema.bundle;
107
109
 
108
110
  const arrayOfBuildedInputs = Object.values(schema) as BuildedInput<unknown>[];
@@ -154,6 +156,7 @@ export class ReflectorFile {
154
156
  export class QueryBuilder<T> {
155
157
  private readonly key: string = '';
156
158
  value = $state<T | null>(null);
159
+ readonly kind = 'query';
157
160
 
158
161
  constructor(params: { key: string; value: T | null }) {
159
162
  const { key, value } = params;
package/dist/schema.js CHANGED
@@ -48,12 +48,9 @@ export class Schema {
48
48
  keys.push(prop.classBuild());
49
49
  bundleParams.push(prop.bundleBuild());
50
50
  });
51
- const hasAttributes = keys.length > 0;
52
- const constructorCode = hasAttributes
53
- ? `constructor(params?: { data?: ${this.name}Interface | undefined, empty?: boolean }) {
51
+ const constructorCode = `constructor(params?: { data?: ${this.name}Interface | undefined, empty?: boolean }) {
54
52
  ${constructorThis.join(";\n")}
55
- }`
56
- : "";
53
+ }`;
57
54
  this.schema = `
58
55
  export class ${this.name} {
59
56
  ${keys.join(";")}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-reflector",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "Reflects zod types from openAPI schemas",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",