从Promise解析HTTP响应返回Observable

时间:2019-07-15 12:30:34

标签: angular ionic2 observable

嗨,我正在尝试编写脚本以确保成功完成sslCertificate Check之后的api请求 在我现有的脚本上,但是坚持下面的问题,请友好地指导。

1)(http-request-client.ts)所有请求的集中文件:获取或发布

createAuthHeader(headers: Headers) {
        if(localStorage.getItem('token')){
            this.token = localStorage.getItem('token');
            headers.append('Authorization', 'Bearer ' + this.token);
        }
    }

post(url: string, data: any) {

    let headers = new Headers();
    this.createAuthHeader(headers);
    return this.http.post(url, data,{headers: headers}
    ).catch(err => {
        return Observable.throw('Unauthorized');
    });

    }

2) Auth.ts (get getEnterpriseDetails)

getEnterpriseDetails(userdata : any): Observable<any> {
        return this.httpRequestClient.post(this.apiv2EndPoint+this.appconfig.getEnterpriseConfigUrl,userdata).map(
            (response) => {
                let data = response.text() ? response.json() : [{}];
                return data;
            }
        );
    }

 2) Login.ts

 getEnterpriseInfo(){
     this.auth.getEnterpriseDetails(userdata).toPromise().then((response) => {
        console.log(response);
    });
 }

From login.ts if i call the method getEnterpriseInfo i get the response (working as expected)
Now i need to add ssl pinning on the same script


checkSecurity(URL) {
        return new Promise((resolve,reject) => {

                let fingerprint = '5D 54 11 DE 3064 4F 21 65 05 D6 3E A2 11 2C A1 9F 09 91  3B FF 51 CE C2 58 AB 17 E8 34 04 25 D8';
                window.plugins.sslCertificateChecker.check(
                (message) => {
                        resolve(message);
                },
                (error) => {
                        reject(error);
                },
                URL,
                fingerprint
                );
        })
 }

http-request-client.ts (changed function after adding checkSecurity script)

 post(url: string, data: any) {
    this.checkSecurity(url).then((verified)=>{
            let headers = new Headers();
                this.createAuthHeader(headers);
                return this.http.post(url, data,{headers: headers}
                ).catch(err => {
                    return Observable.throw('Unauthorized');
                });
    }).catch((err)=>{
        console.log(err);
    })

    }

现在,如果我从login.ts调用getEnterpriseInfo(),它将引发错误

TypeError:无法读取Auth.ts上未定义的属性“地图” 我无法将Observable传递给Auth.ts(getEnterpriseDetails()) 请指导。

0 个答案:

没有答案