gpt4 book ai didi

angular - 无法使用 bindNodeCallback

转载 作者:搜寻专家 更新时间:2023-10-30 21:29:31 26 4
gpt4 key购买 nike

我正在尝试这个:

返回 Observable.bindNodeCallback(this.webAuth.client.userInfo)(this.accessToken);

我正在尝试从 Auth0 userInfo 函数创建一个可观察对象,以便我可以在 AuthGuard 的 canActivate 中使用它。

但是我收到了这个错误:

TS2346:提供的参数与调用目标的任何签名都不匹配。

上面的库函数是:

Authentication.prototype.userInfo = function(accessToken, cb) {
var url;
assert.check(accessToken, {
type: 'string',
message: 'accessToken parameter is not valid'
});
assert.check(cb, {
type: 'function',
message: 'cb parameter is not valid'
});
url = urljoin(this.baseOptions.rootUrl, 'userinfo');
return this.request
.get(url)
.set('Authorization', 'Bearer ' + accessToken)
.end(responseHandler(cb, {
ignoreCasing: true
}));
};

最佳答案

我遇到了完全相同的问题。最终,我没有使用 bindNodeCallback(),而是 discovered you can just wrap the entire call to userInfo() in Observable.create()像这样(注意:外部 userInfo() 函数是我的 AuthProvider 类的一部分):

userInfo = (token: string): Observable<User> => Observable.create(observer => {
this.auth0.client.userInfo(token, (err: any, user: User) => {
if (err) {
observer.error(err);
}
observer.next(user);
observer.complete();
});
});

测试(使用 Jest)

如果你想对上面的代码进行单元测试,我会使用惊人的 rxjs-marbles library@cartant 开发(谁在上面的评论中做出了回应)。以下是我测试相关部分的一些片段:

import { TestBed } from '@angular/core/testing';
import { marbles } from 'rxjs-marbles/jest'; // <-- add /jest here to level-up!
import { Observable } from 'rxjs';
import { StoreModule, Store } from '@ngrx/store';
import { AuthProvider } from './auth'; // my authentication service

// Here's a mock for the relevant bits of the Auth0 library
jest.mock('auth0-js', () => ({
WebAuth: options => ({
client: {
userInfo: (token, cb) => {
if ( "error" === token ) {
cb(new Error("Profile error!"), null);
} else {
cb(null, {name: "Alice"});
}
}
}
})
}));

describe("Auth0.WebAuth.client", () => {
let auth: AuthProvider;
let store: Store<any>;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ StoreModule.forRoot({}) ],
providers: [ AuthProvider ]
});
store = TestBed.get(Store);
spyOn(store, 'pipe');
auth = TestBed.get(AuthProvider);
});

it("should return Observable<User> when calling userInfo()", marbles((m) => {
const user = { name: "Alice" }; // must match mocked value above
const source = m.cold("--a", { a: "dummy-access-token" });
const target = m.cold("--b", { b: user });
const profile = source.flatMap(auth.userInfo);

m.expect(profile).toBeObservable(target);
}));

it("throw Error on fail when calling userInfo()", marbles((m) => {
const err = new Error("Profile error!"); // must match mocked value above
const source = m.cold("--a", { a: "error" }); // this value triggers mock error
const target = m.cold("--#", null, err);
const profile = source.flatMap(auth.userInfo);

m.expect(profile).toBeObservable(target);
}));
});

我遇到的一些陷阱:

  • 确保在测试中使用 flatMap()!我永远卡在那个上面
  • 我是 marble testing 的新手我不得不 read the manual在我真正掌握发生的事情之前,已经经历了很多次

关于angular - 无法使用 bindNodeCallback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44071996/

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