-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackend.service.ts
144 lines (122 loc) · 4.6 KB
/
backend.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { Observable } from 'rxjs';
import { Pagination } from '../shared';
import { User } from '../users/user.model';
import { CachedValue, StorageService } from './storage.service';
import { AuthService } from './auth.service';
/**
* The Backend fetch behavior.
*
* Defines if the service shall only rely on the backend or should also look in the storage.
*/
export enum BackendFetchMode {
/**
* Only use the Backend services to retrieve data.
*/
RemoteOnly,
/**
* First attempt to use the storage, then call the backend if nothing found.
*/
StorageThenRemote
}
export interface SimpleHeader {
[name: string]: string | string[];
}
export abstract class BackendService {
fetchBehavior: BackendFetchMode = BackendFetchMode.RemoteOnly;
constructor(private storageService: StorageService,
private authService: AuthService) {
// Clear the backend's store if not valid anymore
if (!this.isLoggedIn()) {
this.clear();
}
}
// Storage mechanisms
protected getFromStore(key: string): any {
return this.storageService.getItem(key);
}
public getFromCachedStore(key: string): any {
let value: any = this.getFromStore(key);
if (value) {
const json: any = JSON.parse(value);
if (json.date && new Date(json.date).getTime() > new Date().getTime()) {
value = json;
} else {
this.removeFromStore(key);
value = null;
}
}
return value;
}
public pushToStore(key: string, value: any): void {
if (typeof value === 'string') {
return this.storageService.setItem(key, value);
} else {
return this.storageService.setItem(key, JSON.stringify(value));
}
}
pushToCachedStore(key: string, value: any, date?: Date): void {
return this.pushToStore(key, new CachedValue(value, date));
}
protected removeFromStore(key: string): void {
return this.storageService.removeItem(key);
}
protected clearStore(): void {
return this.storageService.clear();
}
// Authentication mechanisms
public get user(): User {
return this.authService.user;
}
public get currentUser(): Observable<User> {
return this.authService.currentUser;
}
public authentifyUser(data: any): User {
return this.authService.authentifyUser(data);
}
clear() {
this.authService.clear();
// TODO Track other services / entries that need to be cleared
}
public isLoggedIn(): boolean {
return this.authService.isLoggedIn();
}
protected get token(): string {
return this.authService.token;
}
protected get tokenExpiration(): number {
return this.authService.tokenExpiration;
}
protected get refreshToken(): string {
return this.authService.refreshToken;
}
public get userId(): string {
return this.authService.userId;
}
public hasRole(role: string): boolean {
return this.authService.hasRole(role);
}
abstract get clientId(): string;
abstract get clientSecret(): string;
abstract get apiUrl(): string;
// Backend CRUD operations
abstract load(basePath: string | URL, pagination?: Pagination, params?: any, headers?: any);
getBlobById(basePath: string, id: string, params?: any, headers?: SimpleHeader): Observable<any> {
return this.getByIds(basePath, [id], params, headers);
}
getById(basePath: string, id: string, params?: any, headers?: SimpleHeader): Observable<any> {
return this.getByIds(basePath, [id], params, headers);
}
abstract getByIds(basePath: string, ids: string[], params?: any, headers?: SimpleHeader): Observable<any>;
push(basePath: string, value: any, params?: any, headers?: SimpleHeader): Promise<any> {
return this.pushAll(basePath, [value], params, headers);
}
abstract pushAll(basePath: string, values: any[], params?: any, headers?: SimpleHeader): Promise<any>;
set(basePath: string, id: string, value: any, params?: any, headers?: SimpleHeader): Promise<any> {
return this.setAll(basePath, [id], value, params, headers);
}
abstract setAll(basePath: string, ids: string[], values: any, params?: any, headers?: SimpleHeader): Promise<any>;
remove(basePath: string, id: string, params?: any, headers?: SimpleHeader): Promise<any> {
return this.removeAll(basePath, [id], params, headers);
}
abstract removeAll(basePath: string, ids: string[], params?: any, headers?: SimpleHeader): Promise<any>;
}