好的 - 让我们看看 admin on rest 的来源(文件 admin-on-rest/src/util/fetch.js),我们对 fetchJson 方法很感兴趣。
该方法返回 fetch promise,它尝试在该代码中解析 json:
try {
json = JSON.parse(body);
} catch (e) {
// not json, no big deal
}
然后它返回:return { status, headers, body, json };
但是我们在结果中有 body 并且可以重用它,或者我们可以在 json 中使用已解析的对象
对于您的示例,我们可能会这样做(缺少一些代码):
const httpClient = (url, options = {}) => {
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' });
}
options.withCredentials = true;
return fetchUtils.fetchJson(url, options).then(({status, headers, body, json}) => {
json = json['results'] ? json['results'] : json;
return {status, headers, body, json};
});
}
所以我们只是在该行的架构中通过集合中的“结果”覆盖了 json 对象:
json = json['results'] ? json['results'] : json;
现在您可以在 Admin 中使用该客户端
<Admin restClient={restClient}>
...
</Admin>
警告!!!这将影响管理员的所有请求。但是您可以添加其他参数。如果您不想使用json = json['results'] ? json['results'] : json;,您可以添加其他参数或签入方法 fetch