fluid-oas
Version:
Build declarative OpenApiv3.* specifications.
42 lines (41 loc) • 1.54 kB
JavaScript
import { withExternalDocs, withTags, withSecurityArray, withWebhooks, withPaths, withServersArray, withJSONSchemaDialect, withInfo, withOpenApi, withComponents, } from "./common";
import { Base } from "./lib/base";
import * as fs from "fs";
const OpenApiBase = withComponents(withExternalDocs(withTags(withSecurityArray(withWebhooks(withPaths(withServersArray(withJSONSchemaDialect(withInfo(withOpenApi(Base))))())))())()));
class _OpenApiV3 extends OpenApiBase {
async writeOASImpl(fileWriteFn, filePath, pretty) {
let json;
if (pretty) {
json = JSON.stringify(this, null, 2);
}
else {
json = JSON.stringify(this);
}
if (!filePath) {
console.log(json);
return;
}
await fileWriteFn(filePath, json);
}
async writeOASASync(filePath, pretty) {
const fn = async (filePath, json) => await fs.writeFile(filePath, json, { encoding: "utf-8" }, err => {
if (err) {
console.error("Error writing file.", err.message);
}
});
this.writeOASImpl(fn, filePath, pretty);
}
writeOASSync(filePath, pretty) {
const fn = (filePath, json) => fs.writeFileSync(filePath, json);
this.writeOASImpl(fn, filePath, pretty);
}
}
export const OpenApiV3 = {
addOpenApiVersion(version) {
return {
addInfo(info) {
return new _OpenApiV3().addOpenApiVersion(version).addInfo(info);
},
};
},
};