Skip to content

Fix breaking JSDoc changes (#1012) #1029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,944 changes: 1,972 additions & 1,972 deletions examples/digital-ocean-api.ts

Large diffs are not rendered by default.

17,932 changes: 8,966 additions & 8,966 deletions examples/github-api-next.ts

Large diffs are not rendered by default.

12,190 changes: 6,095 additions & 6,095 deletions examples/github-api.ts

Large diffs are not rendered by default.

1,620 changes: 810 additions & 810 deletions examples/octokit-ghes-3.6-diff-to-api.ts

Large diffs are not rendered by default.

3,410 changes: 1,705 additions & 1,705 deletions examples/stripe-api.ts

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import transformParameterObject from "./transform/parameter-object.js";
import transformRequestBodyObject from "./transform/request-body-object.js";
import transformResponseObject from "./transform/response-object.js";
import transformSchemaObject from "./transform/schema-object.js";
import { error, escObjKey, getDefaultFetch, getEntries, indent } from "./utils.js";
import { error, escObjKey, getDefaultFetch, getEntries, getSchemaObjectComment, indent } from "./utils.js";
export * from "./types.js"; // expose all types to consumers

const EMPTY_OBJECT_RE = /^\s*\{?\s*\}?\s*$/;
Expand Down Expand Up @@ -125,6 +125,7 @@ async function openapiTS(
const key = escObjKey(subschemaID);
const path = `${subschemaID}#`;
let subschemaOutput = "";
let comment: string | undefined;
switch (subschema.hint) {
case "OpenAPI3": {
const subschemaTypes = transformSchema(subschema.schema, { ...ctx, indentLv });
Expand All @@ -144,6 +145,7 @@ async function openapiTS(
break;
}
case "OperationObject": {
comment = getSchemaObjectComment(subschema.schema, indentLv);
subschemaOutput = transformOperationObject(subschema.schema, { path, ctx: { ...ctx, indentLv } });
break;
}
Expand All @@ -169,6 +171,7 @@ async function openapiTS(
}
}
if (subschemaOutput && !EMPTY_OBJECT_RE.test(subschemaOutput)) {
if (comment) output.push(indent(comment, indentLv));
output.push(indent(`${key}: ${subschemaOutput}`, indentLv));
}
delete allSchemas[subschemaID];
Expand All @@ -182,8 +185,9 @@ async function openapiTS(
// 3. operations (only get fully built after all external schemas transformed)
if (Object.keys(ctx.operations).length) {
output.push(options.exportType ? "export type operations = {" : "export interface operations {", "");
for (const k of Object.keys(ctx.operations)) {
output.push(indent(`${escObjKey(k)}: ${ctx.operations[k]};`, 1));
for (const [key, { operationType, comment }] of Object.entries(ctx.operations)) {
if (comment) output.push(indent(comment, 1));
output.push(indent(`${escObjKey(key)}: ${operationType};`, 1));
}
output.push(`}${options.exportType ? ";" : ""}`, "");
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/transform/operation-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export default function transformOperationObject(
let { indentLv } = ctx;
const output: string[] = wrapObject ? ["{"] : [];
indentLv++;
const c = getSchemaObjectComment(operationObject, indentLv);
if (c) output.push(indent(c, indentLv));

// parameters
{
Expand All @@ -51,7 +49,7 @@ export default function transformOperationObject(
key = tsOptionalProperty(key);
}
const c = getSchemaObjectComment(p, indentLv);
if (c) parameterOutput.push(indent(c, indentLv));
if (c) inlineOutput.push(indent(c, indentLv));
const parameterType = transformParameterObject(p, {
path: `${path}/parameters/${p.name}`,
ctx: { ...ctx, indentLv },
Expand Down
5 changes: 4 additions & 1 deletion src/transform/path-item-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export default function transformPathItemObject(
// if operationId exists, move into an `operations` export and pass the reference in here
else if (operationObject.operationId) {
const operationType = transformOperationObject(operationObject, { path, ctx: { ...ctx, indentLv: 1 } });
ctx.operations[operationObject.operationId] = operationType;
ctx.operations[operationObject.operationId] = {
operationType,
comment: getSchemaObjectComment(operationObject, 1),
};
output.push(indent(`${method}: operations[${escStr(operationObject.operationId)}];`, indentLv));
} else {
const operationType = transformOperationObject(operationObject, { path, ctx: { ...ctx, indentLv } });
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,13 @@ export interface GlobalContext {
postTransform: OpenAPITSOptions["postTransform"];
immutableTypes: boolean;
indentLv: number;
operations: Record<string, string>;
operations: Record<
string,
{
comment?: string;
operationType: string;
}
>;
pathParamsAsTypes: boolean;
silent: boolean;
supportArrayLength: boolean;
Expand Down
15 changes: 12 additions & 3 deletions test/path-item-object.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PathItemObject } from "../src/types";
import type { GlobalContext, PathItemObject } from "../src/types";
import transformPathItemObject, { TransformPathItemObjectOptions } from "../src/transform/path-item-object.js";

const options: TransformPathItemObjectOptions = {
Expand All @@ -23,6 +23,7 @@ describe("Path Item Object", () => {
test("basic", () => {
const schema: PathItemObject = {
get: {
description: "Basic GET",
responses: {
200: {
description: "OK",
Expand Down Expand Up @@ -57,6 +58,7 @@ describe("Path Item Object", () => {
},
},
post: {
description: "Basic POST",
requestBody: {
content: {
"application/json": { $ref: 'components["schemas"]["User"]' },
Expand All @@ -70,6 +72,7 @@ describe("Path Item Object", () => {
};
const generated = transformPathItemObject(schema, options);
expect(generated).toBe(`{
/** @description Basic GET */
get: {
responses: {
/** @description OK */
Expand All @@ -92,6 +95,7 @@ describe("Path Item Object", () => {
};
};
};
/** @description Basic POST */
post: {
requestBody?: {
content: {
Expand All @@ -107,9 +111,10 @@ describe("Path Item Object", () => {
});

test("operations", () => {
const operations: Record<string, string> = {};
const operations: GlobalContext["operations"] = {};
const schema: PathItemObject = {
get: {
description: "Get a user",
operationId: "getUser",
responses: {
200: {
Expand All @@ -132,10 +137,13 @@ describe("Path Item Object", () => {
};
const generated = transformPathItemObject(schema, { ...options, ctx: { ...options.ctx, operations } });
expect(generated).toBe(`{
/** @description Get a user */
get: operations["getUser"];
}`);
expect(operations).toEqual({
getUser: `{
getUser: {
comment: "/** @description Get a user */",
operationType: `{
responses: {
/** @description Get User */
200: {
Expand All @@ -148,6 +156,7 @@ describe("Path Item Object", () => {
};
};
}`,
},
});
});
});
5 changes: 3 additions & 2 deletions test/paths-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("Paths Object", () => {
"/api/v1/user/{user_id}": {
parameters: [{ name: "page", in: "query", schema: { type: "number" }, description: "Page number." }],
get: {
parameters: [{ name: "user_id", in: "path" }],
parameters: [{ name: "user_id", in: "path", description: "User ID." }],
responses: {
200: {
description: "OK",
Expand Down Expand Up @@ -58,6 +58,7 @@ describe("Paths Object", () => {
get: {
parameters: {
path: {
/** @description User ID. */
user_id: string;
};
};
Expand All @@ -79,8 +80,8 @@ describe("Paths Object", () => {
};
};
parameters: {
/** @description Page number. */
query: {
/** @description Page number. */
page?: number;
};
};
Expand Down