gpt4 book ai didi

javascript - 如何使用rxjs尽早逃脱捕获链?

转载 作者:行者123 更新时间:2023-12-03 08:49:39 25 4
gpt4 key购买 nike

我做了一个包装,用于使用身份验证 header 访问“rxjs/observable/dom/ajax”功能。那部分没问题。

export const authjax = {
create: urlOrRequest => ajax(typeof urlOrRequest === 'string'
? {url: urlOrRequest, headers: authHeader()} : _.merge({}, urlOrRequest, {headers: authHeader()})
),
get: (url, headers = {}) => ajax.get(url, {...headers, ...authHeader()}),
post: (url, body = {}, headers = {}) => ajax.post(url, body, {...headers, ...authHeader()}),
put: (url, body = {}, headers = {}) => ajax.put(url, body, {...headers, ...authHeader()}),
patch: (url, body = {}, headers = {}) => ajax.patch(url, body, {...headers, ...authHeader()}),
getJSON: (url, headers = {}) => ajax.getJSON(url, {...headers, ...authHeader()})
};

因此,这已代替ajax被使用。这是史诗中使用它的一个示例。
export const getBatchesEpic = (action$, store) =>
action$.ofType(actions.GET_BATCHES)
.switchMap(action => {
const {paginate, refresh} = action;
const paginatorNext = _.get(store.getState(), 'manager.paginator.batches.next');
const usePaginatorNext = paginatorNext && paginate && !refresh;
return authjax.get(usePaginatorNext ? paginatorNext : `${API_URL}/batches/`)
.concatMap(({response}) => {
const incompleteBatches = response.results.filter(batch => !batch.completed);
const checkBatchActions = incompleteBatches.map(batch => checkBatch(batch.id));
return [
{type: actions.BATCHES_RECEIVED, data: response, next: response.next, paginate},
...checkBatchActions
];
})
.catch(error => Observable.of({type: actions.GET_BATCHES_ERROR, error})
);
});

现在,我处于尝试仅使用 authjax.get方法进行简单测试通过的第一阶段。我希望来自服务器的401响应被authjax捕获,并让authjax返回注销操作,以及取消以下任何链接方法。

这是我对 authjax.get的最新尝试。它适用于成功的ajax响应和非401 ajax响应。当它到达401时,它不返回任何内容,甚至不执行注销操作。
export const authjax = {
create: urlOrRequest => ajax(typeof urlOrRequest === 'string'
? {url: urlOrRequest, headers: authHeader()} : _.merge({}, urlOrRequest, {headers: authHeader()})
),
get: (url, headers = {}) => {
const call = ajax.get(url, {...headers, ...authHeader()});

return call.catch(error => {
if (_.get(error, 'status') === 401) {
console.log('401!!!', Observable.of({type: actions.LOGOUT}));
return Observable.of({type: actions.LOGOUT}).ignoreElements();
}
return call;
});

},
post: (url, body = {}, headers = {}) => ajax.post(url, body, {...headers, ...authHeader()}),
put: (url, body = {}, headers = {}) => ajax.put(url, body, {...headers, ...authHeader()}),
patch: (url, body = {}, headers = {}) => ajax.patch(url, body, {...headers, ...authHeader()}),
getJSON: (url, headers = {}) => ajax.getJSON(url, {...headers, ...authHeader()})
};

我正在阅读rxjs文档,但是发现有一个假设,即您将永远不需要切换或取消流中的流。我知道也许可以在Epic本身中处理更多选项,但是我希望authjax能够处理其自身的身份验证问题,因此我可以将其用作使用同一auth的多个应用程序之间的导入。

最佳答案

这是我目前想出的答案。

export const wrapAjax = ({method, args = {}}) => {
const headers = {...args.headers || {}, ...authHeader()};

const call = method
? ajax[method](..._.values(_.omit(args, ['headers'])), headers)
: ajax(_.merge({}, args, {headers}));

return call.catch(error => {
if (_.get(error, 'status') === 401) {
return Observable.throw({...error, action: {type: actions.AUTH_401, error}}).ignoreElements();
}
return call;
});
};

如果状态为401,它将停止任何链方法,并命中任何后续的catch语句。令人遗憾的是,史诗中的catch语句必须调度返回的action属性。不被激起,但这就是我现在继续前进的方式。

关于javascript - 如何使用rxjs尽早逃脱捕获链?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44572459/

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