Aurelia - 在 Aurelia Fetch 客户端中设置 headers

Aurelia - Setting headers in Aurelia Fetch Client

我正在使用 aurelia-http-client 并努力使用拦截器并为我的请求设置 headers。

我需要实现的是;

让拦截器正确发布事件的唯一方法是在 main.js 中使用 aurelia.container,如下所示;

import {HttpClient} from 'aurelia-http-client';
import {EventAggregator} from 'aurelia-event-aggregator';

export function configure(aurelia) {
  const container = aurelia.container;

  const httpClient = container.get(HttpClient);
  const ea = container.get(EventAggregator);

  httpClient.configure(config => {
    config.withInterceptor({
      request(request) {
        ea.publish('http-request', request);
        return request;
      },
      requestError(error) {
        ea.publish('http-request-error', error);
       throw error;
      },
      response(response) {
        ea.publish('http-response', response);
        return response;
      },
      responseError(error) {
        ea.publish('http-response-error', error);
        throw error;
      }
    });
  });

  aurelia.use
    .standardConfiguration()
    .developmentLogging()
    .singleton(HttpClient, httpClient);


  aurelia.start().then(() => aurelia.setRoot());
}

因为我的请求的 header 必须在应用程序初始化后设置 - 我不能像大多数在线教程那样在上面的配置中进行设置。

而是需要如下设置;

import {inject} from "aurelia-framework";
import {HttpClient} from "aurelia-http-client";
import {EventAggregator} from "aurelia-event-aggregator";

@inject(HttpClient, EventAggregator)
export class Dashboard {

  requestMethod = "GET";

  constructor(HttpClient, EventAggregator) {
    this.http = HttpClient;
    this.ea = EventAggregator;
  }

  triggerGet() {
    // HEADER NEEDS TO BE SET HERE USING THIS.FOO
    this.http.get(this.url).then(response => {
      console.log("GET Response", response);
    });
  }
}

我试过各种变体;

this.http.configure((configure) => {
  if(this.username && this.password) {
    configure.withDefaults({
      headers: {
        'Authorization': 'Basic ' + btoa(this.username + ":" + this.password)
      }
    });
  }
})

但是我无法在需要的地方更改 header,也无法维护我在 main.js 中设置的配置

评论中的 Fabio Luiz 让我找到了一个可行的解决方案。我认为这并不理想,但它确实有效。

我基本上创建了一个 AppState class,我用它来将 username/password 传递给拦截器;

export class AppState { 

  properties = {};

  clear() {
    this.properties = {};
  }

  set(key, value) {
    this.properties[key] = value;
  }

  get(key) {
    if(this.properties[key]) {
      return this.properties[key];
    } else {
      return false;
    }
  }
}

它非常粗糙并且准备就绪,但它仅用于测试应用程序,所以我很满意。

这是它的用法;

import {inject} from "aurelia-framework";
import {HttpClient} from "aurelia-http-client";
import {EventAggregator} from "aurelia-event-aggregator";
import {AppState} from "services/appState";

@inject(HttpClient, EventAggregator, AppState)
export class Dashboard {

    constructor(HttpClient, EventAggregator, AppState) {
        this.http = HttpClient;
        this.ea = EventAggregator;
        this.appState = AppState;

        // Create listeners
    }

    run() {
        if(this.username && this.password) {
            this.appState.set('authenticate', true);
            this.appState.set('username', this.username);
            this.appState.set('password', this.password);
        }

        // Trigger HTTP Requests
    }
}

然后是我的main.js文件;

import {HttpClient} from 'aurelia-http-client';
import {EventAggregator} from 'aurelia-event-aggregator';
import {AppState} from 'services/appState';

export function configure(aurelia) {
    const container = aurelia.container;
    const httpClient = container.get(HttpClient);
    const ea = container.get(EventAggregator);
    const appState = container.get(AppState);

    httpClient.configure(config => {
        config.withInterceptor({
            request(request) {
                if(appState.get('authenticate')) {
                    let username = appState.get('username');
                    let password = appState.get('password');
                    request.headers.add("Authorization", "Basic " + btoa(username + ":" + password));
                }
                ea.publish('http-request', request);
                return request;
            },
            requestError(error) {
                ea.publish('http-request-error', error);
                throw error;
            },
            response(response) {
                ea.publish('http-response', response);
                return response;
            },
            responseError(error) {
                ea.publish('http-response-error', error);
                throw error;
            }
        });
    });

    aurelia.use
        .standardConfiguration()
        .developmentLogging()
        .singleton(HttpClient, httpClient);


      aurelia.start().then(() => aurelia.setRoot());
}

尝试像这样创建一个实际的 headers object:

this.http.configure((configure) => {
  if(this.username && this.password) {
    configure.withDefaults({
        headers: new Headers({
          'Authorization': 'Basic ' + btoa(this.username + ":" + this.password)
        })
    });
  }
})