Skip to content

Commit

Permalink
feat: override openapi if exists json
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Aug 15, 2024
1 parent 8667f1d commit 3b19d44
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions samples/openapi.json
Original file line number Diff line number Diff line change
@@ -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}": {
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -42,13 +42,18 @@ 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: {
title: `${config.output.split('/').at(-1)?.replace('.json', '')} api`,
version: 'v0.0',
},
servers: config.baseURL ? [{ url: config.baseURL }] : undefined,
...existingDoc,
paths: {},
components: { schemas: schema?.definitions as any },
};
Expand Down

0 comments on commit 3b19d44

Please sign in to comment.