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:
Jovan Krunić
2019-03-05 17:04:47 +01:00
parent 8c3c2810e5
commit 2558163ad6
5 changed files with 534 additions and 9 deletions

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 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 {SCBackendAggregationConfiguration, SCThingType} from '@openstapps/core';
// provides sample aggregations to be used in tests or backendless development
export const sampleAggregations: SCBackendAggregationConfiguration[] = [
{
fieldName: 'categories',
onlyOnTypes: [
SCThingType.AcademicEvent,
SCThingType.Article,
SCThingType.Building,
SCThingType.Catalog,
SCThingType.Dish,
SCThingType.PointOfInterest,
SCThingType.Room,
],
},
{
fieldName: 'inPlace.name',
onlyOnTypes: [
SCThingType.DateSeries,
SCThingType.Dish,
SCThingType.Floor,
SCThingType.Organization,
SCThingType.PointOfInterest,
SCThingType.Room,
SCThingType.Ticket,
],
},
{
fieldName: 'academicTerms.acronym',
onlyOnTypes: [
SCThingType.AcademicEvent,
SCThingType.SportCourse,
],
},
{
fieldName: 'academicTerm.acronym',
onlyOnTypes: [
SCThingType.Catalog,
],
},
{
fieldName: 'majors',
onlyOnTypes: [
SCThingType.AcademicEvent,
],
},
{
fieldName: 'keywords',
onlyOnTypes: [
SCThingType.Article,
SCThingType.Book,
SCThingType.Message,
SCThingType.Video,
],
},
{
fieldName: 'type',
},
];

View File

