Skip to content

Instantly share code, notes, and snippets.

@surajmandalcell
Last active February 13, 2022 07:33
Show Gist options
  • Save surajmandalcell/ec19232e624f64f91a8febdb2cafa657 to your computer and use it in GitHub Desktop.
Save surajmandalcell/ec19232e624f64f91a8febdb2cafa657 to your computer and use it in GitHub Desktop.
Angular observable mock & error handling utils to shift from direct api to mock data when developing(for legacy code)
import { HttpErrorResponse } from '@angular/common/http';
import { throwError } from 'rxjs/internal/observable/throwError';
export function errorHandler(error: HttpErrorResponse) {
return throwError(error);
}
// Inside service
apiCall(data: any) {
this.getHearder();
if (environment.production) {
return this.http
.post<any>(this.baseUrl, data, this.httpOptions)
.pipe(catchError(errorHandler));
}
else {
return obsMock(keywordOverviewMock);
}
}
import { Observable } from 'rxjs';
export function obsMock(data: any, time: number = 1000) {
return new Observable((subscriber) => {
setTimeout(() => {
subscriber.next(data);
subscriber.complete();
}, time);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment