atmx-cli 0.31.0 → 0.33.0
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.
|
@@ -5,16 +5,18 @@ const utils_1 = require("./utils");
|
|
|
5
5
|
function generateSdk(multiIr) {
|
|
6
6
|
const lines = [
|
|
7
7
|
`// GENERATED CODE – DO NOT EDIT.`,
|
|
8
|
-
`import { useAxiomQuery, useAxiomMutation } from 'atmx-react';`,
|
|
9
|
-
`import type { AxiomQueryDef } from 'atmx-react';`,
|
|
10
8
|
`import * as models from './models';\n`,
|
|
11
9
|
];
|
|
12
10
|
for (const [ns, ir] of Object.entries(multiIr)) {
|
|
13
11
|
const pascalNs = (0, utils_1.pascalCase)(ns);
|
|
14
12
|
lines.push(`export class ${pascalNs}Module {`);
|
|
15
|
-
|
|
13
|
+
// Support both old array format and new object map format
|
|
14
|
+
const endpointsMap = ir.endpoints || {};
|
|
15
|
+
const endpoints = Array.isArray(endpointsMap)
|
|
16
|
+
? endpointsMap
|
|
17
|
+
: Object.values(endpointsMap);
|
|
16
18
|
endpoints.forEach((ep) => {
|
|
17
|
-
lines.push(
|
|
19
|
+
lines.push(generateEndpointMethod(ep, ns, pascalNs));
|
|
18
20
|
});
|
|
19
21
|
lines.push(`}\n`);
|
|
20
22
|
}
|
|
@@ -25,68 +27,30 @@ function generateSdk(multiIr) {
|
|
|
25
27
|
lines.push(`}\nexport const sdk = new AxiomSdk();`);
|
|
26
28
|
return lines.join("\n");
|
|
27
29
|
}
|
|
28
|
-
function
|
|
29
|
-
const isQuery = ep.method ? ep.method.toUpperCase() === "GET" : true;
|
|
30
|
-
const rawReturnType = (0, utils_1.mapTypeToTs)(ep.returnType, pascalNs);
|
|
31
|
-
const returnType = rawReturnType === "void" || rawReturnType === "any"
|
|
32
|
-
? rawReturnType
|
|
33
|
-
: prefixModels(rawReturnType);
|
|
30
|
+
function generateEndpointMethod(ep, ns, pascalNs) {
|
|
34
31
|
const params = ep.parameters || [];
|
|
35
32
|
const argType = params.length > 0
|
|
36
33
|
? `{ ${params.map((p) => `${(0, utils_1.camelCase)(p.name)}${p.isOptional ? "?" : ""}: ${prefixModels((0, utils_1.mapTypeToTs)(p.typeRef, pascalNs))}`).join(", ")} }`
|
|
37
34
|
: "void";
|
|
38
|
-
|
|
39
|
-
// NEW: Every endpoint now generates BOTH a raw definition getter and a hook!
|
|
35
|
+
// We generate a method that takes the typed arguments and returns the exact string ATMX expects
|
|
40
36
|
return `
|
|
41
|
-
/**
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
/** Reactive hook for functional components */
|
|
47
|
-
use${(0, utils_1.pascalCase)(ep.name)}${!isQuery ? "Mutation" : ""}(${isQuery ? `args: ${argType}, options?: { enabled?: boolean }` : ""}) {
|
|
48
|
-
const factory = ${factoryLogic};
|
|
49
|
-
return ${isQuery ? `useAxiomQuery<${returnType}>(factory(args), options)` : `useAxiomMutation<${returnType}, ${argType}>(factory)`};
|
|
37
|
+
/** RPC String Generator for <AxQuery> or <AxMutate> */
|
|
38
|
+
${(0, utils_1.camelCase)(ep.name)}(args${params.length > 0 ? "" : "?"}: ${argType}): string {
|
|
39
|
+
const argsStr = args && Object.keys(args).length > 0 ? JSON.stringify(args) : '';
|
|
40
|
+
return \`${ns}.${ep.name}(\${argsStr})\`;
|
|
50
41
|
}\n`;
|
|
51
42
|
}
|
|
52
|
-
function generateQueryDefFactory(ep, returnType, ns) {
|
|
53
|
-
const params = ep.parameters || [];
|
|
54
|
-
const bodyParam = params.find((p) => p.source === "body");
|
|
55
|
-
const payloadLogic = bodyParam
|
|
56
|
-
? `const payload = (args as any).${(0, utils_1.camelCase)(bodyParam.name)};`
|
|
57
|
-
: `const payload = undefined;`;
|
|
58
|
-
const decLogic = generateLambda(ep.returnType, "fromJson", ns);
|
|
59
|
-
const serLogic = bodyParam
|
|
60
|
-
? generateLambda(bodyParam.typeRef, "toJson", ns)
|
|
61
|
-
: `(p: any) => p`;
|
|
62
|
-
// THIN DEFINITION: Leaves URL logic up to ATMX Core Router
|
|
63
|
-
return `(args: any): AxiomQueryDef<${returnType}> => {
|
|
64
|
-
${payloadLogic}
|
|
65
|
-
return {
|
|
66
|
-
namespace: "${ns}",
|
|
67
|
-
name: "${ep.name}",
|
|
68
|
-
endpointId: ${ep.id},
|
|
69
|
-
method: "${ep.method ? ep.method.toUpperCase() : "GET"}",
|
|
70
|
-
path: "${ep.path}",
|
|
71
|
-
payload: payload,
|
|
72
|
-
args: args as any,
|
|
73
|
-
decoder: ${decLogic},
|
|
74
|
-
serializer: ${serLogic},
|
|
75
|
-
isStream: ${ep.isStream === true}
|
|
76
|
-
};
|
|
77
|
-
}`;
|
|
78
|
-
}
|
|
79
|
-
function generateLambda(typeRef, mode, ns) {
|
|
80
|
-
if (!typeRef || !typeRef.kind || typeRef.kind === "void")
|
|
81
|
-
return mode === "fromJson" ? `() => undefined` : `(p: any) => p`;
|
|
82
|
-
if (typeRef.kind === "list" && typeRef.value?.kind === "named")
|
|
83
|
-
return `(data: any[]) => data.map(models.Mappers.${(0, utils_1.camelCase)(ns)}.${(0, utils_1.pascalCase)(typeRef.value.value)}.${mode})`;
|
|
84
|
-
if (typeRef.kind === "named")
|
|
85
|
-
return `models.Mappers.${(0, utils_1.camelCase)(ns)}.${(0, utils_1.pascalCase)(typeRef.value)}.${mode}`;
|
|
86
|
-
return `(data: any) => data`;
|
|
87
|
-
}
|
|
88
43
|
function prefixModels(type) {
|
|
89
|
-
|
|
44
|
+
const primitives = [
|
|
45
|
+
"string",
|
|
46
|
+
"number",
|
|
47
|
+
"boolean",
|
|
48
|
+
"Date",
|
|
49
|
+
"Uint8Array",
|
|
50
|
+
"void",
|
|
51
|
+
"any",
|
|
52
|
+
];
|
|
53
|
+
if (!type || primitives.includes(type))
|
|
90
54
|
return type;
|
|
91
55
|
if (type.endsWith("[]"))
|
|
92
56
|
return `${prefixModels(type.slice(0, -2))}[]`;
|
package/package.json
CHANGED
|
@@ -5,17 +5,21 @@ import { pascalCase, camelCase, mapTypeToTs } from "./utils";
|
|
|
5
5
|
export function generateSdk(multiIr: MultiIR): string {
|
|
6
6
|
const lines: string[] = [
|
|
7
7
|
`// GENERATED CODE – DO NOT EDIT.`,
|
|
8
|
-
`import { useAxiomQuery, useAxiomMutation } from 'atmx-react';`,
|
|
9
|
-
`import type { AxiomQueryDef } from 'atmx-react';`,
|
|
10
8
|
`import * as models from './models';\n`,
|
|
11
9
|
];
|
|
12
10
|
|
|
13
11
|
for (const [ns, ir] of Object.entries(multiIr)) {
|
|
14
12
|
const pascalNs = pascalCase(ns);
|
|
15
13
|
lines.push(`export class ${pascalNs}Module {`);
|
|
16
|
-
|
|
14
|
+
|
|
15
|
+
// Support both old array format and new object map format
|
|
16
|
+
const endpointsMap = ir.endpoints || {};
|
|
17
|
+
const endpoints = Array.isArray(endpointsMap)
|
|
18
|
+
? endpointsMap
|
|
19
|
+
: Object.values(endpointsMap);
|
|
20
|
+
|
|
17
21
|
endpoints.forEach((ep: any) => {
|
|
18
|
-
lines.push(
|
|
22
|
+
lines.push(generateEndpointMethod(ep, ns, pascalNs));
|
|
19
23
|
});
|
|
20
24
|
lines.push(`}\n`);
|
|
21
25
|
}
|
|
@@ -31,89 +35,38 @@ export function generateSdk(multiIr: MultiIR): string {
|
|
|
31
35
|
return lines.join("\n");
|
|
32
36
|
}
|
|
33
37
|
|
|
34
|
-
function
|
|
38
|
+
function generateEndpointMethod(
|
|
35
39
|
ep: AxiomEndpoint,
|
|
36
40
|
ns: string,
|
|
37
41
|
pascalNs: string,
|
|
38
42
|
): string {
|
|
39
|
-
const isQuery = ep.method ? ep.method.toUpperCase() === "GET" : true;
|
|
40
|
-
const rawReturnType = mapTypeToTs(ep.returnType, pascalNs);
|
|
41
|
-
const returnType =
|
|
42
|
-
rawReturnType === "void" || rawReturnType === "any"
|
|
43
|
-
? rawReturnType
|
|
44
|
-
: prefixModels(rawReturnType);
|
|
45
|
-
|
|
46
43
|
const params = ep.parameters || [];
|
|
44
|
+
|
|
47
45
|
const argType =
|
|
48
46
|
params.length > 0
|
|
49
47
|
? `{ ${params.map((p) => `${camelCase(p.name)}${p.isOptional ? "?" : ""}: ${prefixModels(mapTypeToTs(p.typeRef, pascalNs))}`).join(", ")} }`
|
|
50
48
|
: "void";
|
|
51
49
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
// NEW: Every endpoint now generates BOTH a raw definition getter and a hook!
|
|
50
|
+
// We generate a method that takes the typed arguments and returns the exact string ATMX expects
|
|
55
51
|
return `
|
|
56
|
-
/**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
/** Reactive hook for functional components */
|
|
62
|
-
use${pascalCase(ep.name)}${!isQuery ? "Mutation" : ""}(${isQuery ? `args: ${argType}, options?: { enabled?: boolean }` : ""}) {
|
|
63
|
-
const factory = ${factoryLogic};
|
|
64
|
-
return ${isQuery ? `useAxiomQuery<${returnType}>(factory(args), options)` : `useAxiomMutation<${returnType}, ${argType}>(factory)`};
|
|
52
|
+
/** RPC String Generator for <AxQuery> or <AxMutate> */
|
|
53
|
+
${camelCase(ep.name)}(args${params.length > 0 ? "" : "?"}: ${argType}): string {
|
|
54
|
+
const argsStr = args && Object.keys(args).length > 0 ? JSON.stringify(args) : '';
|
|
55
|
+
return \`${ns}.${ep.name}(\${argsStr})\`;
|
|
65
56
|
}\n`;
|
|
66
57
|
}
|
|
67
58
|
|
|
68
|
-
function generateQueryDefFactory(
|
|
69
|
-
ep: AxiomEndpoint,
|
|
70
|
-
returnType: string,
|
|
71
|
-
ns: string,
|
|
72
|
-
): string {
|
|
73
|
-
const params = ep.parameters || [];
|
|
74
|
-
const bodyParam = params.find((p) => p.source === "body");
|
|
75
|
-
const payloadLogic = bodyParam
|
|
76
|
-
? `const payload = (args as any).${camelCase(bodyParam.name)};`
|
|
77
|
-
: `const payload = undefined;`;
|
|
78
|
-
const decLogic = generateLambda(ep.returnType, "fromJson", ns);
|
|
79
|
-
const serLogic = bodyParam
|
|
80
|
-
? generateLambda(bodyParam.typeRef, "toJson", ns)
|
|
81
|
-
: `(p: any) => p`;
|
|
82
|
-
|
|
83
|
-
// THIN DEFINITION: Leaves URL logic up to ATMX Core Router
|
|
84
|
-
return `(args: any): AxiomQueryDef<${returnType}> => {
|
|
85
|
-
${payloadLogic}
|
|
86
|
-
return {
|
|
87
|
-
namespace: "${ns}",
|
|
88
|
-
name: "${ep.name}",
|
|
89
|
-
endpointId: ${ep.id},
|
|
90
|
-
method: "${ep.method ? ep.method.toUpperCase() : "GET"}",
|
|
91
|
-
path: "${ep.path}",
|
|
92
|
-
payload: payload,
|
|
93
|
-
args: args as any,
|
|
94
|
-
decoder: ${decLogic},
|
|
95
|
-
serializer: ${serLogic},
|
|
96
|
-
isStream: ${ep.isStream === true}
|
|
97
|
-
};
|
|
98
|
-
}`;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function generateLambda(
|
|
102
|
-
typeRef: any,
|
|
103
|
-
mode: "fromJson" | "toJson",
|
|
104
|
-
ns: string,
|
|
105
|
-
): string {
|
|
106
|
-
if (!typeRef || !typeRef.kind || typeRef.kind === "void")
|
|
107
|
-
return mode === "fromJson" ? `() => undefined` : `(p: any) => p`;
|
|
108
|
-
if (typeRef.kind === "list" && typeRef.value?.kind === "named")
|
|
109
|
-
return `(data: any[]) => data.map(models.Mappers.${camelCase(ns)}.${pascalCase(typeRef.value.value)}.${mode})`;
|
|
110
|
-
if (typeRef.kind === "named")
|
|
111
|
-
return `models.Mappers.${camelCase(ns)}.${pascalCase(typeRef.value)}.${mode}`;
|
|
112
|
-
return `(data: any) => data`;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
59
|
function prefixModels(type: string): string {
|
|
116
|
-
|
|
60
|
+
const primitives = [
|
|
61
|
+
"string",
|
|
62
|
+
"number",
|
|
63
|
+
"boolean",
|
|
64
|
+
"Date",
|
|
65
|
+
"Uint8Array",
|
|
66
|
+
"void",
|
|
67
|
+
"any",
|
|
68
|
+
];
|
|
69
|
+
if (!type || primitives.includes(type)) return type;
|
|
117
70
|
if (type.endsWith("[]")) return `${prefixModels(type.slice(0, -2))}[]`;
|
|
118
71
|
if (type.startsWith("models.")) return type;
|
|
119
72
|
return `models.${type}`;
|