mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 01:22:54 +00:00
feat: use http interceptor for backendless development
Note: intercept method of the FakeBackendInterceptor should be developed further whenever it is needed to fake a response from the backend. Closes #37
This commit is contained in:
54
src/app/_helpers/fake-backend.interceptor.ts
Normal file
54
src/app/_helpers/fake-backend.interceptor.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2018, 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {HTTP_INTERCEPTORS, HttpEvent,
|
||||
HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';
|
||||
import {Injectable} from '@angular/core';
|
||||
import {SCThing} from '@openstapps/core';
|
||||
import {Observable, of} from 'rxjs';
|
||||
import {getSampleThings} from './data/sampleThings';
|
||||
|
||||
@Injectable()
|
||||
export class FakeBackendInterceptor implements HttpInterceptor {
|
||||
// tslint:disable-next-line:no-empty
|
||||
constructor() {
|
||||
}
|
||||
|
||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
let data: SCThing[] = [];
|
||||
// fake responses for search requests for easier (backendless) development process
|
||||
if (request.url.endsWith('/search') && request.method === 'POST') {
|
||||
if (typeof request.body.filter !== 'undefined' && typeof request.body.filter.arguments !== 'undefined') {
|
||||
if (request.body.filter.arguments.field === 'uid') {
|
||||
// provide items with given uid for search requests requesting single items (detail view)
|
||||
data = getSampleThings(request.body.filter.arguments.value);
|
||||
}
|
||||
} else {
|
||||
// if filter and arguments are not set, then provide all sample items (things)
|
||||
data = getSampleThings();
|
||||
}
|
||||
// fake a response of the backend with previously defined data
|
||||
return of(new HttpResponse({status: 200, body: {data: data}}));
|
||||
} else {
|
||||
// for all other requests, forward the requests to actually requested URL (backend)
|
||||
return next.handle(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const fakeBackendProvider = {
|
||||
multi: true,
|
||||
provide: HTTP_INTERCEPTORS,
|
||||
useClass: FakeBackendInterceptor,
|
||||
};
|
||||
Reference in New Issue
Block a user