typescript - Chyba: Nezachyceno (v promise): Nelze vyřešit všechny parametry pro AuthConfig
takže se snažím rozšířit třídu AuthHttp z tohoto repozitáře, abych ji mohl požádat o provedení věcí, které jsou relevantnější pro můj případ použití, konkrétně automaticky kontrolovat, zda je bearer token expirovaný, a pak použít refresh token k získání dalšího auth tokenu a následně znovu provést původní požadavek. Prakticky standardní věci kolem OAuth 2. Také jsem nováček v Angular2 a narazil jsem na problém s DI. Zkoušel jsem spoustu věcí, abych to opravil, ale prostě nemohu přijít na to, jak chybu opravit.
Zde je zjednodušená verze mé třídy CustomHttp rozšiřující AuthHttp:
import { Injectable } from '@angular/core';
import { Request, Http, RequestOptionsArgs, Headers, Response, RequestOptions } from '@angular/http';
import { AuthHttp, AuthConfig, IAuthConfig, tokenNotExpired } from 'angular2-jwt';
import { Observable } from 'rxjs/Rx';
@Injectable()
export class CustomHttp extends AuthHttp {
private tokenUrl = "token_url";
public apiBase = "api_url";
private _configuration: IAuthConfig;
constructor(options: AuthConfig, private xhttp: Http, private _xdefOpts?: RequestOptions) {
super(options, xhttp);
this._configuration = options.getConfig();
}
private refreshToken() : Observable<Response>{
let headers = new Headers();
headers.append('Authorization','basic_auth_token_here');
headers.append('content-type', 'application/x-www-form-urlencoded');
return this.post(
this.tokenUrl,
"refresh_token="+localStorage.getItem("refresh_token")+"&grant_type="+"refresh_token",
{ headers }
);
}
setAuthHeader(req: Request){
req.headers.set(this._configuration.headerName, this._configuration.headerPrefix + this._configuration.tokenGetter());
}
//overload the default authHttp request call (which gets called on every single request btw), and refresh the token if you can
request(url: string | Request, options?: RequestOptionsArgs) : Observable<Response> {
if (typeof url === 'string') {
return this.get(url, options); // Recursion: transform url from String to Request
}
// from this point url is always an instance of Request;
let req: Request = <Request>url;
if (!tokenNotExpired(null, this._configuration.tokenGetter())) {
if (!this._configuration.noJwtError) {
return new Observable<Response>((obs: any) => {
// reauthenticate if has refresh_token
this.refreshToken()
.map(res => res.json())
.map((res) => {
if(res.access_token){
localStorage.setItem('id_token', res.access_token);
localStorage.setItem('refresh_token', res.refresh_token);
this.setAuthHeader(req);
}else{
obs.error(new Error('No JWT present or has expired'));
}
});
});
}
} else {
this.setAuthHeader(req);
}
return this.xhttp.request(req);
}
}
a používám to takto:
import { Component } from '@angular/core';
import { CustomHttp } from '../../shared/customHttp/index';
import { AuthConfig } from 'angular2-jwt';
/**
* This class represents the lazy loaded HomeComponent.
*/
@Component({
moduleId: module.id,
templateUrl: 'view.component.html',
styleUrls: ['view.component.css'],
providers: [ CustomHttp, AuthConfig ]
})
export class ViewComponent{
constructor(private customHttp: CustomHttp) {}
public getSomeDataFromApi(){
this.customHttp.get(this.customHttp.apiBase+"protected_resource_url").map(res => res.json()).map(res => {
console.log(res);
});
}
}
Chyba říká: VÝJIMKA: Chyba: Nezachyceno (v příslibu): Nelze vyřešit všechny parametry pro AuthConfig: (?)
Cokoliv, co byste mohli navrhnout na pomoc, by bylo fantastické. Zkoušel jsem @Inject(type_here) se všemi parametry konstruktoru třídy CustomHttp, abych to opravil, a importoval různé typy všude, kde to bylo možné, ale bez úspěchu. Moc díky.
Také, pokud víte o knihovně, která už dělá to, co hledám, hned po vybalení, zvážil bych také přechod na něco méně domácí výroby.