@geekmidas/cli 10.0.0-alpha.13 → 10.0.0-alpha.14
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/index.cjs +24 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +24 -38
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -22
- package/dist/openapi-react-query.cjs +0 -158
- package/dist/openapi-react-query.cjs.map +0 -1
- package/dist/openapi-react-query.d.cts +0 -9
- package/dist/openapi-react-query.d.cts.map +0 -1
- package/dist/openapi-react-query.d.mts +0 -9
- package/dist/openapi-react-query.d.mts.map +0 -1
- package/dist/openapi-react-query.mjs +0 -158
- package/dist/openapi-react-query.mjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/cli",
|
|
3
|
-
"version": "10.0.0-alpha.
|
|
3
|
+
"version": "10.0.0-alpha.14",
|
|
4
4
|
"description": "CLI tools for building Lambda handlers, server applications, and generating OpenAPI specs",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
@@ -54,16 +54,6 @@
|
|
|
54
54
|
"types": "./dist/openapi.d.cts",
|
|
55
55
|
"default": "./dist/openapi.cjs"
|
|
56
56
|
}
|
|
57
|
-
},
|
|
58
|
-
"./openapi-react-query": {
|
|
59
|
-
"import": {
|
|
60
|
-
"types": "./dist/openapi-react-query.d.mts",
|
|
61
|
-
"default": "./dist/openapi-react-query.mjs"
|
|
62
|
-
},
|
|
63
|
-
"require": {
|
|
64
|
-
"types": "./dist/openapi-react-query.d.cts",
|
|
65
|
-
"default": "./dist/openapi-react-query.cjs"
|
|
66
|
-
}
|
|
67
57
|
}
|
|
68
58
|
},
|
|
69
59
|
"files": [
|
|
@@ -92,19 +82,18 @@
|
|
|
92
82
|
"get-tsconfig": "~4.10.1",
|
|
93
83
|
"hono": "~4.13.8",
|
|
94
84
|
"lodash.kebabcase": "~4.1.1",
|
|
95
|
-
"openapi-typescript": "~7.13.0",
|
|
96
85
|
"pg": "~8.23.0",
|
|
97
86
|
"prompts": "~2.4.2",
|
|
98
87
|
"tsx": "~4.23.15",
|
|
99
88
|
"yaml": "~2.9.1",
|
|
100
|
-
"@geekmidas/cache": "10.0.0-alpha.
|
|
101
|
-
"@geekmidas/constructs": "~10.0.0-alpha.
|
|
102
|
-
"@geekmidas/db": "10.0.0-alpha.
|
|
103
|
-
"@geekmidas/envkit": "~10.0.0-alpha.
|
|
104
|
-
"@geekmidas/errors": "~10.0.0-alpha.
|
|
105
|
-
"@geekmidas/logger": "~10.0.0-alpha.
|
|
106
|
-
"@geekmidas/manifest": "10.0.0-alpha.
|
|
107
|
-
"@geekmidas/schema": "~10.0.0-alpha.
|
|
89
|
+
"@geekmidas/cache": "10.0.0-alpha.14",
|
|
90
|
+
"@geekmidas/constructs": "~10.0.0-alpha.14",
|
|
91
|
+
"@geekmidas/db": "10.0.0-alpha.14",
|
|
92
|
+
"@geekmidas/envkit": "~10.0.0-alpha.14",
|
|
93
|
+
"@geekmidas/errors": "~10.0.0-alpha.14",
|
|
94
|
+
"@geekmidas/logger": "~10.0.0-alpha.14",
|
|
95
|
+
"@geekmidas/manifest": "10.0.0-alpha.14",
|
|
96
|
+
"@geekmidas/schema": "~10.0.0-alpha.14"
|
|
108
97
|
},
|
|
109
98
|
"devDependencies": {
|
|
110
99
|
"@types/lodash.kebabcase": "~4.1.9",
|
|
@@ -114,10 +103,10 @@
|
|
|
114
103
|
"typescript": "~6.0.3",
|
|
115
104
|
"vitest": "~5.0.2",
|
|
116
105
|
"zod": "~4.6.5",
|
|
117
|
-
"@geekmidas/testkit": "10.0.0-alpha.
|
|
106
|
+
"@geekmidas/testkit": "10.0.0-alpha.14"
|
|
118
107
|
},
|
|
119
108
|
"peerDependencies": {
|
|
120
|
-
"@geekmidas/telescope": "~10.0.0-alpha.
|
|
109
|
+
"@geekmidas/telescope": "~10.0.0-alpha.14"
|
|
121
110
|
},
|
|
122
111
|
"peerDependenciesMeta": {
|
|
123
112
|
"@geekmidas/telescope": {
|
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env -S npx tsx
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
let node_fs = require("node:fs");
|
|
4
|
-
let node_fs_promises = require("node:fs/promises");
|
|
5
|
-
let node_path = require("node:path");
|
|
6
|
-
let node_child_process = require("node:child_process");
|
|
7
|
-
//#region src/openapi-react-query.ts
|
|
8
|
-
const execAsync = (0, require("node:util").promisify)(node_child_process.exec);
|
|
9
|
-
async function generateReactQueryCommand(options = {}) {
|
|
10
|
-
const logger = console;
|
|
11
|
-
try {
|
|
12
|
-
const inputPath = options.input || (0, node_path.join)(process.cwd(), "openapi.json");
|
|
13
|
-
if (!(0, node_fs.existsSync)(inputPath)) throw new Error(`OpenAPI spec not found at ${inputPath}. Run 'npx @geekmidas/cli openapi' first.`);
|
|
14
|
-
const specContent = await (0, node_fs_promises.readFile)(inputPath, "utf-8");
|
|
15
|
-
const spec = JSON.parse(specContent);
|
|
16
|
-
const outputDir = (0, node_path.dirname)(options.output || (0, node_path.join)(process.cwd(), "src", "api", "hooks.ts"));
|
|
17
|
-
const typesPath = (0, node_path.join)(outputDir, "openapi-types.d.ts");
|
|
18
|
-
logger.log("Generating TypeScript types from OpenAPI spec...");
|
|
19
|
-
try {
|
|
20
|
-
await execAsync(`npx openapi-typescript "${inputPath}" -o "${typesPath}"`, { cwd: process.cwd() });
|
|
21
|
-
logger.log(`TypeScript types generated: ${typesPath}`);
|
|
22
|
-
} catch (_error) {
|
|
23
|
-
logger.warn("Could not generate types with openapi-typescript. Install it for better type inference.");
|
|
24
|
-
logger.warn("Run: npm install -D openapi-typescript");
|
|
25
|
-
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(typesPath), { recursive: true });
|
|
26
|
-
await (0, node_fs_promises.writeFile)(typesPath, `// Auto-generated placeholder types
|
|
27
|
-
export interface paths {
|
|
28
|
-
[path: string]: {
|
|
29
|
-
[method: string]: {
|
|
30
|
-
operationId?: string;
|
|
31
|
-
parameters?: any;
|
|
32
|
-
requestBody?: any;
|
|
33
|
-
responses?: any;
|
|
34
|
-
};
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
`);
|
|
38
|
-
}
|
|
39
|
-
const operations = extractOperations(spec);
|
|
40
|
-
const code = generateReactQueryCode(spec, operations, options.name || "API");
|
|
41
|
-
const outputPath = options.output || (0, node_path.join)(process.cwd(), "src", "api", "hooks.ts");
|
|
42
|
-
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(outputPath), { recursive: true });
|
|
43
|
-
await (0, node_fs_promises.writeFile)(outputPath, code);
|
|
44
|
-
logger.log(`React Query hooks generated: ${outputPath}`);
|
|
45
|
-
logger.log(`Generated ${operations.length} hooks`);
|
|
46
|
-
} catch (error) {
|
|
47
|
-
throw new Error(`React Query generation failed: ${error.message}`);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
function extractOperations(spec) {
|
|
51
|
-
const operations = [];
|
|
52
|
-
Object.entries(spec.paths).forEach(([path, methods]) => {
|
|
53
|
-
Object.entries(methods).forEach(([method, operation]) => {
|
|
54
|
-
if (operation.operationId) operations.push({
|
|
55
|
-
operationId: operation.operationId,
|
|
56
|
-
path,
|
|
57
|
-
method: method.toUpperCase(),
|
|
58
|
-
endpoint: `${method.toUpperCase()} ${path}`,
|
|
59
|
-
parameters: operation.parameters,
|
|
60
|
-
requestBody: !!operation.requestBody,
|
|
61
|
-
responseType: extractResponseType(operation)
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
return operations;
|
|
66
|
-
}
|
|
67
|
-
function extractResponseType(operation) {
|
|
68
|
-
const responses = operation.responses;
|
|
69
|
-
if (!responses) return "unknown";
|
|
70
|
-
const successResponse = responses["200"] || responses["201"];
|
|
71
|
-
if (!successResponse?.content?.["application/json"]?.schema) return "unknown";
|
|
72
|
-
const schema = successResponse.content["application/json"].schema;
|
|
73
|
-
return schemaToTypeString(schema);
|
|
74
|
-
}
|
|
75
|
-
function schemaToTypeString(schema) {
|
|
76
|
-
if (!schema) return "unknown";
|
|
77
|
-
switch (schema.type) {
|
|
78
|
-
case "string": return "string";
|
|
79
|
-
case "number":
|
|
80
|
-
case "integer": return "number";
|
|
81
|
-
case "boolean": return "boolean";
|
|
82
|
-
case "array": return `Array<${schemaToTypeString(schema.items)}>`;
|
|
83
|
-
case "object":
|
|
84
|
-
if (schema.properties) return `{ ${Object.entries(schema.properties).map(([key, value]) => `${key}: ${schemaToTypeString(value)}`).join("; ")} }`;
|
|
85
|
-
return "Record<string, unknown>";
|
|
86
|
-
default: return "unknown";
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
function generateReactQueryCode(_spec, operations, apiName) {
|
|
90
|
-
return `${`import { createTypedQueryClient } from '@geekmidas/client/react-query';
|
|
91
|
-
import type { paths } from './openapi-types';
|
|
92
|
-
|
|
93
|
-
// Create typed query client
|
|
94
|
-
export const ${apiName.toLowerCase()} = createTypedQueryClient<paths>({
|
|
95
|
-
baseURL: process.env.NEXT_PUBLIC_API_URL || '/api',
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
// Export individual hooks for better DX
|
|
99
|
-
`}
|
|
100
|
-
// Query Hooks
|
|
101
|
-
${operations.filter((op) => op.method === "GET").map((op) => generateQueryHook(op, apiName)).join("\n\n")}
|
|
102
|
-
|
|
103
|
-
// Mutation Hooks
|
|
104
|
-
${operations.filter((op) => op.method !== "GET").map((op) => generateMutationHook(op, apiName)).join("\n\n")}
|
|
105
|
-
|
|
106
|
-
// Type exports for convenience
|
|
107
|
-
${generateTypeExports(operations)}
|
|
108
|
-
|
|
109
|
-
// Re-export the api for advanced usage
|
|
110
|
-
export { ${apiName.toLowerCase()} };
|
|
111
|
-
`;
|
|
112
|
-
}
|
|
113
|
-
function generateQueryHook(op, apiName) {
|
|
114
|
-
const hookName = `use${capitalize(op.operationId)}`;
|
|
115
|
-
const endpoint = op.endpoint;
|
|
116
|
-
const hasParams = op.parameters?.some((p) => p.in === "path");
|
|
117
|
-
const hasQuery = op.parameters?.some((p) => p.in === "query");
|
|
118
|
-
let params = "";
|
|
119
|
-
let args = "";
|
|
120
|
-
if (hasParams || hasQuery) {
|
|
121
|
-
const paramParts = [];
|
|
122
|
-
if (hasParams) {
|
|
123
|
-
const pathParams = op.parameters?.filter((p) => p.in === "path").map((p) => p.name) || [];
|
|
124
|
-
paramParts.push(`params: { ${pathParams.map((p) => `${p}: string`).join("; ")} }`);
|
|
125
|
-
}
|
|
126
|
-
if (hasQuery) paramParts.push(`query?: Record<string, any>`);
|
|
127
|
-
params = `config: { ${paramParts.join("; ")} }, `;
|
|
128
|
-
args = ", config";
|
|
129
|
-
}
|
|
130
|
-
return `export const ${hookName} = (
|
|
131
|
-
${params}options?: Parameters<typeof ${apiName.toLowerCase()}.useQuery>[2]
|
|
132
|
-
) => {
|
|
133
|
-
return ${apiName.toLowerCase()}.useQuery('${endpoint}' as any${args}, options);
|
|
134
|
-
};`;
|
|
135
|
-
}
|
|
136
|
-
function generateMutationHook(op, apiName) {
|
|
137
|
-
const hookName = `use${capitalize(op.operationId)}`;
|
|
138
|
-
const endpoint = op.endpoint;
|
|
139
|
-
return `export const ${hookName} = (
|
|
140
|
-
options?: Parameters<typeof ${apiName.toLowerCase()}.useMutation>[1]
|
|
141
|
-
) => {
|
|
142
|
-
return ${apiName.toLowerCase()}.useMutation('${endpoint}' as any, options);
|
|
143
|
-
};`;
|
|
144
|
-
}
|
|
145
|
-
function generateTypeExports(operations) {
|
|
146
|
-
return operations.map((op) => {
|
|
147
|
-
const typeName = capitalize(op.operationId);
|
|
148
|
-
if (op.method === "GET") return `export type ${typeName}Response = Awaited<ReturnType<ReturnType<typeof use${typeName}>['data']>>;`;
|
|
149
|
-
else return `export type ${typeName}Response = Awaited<ReturnType<ReturnType<typeof use${typeName}>['mutateAsync']>>;`;
|
|
150
|
-
}).join("\n");
|
|
151
|
-
}
|
|
152
|
-
function capitalize(str) {
|
|
153
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
154
|
-
}
|
|
155
|
-
//#endregion
|
|
156
|
-
exports.generateReactQueryCommand = generateReactQueryCommand;
|
|
157
|
-
|
|
158
|
-
//# sourceMappingURL=openapi-react-query.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"openapi-react-query.cjs","names":["promisify","exec","join","existsSync","readFile","dirname","mkdir","writeFile"],"sources":["../src/openapi-react-query.ts"],"sourcesContent":["#!/usr/bin/env -S npx tsx\n\nimport { exec } from 'node:child_process';\nimport { existsSync } from 'node:fs';\nimport { mkdir, readFile, writeFile } from 'node:fs/promises';\nimport { dirname, join } from 'node:path';\nimport { promisify } from 'node:util';\n\nconst execAsync = promisify(exec);\n\ninterface ReactQueryOptions {\n\tinput?: string;\n\toutput?: string;\n\tname?: string;\n}\n\ninterface OpenAPISpec {\n\topenapi: string;\n\tinfo?: {\n\t\ttitle?: string;\n\t\tversion?: string;\n\t};\n\tpaths: Record<string, Record<string, any>>;\n}\n\nexport async function generateReactQueryCommand(\n\toptions: ReactQueryOptions = {},\n): Promise<void> {\n\tconst logger = console;\n\n\ttry {\n\t\t// Read OpenAPI spec\n\t\tconst inputPath = options.input || join(process.cwd(), 'openapi.json');\n\n\t\tif (!existsSync(inputPath)) {\n\t\t\tthrow new Error(\n\t\t\t\t`OpenAPI spec not found at ${inputPath}. Run 'npx @geekmidas/cli openapi' first.`,\n\t\t\t);\n\t\t}\n\n\t\tconst specContent = await readFile(inputPath, 'utf-8');\n\t\tconst spec: OpenAPISpec = JSON.parse(specContent);\n\n\t\t// Generate TypeScript types from OpenAPI spec\n\t\tconst outputDir = dirname(\n\t\t\toptions.output || join(process.cwd(), 'src', 'api', 'hooks.ts'),\n\t\t);\n\t\tconst typesPath = join(outputDir, 'openapi-types.d.ts');\n\n\t\tlogger.log('Generating TypeScript types from OpenAPI spec...');\n\n\t\ttry {\n\t\t\t// Use npx to run openapi-typescript\n\t\t\tawait execAsync(\n\t\t\t\t`npx openapi-typescript \"${inputPath}\" -o \"${typesPath}\"`,\n\t\t\t\t{ cwd: process.cwd() },\n\t\t\t);\n\t\t\tlogger.log(`TypeScript types generated: ${typesPath}`);\n\t\t} catch (_error) {\n\t\t\tlogger.warn(\n\t\t\t\t'Could not generate types with openapi-typescript. Install it for better type inference.',\n\t\t\t);\n\t\t\tlogger.warn('Run: npm install -D openapi-typescript');\n\n\t\t\t// Generate basic types file\n\t\t\tawait mkdir(dirname(typesPath), { recursive: true });\n\t\t\tawait writeFile(\n\t\t\t\ttypesPath,\n\t\t\t\t`// Auto-generated placeholder types\nexport interface paths {\n [path: string]: {\n [method: string]: {\n operationId?: string;\n parameters?: any;\n requestBody?: any;\n responses?: any;\n };\n };\n}\n`,\n\t\t\t);\n\t\t}\n\n\t\t// Extract operation info\n\t\tconst operations = extractOperations(spec);\n\n\t\t// Generate TypeScript code\n\t\tconst code = generateReactQueryCode(\n\t\t\tspec,\n\t\t\toperations,\n\t\t\toptions.name || 'API',\n\t\t);\n\n\t\t// Write output\n\t\tconst outputPath =\n\t\t\toptions.output || join(process.cwd(), 'src', 'api', 'hooks.ts');\n\t\tawait mkdir(dirname(outputPath), { recursive: true });\n\t\tawait writeFile(outputPath, code);\n\n\t\tlogger.log(`React Query hooks generated: ${outputPath}`);\n\t\tlogger.log(`Generated ${operations.length} hooks`);\n\t} catch (error) {\n\t\tthrow new Error(\n\t\t\t`React Query generation failed: ${(error as Error).message}`,\n\t\t);\n\t}\n}\n\ninterface OperationInfo {\n\toperationId: string;\n\tpath: string;\n\tmethod: string;\n\tendpoint: string; // Full endpoint like 'GET /users/{id}'\n\tparameters?: Array<{ name: string; in: string; required?: boolean }>;\n\trequestBody?: boolean;\n\tresponseType?: string;\n}\n\nfunction extractOperations(spec: OpenAPISpec): OperationInfo[] {\n\tconst operations: OperationInfo[] = [];\n\n\tObject.entries(spec.paths).forEach(([path, methods]) => {\n\t\tObject.entries(methods).forEach(([method, operation]) => {\n\t\t\tif (operation.operationId) {\n\t\t\t\toperations.push({\n\t\t\t\t\toperationId: operation.operationId,\n\t\t\t\t\tpath,\n\t\t\t\t\tmethod: method.toUpperCase(),\n\t\t\t\t\tendpoint: `${method.toUpperCase()} ${path}`,\n\t\t\t\t\tparameters: operation.parameters,\n\t\t\t\t\trequestBody: !!operation.requestBody,\n\t\t\t\t\tresponseType: extractResponseType(operation),\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t});\n\n\treturn operations;\n}\n\nfunction extractResponseType(operation: any): string {\n\tconst responses = operation.responses;\n\tif (!responses) return 'unknown';\n\n\tconst successResponse = responses['200'] || responses['201'];\n\tif (!successResponse?.content?.['application/json']?.schema) {\n\t\treturn 'unknown';\n\t}\n\n\t// Basic type inference from schema\n\tconst schema = successResponse.content['application/json'].schema;\n\treturn schemaToTypeString(schema);\n}\n\nfunction schemaToTypeString(schema: any): string {\n\tif (!schema) return 'unknown';\n\n\tswitch (schema.type) {\n\t\tcase 'string':\n\t\t\treturn 'string';\n\t\tcase 'number':\n\t\tcase 'integer':\n\t\t\treturn 'number';\n\t\tcase 'boolean':\n\t\t\treturn 'boolean';\n\t\tcase 'array':\n\t\t\treturn `Array<${schemaToTypeString(schema.items)}>`;\n\t\tcase 'object':\n\t\t\tif (schema.properties) {\n\t\t\t\tconst props = Object.entries(schema.properties)\n\t\t\t\t\t.map(\n\t\t\t\t\t\t([key, value]: [string, any]) =>\n\t\t\t\t\t\t\t`${key}: ${schemaToTypeString(value)}`,\n\t\t\t\t\t)\n\t\t\t\t\t.join('; ');\n\t\t\t\treturn `{ ${props} }`;\n\t\t\t}\n\t\t\treturn 'Record<string, unknown>';\n\t\tdefault:\n\t\t\treturn 'unknown';\n\t}\n}\n\nfunction generateReactQueryCode(\n\t_spec: OpenAPISpec,\n\toperations: OperationInfo[],\n\tapiName: string,\n): string {\n\tconst imports = `import { createTypedQueryClient } from '@geekmidas/client/react-query';\nimport type { paths } from './openapi-types';\n\n// Create typed query client\nexport const ${apiName.toLowerCase()} = createTypedQueryClient<paths>({\n baseURL: process.env.NEXT_PUBLIC_API_URL || '/api',\n});\n\n// Export individual hooks for better DX\n`;\n\n\tconst queryHooks = operations\n\t\t.filter((op) => op.method === 'GET')\n\t\t.map((op) => generateQueryHook(op, apiName))\n\t\t.join('\\n\\n');\n\n\tconst mutationHooks = operations\n\t\t.filter((op) => op.method !== 'GET')\n\t\t.map((op) => generateMutationHook(op, apiName))\n\t\t.join('\\n\\n');\n\n\tconst typeExports = generateTypeExports(operations);\n\n\treturn `${imports}\n// Query Hooks\n${queryHooks}\n\n// Mutation Hooks\n${mutationHooks}\n\n// Type exports for convenience\n${typeExports}\n\n// Re-export the api for advanced usage\nexport { ${apiName.toLowerCase()} };\n`;\n}\n\nfunction generateQueryHook(op: OperationInfo, apiName: string): string {\n\tconst hookName = `use${capitalize(op.operationId)}`;\n\tconst endpoint = op.endpoint;\n\tconst hasParams = op.parameters?.some((p) => p.in === 'path');\n\tconst hasQuery = op.parameters?.some((p) => p.in === 'query');\n\n\t// Generate properly typed hook\n\tlet params = '';\n\tlet args = '';\n\n\tif (hasParams || hasQuery) {\n\t\tconst paramParts: string[] = [];\n\t\tif (hasParams) {\n\t\t\tconst pathParams =\n\t\t\t\top.parameters?.filter((p) => p.in === 'path').map((p) => p.name) || [];\n\t\t\tparamParts.push(\n\t\t\t\t`params: { ${pathParams.map((p) => `${p}: string`).join('; ')} }`,\n\t\t\t);\n\t\t}\n\t\tif (hasQuery) {\n\t\t\tparamParts.push(`query?: Record<string, any>`);\n\t\t}\n\t\tparams = `config: { ${paramParts.join('; ')} }, `;\n\t\targs = ', config';\n\t}\n\n\treturn `export const ${hookName} = (\n ${params}options?: Parameters<typeof ${apiName.toLowerCase()}.useQuery>[2]\n) => {\n return ${apiName.toLowerCase()}.useQuery('${endpoint}' as any${args}, options);\n};`;\n}\n\nfunction generateMutationHook(op: OperationInfo, apiName: string): string {\n\tconst hookName = `use${capitalize(op.operationId)}`;\n\tconst endpoint = op.endpoint;\n\n\treturn `export const ${hookName} = (\n options?: Parameters<typeof ${apiName.toLowerCase()}.useMutation>[1]\n) => {\n return ${apiName.toLowerCase()}.useMutation('${endpoint}' as any, options);\n};`;\n}\n\nfunction generateTypeExports(operations: OperationInfo[]): string {\n\tconst exports = operations.map((op) => {\n\t\tconst typeName = capitalize(op.operationId);\n\t\tconst isQuery = op.method === 'GET';\n\n\t\tif (isQuery) {\n\t\t\treturn `export type ${typeName}Response = Awaited<ReturnType<ReturnType<typeof use${typeName}>['data']>>;`;\n\t\t} else {\n\t\t\treturn `export type ${typeName}Response = Awaited<ReturnType<ReturnType<typeof use${typeName}>['mutateAsync']>>;`;\n\t\t}\n\t});\n\n\treturn exports.join('\\n');\n}\n\nfunction capitalize(str: string): string {\n\treturn str.charAt(0).toUpperCase() + str.slice(1);\n}\n"],"mappings":";;;;;;;AAQA,MAAM,aAAA,sBAAYA,CAAAA,CAAAA,UAAAA,CAAUC,mBAAAA,IAAI;AAiBhC,eAAsB,0BACrB,UAA6B,CAAC,GACd;CAChB,MAAM,SAAS;CAEf,IAAI;EAEH,MAAM,YAAY,QAAQ,UAAA,GAASC,UAAAA,KAAAA,CAAK,QAAQ,IAAI,GAAG,cAAc;EAErE,IAAI,EAAA,GAACC,QAAAA,WAAAA,CAAW,SAAS,GACxB,MAAM,IAAI,MACT,6BAA6B,UAAU,0CACxC;EAGD,MAAM,cAAc,OAAA,GAAMC,iBAAAA,SAAAA,CAAS,WAAW,OAAO;EACrD,MAAM,OAAoB,KAAK,MAAM,WAAW;EAGhD,MAAM,aAAA,GAAYC,UAAAA,QAAAA,CACjB,QAAQ,WAAA,GAAUH,UAAAA,KAAAA,CAAK,QAAQ,IAAI,GAAG,OAAO,OAAO,UAAU,CAC/D;EACA,MAAM,aAAA,GAAYA,UAAAA,KAAAA,CAAK,WAAW,oBAAoB;EAEtD,OAAO,IAAI,kDAAkD;E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I,IAAI,MAAM,CAAC;AACjD"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
//#region src/openapi-react-query.d.ts
|
|
2
|
-
interface ReactQueryOptions {
|
|
3
|
-
input?: string;
|
|
4
|
-
output?: string;
|
|
5
|
-
name?: string;
|
|
6
|
-
}
|
|
7
|
-
export declare function generateReactQueryCommand(options?: ReactQueryOptions): Promise<void>;
|
|
8
|
-
//#endregion
|
|
9
|
-
//# sourceMappingURL=openapi-react-query.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"openapi-react-query.d.cts","names":[],"sources":["../src/openapi-react-query.ts"],"mappings":";UAUU;EACT;EACA;EACA;;wBAYqB,0BACrB,UAAS,oBACP"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
//#region src/openapi-react-query.d.ts
|
|
2
|
-
interface ReactQueryOptions {
|
|
3
|
-
input?: string;
|
|
4
|
-
output?: string;
|
|
5
|
-
name?: string;
|
|
6
|
-
}
|
|
7
|
-
export declare function generateReactQueryCommand(options?: ReactQueryOptions): Promise<void>;
|
|
8
|
-
//#endregion
|
|
9
|
-
//# sourceMappingURL=openapi-react-query.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"openapi-react-query.d.mts","names":[],"sources":["../src/openapi-react-query.ts"],"mappings":";UAUU;EACT;EACA;EACA;;wBAYqB,0BACrB,UAAS,oBACP"}
|
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env -S npx tsx
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
4
|
-
import { dirname, join } from "node:path";
|
|
5
|
-
import { exec } from "node:child_process";
|
|
6
|
-
import { promisify } from "node:util";
|
|
7
|
-
//#region src/openapi-react-query.ts
|
|
8
|
-
const execAsync = promisify(exec);
|
|
9
|
-
async function generateReactQueryCommand(options = {}) {
|
|
10
|
-
const logger = console;
|
|
11
|
-
try {
|
|
12
|
-
const inputPath = options.input || join(process.cwd(), "openapi.json");
|
|
13
|
-
if (!existsSync(inputPath)) throw new Error(`OpenAPI spec not found at ${inputPath}. Run 'npx @geekmidas/cli openapi' first.`);
|
|
14
|
-
const specContent = await readFile(inputPath, "utf-8");
|
|
15
|
-
const spec = JSON.parse(specContent);
|
|
16
|
-
const outputDir = dirname(options.output || join(process.cwd(), "src", "api", "hooks.ts"));
|
|
17
|
-
const typesPath = join(outputDir, "openapi-types.d.ts");
|
|
18
|
-
logger.log("Generating TypeScript types from OpenAPI spec...");
|
|
19
|
-
try {
|
|
20
|
-
await execAsync(`npx openapi-typescript "${inputPath}" -o "${typesPath}"`, { cwd: process.cwd() });
|
|
21
|
-
logger.log(`TypeScript types generated: ${typesPath}`);
|
|
22
|
-
} catch (_error) {
|
|
23
|
-
logger.warn("Could not generate types with openapi-typescript. Install it for better type inference.");
|
|
24
|
-
logger.warn("Run: npm install -D openapi-typescript");
|
|
25
|
-
await mkdir(dirname(typesPath), { recursive: true });
|
|
26
|
-
await writeFile(typesPath, `// Auto-generated placeholder types
|
|
27
|
-
export interface paths {
|
|
28
|
-
[path: string]: {
|
|
29
|
-
[method: string]: {
|
|
30
|
-
operationId?: string;
|
|
31
|
-
parameters?: any;
|
|
32
|
-
requestBody?: any;
|
|
33
|
-
responses?: any;
|
|
34
|
-
};
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
`);
|
|
38
|
-
}
|
|
39
|
-
const operations = extractOperations(spec);
|
|
40
|
-
const code = generateReactQueryCode(spec, operations, options.name || "API");
|
|
41
|
-
const outputPath = options.output || join(process.cwd(), "src", "api", "hooks.ts");
|
|
42
|
-
await mkdir(dirname(outputPath), { recursive: true });
|
|
43
|
-
await writeFile(outputPath, code);
|
|
44
|
-
logger.log(`React Query hooks generated: ${outputPath}`);
|
|
45
|
-
logger.log(`Generated ${operations.length} hooks`);
|
|
46
|
-
} catch (error) {
|
|
47
|
-
throw new Error(`React Query generation failed: ${error.message}`);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
function extractOperations(spec) {
|
|
51
|
-
const operations = [];
|
|
52
|
-
Object.entries(spec.paths).forEach(([path, methods]) => {
|
|
53
|
-
Object.entries(methods).forEach(([method, operation]) => {
|
|
54
|
-
if (operation.operationId) operations.push({
|
|
55
|
-
operationId: operation.operationId,
|
|
56
|
-
path,
|
|
57
|
-
method: method.toUpperCase(),
|
|
58
|
-
endpoint: `${method.toUpperCase()} ${path}`,
|
|
59
|
-
parameters: operation.parameters,
|
|
60
|
-
requestBody: !!operation.requestBody,
|
|
61
|
-
responseType: extractResponseType(operation)
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
return operations;
|
|
66
|
-
}
|
|
67
|
-
function extractResponseType(operation) {
|
|
68
|
-
const responses = operation.responses;
|
|
69
|
-
if (!responses) return "unknown";
|
|
70
|
-
const successResponse = responses["200"] || responses["201"];
|
|
71
|
-
if (!successResponse?.content?.["application/json"]?.schema) return "unknown";
|
|
72
|
-
const schema = successResponse.content["application/json"].schema;
|
|
73
|
-
return schemaToTypeString(schema);
|
|
74
|
-
}
|
|
75
|
-
function schemaToTypeString(schema) {
|
|
76
|
-
if (!schema) return "unknown";
|
|
77
|
-
switch (schema.type) {
|
|
78
|
-
case "string": return "string";
|
|
79
|
-
case "number":
|
|
80
|
-
case "integer": return "number";
|
|
81
|
-
case "boolean": return "boolean";
|
|
82
|
-
case "array": return `Array<${schemaToTypeString(schema.items)}>`;
|
|
83
|
-
case "object":
|
|
84
|
-
if (schema.properties) return `{ ${Object.entries(schema.properties).map(([key, value]) => `${key}: ${schemaToTypeString(value)}`).join("; ")} }`;
|
|
85
|
-
return "Record<string, unknown>";
|
|
86
|
-
default: return "unknown";
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
function generateReactQueryCode(_spec, operations, apiName) {
|
|
90
|
-
return `${`import { createTypedQueryClient } from '@geekmidas/client/react-query';
|
|
91
|
-
import type { paths } from './openapi-types';
|
|
92
|
-
|
|
93
|
-
// Create typed query client
|
|
94
|
-
export const ${apiName.toLowerCase()} = createTypedQueryClient<paths>({
|
|
95
|
-
baseURL: process.env.NEXT_PUBLIC_API_URL || '/api',
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
// Export individual hooks for better DX
|
|
99
|
-
`}
|
|
100
|
-
// Query Hooks
|
|
101
|
-
${operations.filter((op) => op.method === "GET").map((op) => generateQueryHook(op, apiName)).join("\n\n")}
|
|
102
|
-
|
|
103
|
-
// Mutation Hooks
|
|
104
|
-
${operations.filter((op) => op.method !== "GET").map((op) => generateMutationHook(op, apiName)).join("\n\n")}
|
|
105
|
-
|
|
106
|
-
// Type exports for convenience
|
|
107
|
-
${generateTypeExports(operations)}
|
|
108
|
-
|
|
109
|
-
// Re-export the api for advanced usage
|
|
110
|
-
export { ${apiName.toLowerCase()} };
|
|
111
|
-
`;
|
|
112
|
-
}
|
|
113
|
-
function generateQueryHook(op, apiName) {
|
|
114
|
-
const hookName = `use${capitalize(op.operationId)}`;
|
|
115
|
-
const endpoint = op.endpoint;
|
|
116
|
-
const hasParams = op.parameters?.some((p) => p.in === "path");
|
|
117
|
-
const hasQuery = op.parameters?.some((p) => p.in === "query");
|
|
118
|
-
let params = "";
|
|
119
|
-
let args = "";
|
|
120
|
-
if (hasParams || hasQuery) {
|
|
121
|
-
const paramParts = [];
|
|
122
|
-
if (hasParams) {
|
|
123
|
-
const pathParams = op.parameters?.filter((p) => p.in === "path").map((p) => p.name) || [];
|
|
124
|
-
paramParts.push(`params: { ${pathParams.map((p) => `${p}: string`).join("; ")} }`);
|
|
125
|
-
}
|
|
126
|
-
if (hasQuery) paramParts.push(`query?: Record<string, any>`);
|
|
127
|
-
params = `config: { ${paramParts.join("; ")} }, `;
|
|
128
|
-
args = ", config";
|
|
129
|
-
}
|
|
130
|
-
return `export const ${hookName} = (
|
|
131
|
-
${params}options?: Parameters<typeof ${apiName.toLowerCase()}.useQuery>[2]
|
|
132
|
-
) => {
|
|
133
|
-
return ${apiName.toLowerCase()}.useQuery('${endpoint}' as any${args}, options);
|
|
134
|
-
};`;
|
|
135
|
-
}
|
|
136
|
-
function generateMutationHook(op, apiName) {
|
|
137
|
-
const hookName = `use${capitalize(op.operationId)}`;
|
|
138
|
-
const endpoint = op.endpoint;
|
|
139
|
-
return `export const ${hookName} = (
|
|
140
|
-
options?: Parameters<typeof ${apiName.toLowerCase()}.useMutation>[1]
|
|
141
|
-
) => {
|
|
142
|
-
return ${apiName.toLowerCase()}.useMutation('${endpoint}' as any, options);
|
|
143
|
-
};`;
|
|
144
|
-
}
|
|
145
|
-
function generateTypeExports(operations) {
|
|
146
|
-
return operations.map((op) => {
|
|
147
|
-
const typeName = capitalize(op.operationId);
|
|
148
|
-
if (op.method === "GET") return `export type ${typeName}Response = Awaited<ReturnType<ReturnType<typeof use${typeName}>['data']>>;`;
|
|
149
|
-
else return `export type ${typeName}Response = Awaited<ReturnType<ReturnType<typeof use${typeName}>['mutateAsync']>>;`;
|
|
150
|
-
}).join("\n");
|
|
151
|
-
}
|
|
152
|
-
function capitalize(str) {
|
|
153
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
154
|
-
}
|
|
155
|
-
//#endregion
|
|
156
|
-
export { generateReactQueryCommand };
|
|
157
|
-
|
|
158
|
-
//# sourceMappingURL=openapi-react-query.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"openapi-react-query.mjs","names":[],"sources":["../src/openapi-react-query.ts"],"sourcesContent":["#!/usr/bin/env -S npx tsx\n\nimport { exec } from 'node:child_process';\nimport { existsSync } from 'node:fs';\nimport { mkdir, readFile, writeFile } from 'node:fs/promises';\nimport { dirname, join } from 'node:path';\nimport { promisify } from 'node:util';\n\nconst execAsync = promisify(exec);\n\ninterface ReactQueryOptions {\n\tinput?: string;\n\toutput?: string;\n\tname?: string;\n}\n\ninterface OpenAPISpec {\n\topenapi: string;\n\tinfo?: {\n\t\ttitle?: string;\n\t\tversion?: string;\n\t};\n\tpaths: Record<string, Record<string, any>>;\n}\n\nexport async function generateReactQueryCommand(\n\toptions: ReactQueryOptions = {},\n): Promise<void> {\n\tconst logger = console;\n\n\ttry {\n\t\t// Read OpenAPI spec\n\t\tconst inputPath = options.input || join(process.cwd(), 'openapi.json');\n\n\t\tif (!existsSync(inputPath)) {\n\t\t\tthrow new Error(\n\t\t\t\t`OpenAPI spec not found at ${inputPath}. Run 'npx @geekmidas/cli openapi' first.`,\n\t\t\t);\n\t\t}\n\n\t\tconst specContent = await readFile(inputPath, 'utf-8');\n\t\tconst spec: OpenAPISpec = JSON.parse(specContent);\n\n\t\t// Generate TypeScript types from OpenAPI spec\n\t\tconst outputDir = dirname(\n\t\t\toptions.output || join(process.cwd(), 'src', 'api', 'hooks.ts'),\n\t\t);\n\t\tconst typesPath = join(outputDir, 'openapi-types.d.ts');\n\n\t\tlogger.log('Generating TypeScript types from OpenAPI spec...');\n\n\t\ttry {\n\t\t\t// Use npx to run openapi-typescript\n\t\t\tawait execAsync(\n\t\t\t\t`npx openapi-typescript \"${inputPath}\" -o \"${typesPath}\"`,\n\t\t\t\t{ cwd: process.cwd() },\n\t\t\t);\n\t\t\tlogger.log(`TypeScript types generated: ${typesPath}`);\n\t\t} catch (_error) {\n\t\t\tlogger.warn(\n\t\t\t\t'Could not generate types with openapi-typescript. Install it for better type inference.',\n\t\t\t);\n\t\t\tlogger.warn('Run: npm install -D openapi-typescript');\n\n\t\t\t// Generate basic types file\n\t\t\tawait mkdir(dirname(typesPath), { recursive: true });\n\t\t\tawait writeFile(\n\t\t\t\ttypesPath,\n\t\t\t\t`// Auto-generated placeholder types\nexport interface paths {\n [path: string]: {\n [method: string]: {\n operationId?: string;\n parameters?: any;\n requestBody?: any;\n responses?: any;\n };\n };\n}\n`,\n\t\t\t);\n\t\t}\n\n\t\t// Extract operation info\n\t\tconst operations = extractOperations(spec);\n\n\t\t// Generate TypeScript code\n\t\tconst code = generateReactQueryCode(\n\t\t\tspec,\n\t\t\toperations,\n\t\t\toptions.name || 'API',\n\t\t);\n\n\t\t// Write output\n\t\tconst outputPath =\n\t\t\toptions.output || join(process.cwd(), 'src', 'api', 'hooks.ts');\n\t\tawait mkdir(dirname(outputPath), { recursive: true });\n\t\tawait writeFile(outputPath, code);\n\n\t\tlogger.log(`React Query hooks generated: ${outputPath}`);\n\t\tlogger.log(`Generated ${operations.length} hooks`);\n\t} catch (error) {\n\t\tthrow new Error(\n\t\t\t`React Query generation failed: ${(error as Error).message}`,\n\t\t);\n\t}\n}\n\ninterface OperationInfo {\n\toperationId: string;\n\tpath: string;\n\tmethod: string;\n\tendpoint: string; // Full endpoint like 'GET /users/{id}'\n\tparameters?: Array<{ name: string; in: string; required?: boolean }>;\n\trequestBody?: boolean;\n\tresponseType?: string;\n}\n\nfunction extractOperations(spec: OpenAPISpec): OperationInfo[] {\n\tconst operations: OperationInfo[] = [];\n\n\tObject.entries(spec.paths).forEach(([path, methods]) => {\n\t\tObject.entries(methods).forEach(([method, operation]) => {\n\t\t\tif (operation.operationId) {\n\t\t\t\toperations.push({\n\t\t\t\t\toperationId: operation.operationId,\n\t\t\t\t\tpath,\n\t\t\t\t\tmethod: method.toUpperCase(),\n\t\t\t\t\tendpoint: `${method.toUpperCase()} ${path}`,\n\t\t\t\t\tparameters: operation.parameters,\n\t\t\t\t\trequestBody: !!operation.requestBody,\n\t\t\t\t\tresponseType: extractResponseType(operation),\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t});\n\n\treturn operations;\n}\n\nfunction extractResponseType(operation: any): string {\n\tconst responses = operation.responses;\n\tif (!responses) return 'unknown';\n\n\tconst successResponse = responses['200'] || responses['201'];\n\tif (!successResponse?.content?.['application/json']?.schema) {\n\t\treturn 'unknown';\n\t}\n\n\t// Basic type inference from schema\n\tconst schema = successResponse.content['application/json'].schema;\n\treturn schemaToTypeString(schema);\n}\n\nfunction schemaToTypeString(schema: any): string {\n\tif (!schema) return 'unknown';\n\n\tswitch (schema.type) {\n\t\tcase 'string':\n\t\t\treturn 'string';\n\t\tcase 'number':\n\t\tcase 'integer':\n\t\t\treturn 'number';\n\t\tcase 'boolean':\n\t\t\treturn 'boolean';\n\t\tcase 'array':\n\t\t\treturn `Array<${schemaToTypeString(schema.items)}>`;\n\t\tcase 'object':\n\t\t\tif (schema.properties) {\n\t\t\t\tconst props = Object.entries(schema.properties)\n\t\t\t\t\t.map(\n\t\t\t\t\t\t([key, value]: [string, any]) =>\n\t\t\t\t\t\t\t`${key}: ${schemaToTypeString(value)}`,\n\t\t\t\t\t)\n\t\t\t\t\t.join('; ');\n\t\t\t\treturn `{ ${props} }`;\n\t\t\t}\n\t\t\treturn 'Record<string, unknown>';\n\t\tdefault:\n\t\t\treturn 'unknown';\n\t}\n}\n\nfunction generateReactQueryCode(\n\t_spec: OpenAPISpec,\n\toperations: OperationInfo[],\n\tapiName: string,\n): string {\n\tconst imports = `import { createTypedQueryClient } from '@geekmidas/client/react-query';\nimport type { paths } from './openapi-types';\n\n// Create typed query client\nexport const ${apiName.toLowerCase()} = createTypedQueryClient<paths>({\n baseURL: process.env.NEXT_PUBLIC_API_URL || '/api',\n});\n\n// Export individual hooks for better DX\n`;\n\n\tconst queryHooks = operations\n\t\t.filter((op) => op.method === 'GET')\n\t\t.map((op) => generateQueryHook(op, apiName))\n\t\t.join('\\n\\n');\n\n\tconst mutationHooks = operations\n\t\t.filter((op) => op.method !== 'GET')\n\t\t.map((op) => generateMutationHook(op, apiName))\n\t\t.join('\\n\\n');\n\n\tconst typeExports = generateTypeExports(operations);\n\n\treturn `${imports}\n// Query Hooks\n${queryHooks}\n\n// Mutation Hooks\n${mutationHooks}\n\n// Type exports for convenience\n${typeExports}\n\n// Re-export the api for advanced usage\nexport { ${apiName.toLowerCase()} };\n`;\n}\n\nfunction generateQueryHook(op: OperationInfo, apiName: string): string {\n\tconst hookName = `use${capitalize(op.operationId)}`;\n\tconst endpoint = op.endpoint;\n\tconst hasParams = op.parameters?.some((p) => p.in === 'path');\n\tconst hasQuery = op.parameters?.some((p) => p.in === 'query');\n\n\t// Generate properly typed hook\n\tlet params = '';\n\tlet args = '';\n\n\tif (hasParams || hasQuery) {\n\t\tconst paramParts: string[] = [];\n\t\tif (hasParams) {\n\t\t\tconst pathParams =\n\t\t\t\top.parameters?.filter((p) => p.in === 'path').map((p) => p.name) || [];\n\t\t\tparamParts.push(\n\t\t\t\t`params: { ${pathParams.map((p) => `${p}: string`).join('; ')} }`,\n\t\t\t);\n\t\t}\n\t\tif (hasQuery) {\n\t\t\tparamParts.push(`query?: Record<string, any>`);\n\t\t}\n\t\tparams = `config: { ${paramParts.join('; ')} }, `;\n\t\targs = ', config';\n\t}\n\n\treturn `export const ${hookName} = (\n ${params}options?: Parameters<typeof ${apiName.toLowerCase()}.useQuery>[2]\n) => {\n return ${apiName.toLowerCase()}.useQuery('${endpoint}' as any${args}, options);\n};`;\n}\n\nfunction generateMutationHook(op: OperationInfo, apiName: string): string {\n\tconst hookName = `use${capitalize(op.operationId)}`;\n\tconst endpoint = op.endpoint;\n\n\treturn `export const ${hookName} = (\n options?: Parameters<typeof ${apiName.toLowerCase()}.useMutation>[1]\n) => {\n return ${apiName.toLowerCase()}.useMutation('${endpoint}' as any, options);\n};`;\n}\n\nfunction generateTypeExports(operations: OperationInfo[]): string {\n\tconst exports = operations.map((op) => {\n\t\tconst typeName = capitalize(op.operationId);\n\t\tconst isQuery = op.method === 'GET';\n\n\t\tif (isQuery) {\n\t\t\treturn `export type ${typeName}Response = Awaited<ReturnType<ReturnType<typeof use${typeName}>['data']>>;`;\n\t\t} else {\n\t\t\treturn `export type ${typeName}Response = Awaited<ReturnType<ReturnType<typeof use${typeName}>['mutateAsync']>>;`;\n\t\t}\n\t});\n\n\treturn exports.join('\\n');\n}\n\nfunction capitalize(str: string): string {\n\treturn str.charAt(0).toUpperCase() + str.slice(1);\n}\n"],"mappings":";;;;;;;AAQA,MAAM,YAAY,UAAU,IAAI;AAiBhC,eAAsB,0BACr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