Skip to content

Commit 667f647

Browse files
committed
add jsdocs to declared operations
1 parent f557b3e commit 667f647

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/core/resolvers/declared-operation.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import getContentSchema from "./content";
22
import resolveEndpoints from "./enpoint";
33
import { defaultOperationName } from "./operation-name";
4-
import { resolveOperationParams } from "./operation-param";
4+
import { resolveDocParams, resolveOperationParams } from "./operation-param";
55
import { getResponse } from "./response";
66
import { resolveSchema } from "./schema-definition";
77
import type { PathsObject } from "@omer-x/openapi-types/paths";
88
import type { ResponsesObject } from "@omer-x/openapi-types/response";
99

1010
export type DeclaredOperation = {
1111
name: string,
12+
desription: string,
1213
parameters: string,
14+
docParams: {
15+
name: string,
16+
type: string,
17+
description: string,
18+
}[],
1319
result: string,
1420
};
1521

1622
function resolveOperationResult(responses?: ResponsesObject) {
1723
if (!responses) return "unknown";
1824
const schemas = Object.values(responses).map(resp => {
19-
if (!resp) return null;
2025
const response = getResponse(resp);
2126
if (!response.content) return null;
2227
const schema = getContentSchema(response.content);
@@ -29,7 +34,9 @@ function resolveOperationResult(responses?: ResponsesObject) {
2934
export function resolveDeclaredOperations(paths: PathsObject, framework: string | null) {
3035
return resolveEndpoints(paths).map<DeclaredOperation>(({ method, path, operation }) => ({
3136
name: operation.operationId || defaultOperationName(method, path),
37+
desription: operation.description || "missing-description",
3238
parameters: resolveOperationParams(operation, method, true, framework).join(", "),
39+
docParams: resolveDocParams(operation, method, framework),
3340
result: resolveOperationResult(operation.responses),
3441
}));
3542
}

src/core/resolvers/operation-param.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,41 @@ export function resolveOperationParams(operation: OperationObject, method: strin
5353
return collection;
5454
}
5555

56+
function resolveDocParam(parameter: ParameterObject | ReferenceObject) {
57+
const param = getParameter(parameter);
58+
return {
59+
name: param.name,
60+
type: resolveSchema(param.schema),
61+
description: param.description || "missing-description",
62+
};
63+
}
64+
65+
export function resolveDocParams(operation: OperationObject, method: string, framework: string | null) {
66+
const resolvedParams = (operation.parameters ?? [])
67+
.filter(param => "in" in param && (param.in === "path" || param.in === "query"))
68+
.toSorted(sortRequiredParamsFirst)
69+
.map(resolveDocParam);
70+
const collection = [
71+
...resolvedParams,
72+
];
73+
if (operation.requestBody) {
74+
const body = getBodyRequest(operation.requestBody);
75+
collection.push({
76+
name: hasFormData(operation) ? "formBody" : "requestBody",
77+
type: resolveSchema(getContentSchema(body.content)),
78+
description: "Request body",
79+
});
80+
}
81+
if (framework === "next" && method.toUpperCase() === "GET") {
82+
collection.unshift({
83+
name: "cacheTag",
84+
type: "string | null",
85+
description: "Tag name of Next.js fetch cache",
86+
});
87+
}
88+
return collection;
89+
}
90+
5691
export function getParameter(source: ParameterObject | ReferenceObject) {
5792
if ("$ref" in source) throw new Error("Parameter references not implemented yet.");
5893
return source;

src/templates/declaration.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@
22
import type {{this}} from "./schemas/{{this}}";
33
{{/each}}
44
{{#each operations}}
5+
/**
6+
* {{desription}}
7+
{{#each docParams}}
8+
* @param { {{~type~}} } {{name}} - {{description}}
9+
{{/each}}
10+
*/
511
export declare function {{name}}({{{parameters}}}): Promise<{{result}}>;
612
{{/each}}

0 commit comments

Comments
 (0)