From 3b19d445c25aa548d50f0ecf8ba82999dde8aa1b Mon Sep 17 00:00:00 2001 From: solufa Date: Fri, 16 Aug 2024 03:17:00 +0900 Subject: [PATCH] feat: override openapi if exists json --- package-lock.json | 5 ++--- package.json | 1 + samples/openapi.json | 4 ++-- src/index.ts | 7 ++++++- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 73eeae0..4974fa3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ }, "devDependencies": { "@types/minimist": "^1.2.5", + "@types/node": "^22.3.0", "eslint": "^9.9.0", "eslint-config-flat-gitignore": "^0.1.8", "eslint-config-prettier": "^9.1.0", @@ -898,7 +899,6 @@ "version": "22.3.0", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.3.0.tgz", "integrity": "sha512-nrWpWVaDZuaVc5X84xJ0vNrLvomM205oQyLsRt7OHNZbSHslcWsvgFR7O7hire2ZonjLrWBbedmotmIlJDVd6g==", - "peer": true, "dependencies": { "undici-types": "~6.18.2" } @@ -3041,8 +3041,7 @@ "node_modules/undici-types": { "version": "6.18.2", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.18.2.tgz", - "integrity": "sha512-5ruQbENj95yDYJNS3TvcaxPMshV7aizdv/hWYjGIKoANWKjhWNBsr2YEuYZKodQulB1b8l7ILOuDQep3afowQQ==", - "peer": true + "integrity": "sha512-5ruQbENj95yDYJNS3TvcaxPMshV7aizdv/hWYjGIKoANWKjhWNBsr2YEuYZKodQulB1b8l7ILOuDQep3afowQQ==" }, "node_modules/uri-js": { "version": "4.4.1", diff --git a/package.json b/package.json index bb6ce90..5878abe 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ }, "devDependencies": { "@types/minimist": "^1.2.5", + "@types/node": "^22.3.0", "eslint": "^9.9.0", "eslint-config-flat-gitignore": "^0.1.8", "eslint-config-prettier": "^9.1.0", diff --git a/samples/openapi.json b/samples/openapi.json index 13aede5..12fd1d7 100644 --- a/samples/openapi.json +++ b/samples/openapi.json @@ -1,8 +1,8 @@ { "openapi": "3.1.0", "info": { - "title": "openapi api", - "version": "v0.0" + "title": "title test api", + "version": "v1.1" }, "paths": { "/api/stream/v1/stories/{storyId}": { diff --git a/src/index.ts b/src/index.ts index f7ec73a..5968eaf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import type { AspidaMethodParams } from 'aspida'; import type { DirentTree } from 'aspida/dist/cjs/getDirentTree'; import { getDirentTree } from 'aspida/dist/cjs/getDirentTree'; -import { unlinkSync, writeFileSync } from 'fs'; +import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs'; import type { OpenAPIV3_1 } from 'openapi-types'; import { join } from 'path'; import * as TJS from 'typescript-json-schema'; @@ -42,6 +42,10 @@ type AllMethods = [${paths.map((_, i) => `Methods${i}`).join(', ')}]`; const program = TJS.getProgramFromFiles([typeFilePath], compilerOptions); const schema = TJS.generateSchema(program, 'AllMethods', { required: true }); + const existingDoc: OpenAPIV3_1.Document | undefined = existsSync(config.output) + ? JSON.parse(readFileSync(config.output, 'utf8')) + : undefined; + const doc: OpenAPIV3_1.Document = { openapi: '3.1.0', info: { @@ -49,6 +53,7 @@ type AllMethods = [${paths.map((_, i) => `Methods${i}`).join(', ')}]`; version: 'v0.0', }, servers: config.baseURL ? [{ url: config.baseURL }] : undefined, + ...existingDoc, paths: {}, components: { schemas: schema?.definitions as any }, };