From 3f5b56844fb3343e0bd0f85c69eee60a616e6e52 Mon Sep 17 00:00:00 2001 From: liangsky <640634387@qq.com> Date: Sat, 2 Jul 2022 15:02:11 +0800 Subject: [PATCH] feat(mock): support text/plain media type --- .../src/gen/gen-mock-interface-file.ts | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/packages/http-mock-gen/src/gen/gen-mock-interface-file.ts b/packages/http-mock-gen/src/gen/gen-mock-interface-file.ts index f7328ed..a848ec1 100644 --- a/packages/http-mock-gen/src/gen/gen-mock-interface-file.ts +++ b/packages/http-mock-gen/src/gen/gen-mock-interface-file.ts @@ -18,6 +18,10 @@ type IDefaultOpts = { prettierOptions?: prettier.Options; }; +const getMediaTypeData = (content: any) => { + return content?.['application/json'] ?? content?.['text/plain'] ?? {}; +}; + const genDefaultCustomData = async (opts: IDefaultOpts) => { const { mockData, genCustomDataPath, interfaceApiRelativePath, prettierOptions } = opts; if (!fs.pathExistsSync(path.join(genCustomDataPath, 'index.ts'))) { @@ -26,15 +30,11 @@ const genDefaultCustomData = async (opts: IDefaultOpts) => { let responseData = {}; if (itemValue.get) { const content = itemValue.get?.responses?.['200']?.content; - if (content && content['application/json']) { - responseData = content['application/json']; - } + responseData = getMediaTypeData(content); } if (itemValue.post) { const content = itemValue.post?.responses?.['200']?.content; - if (content && content['application/json']) { - responseData = content['application/json']; - } + responseData = getMediaTypeData(content); } fs.ensureDirSync(genCustomDataPath); @@ -110,20 +110,12 @@ const genMockInterfaceFile = async (opts: IOpts) => { if (itemValue.get) { method = 'GET'; const content = itemValue.get?.responses?.['200']?.content; - if (content && content['application/json']) { - data = content['application/json']; - } else { - data = {}; - } + data = getMediaTypeData(content); } if (itemValue.post) { method = 'POST'; const content = itemValue.post?.responses?.['200']?.content; - if (content && content['application/json']) { - data = content['application/json']; - } else { - data = {}; - } + data = getMediaTypeData(content); } if (method) { interfaceMockData.push(`'${method} ${item}': (req: Request, res: Response) => {