Skip to content

Commit bc6da34

Browse files
author
William Duncan
committed
add return description to operations
1 parent 012d58f commit bc6da34

File tree

5 files changed

+24
-3
lines changed

5 files changed

+24
-3
lines changed

gen/Schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace Schema {
3131
export interface Response {
3232
status: number;
3333
type: Schema.Type;
34+
description?: string;
3435
}
3536

3637
export interface Namespace {

gen/docs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Schema from "./Schema";
22
import {Config} from "./Config";
3-
import {getReturnType, getThrows, replaceModelTypes} from "./util.js";
3+
import {getReturnDescription, getReturnType, getThrows, replaceModelTypes} from "./util.js";
44
import DocSchema from "./DocSchema.js";
55
import fs from "node:fs/promises";
66
import Package from "./Package";
@@ -63,7 +63,7 @@ export function generateDocSchema (schema: Schema, config: Config, pkg: Package)
6363
}
6464
// make operations into methods
6565
const operationMethods = operations.map(operation => {
66-
const returns = {type: getReturnType(operation, schema, config)};
66+
const returns = {type: getReturnType(operation, schema, config), description: getReturnDescription(operation, schema, config)};
6767
const throws = getThrows(operation, schema, config).map(type => ({type}));
6868
const pathParams = Object.entries(operation.parameters.path ?? {}).map(([name, parameter]) => new DocSchema.Parameter(name, parameter.type, parameter.description, parameter.required, parameter.default));
6969
const queryParams = Object.entries(operation.parameters.query ?? {}).map(([name, parameter]) => new DocSchema.Parameter(name, parameter.type, parameter.description, parameter.required, parameter.default));

gen/source.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import fs from "node:fs/promises";
33
import path from "node:path";
44
import Mustache from "mustache";
55
import {Config} from "./Config";
6-
import {getReturnType, getThrows, replaceModelTypes} from "./util.js";
6+
import {getReturnDescription, getReturnType, getThrows, replaceModelTypes} from "./util.js";
77
import Package from "./Package";
88

99
interface FlatOperation {
1010
name: string;
1111
returnType: string;
12+
returnDescription?: string;
1213
params: {
1314
path: string;
1415
query: string;
@@ -43,6 +44,7 @@ export default async (schema: Schema, config: Config, pkg: Package) => {
4344
const operations: FlatOperation[] = [];
4445
for (const [name, operation] of input) {
4546
const returnType = getReturnType(operation, schema, config);
47+
const returnDescription = getReturnDescription(operation, schema, config);
4648
const toFlatParam = ([name, parameter]: [string, Schema.Operation.Parameter]): NamedParameter => {
4749
const ts = `${name}${!parameter.required && !parameter.default ? "?: " : ": "}${parameter.type}${parameter.default ? ` = ${parameter.default}` : ""}`;
4850
return {name, ts, ...parameter};
@@ -68,6 +70,7 @@ export default async (schema: Schema, config: Config, pkg: Package) => {
6870
operations.push({
6971
name,
7072
returnType,
73+
returnDescription,
7174
params,
7275
allParams,
7376
tsArgs,

gen/templates/main.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ class {{config.name}} {
206206
* @throws {<%& . %>}
207207
<%={{ }}=%>
208208
{{/throws}}
209+
{{#returnDescription}}
210+
* @returns {{{returnDescription}}}
211+
{{/returnDescription}}
209212
*/
210213
{{name}}: async ({{{tsArgs}}}): Promise<{{config.name}}.ApiResponse<{{{returnType}}}>> => {
211214
return await this.#sendRequest<{{{returnType}}}>({{{operation}}}, {{{params.path}}}, {{{params.query}}}, {{{params.body}}});
@@ -226,6 +229,9 @@ class {{config.name}} {
226229
* @throws {<%& . %>}
227230
<%={{ }}=%>
228231
{{/throws}}
232+
{{#returnDescription}}
233+
* @returns {{{returnDescription}}}
234+
{{/returnDescription}}
229235
*/
230236
public async {{name}}({{{tsArgs}}}): Promise<{{config.name}}.ApiResponse<{{{returnType}}}>> {
231237
return await this.#sendRequest<{{{returnType}}}>({{{operation}}}, {{{params.path}}}, {{{params.query}}}, {{{params.body}}});

gen/util.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ export function getReturnType(operation: Schema.Operation, schema: Schema, confi
1212
return operation.returns.filter(r => r.status >= 200 && r.status < 300).map(r => r.type.endsWith("[]") ? `${config.name}.PaginatedData<${schema.models.find(m => m.name === r.type.slice(0, -2)) ? `${config.name}.${r.type}` : r.type}>` : schema.models.find(m => m.name === r.type) ? `${config.name}.${r.type}` : r.type).join(" | ");
1313
}
1414

15+
/**
16+
* Get return description of operation
17+
* @param operation
18+
* @param schema
19+
* @param config
20+
* @returns The combined return description
21+
*/
22+
export function getReturnDescription(operation: Schema.Operation, schema: Schema, config: Config): string {
23+
return operation.returns.filter(r => r.status >= 200 && r.status < 300 && r.description).map(r => r.description).join(" ");
24+
}
25+
1526
/**
1627
* Get types the operation throws
1728
* @param operation

0 commit comments

Comments
 (0)