svelte-reflector 2.1.0 → 2.1.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/core/generators/ApiCallStrategy.js +6 -0
- package/dist/core/generators/CallMethodGenerator.js +55 -55
- package/dist/core/generators/ModuleCallStrategy.d.ts +0 -1
- package/dist/core/generators/ModuleCallStrategy.js +8 -14
- package/dist/core/generators/queryOverride.d.ts +12 -0
- package/dist/core/generators/queryOverride.js +22 -0
- package/package.json +1 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { queryOverrideTypeLiteral } from "./queryOverride.js";
|
|
1
2
|
export class ApiCallStrategy {
|
|
2
3
|
listStateAccess(_method) {
|
|
3
4
|
return "this.data";
|
|
@@ -15,6 +16,11 @@ export class ApiCallStrategy {
|
|
|
15
16
|
buildParamsType(method) {
|
|
16
17
|
const responseType = method.responseTypeInterface;
|
|
17
18
|
const pathsBlock = this.buildPathsInfo(method);
|
|
19
|
+
const queryBlock = queryOverrideTypeLiteral(method.analyzers.props.querys);
|
|
20
|
+
if (queryBlock) {
|
|
21
|
+
const pathsArg = pathsBlock ?? "void";
|
|
22
|
+
return `ApiCallParams<${responseType}, ${pathsArg}, ${queryBlock}>`;
|
|
23
|
+
}
|
|
18
24
|
if (pathsBlock) {
|
|
19
25
|
return `ApiCallParams<${responseType}, ${pathsBlock}>`;
|
|
20
26
|
}
|
|
@@ -8,36 +8,36 @@ export class CallMethodGenerator {
|
|
|
8
8
|
const { inside, outside } = this.buildApiCall(method, strategy);
|
|
9
9
|
const methodReturn = this.buildMethodReturn(method, strategy);
|
|
10
10
|
const signature = strategy.buildSignature(method);
|
|
11
|
-
return `
|
|
12
|
-
${description}
|
|
13
|
-
${signature} {
|
|
14
|
-
|
|
15
|
-
const behavior = params?.behavior ?? new Behavior();
|
|
16
|
-
const { onError, onSuccess } = behavior;
|
|
17
|
-
|
|
18
|
-
this.loading = true;
|
|
19
|
-
${props}
|
|
20
|
-
const endpoint = ${endpoint}
|
|
21
|
-
|
|
22
|
-
${outside}
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
${inside}
|
|
26
|
-
await onSuccess?.(response);
|
|
27
|
-
|
|
28
|
-
return ${methodReturn};
|
|
29
|
-
} catch (e) {
|
|
30
|
-
let parsedError: ApiErrorResponse;
|
|
31
|
-
try {
|
|
32
|
-
parsedError = JSON.parse((e as Error).message) as ApiErrorResponse;
|
|
33
|
-
} catch {
|
|
34
|
-
parsedError = { error: 'unknown', message: (e as Error).message ?? String(e) };
|
|
35
|
-
}
|
|
36
|
-
return await onError?.(parsedError);
|
|
37
|
-
} finally {
|
|
38
|
-
this.loading = false;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
11
|
+
return `
|
|
12
|
+
${description}
|
|
13
|
+
${signature} {
|
|
14
|
+
|
|
15
|
+
const behavior = params?.behavior ?? new Behavior();
|
|
16
|
+
const { onError, onSuccess } = behavior;
|
|
17
|
+
|
|
18
|
+
this.loading = true;
|
|
19
|
+
${props}
|
|
20
|
+
const endpoint = ${endpoint}
|
|
21
|
+
|
|
22
|
+
${outside}
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
${inside}
|
|
26
|
+
await onSuccess?.(response);
|
|
27
|
+
|
|
28
|
+
return ${methodReturn};
|
|
29
|
+
} catch (e) {
|
|
30
|
+
let parsedError: ApiErrorResponse;
|
|
31
|
+
try {
|
|
32
|
+
parsedError = JSON.parse((e as Error).message) as ApiErrorResponse;
|
|
33
|
+
} catch {
|
|
34
|
+
parsedError = { error: 'unknown', message: (e as Error).message ?? String(e) };
|
|
35
|
+
}
|
|
36
|
+
return await onError?.(parsedError);
|
|
37
|
+
} finally {
|
|
38
|
+
this.loading = false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
41
|
`;
|
|
42
42
|
}
|
|
43
43
|
buildProps(method) {
|
|
@@ -73,13 +73,13 @@ export class CallMethodGenerator {
|
|
|
73
73
|
}
|
|
74
74
|
buildListCall(method, responseType, strategy) {
|
|
75
75
|
const querys = this.joinNames(method.analyzers.props.querys);
|
|
76
|
-
const inside = `
|
|
77
|
-
const response = await api.get<${responseType}, unknown>({
|
|
78
|
-
endpoint,
|
|
79
|
-
queryData: { ${querys} }
|
|
80
|
-
})
|
|
81
|
-
${strategy.listStateAccess(method)} = ${method.analyzers.request.responseType}.from(response.data);
|
|
82
|
-
this.totalPages = response.totalPages;
|
|
76
|
+
const inside = `
|
|
77
|
+
const response = await api.get<${responseType}, unknown>({
|
|
78
|
+
endpoint,
|
|
79
|
+
queryData: { ${querys} }
|
|
80
|
+
})
|
|
81
|
+
${strategy.listStateAccess(method)} = ${method.analyzers.request.responseType}.from(response.data);
|
|
82
|
+
this.totalPages = response.totalPages;
|
|
83
83
|
`;
|
|
84
84
|
return { inside, outside: "" };
|
|
85
85
|
}
|
|
@@ -90,13 +90,13 @@ export class CallMethodGenerator {
|
|
|
90
90
|
const assignment = rType && !isPrimitive
|
|
91
91
|
? `${strategy.entityStateAccess(method)} = new ${rType}({ data: response })`
|
|
92
92
|
: "";
|
|
93
|
-
const inside = `
|
|
94
|
-
const response = await api.get<${responseType}, unknown>({
|
|
95
|
-
endpoint,
|
|
96
|
-
${querys}
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
${assignment}
|
|
93
|
+
const inside = `
|
|
94
|
+
const response = await api.get<${responseType}, unknown>({
|
|
95
|
+
endpoint,
|
|
96
|
+
${querys}
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
${assignment}
|
|
100
100
|
`;
|
|
101
101
|
return { inside, outside: "" };
|
|
102
102
|
}
|
|
@@ -111,23 +111,23 @@ export class CallMethodGenerator {
|
|
|
111
111
|
if (hasHeaders) {
|
|
112
112
|
outside.push(`const headers = this.headers.bundle()`);
|
|
113
113
|
}
|
|
114
|
-
const inside = `
|
|
115
|
-
const response = await api.${apiType}<${responseType}>({
|
|
116
|
-
endpoint,
|
|
117
|
-
${hasData ? "data," : ""}
|
|
118
|
-
${hasHeaders ? "headers," : ""}
|
|
119
|
-
})
|
|
114
|
+
const inside = `
|
|
115
|
+
const response = await api.${apiType}<${responseType}>({
|
|
116
|
+
endpoint,
|
|
117
|
+
${hasData ? "data," : ""}
|
|
118
|
+
${hasHeaders ? "headers," : ""}
|
|
119
|
+
})
|
|
120
120
|
`;
|
|
121
121
|
return { inside, outside: outside.join("\n") };
|
|
122
122
|
}
|
|
123
123
|
buildDeleteCall(method, responseType, bodyType, strategy) {
|
|
124
124
|
const hasData = !!bodyType;
|
|
125
125
|
const outside = hasData ? `const data = ${strategy.formStateAccess(method)}.bundle()` : "";
|
|
126
|
-
const inside = `
|
|
127
|
-
const response = await api.delete<${responseType}, unknown>({
|
|
128
|
-
endpoint,
|
|
129
|
-
${hasData ? "data," : ""}
|
|
130
|
-
})
|
|
126
|
+
const inside = `
|
|
127
|
+
const response = await api.delete<${responseType}, unknown>({
|
|
128
|
+
endpoint,
|
|
129
|
+
${hasData ? "data," : ""}
|
|
130
|
+
})
|
|
131
131
|
`;
|
|
132
132
|
return { inside, outside };
|
|
133
133
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { treatByUppercase } from "../../helpers/helpers.js";
|
|
2
|
+
import { queryOverrideEntryType } from "./queryOverride.js";
|
|
2
3
|
export class ModuleCallStrategy {
|
|
3
4
|
listStateAccess(method) {
|
|
4
5
|
return `this.list${method.stateSuffix}`;
|
|
@@ -21,21 +22,21 @@ export class ModuleCallStrategy {
|
|
|
21
22
|
const blocks = [`behavior?: ${behaviorType};`, pathsBlock, queryBlock]
|
|
22
23
|
.filter((b) => !!b)
|
|
23
24
|
.join("\n");
|
|
24
|
-
return `{
|
|
25
|
-
${blocks}
|
|
25
|
+
return `{
|
|
26
|
+
${blocks}
|
|
26
27
|
}`;
|
|
27
28
|
}
|
|
28
29
|
buildPathsBlock(method) {
|
|
29
30
|
const paths = method.analyzers.props.paths;
|
|
30
31
|
if (paths.length === 0)
|
|
31
32
|
return undefined;
|
|
32
|
-
return `paths?: {
|
|
33
|
+
return `paths?: {
|
|
33
34
|
${paths
|
|
34
35
|
.map((p) => {
|
|
35
36
|
const type = p.rawType ?? p.type;
|
|
36
37
|
return `${p.name}: ${type}`;
|
|
37
38
|
})
|
|
38
|
-
.join("\n")}
|
|
39
|
+
.join("\n")}
|
|
39
40
|
};`;
|
|
40
41
|
}
|
|
41
42
|
buildQueryOverrideBlock(method) {
|
|
@@ -43,17 +44,10 @@ export class ModuleCallStrategy {
|
|
|
43
44
|
if (querys.length === 0)
|
|
44
45
|
return undefined;
|
|
45
46
|
const fields = querys
|
|
46
|
-
.map((q) => `${q.name}?: ${
|
|
47
|
+
.map((q) => `${q.name}?: ${queryOverrideEntryType(q)}`)
|
|
47
48
|
.join("\n");
|
|
48
|
-
return `queryOverride?: {
|
|
49
|
-
${fields}
|
|
49
|
+
return `queryOverride?: {
|
|
50
|
+
${fields}
|
|
50
51
|
};`;
|
|
51
52
|
}
|
|
52
|
-
queryOverrideType(q) {
|
|
53
|
-
if ("isEnum" in q && q.isEnum)
|
|
54
|
-
return `${q.type}[]`;
|
|
55
|
-
if (!("rawType" in q) && !("enumName" in q))
|
|
56
|
-
return "string[]";
|
|
57
|
-
return "string | null";
|
|
58
|
-
}
|
|
59
53
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AttributeProp } from "../../types/types.js";
|
|
2
|
+
/** TS type literal for a single query override entry. Mirrors the
|
|
3
|
+
* shape produced by each Prop's bundle entry:
|
|
4
|
+
* PrimitiveProp / EnumProp → `string | null`
|
|
5
|
+
* ArrayProp (enum) → `${type}[]`
|
|
6
|
+
* ArrayProp (non-enum, raro) → `string[]`
|
|
7
|
+
*/
|
|
8
|
+
export declare function queryOverrideEntryType(q: AttributeProp): string;
|
|
9
|
+
/** Inline TS object type literal for the full queryOverride bag,
|
|
10
|
+
* derived from the method's query props. Returns undefined when there
|
|
11
|
+
* are no query params (caller should skip emitting the slot). */
|
|
12
|
+
export declare function queryOverrideTypeLiteral(querys: AttributeProp[]): string | undefined;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** TS type literal for a single query override entry. Mirrors the
|
|
2
|
+
* shape produced by each Prop's bundle entry:
|
|
3
|
+
* PrimitiveProp / EnumProp → `string | null`
|
|
4
|
+
* ArrayProp (enum) → `${type}[]`
|
|
5
|
+
* ArrayProp (non-enum, raro) → `string[]`
|
|
6
|
+
*/
|
|
7
|
+
export function queryOverrideEntryType(q) {
|
|
8
|
+
if ("isEnum" in q && q.isEnum)
|
|
9
|
+
return `${q.type}[]`;
|
|
10
|
+
if (!("rawType" in q) && !("enumName" in q))
|
|
11
|
+
return "string[]";
|
|
12
|
+
return "string | null";
|
|
13
|
+
}
|
|
14
|
+
/** Inline TS object type literal for the full queryOverride bag,
|
|
15
|
+
* derived from the method's query props. Returns undefined when there
|
|
16
|
+
* are no query params (caller should skip emitting the slot). */
|
|
17
|
+
export function queryOverrideTypeLiteral(querys) {
|
|
18
|
+
if (querys.length === 0)
|
|
19
|
+
return undefined;
|
|
20
|
+
const fields = querys.map((q) => `${q.name}?: ${queryOverrideEntryType(q)}`).join("; ");
|
|
21
|
+
return `{ ${fields} }`;
|
|
22
|
+
}
|