Skip to content

Commit

Permalink
patch(jsdoc): add jsdoc codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
seriouslag committed Jan 3, 2024
1 parent b0f2f6d commit ac1f8a4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/react-app/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ paths:
$ref: '#/components/schemas/Error'
/not-defined:
get:
deprecated: true
description: This path is not fully defined.
responses:
default:
Expand Down
22 changes: 20 additions & 2 deletions src/createExports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ts from "typescript";
import ts, { JSDoc } from "typescript";
import { sync } from "glob";
import { join } from "path";
import fs from "fs";
Expand Down Expand Up @@ -45,8 +45,26 @@ export const createExports = (generatedClientsPath: string) => {
const httpMethodName = properties
.find((p) => p.name?.getText(node) === "method")
?.initializer?.getText(node)!;


const getAllChildren = (tsNode: ts.Node): Array<ts.Node> => {
const childItems = tsNode.getChildren(node);
if (childItems.length) {
const allChildren = childItems.map(getAllChildren);
return [tsNode].concat(allChildren.flat());
}
return [tsNode];
}

const children = getAllChildren(method);
const jsDoc = children.filter((c) => c.kind === ts.SyntaxKind.JSDoc).map((c) => {
return (c as JSDoc).comment
});
const hasDeprecated = children
.some((c) => c.kind === ts.SyntaxKind.JSDocDeprecatedTag);

return httpMethodName === "'GET'"
? createUseQuery(node, className, method)
? createUseQuery(node, className, method, jsDoc, hasDeprecated)
: createUseMutation(node, className, method);
})
.flat();
Expand Down
42 changes: 40 additions & 2 deletions src/createUseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { capitalizeFirstLetter } from "./common";
export const createUseQuery = (
node: ts.SourceFile,
className: string,
method: ts.MethodDeclaration
method: ts.MethodDeclaration,
jsDoc: (string | ts.NodeArray<ts.JSDocComment> | undefined)[] = [],
deprecated: boolean = false
) => {
const methodName = method.name?.getText(node)!;
let requestParam = [];
Expand Down Expand Up @@ -312,5 +314,41 @@ export const createUseQuery = (
)
);

return [defaultApiResponse, returnTypeExport, queryKeyExport, hookExport];
const deprecatedString = deprecated ? "@deprecated" : "";

const jsDocString = [deprecatedString]
.concat(
jsDoc.map((comment) => {
if (typeof comment === "string") {
return comment;
}
if (Array.isArray(comment)) {
return comment
.map((c) => c.getText(node))
.join("\n");
}
return "";
})
)
// remove empty lines
.filter(Boolean)
// trim
.map((comment) => comment.trim())
// add * to each line
.map((comment) => `* ${comment}`)
// join lines
.join("\n")
// replace new lines with \n *
.replace(/\n/g, "\n * ");

const hookWithJSDoc = jsDocString
? ts.addSyntheticLeadingComment(
hookExport,
ts.SyntaxKind.MultiLineCommentTrivia,
`*\n ${jsDocString}\n `,
true,
)
: hookExport;

return [defaultApiResponse, returnTypeExport, queryKeyExport, hookWithJSDoc];
};

0 comments on commit ac1f8a4

Please sign in to comment.