@jsonpang/openapi-react-query-code-generator
Advanced tools
Comparing version 0.1.5 to 0.1.6
{ | ||
"name": "@jsonpang/openapi-react-query-code-generator", | ||
"description": "Uses openapi-typescript-codegen under the hood, and then analyzes the TypeScript AST of the generated files to create TanStack React Query hooks. Supports server-sided rendering w/ NextJS by using client instances of openapi-typescript-codegen.", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"dependencies": { | ||
@@ -6,0 +6,0 @@ "@tanstack/react-query": "^5.17.15", |
@@ -73,2 +73,6 @@ "use strict"; | ||
.join("\n"), | ||
isQuery: !!method | ||
.getBody() | ||
?.getText() | ||
.match(/method:.*get/i), | ||
params: method | ||
@@ -86,3 +90,2 @@ .getParameters()[0] | ||
}), | ||
typeParams: method.getTypeParameters(), | ||
})); | ||
@@ -92,2 +95,3 @@ } | ||
async function generateReactQueryHooksFile({ methods, outputPath, }) { | ||
console.log("Methods:", methods); | ||
const project = new ts_morph_1.Project({ | ||
@@ -119,4 +123,13 @@ compilerOptions: { | ||
${methods | ||
.map((method) => ` | ||
${methods.map((method) => (method.isQuery ? createUseQueryFunction({ method }) : createUseMutationFunction({ method }))).join("\n")} | ||
`); | ||
await file.save(); | ||
formatFileWithPrettier(file.getFilePath(), file.getFilePath()); | ||
} | ||
exports.generateReactQueryHooksFile = generateReactQueryHooksFile; | ||
function createUseQueryFunction({ method }) { | ||
return ` | ||
export const QueryKey${(0, exports.capitalizeFirstLetter)(method.name)} = '${method.name}'; | ||
export const use${(0, exports.capitalizeFirstLetter)(method.name)} = < | ||
@@ -133,3 +146,3 @@ TData = Awaited<ReturnType<typeof DefaultService.prototype.${method.name}>>, | ||
return useQuery<TData, TError>({ | ||
queryKey: ['${method.name}', params], | ||
queryKey: [QueryKey${(0, exports.capitalizeFirstLetter)(method.name)}, params], | ||
queryFn: () => | ||
@@ -139,9 +152,24 @@ apiService.${method.name}(params) as TData, | ||
})}; | ||
`) | ||
.join("\n")} | ||
`); | ||
await file.save(); | ||
formatFileWithPrettier(file.getFilePath(), file.getFilePath()); | ||
`; | ||
} | ||
exports.generateReactQueryHooksFile = generateReactQueryHooksFile; | ||
function createUseMutationFunction({ method }) { | ||
const paramType = `{ ${method.params.map((param) => `${param.name}: ${param.type};`).join("\n ")} }`; | ||
return ` | ||
export const use${(0, exports.capitalizeFirstLetter)(method.name)} = < | ||
TData = Awaited<ReturnType<typeof DefaultService.prototype.${method.name}>>, | ||
TError = unknown, | ||
TContext = unknown | ||
>( | ||
mutationOptions?: UseMutationOptions<TData, TError, ${paramType}, TContext> | ||
) => { | ||
const apiService = useContext(ApiServiceContext) as DefaultService; | ||
return useMutation<TData, TError, ${paramType}, TContext>({ | ||
mutationFn: ( | ||
params: ${paramType} | ||
) => | ||
apiService.${method.name}(params) as Promise<TData>, | ||
...mutationOptions, | ||
})}; | ||
`; | ||
} | ||
//# sourceMappingURL=Program.js.map |
{ | ||
"name": "@jsonpang/openapi-react-query-code-generator", | ||
"description": "Uses openapi-typescript-codegen under the hood, and then analyzes the TypeScript AST of the generated files to create TanStack React Query hooks. Supports server-sided rendering w/ NextJS by using client instances of openapi-typescript-codegen.", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"dependencies": { | ||
@@ -6,0 +6,0 @@ "@tanstack/react-query": "^5.17.15", |
@@ -87,2 +87,3 @@ import { generate as openapiTypescriptCodegenWriter } from "openapi-typescript-codegen"; | ||
docs: string; | ||
isQuery: boolean; | ||
params: Array<{ name: string; type: string }>; | ||
@@ -115,2 +116,6 @@ } | ||
.join("\n"), | ||
isQuery: !!method | ||
.getBody() | ||
?.getText() | ||
.match(/method:.*get/i), | ||
params: method | ||
@@ -131,3 +136,2 @@ .getParameters()[0] | ||
}), | ||
typeParams: method.getTypeParameters(), | ||
})) as AnalyzedMethod[]; | ||
@@ -143,2 +147,3 @@ } | ||
}) { | ||
console.log("Methods:", methods); | ||
const project = new Project({ | ||
@@ -178,5 +183,14 @@ compilerOptions: { | ||
${methods | ||
.map( | ||
(method) => ` | ||
${methods.map((method) => (method.isQuery ? createUseQueryFunction({ method }) : createUseMutationFunction({ method }))).join("\n")} | ||
` | ||
); | ||
await file.save(); | ||
formatFileWithPrettier(file.getFilePath(), file.getFilePath()); | ||
} | ||
function createUseQueryFunction({ method }: { method: AnalyzedMethod }) { | ||
return ` | ||
export const QueryKey${capitalizeFirstLetter(method.name)} = '${method.name}'; | ||
export const use${capitalizeFirstLetter(method.name)} = < | ||
@@ -193,3 +207,3 @@ TData = Awaited<ReturnType<typeof DefaultService.prototype.${method.name}>>, | ||
return useQuery<TData, TError>({ | ||
queryKey: ['${method.name}', params], | ||
queryKey: [QueryKey${capitalizeFirstLetter(method.name)}, params], | ||
queryFn: () => | ||
@@ -199,9 +213,24 @@ apiService.${method.name}(params) as TData, | ||
})}; | ||
` | ||
) | ||
.join("\n")} | ||
` | ||
); | ||
await file.save(); | ||
formatFileWithPrettier(file.getFilePath(), file.getFilePath()); | ||
`; | ||
} | ||
function createUseMutationFunction({ method }: { method: AnalyzedMethod }) { | ||
const paramType = `{ ${method.params.map((param) => `${param.name}: ${param.type};`).join("\n ")} }`; | ||
return ` | ||
export const use${capitalizeFirstLetter(method.name)} = < | ||
TData = Awaited<ReturnType<typeof DefaultService.prototype.${method.name}>>, | ||
TError = unknown, | ||
TContext = unknown | ||
>( | ||
mutationOptions?: UseMutationOptions<TData, TError, ${paramType}, TContext> | ||
) => { | ||
const apiService = useContext(ApiServiceContext) as DefaultService; | ||
return useMutation<TData, TError, ${paramType}, TContext>({ | ||
mutationFn: ( | ||
params: ${paramType} | ||
) => | ||
apiService.${method.name}(params) as Promise<TData>, | ||
...mutationOptions, | ||
})}; | ||
`; | ||
} |
Sorry, the diff of this file is not supported yet
31296
701