Skip to content

Commit 54f7f35

Browse files
committed
fix: ctrl-c interrupt & request body for limited methods
1 parent 9fb5bb8 commit 54f7f35

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/APICollector.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export default class APICollector implements APICollectorInterface {
267267
if (!url) throw new Error('No url found');
268268
const method = this.extractMethod(serverResponse);
269269
const operationObj: OpenAPIV3_1.OperationObject = {};
270+
const methodsHasReqBody = ['post', 'put']
270271

271272
if (statusCode < 400) {
272273
const headers = this.extractHeaders(serverResponse);
@@ -277,9 +278,11 @@ export default class APICollector implements APICollectorInterface {
277278
operationObj.parameters = parameters;
278279
}
279280

280-
const requestBody = this.extractRequestBody(serverResponse);
281-
if (requestBody) {
282-
operationObj.requestBody = requestBody;
281+
if (methodsHasReqBody.includes(method)) {
282+
const requestBody = this.extractRequestBody(serverResponse);
283+
if (requestBody) {
284+
operationObj.requestBody = requestBody;
285+
}
283286
}
284287
}
285288

src/index.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { spawn } from 'child_process';
22
import fsPromises from 'fs/promises';
3+
import { existsSync } from 'fs'
34

45
import {
56
PREFIX_RESPONSE_BODY_DATA,
@@ -40,10 +41,14 @@ export async function runner (
4041
await fsPromises.writeFile(mainFileAbsolutePath, injectedCodes, { flag: "a" })
4142
await fsPromises.appendFile(mainFileAbsolutePath, "// @ts-nocheck")
4243

43-
const childProcess = spawn(args[0], args.slice(1), { stdio: ["inherit", "pipe", "inherit"] });
44+
const childProcess = spawn(args[0], args.slice(1), {
45+
detached: true,
46+
stdio: ["inherit", "pipe", "inherit"]
47+
});
4448

4549
childProcess.stdout.on('data', (data) => {
4650
const dataStr = data.toString()
51+
4752
if (dataStr.startsWith(PREFIX_RESPONSE_BODY_DATA)) {
4853
try {
4954
const res = JSON.parse(dataStr.substr(PREFIX_RESPONSE_BODY_DATA.length))
@@ -79,8 +84,11 @@ export async function runner (
7984
})
8085

8186
childProcess.on('close', async (code) => {
82-
await fsPromises.copyFile(projectCWD + "/outdoc_tmp_file", mainFileAbsolutePath)
83-
await fsPromises.rm(projectCWD + "/outdoc_tmp_file")
87+
const tmpFilePath = projectCWD + "/outdoc_tmp_file"
88+
if (existsSync(tmpFilePath)) {
89+
await fsPromises.copyFile(projectCWD + "/outdoc_tmp_file", mainFileAbsolutePath)
90+
await fsPromises.rm(projectCWD + "/outdoc_tmp_file")
91+
}
8492

8593
if (code === 0) {
8694
try {
@@ -101,4 +109,12 @@ export async function runner (
101109
}
102110
}
103111
});
112+
113+
process.on('SIGINT', async () => {
114+
const tmpFilePath = projectCWD + "/outdoc_tmp_file"
115+
if (existsSync(tmpFilePath)) {
116+
await fsPromises.copyFile(tmpFilePath, mainFileAbsolutePath)
117+
await fsPromises.rm(tmpFilePath)
118+
}
119+
});
104120
}

0 commit comments

Comments
 (0)