@@ -0,0 +1,370 @@
/*
* Copyright (C) 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 {SCAcademicEvent, SCArticle, SCBook, SCBuilding, SCCatalog,
SCDateSeries, SCDish, SCFavorite, SCMessage, SCPerson, SCRoom,
SCThing, SCThingOriginType, SCThingType, SCToDo, SCToDoPriority} from '@openstapps/core';
const sampleMessages: SCMessage[] = [
{
audiences: ['students'],
message: 'Foo Message Text',
name: 'Foo Message',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Message,
uid: 'message-123',
},
{
audiences: ['employees'],
message: 'Bar Message Text',
name: 'Bar Message',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Message,
uid: 'message-456',
},
];
const sampleDishes: SCDish[] = [
{
categories: ['main dish'],
name: 'Foo Dish',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Dish,
uid: 'dish-123',
},
{
categories: ['side dish'],
name: 'Bar Dish',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Dish,
uid: 'dish-456',
},
];
const sampleBuildings: SCBuilding[] = [
{
categories: ['education'],
geo: {
point: {type: 'Point', coordinates: [12, 12]},
},
name: 'Foo Building',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Building,
uid: 'building-123',
},
];
const sampleRooms: SCRoom[] = [
{
categories: ['library'],
geo: {
point: {type: 'Point', coordinates: [12, 12]},
},
name: 'Foo Room',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Room,
uid: 'room-123',
},
];
const sampleArticles: SCArticle[] = [
{
articleBody: 'Foo Text',
categories: ['unipedia'],
name: 'Foo Article',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Article,
uid: 'article-123',
},
{
articleBody: 'Bar Text',
categories: ['unipedia'],
name: 'Bar Article',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Article,
uid: 'article-456',
},
];
const samplePersons: SCPerson[] = [
{
familyName: 'Person',
givenName: 'Foo',
name: 'Foo Person',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Person,
uid: 'person-123',
},
{
familyName: 'Person',
givenName: 'Bar',
name: 'Bar Person',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Person,
uid: 'person-456',
},
];
const sampleBooks: SCBook[] = [
{
authors: samplePersons,
isbn: '123456',
name: 'Foo Book',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Book,
uid: 'book-123',
},
{
authors: [],
isbn: '123456',
name: 'Bar Book',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Book,
uid: 'book-234',
},
];
const sampleCatalogs: SCCatalog[] = [
{
categories: ['university events'],
level: 1,
name: 'Foo Catalog',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Catalog,
uid: 'catalog-123',
},
{
categories: ['university events'],
level: 1,
name: 'Bar Catalog',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.Catalog,
uid: 'catalog-456',
},
];
const sampleTodos: SCToDo[] = [
{
categories: ['foo category'],
done: false,
name: 'Foo Todo',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
priority: SCToDoPriority.LOW,
type: SCThingType.ToDo,
uid: 'todo-123',
},
{
categories: ['bar category'],
done: true,
name: 'Bar Todo',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
priority: SCToDoPriority.HIGH,
type: SCThingType.ToDo,
uid: 'todo-456',
},
];
const sampleFavorites: SCFavorite[] = [
{
data: sampleBuildings[0],
name: 'Foo Favorite',
origin: {
created: 'SOME-DATE',
type: SCThingOriginType.User,
},
type: SCThingType.Favorite,
uid: 'favorite-123',
},
{
data: samplePersons[1],
name: 'Bar Favorite',
origin: {
created: 'SOME-DATE',
type: SCThingOriginType.User,
},
type: SCThingType.Favorite,
uid: 'favorite-456',
},
];
const sampleAcademicEvents: SCAcademicEvent[] = [
{
categories: ['course'],
majors: ['Major One', 'Major Two'],
name: 'Foo Academic Event',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
performers: samplePersons,
type: SCThingType.AcademicEvent,
uid: 'academic-event-123',
},
{
categories: ['practicum'],
majors: ['Major Two', 'Major Three'],
name: 'Bar Academic Event',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
performers: samplePersons,
type: SCThingType.AcademicEvent,
uid: 'academic-event-456',
},
];
const sampleDateSeries: SCDateSeries[] = [
{
dates: ['2019-03-01T17:00:00+00:00', '2019-03-08T17:00:00+00:00'],
duration: 'PT2H',
event: sampleAcademicEvents[0],
frequency: 'once',
name: 'Foo Date Event - Date Series',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.DateSeries,
uid: 'date-series-123',
},
{
dates: ['2019-03-03T10:00:00+00:00', '2019-03-11T10:00:00+00:00'],
duration: 'PT2H',
event: sampleAcademicEvents[1],
frequency: 'weekly',
name: 'Bar Date Event - Date Series',
origin: {
indexed: 'SOME-DATE',
name: 'some name',
type: SCThingOriginType.Remote,
},
type: SCThingType.DateSeries,
uid: 'date-series-456',
},
];
export const sampleThingsMap: {[key in SCThingType | string]: SCThing[]} = {
'academic event': sampleAcademicEvents,
article: sampleArticles,
book: sampleBooks,
building: sampleBuildings,
catalog: sampleCatalogs,
'course of studies': [],
'date series': sampleDateSeries,
diff: [],
dish: sampleDishes,
favorite: sampleFavorites,
floor: [],
message: sampleMessages,
organization: [],
person: samplePersons,
'point of interest': [],
room: sampleRooms,
semester: [],
setting: [],
'sport course': [],
ticket: [],
todo: sampleTodos,
tour: [],
video: [],
};
// provides all or certain sample things to be used in tests or backendless development
export function getSampleThings(...uids: string[]): SCThing[] {
const sampleThings: SCThing[] = [];
if (typeof uids !== 'undefined' && uids.length > 0) {
uids.forEach((uid) => {
Object.keys(sampleThingsMap).forEach((key) => {
sampleThingsMap[key].forEach((thing) => {
if (thing.uid === uid) {
sampleThings.push(thing);
}
});
});
});
} else {
Object.keys(sampleThingsMap).forEach((key) => {
sampleThings.push(...sampleThingsMap[key]);
});
}
return sampleThings;
}

View 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,
};

View File

@@ -23,6 +23,7 @@ import {StatusBar} from '@ionic-native/status-bar/ngx';
import {IonicModule, IonicRouteStrategy} from '@ionic/angular';
import {TranslateLoader, TranslateModule} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
import {fakeBackendProvider} from './_helpers/fake-backend.interceptor';
import {AppRoutingModule} from './app-routing.module';
import {AppComponent} from './app.component';
import {ConfigModule} from './modules/config/config.module';
@@ -59,6 +60,8 @@ export function createTranslateLoader(http: HttpClient) {
CommonModule,
],
providers: [
// use fake backend in place of Http service for backendless development
fakeBackendProvider,
StatusBar,
SplashScreen,
{provide: RouteReuseStrategy, useClass: IonicRouteStrategy},