Skip to content

Commit

Permalink
feat: update the openapi transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanCassiere committed Jun 23, 2024
1 parent e96c70c commit 9ad8227
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/docs/openapi.v2.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.0
info:
title: Simple Logging Server API
version: 2.2.0
version: 0.0.0
description: |-
This is a simple API for logging messages. It is intended to be a basic interface for logging messages according to an allowed list of clients.
### Usage
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import v2Router from "@/routers/v2";
import docsRouter from "@/routers/docs";

import { transformOpenapiYmlDoc, openapiYmlVersioner } from "@/utils/openapi-docs";
import { getPackageInfo } from "@/utils/package";
import { env } from "@/config/env";
import type { ServerContext } from "@/types/hono";

const packageJson = require("../package.json");
const packageJson = getPackageInfo();

const app = new Hono<ServerContext>();
app.use(cors({ origin: "*" }));
Expand Down
15 changes: 8 additions & 7 deletions src/utils/openapi-docs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from "node:path";
import { readFileSync, writeFileSync } from "node:fs";

type OpenApiDocTransformer = (dirVersion: string, doc: string) => string;
type OpenApiDocTransformer = (filename: string, source_path: string, dirVersion: string, doc: string) => string;

/**
* Modify the OpenAPI doc with the provided transformers and write the file to the static directory.
Expand All @@ -16,25 +16,26 @@ export function transformOpenapiYmlDoc(dir_version: string, transformers: OpenAp

let newOpenapiYmlDoc = openapiYmlInputDoc;

console.log(`📖 Starting to update /static/${openapiFilename}`);

for (const transformer of transformers) {
newOpenapiYmlDoc = transformer(dir_version, newOpenapiYmlDoc);
newOpenapiYmlDoc = transformer(openapiFilename, openapiYmlSourcePath, dir_version, newOpenapiYmlDoc);
}

writeFileSync(openapiYmlOutPath, newOpenapiYmlDoc);

console.log(`📖 Updated /static/${openapiFilename}`);
console.log(`📖 Finishing updating /static/${openapiFilename}`);
return;
}

const ymlVersionRegex = new RegExp(/^version: (0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/);

/**
* Transformer to update the version in the OpenAPI doc.
* @param version
* @returns
*/
export function openapiYmlVersioner(version: string): OpenApiDocTransformer {
return (_, doc) => {
return doc.replace(ymlVersionRegex, `version: ${version}`);
return (filename, _, __, doc) => {
console.log(`📦 Updating the version in OpenAPI document "${filename}" to "${version}"`);
return doc.replace(/version:\s*0\.0\.0/g, `version: ${version}`);
};
}
15 changes: 15 additions & 0 deletions src/utils/package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from "node:path";
import { readFileSync } from "node:fs";
import { z } from "zod";

const PackageSchema = z.object({
name: z.string(),
version: z.string(),
});

export function getPackageInfo() {
const pathname = join(process.cwd(), "package.json");
const packageJson = JSON.parse(readFileSync(pathname, "utf8"));

return PackageSchema.parse(packageJson);
}

0 comments on commit 9ad8227

Please sign in to comment.