From 773700cf68da1bda8f18958ac9ea1d7e150b7b7e Mon Sep 17 00:00:00 2001 From: liaoxuezhi <2betop.cn@gmail.com> Date: Mon, 9 May 2022 10:43:45 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20jssdk=20=E7=9A=84=20responseAdaptor=20?= =?UTF-8?q?=E6=89=A9=E5=85=85=E5=8F=82=E6=95=B0,=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=8B=BF=20response=20=E6=95=B4=E4=B8=AA=E5=AF=B9=E8=B1=A1,?= =?UTF-8?q?=E5=8C=85=E6=8B=AC=E8=BF=94=E5=9B=9E=E7=9A=84=20headers=20?= =?UTF-8?q?=E5=92=8C=E2=80=A6=20(#4229)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: jssdk 的 responseAdpator 扩充参数,支持拿 response 整个对象,包括返回的 headers 和状态码信息 * chore: jssdk 的 responseAdpator 扩充参数,支持拿 response 整个对象,包括返回的 headers 和状态码信息 --- docs/zh-CN/start/getting-started.md | 4 ++-- examples/embed.tsx | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/docs/zh-CN/start/getting-started.md b/docs/zh-CN/start/getting-started.md index c4c11b38c..20a271796 100644 --- a/docs/zh-CN/start/getting-started.md +++ b/docs/zh-CN/start/getting-started.md @@ -156,8 +156,8 @@ let amisScoped = amis.embed( // // 全局 api 适配器。 // 另外在 amis 配置项中的 api 也可以配置适配器,针对某个特定接口单独处理。 - // responseAdaptor(api, response, query, request) { - // return response; + // responseAdaptor(api, payload, query, request, response) { + // return payload; // } // // 用来接管页面跳转,比如用 location.href 或 window.open,或者自己实现 amis 配置更新 diff --git a/examples/embed.tsx b/examples/embed.tsx index 71570ebe3..8ac465c12 100644 --- a/examples/embed.tsx +++ b/examples/embed.tsx @@ -58,35 +58,35 @@ export function embed( return request; }; - const responseAdaptor = (api: any) => (value: any) => { - let response = value.data || {}; // blob 下可能会返回内容为空? + const responseAdaptor = (api: any) => (response: any) => { + let payload = response.data || {}; // blob 下可能会返回内容为空? // 之前拼写错了,需要兼容 if (env && env.responseAdpater) { env.responseAdaptor = env.responseAdpater; } if (env && env.responseAdaptor) { const url = api.url; - const idx = api.url.indexOf('?'); - const query = ~idx ? qs.parse(api.url.substring(idx)) : {}; + const idx = url.indexOf('?'); + const query = ~idx ? qs.parse(url.substring(idx)) : {}; const request = { ...api, query: query, body: api.data }; - response = env.responseAdaptor(api, response, query, request); + payload = env.responseAdaptor(api, payload, query, request, response); } else { - if (response.hasOwnProperty('errno')) { - response.status = response.errno; - response.msg = response.errmsg; - } else if (response.hasOwnProperty('no')) { - response.status = response.no; - response.msg = response.error; + if (payload.hasOwnProperty('errno')) { + payload.status = payload.errno; + payload.msg = payload.errmsg; + } else if (payload.hasOwnProperty('no')) { + payload.status = payload.no; + payload.msg = payload.error; } } const result = { - ...value, - data: response + ...response, + data: payload }; return result; };