gpt4 book ai didi

json - React restClient 管理员休息

转载 作者:行者123 更新时间:2023-12-02 05:16:36 26 4
gpt4 key购买 nike

admin-on-rest允许通过编写自定义 rest 客户端来使用任何 JSON 响应。文档中的示例用于使用来自 json-server 的 JSON project这很简单。

我想知道消费它有多容易this api在 admin-on-rest 中对 restClient 进行了细微更改.

最佳答案

好的 - 让我们看看 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 };
但是我们在结果中有主体并且可以重用它或者我们可以在 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['结果'] ? json['结果'] : json;
现在您可以在管理员中使用该客户端

<Admin restClient={restClient}>
...
</Admin>

警告!!!这将影响管理员的所有请求。但是您可以添加其他参数。如果你不想使用 json = json['results'] ? json['results'] : json; 你可以添加额外的参数或检查方法 fetch

关于json - React restClient 管理员休息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39964172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com