Skip to content

Commit 728c361

Browse files
Fix breaking JSDoc changes (#1012) (#1029)
* Fix #1012 * Update examples * Fix the operation comment * Fix subschemas (digital ocean) * indentation * Fix lints and examples
1 parent 319c700 commit 728c361

11 files changed

+19582
-19561
lines changed

examples/digital-ocean-api.ts

+1,972-1,972
Large diffs are not rendered by default.

examples/github-api-next.ts

+8,966-8,966
Large diffs are not rendered by default.

examples/github-api.ts

+6,095-6,095
Large diffs are not rendered by default.

examples/octokit-ghes-3.6-diff-to-api.ts

+810-810
Large diffs are not rendered by default.

examples/stripe-api.ts

+1,705-1,705
Large diffs are not rendered by default.

src/index.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import transformParameterObject from "./transform/parameter-object.js";
99
import transformRequestBodyObject from "./transform/request-body-object.js";
1010
import transformResponseObject from "./transform/response-object.js";
1111
import transformSchemaObject from "./transform/schema-object.js";
12-
import { error, escObjKey, getDefaultFetch, getEntries, indent } from "./utils.js";
12+
import { error, escObjKey, getDefaultFetch, getEntries, getSchemaObjectComment, indent } from "./utils.js";
1313
export * from "./types.js"; // expose all types to consumers
1414

1515
const EMPTY_OBJECT_RE = /^\s*\{?\s*\}?\s*$/;
@@ -125,6 +125,7 @@ async function openapiTS(
125125
const key = escObjKey(subschemaID);
126126
const path = `${subschemaID}#`;
127127
let subschemaOutput = "";
128+
let comment: string | undefined;
128129
switch (subschema.hint) {
129130
case "OpenAPI3": {
130131
const subschemaTypes = transformSchema(subschema.schema, { ...ctx, indentLv });
@@ -144,6 +145,7 @@ async function openapiTS(
144145
break;
145146
}
146147
case "OperationObject": {
148+
comment = getSchemaObjectComment(subschema.schema, indentLv);
147149
subschemaOutput = transformOperationObject(subschema.schema, { path, ctx: { ...ctx, indentLv } });
148150
break;
149151
}
@@ -169,6 +171,7 @@ async function openapiTS(
169171
}
170172
}
171173
if (subschemaOutput && !EMPTY_OBJECT_RE.test(subschemaOutput)) {
174+
if (comment) output.push(indent(comment, indentLv));
172175
output.push(indent(`${key}: ${subschemaOutput}`, indentLv));
173176
}
174177
delete allSchemas[subschemaID];
@@ -182,8 +185,9 @@ async function openapiTS(
182185
// 3. operations (only get fully built after all external schemas transformed)
183186
if (Object.keys(ctx.operations).length) {
184187
output.push(options.exportType ? "export type operations = {" : "export interface operations {", "");
185-
for (const k of Object.keys(ctx.operations)) {
186-
output.push(indent(`${escObjKey(k)}: ${ctx.operations[k]};`, 1));
188+
for (const [key, { operationType, comment }] of Object.entries(ctx.operations)) {
189+
if (comment) output.push(indent(comment, 1));
190+
output.push(indent(`${escObjKey(key)}: ${operationType};`, 1));
187191
}
188192
output.push(`}${options.exportType ? ";" : ""}`, "");
189193
} else {

src/transform/operation-object.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ export default function transformOperationObject(
3030
let { indentLv } = ctx;
3131
const output: string[] = wrapObject ? ["{"] : [];
3232
indentLv++;
33-
const c = getSchemaObjectComment(operationObject, indentLv);
34-
if (c) output.push(indent(c, indentLv));
3533

3634
// parameters
3735
{
@@ -51,7 +49,7 @@ export default function transformOperationObject(
5149
key = tsOptionalProperty(key);
5250
}
5351
const c = getSchemaObjectComment(p, indentLv);
54-
if (c) parameterOutput.push(indent(c, indentLv));
52+
if (c) inlineOutput.push(indent(c, indentLv));
5553
const parameterType = transformParameterObject(p, {
5654
path: `${path}/parameters/${p.name}`,
5755
ctx: { ...ctx, indentLv },

src/transform/path-item-object.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export default function transformPathItemObject(
3030
// if operationId exists, move into an `operations` export and pass the reference in here
3131
else if (operationObject.operationId) {
3232
const operationType = transformOperationObject(operationObject, { path, ctx: { ...ctx, indentLv: 1 } });
33-
ctx.operations[operationObject.operationId] = operationType;
33+
ctx.operations[operationObject.operationId] = {
34+
operationType,
35+
comment: getSchemaObjectComment(operationObject, 1),
36+
};
3437
output.push(indent(`${method}: operations[${escStr(operationObject.operationId)}];`, indentLv));
3538
} else {
3639
const operationType = transformOperationObject(operationObject, { path, ctx: { ...ctx, indentLv } });

src/types.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,13 @@ export interface GlobalContext {
638638
postTransform: OpenAPITSOptions["postTransform"];
639639
immutableTypes: boolean;
640640
indentLv: number;
641-
operations: Record<string, string>;
641+
operations: Record<
642+
string,
643+
{
644+
comment?: string;
645+
operationType: string;
646+
}
647+
>;
642648
pathParamsAsTypes: boolean;
643649
silent: boolean;
644650
supportArrayLength: boolean;

test/path-item-object.test.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { PathItemObject } from "../src/types";
1+
import type { GlobalContext, PathItemObject } from "../src/types";
22
import transformPathItemObject, { TransformPathItemObjectOptions } from "../src/transform/path-item-object.js";
33

44
const options: TransformPathItemObjectOptions = {
@@ -23,6 +23,7 @@ describe("Path Item Object", () => {
2323
test("basic", () => {
2424
const schema: PathItemObject = {
2525
get: {
26+
description: "Basic GET",
2627
responses: {
2728
200: {
2829
description: "OK",
@@ -57,6 +58,7 @@ describe("Path Item Object", () => {
5758
},
5859
},
5960
post: {
61+
description: "Basic POST",
6062
requestBody: {
6163
content: {
6264
"application/json": { $ref: 'components["schemas"]["User"]' },
@@ -70,6 +72,7 @@ describe("Path Item Object", () => {
7072
};
7173
const generated = transformPathItemObject(schema, options);
7274
expect(generated).toBe(`{
75+
/** @description Basic GET */
7376
get: {
7477
responses: {
7578
/** @description OK */
@@ -92,6 +95,7 @@ describe("Path Item Object", () => {
9295
};
9396
};
9497
};
98+
/** @description Basic POST */
9599
post: {
96100
requestBody?: {
97101
content: {
@@ -107,9 +111,10 @@ describe("Path Item Object", () => {
107111
});
108112

109113
test("operations", () => {
110-
const operations: Record<string, string> = {};
114+
const operations: GlobalContext["operations"] = {};
111115
const schema: PathItemObject = {
112116
get: {
117+
description: "Get a user",
113118
operationId: "getUser",
114119
responses: {
115120
200: {
@@ -132,10 +137,13 @@ describe("Path Item Object", () => {
132137
};
133138
const generated = transformPathItemObject(schema, { ...options, ctx: { ...options.ctx, operations } });
134139
expect(generated).toBe(`{
140+
/** @description Get a user */
135141
get: operations["getUser"];
136142
}`);
137143
expect(operations).toEqual({
138-
getUser: `{
144+
getUser: {
145+
comment: "/** @description Get a user */",
146+
operationType: `{
139147
responses: {
140148
/** @description Get User */
141149
200: {
@@ -148,6 +156,7 @@ describe("Path Item Object", () => {
148156
};
149157
};
150158
}`,
159+
},
151160
});
152161
});
153162
});

test/paths-object.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("Paths Object", () => {
2222
"/api/v1/user/{user_id}": {
2323
parameters: [{ name: "page", in: "query", schema: { type: "number" }, description: "Page number." }],
2424
get: {
25-
parameters: [{ name: "user_id", in: "path" }],
25+
parameters: [{ name: "user_id", in: "path", description: "User ID." }],
2626
responses: {
2727
200: {
2828
description: "OK",
@@ -58,6 +58,7 @@ describe("Paths Object", () => {
5858
get: {
5959
parameters: {
6060
path: {
61+
/** @description User ID. */
6162
user_id: string;
6263
};
6364
};
@@ -79,8 +80,8 @@ describe("Paths Object", () => {
7980
};
8081
};
8182
parameters: {
82-
/** @description Page number. */
8383
query: {
84+
/** @description Page number. */
8485
page?: number;
8586
};
8687
};

0 commit comments

Comments
 (0)