Apologies for the newbie/dump question about RxJS, just started since I am learning Angular.
So I've created an asynchronous validator static method that calls an API service method checking the uniqueness of username in the database. The code snippets below works when the backend server is up and running. When it is not, it prints an error log message which I should be handling in the presentation component instead.
What am missing here in the transformation step to handle such generics use case to convert both Observable<SuccessResponse>
and Observable<ErrorResponse>
to Observable<ValidationErrors | null>
.
Validator
static usernameUnique(service: ApiService): AsyncValidatorFn {
return (control: FormControl): Observable<ValidationErrors | null> => {
return service.isUsernameTaken(control.value as string)
.pipe(
map((response: SuccessResponse) => response.data ? { taken: true } : null)
);
};
}
ApiService
isUsernameTaken(username: string): Observable<SuccessResponse | ErrorResponse> {
return this.httpCient.get('/backend/account/checkUnique', { params: { username: username } })
.pipe(catchError(this.handleError));
}
handleError(error: HttpErrorResponse) : Observable<ErrorResponse> {
// return specific payload of ErrorResponse based on error.status
}
Thanks in advance.