Skip to content

Commit 3b19d44

Browse files
committed
feat: override openapi if exists json
1 parent 8667f1d commit 3b19d44

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

package-lock.json

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
},
3737
"devDependencies": {
3838
"@types/minimist": "^1.2.5",
39+
"@types/node": "^22.3.0",
3940
"eslint": "^9.9.0",
4041
"eslint-config-flat-gitignore": "^0.1.8",
4142
"eslint-config-prettier": "^9.1.0",

samples/openapi.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"openapi": "3.1.0",
33
"info": {
4-
"title": "openapi api",
5-
"version": "v0.0"
4+
"title": "title test api",
5+
"version": "v1.1"
66
},
77
"paths": {
88
"/api/stream/v1/stories/{storyId}": {

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { AspidaMethodParams } from 'aspida';
22
import type { DirentTree } from 'aspida/dist/cjs/getDirentTree';
33
import { getDirentTree } from 'aspida/dist/cjs/getDirentTree';
4-
import { unlinkSync, writeFileSync } from 'fs';
4+
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
55
import type { OpenAPIV3_1 } from 'openapi-types';
66
import { join } from 'path';
77
import * as TJS from 'typescript-json-schema';
@@ -42,13 +42,18 @@ type AllMethods = [${paths.map((_, i) => `Methods${i}`).join(', ')}]`;
4242

4343
const program = TJS.getProgramFromFiles([typeFilePath], compilerOptions);
4444
const schema = TJS.generateSchema(program, 'AllMethods', { required: true });
45+
const existingDoc: OpenAPIV3_1.Document | undefined = existsSync(config.output)
46+
? JSON.parse(readFileSync(config.output, 'utf8'))
47+
: undefined;
48+
4549
const doc: OpenAPIV3_1.Document = {
4650
openapi: '3.1.0',
4751
info: {
4852
title: `${config.output.split('/').at(-1)?.replace('.json', '')} api`,
4953
version: 'v0.0',
5054
},
5155
servers: config.baseURL ? [{ url: config.baseURL }] : undefined,
56+
...existingDoc,
5257
paths: {},
5358
components: { schemas: schema?.definitions as any },
5459
};

0 commit comments

Comments
 (0)