Skip to content

Instantly share code, notes, and snippets.

@ygkn
Created February 22, 2023 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ygkn/46c6e23b0703389c5a69737eeb47eb05 to your computer and use it in GitHub Desktop.
Save ygkn/46c6e23b0703389c5a69737eeb47eb05 to your computer and use it in GitHub Desktop.
import SwaggerParser from "@apidevtools/swagger-parser";
import * as OpenAPISampler from "openapi-sampler";
import type { OpenAPI, OpenAPIV3_1 } from "openapi-types";
SwaggerParser.dereference(
"oepnapi.json"
).then((api) => {
if (!api.paths) throw new Error("No paths found in API definition");
Object.entries<NonNullable<OpenAPI.Document["paths"]>[number]>(
api.paths
).forEach(([path, pathItem]) => {
if (!pathItem) return;
Object.entries(pathItem).forEach(([method, operation]) => {
// if (Object.hasOwn(operation, "requestBody")) {
// const requestBody = (operation as OpenAPIV3_1.OperationObject)
// .requestBody;
// if (requestBody === undefined || !Object.hasOwn(requestBody, "content"))
// return;
// const content = (requestBody as OpenAPIV3_1.RequestBodyObject).content;
// const schema = content["application/json"]?.schema;
// if (schema === undefined) return;
// const sample = OpenAPISampler.sample(schema as any);
// console.log(path, method, sample);
// }
if (operation === undefined || !Object.hasOwn(operation, "responses"))
return;
const responses = (operation as OpenAPIV3_1.OperationObject).responses;
// const response =
// responses &&
// Object.entries(responses).find(([statusCode]) =>
// statusCode.startsWith("2")
// );
// if (response === undefined || !Object.hasOwn(response[1], "content"))
// return;
// const content = (response[1] as OpenAPIV3_1.ResponseObject).content;
// const schema = content?.["application/json"]?.schema;
// const sample = schema && OpenAPISampler.sample(schema as any);
// console.log(path, method, sample);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment