From 417c0f824d5a0925ccda0f75d5f0e39b6490ca87 Mon Sep 17 00:00:00 2001 From: mmsen Date: Sat, 1 Jan 2022 08:04:04 +0000 Subject: [PATCH] =?UTF-8?q?update=20src/middleware/http.ts.=20=E8=A7=A3?= =?UTF-8?q?=E5=86=B3controller=E5=86=85=E9=83=A8return=20string=EF=BC=8Cco?= =?UTF-8?q?ntent-type=20=E4=BC=9A=E8=A2=AB=E8=AE=BE=E7=BD=AE=E6=88=90?= =?UTF-8?q?=E4=B8=BAapplication/json,=20=E4=B8=96=E7=95=8C=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E7=9A=84string=EF=BC=8C=E5=88=B0=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E4=BC=9A=E5=8F=98=E6=88=90"string"=EF=BC=8C=E5=B7=A6?= =?UTF-8?q?=E5=8F=B3=E4=B8=A4=E7=AB=AF=E5=9B=A0=E4=B8=BAJSON.stringify?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=8F=8C=E5=BC=95=E5=8F=B7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/middleware/http.ts | 43 ++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/src/middleware/http.ts b/src/middleware/http.ts index fb07ba1..549e76c 100644 --- a/src/middleware/http.ts +++ b/src/middleware/http.ts @@ -17,6 +17,7 @@ const isObject = (field: unknown): field is Data => typeof field !== 'string' const CONTENT_TYPE = 'content-type' const JSON_TYPE = 'application/json' +const STRING_TYPE = 'text/plain' const FORM_TYPE = 'application/x-www-form-urlencoded' function initContextType(headers: Data) { @@ -98,20 +99,42 @@ export async function http(ctx: Context, next: Next) { if (ctx.config.debug === true) { ;(ret as any).stack = e.stack || '' } - return (ctx.body = { - mpserverlessComposedResponse: true, // aliyun - statusCode: 400, - headers: contentTypeHeader, - body: JSON.stringify(ret), - }) + const res = { + mpserverlessComposedResponse: true, // aliyun + statusCode: 400, + headers: contentTypeHeader + } + if( typeof(ret) == 'string' ) + { + res.body = ret + res.headers[CONTENT_TYPE] = STRING_TYPE + } + else + { + res.headers[CONTENT_TYPE] = JSON_TYPE + res.body = JSON.stringify(ret) + } + return (ctx.body = body) } const contextType = ctx.headers[CONTENT_TYPE] || JSON_TYPE - ctx.body = { + + const res = { mpserverlessComposedResponse: true, // aliyun - isBase64Encoded: !!ctx.isBase64Encoded, + isBase64Encoded: !!ctx.isBase64Encoded, statusCode: ctx.status, - headers: Object.assign(ctx.headers, { [CONTENT_TYPE]: contextType }), - body: contextType === JSON_TYPE ? JSON.stringify(ctx.body) : ctx.body, + headers: Object.assign(ctx.headers, { [CONTENT_TYPE]: contextType }) + } + if( typeof(ctx.body) == 'string' ) + { + res.body = ctx.body + res.headers[CONTENT_TYPE] = STRING_TYPE + } + else + { + res.headers[CONTENT_TYPE] = JSON_TYPE + res.body = JSON.stringify(ctx.body) } + + ctx.body = res } } -- Gitee