Aurelia - 在Aurelia Fetch Client中设置标题

时间:2017-07-12 16:14:44

标签: javascript aurelia aurelia-fetch-client aurelia-http-client

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

我需要做的是;

  • 每个拦截器(请求,请求错误,响应和响应错误)在触发时使用aurelia-event-aggregator发出事件。
  • 每个请求都会添加一个标题,其中包含在页面上输入的信息

我有拦截器正确发布事件的唯一方法是在 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());
}

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

相反,它需要设置如下;

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)
      }
    });
  }
})

但我无法改变我需要的标题,并维护我在 main.js中设置的配置

2 个答案:

答案 0 :(得分:1)

尝试创建一个像这样的实际标题对象:

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

答案 1 :(得分:0)

法比奥·路易斯在评论中为我提供了一个有效的解决方案。它不理想我不会想,但它有效。

我基本上创建了一个AppState类,用于将用户名/密码传递给拦截器;

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());
}