Created
April 1, 2019 02:49
-
-
Save ddoronin/f721a7b60e55c9e8093e6c22fe03c4f4 to your computer and use it in GitHub Desktop.
overHttp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ajax } from "rxjs/ajax"; | |
import { switchMap, map, merge, catchError } from "rxjs/operators"; | |
import { BehaviorSubject, of } from "rxjs"; | |
import { Option, some, none } from "monas"; | |
import { IRequest } from "src/models/request-composer"; | |
export const overHttp = switchMap((_: Option<IRequest>) => | |
_.map(req => | |
new BehaviorSubject({ isLoading: true, req: _, resp: none }).pipe( | |
merge( | |
ajax({ url: req.uri, headers: req.headers }).pipe( | |
map(response => ({ | |
isLoading: false, | |
req: _, | |
resp: some(response) | |
})), | |
catchError((error: any) => { | |
return of({ | |
isLoading: false, | |
req: _, | |
resp: some(error) | |
}); | |
}) | |
) | |
) | |
) | |
).getOrElse(new BehaviorSubject({ isLoading: false, req: _, resp: none })) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment