I am using an Angular service to allow the user to upload files.
The implementation of the service is working ; my question is about RxJS and its pipeable operators, but here is the service signature just in case :
askUserForFile(): Observable<File>;
toBase64(file: File): Observable<string>;
isFileValid(file: File, configuration?: { size?: number, extensions?: string | string[] }): boolean;
The call to this service is as follows :
this.fileService
.askUserForFile()
.pipe(
// this is the operator I'm looking for
unknownOperator(file => this.fileService.isFileValid(file, { extensions: ['txt'] }))
mergeMap(file => {
fichier.filename = file.name;
return this.fileService.toBase64(file);
}))
.subscribe(base64 => {
fichier.base64 = base64;
// Rest of my code
}, error => {/* error handling */});
I would like to find an operator in place of unknownOperator that would throw an error if the condition isn't met.
I've tried with
filter: if the condition isn't met, the code stops after it,map: the code continues even if an error is thrown withthrowError
I thought about using the following piping
.pipe(
map(...),
catchError(...),
mergeMap(...)
)
Which I think might work, but I would like to find (if possible) an operator that shortens this piping.
Is it possible ? if not, is there a better piping ?
isValidFilewithin themergeMapand, if it returns a false value, either return athrowErrorobservable or justthrowan error.