ค้นหาเว็บไซต์

typescript - ข้อผิดพลาด: ไม่สามารถจับได้ (ใน promise): ไม่สามารถแก้ไขพารามิเตอร์ทั้งหมดสำหรับ AuthConfig


ดังนั้นฉันกำลังพยายามขยายคลาส AuthHttp จากที่เก็บนี้เพื่อที่ฉันจะสามารถให้มันทำสิ่งที่เกี่ยวข้องกับกรณีการใช้งานของฉันมากขึ้น ได้แก่ การตรวจสอบโดยอัตโนมัติว่าทุกเบียร์โทเค็นหมดอายุหรือไม่ และจากนั้นใช้รีเฟรชโทเค็นเพื่อรับโทเค็นรับรองอื่นแล้วลองส่งคำขอเดิมอีกครั้ง ซึ่งเป็นสิ่งมาตรฐานของ OAuth 2 นอกจากนี้ฉันยังเป็นมือใหม่กับ Angular2 และเจอปัญหากับ DI ฉันได้ลองทำหลายอย่างเพื่อแก้ไขมัน แต่ก็ยังหาวิธีแก้ไขข้อผิดพลาดไม่ได้

นี่คือตัวอย่างเวอร์ชันง่ายของคลาส CustomHttp ของฉันที่สืบทอดมาจาก 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);
  }

}

และฉันกำลังใช้งานมันแบบนี้:

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

}

ข้อผิดพลาดระบุว่า: EXCEPTION: Error: ไม่สามารถจับ (ใน promise) ได้: ไม่สามารถแก้พารามิเตอร์ทั้งหมดสำหรับ AuthConfig: (?)

สิ่งใดก็ตามที่คุณสามารถแนะนำเพื่อช่วยได้จะยอดเยี่ยมมาก ฉันได้ลองใช้ @Inject(type_here) กับทุกพารามิเตอร์ของคอนสตรัคเตอร์คลาส CustomHttp เพื่อพยายามแก้ปัญหา และนำเข้าประเภทต่างๆ ทั่วทุกที่เพื่อพยายามแก้ไข แต่ก็ไม่สำเร็จ ขอบคุณมากๆ

นอกจากนี้ หากคุณรู้จักไลบรารีที่สามารถทำสิ่งที่ฉันกำลังมองหาได้ทันที ฉันก็อาจพิจารณาเปลี่ยนไปใช้สิ่งที่ไม่ใช่ของที่ทำเองด้วย