Skip to content

Commit

Permalink
add json output, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Adis Durakovic committed Mar 19, 2024
1 parent a771e30 commit 9a92a02
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ Route.get("/docs", async () => {

Visit `<url>/docs` to see AutoSwagger in action.

### Functions

- `async docs(routes, conf)`: get the specification in YAML format
- `async json(routes, conf)`: get the specification in JSON format
- `ui(path, conf)`: get default swagger UI
- `rapidoc(path, style)`: get rapidoc UI
- `scalar(path)`: get scalar UI
- `jsonToYaml(json)`: can be used to convert `json()` back to yaml

---

## Configure
Expand Down
33 changes: 23 additions & 10 deletions src/autoswagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import extract from "extract-comments";
import HTTPStatusCode from "http-status-code";
import { camelCase, isEmpty, isUndefined, snakeCase, startCase } from "lodash";
import { existsSync } from "fs";
import { scalarCustomCss } from './scalarCustomCss'
import { scalarCustomCss } from "./scalarCustomCss";

/**
* Autoswagger interfaces
Expand Down Expand Up @@ -189,8 +189,7 @@ export class AutoSwagger {
}

scalar(url: string) {
return (
`
return `
<!doctype html>
<html>
<head>
Expand All @@ -211,18 +210,32 @@ export class AutoSwagger {
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
</body>
</html>
`
);
`;
}

jsonToYaml(json: any) {
return YAML.stringify(json);
}

async json(routes: any, options: options) {
if (process.env.NODE_ENV === "production") {
return this.readFile(options.path, "json");
}
return await this.generate(routes, options);
}

async writeFile(routes: any, options: options) {
const contents = await this.generate(routes, options);
const json = await this.generate(routes, options);
const contents = this.jsonToYaml(json);
const filePath = options.path + "swagger.yml";
const filePathJson = options.path + "swagger.json";

fs.writeFileSync(filePath, contents);
fs.writeFileSync(filePathJson, JSON.stringify(json, null, 2));
}

private async readFile(rootPath) {
const filePath = rootPath + "swagger.yml";
private async readFile(rootPath, type = "yaml") {
const filePath = rootPath + "swagger." + type;
const data = fs.readFileSync(filePath, "utf-8");
if (!data) {
console.error("Error reading file");
Expand All @@ -235,7 +248,7 @@ export class AutoSwagger {
if (process.env.NODE_ENV === "production") {
return this.readFile(options.path);
}
return this.generate(routes, options);
return this.jsonToYaml(await this.generate(routes, options));
}

async generate(adonisRoutes: AdonisRoutes, options: options) {
Expand Down Expand Up @@ -522,7 +535,7 @@ export class AutoSwagger {
docs.tags = globalTags;
docs.paths = paths;
}
return YAML.stringify(docs);
return docs;
}

private mergeParams(initial, custom) {
Expand Down

0 comments on commit 9a92a02

Please sign in to comment.