Contentul 和 Angular 通用的自定义适配器

Contentul and Angular universal with a custom adapter

我正在尝试在我的 Angular 通用应用程序中使用 Contentful,但我想用我自己的适配器覆盖他们的适配器。

我试过这样做:

private client = createClient({
  space: environment.space,
  accessToken: environment.cdaAccessToken,
  adapter: (config: any) => {
    config.adapter = null;
    //console.log(config);
    //console.log(config.headers);
    return this.http
      .request(config.method, `${config.baseURL}/${config.url}`, {
        headers: {
          Accept: config.headers['Accept'],
          Authorization: config.headers['Authorization'],
          'Content-Type': config.headers['Content-Type'],
          'X-Contentful-User-Agent':
            config.headers['X-Contentful-User-Agent'],
        },
        params: config.params,
      })
      .toPromise();
  },
});

哪类作品;我可以看到它正在使用我的提供程序,但它在执行请求时一直报告错误:

Object.defineProperty called on non-object

我可以看到它正在尝试调用 contentful.js 文件中的 wrapEntryCollection 并且期望 response.data 作为参数。对我来说,当使用我的自定义适配器时,response.data 总是未定义的。

有谁知道我可能做错了什么?

经过进一步调查,我发现当不使用我的适配器时,响应是这样的:

{
    config: {},
    data: {},
}

但我的回复只是返回数据。所以我将我的适配器更新为:

private client = createClient({
  space: environment.space,
  accessToken: environment.cdaAccessToken,
  adapter: (config: any) => {
    config.adapter = null;
    //console.log(config);
    console.log(config.headers);
    return this.http
      .request(config.method, `${config.baseURL}/${config.url}`, {
        headers: {
          Accept: config.headers['Accept'],
          Authorization: config.headers['Authorization'],
          'Content-Type': config.headers['Content-Type'],
          'X-Contentful-User-Agent':
            config.headers['X-Contentful-User-Agent'],
        },
        params: config.params,
      })
      .toPromise()
      .then((response: any) => {
        console.log(response);
        return { data: response };
      });
  },
});

成功了