From d4486f6d5485e17cbbb39228dc4985a58536dda5 Mon Sep 17 00:00:00 2001 From: liaoxuezhi Date: Mon, 12 Aug 2019 12:21:21 +0800 Subject: [PATCH] =?UTF-8?q?api=20adpator=20requestAdaptor=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E9=85=8D=E7=BD=AE=20string=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/api.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/utils/api.ts b/src/utils/api.ts index 819c23df1..8b7632500 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -66,6 +66,11 @@ export function buildApi(api: Api, data?: object, options: { const raw = api.url = api.url || ''; api.url = tokenize(api.url, data, '| url_encode'); + + if (ignoreData) { + return api; + } + if (api.data) { api.data = dataMapping(api.data, data); } else if (api.method === 'post' || api.method === 'put') { @@ -78,7 +83,7 @@ export function buildApi(api: Api, data?: object, options: { api.data = data; } - if (api.data && !ignoreData) { + if (api.data) { const idx = api.url.indexOf('?'); if (~idx) { let params = { @@ -97,9 +102,27 @@ export function buildApi(api: Api, data?: object, options: { api.headers = dataMapping(api.headers, data); } + if (api.requestAdaptor && typeof api.requestAdaptor === 'string') { + api.requestAdaptor = str2function(api.requestAdaptor, 'api') as any; + } + + if (api.adaptor && typeof api.adaptor === 'string') { + api.adaptor = str2function(api.adaptor, 'payload', 'response', 'api') as any; + } + return api; } +function str2function(contents:string, ...args:Array):Function | null { + try { + let fn = new Function(...args, contents); + return fn; + } catch (e) { + console.warn(e); + return null; + } +} + function responseAdaptor(ret: fetcherResult) { const data = ret.data;