gpt4 book ai didi

javascript - Aurelia - 在 Aurelia Fetch 客户端中设置 header

转载 作者:行者123 更新时间:2023-12-01 03:22:43 25 4
gpt4 key购买 nike

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

我需要实现的是;

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

让拦截器正确发布事件的唯一方法是在 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 类,用于将用户名/密码传递给拦截器;

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

关于javascript - Aurelia - 在 Aurelia Fetch 客户端中设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062901/

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