-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-data-cache.models.ts
101 lines (86 loc) · 2.69 KB
/
api-data-cache.models.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
import * as lodash from 'lodash';
// ----------------------------------------------------------------------------
// Models
// ----------------------------------------------------------------------------
export class DataModel {
id?: any;
_id?: any;
_cacheOutdated?: boolean;
_serv_timestamp?: any;
_cacheUpdatedAt?: any;
_serv_updatedByMe?: boolean;
_permissions?: any;
_outdated?: boolean;
_selected?: boolean;
_annotation?: any;
static serialize(data, FieldNames, relationalFields) {
let output: any = {};
output = serialize(data, FieldNames, relationalFields);
for (const attribute in output) {
if (attribute.startsWith('_')) { delete output[attribute]; }
}
return output;
}
}
export interface FetchOptions {
url: string;
data?: any;
method: 'GET'| 'PUT' | 'PATCH' | 'DELETE' | 'POST' | 'get' | 'put' | 'patch' | 'delete' | 'post';
}
export interface DataListResponse<T> {
listIds?: number[];
list: T[];
page_request: DataPageRequest;
}
export interface DataCreationResponse<T> {
id?: number;
instance?: T;
}
export class DataPageRequest {
itemsPerPage: number;
num_pages?: number;
page: number;
orderBy: string[];
count?: number;
constructor(){
this.itemsPerPage = 50;
this.page = 1;
this.orderBy= [''];
}
}
export class AdvancedListOptions {
filter: any = {};
searchScope: 'local' | 'server' | 'local+server' = 'server';
includeIds: number[] = [];
fullNesting: boolean = false;
}
export function serialize(originalData: any, fieldNames: string[], relationalFields: string[]) {
if (!originalData) {return null; }
const output = {};
const data = lodash.cloneDeep(originalData);
for (const fieldName of fieldNames) {
if (typeof (data[fieldName]) === 'undefined') {
continue;
}
output[fieldName] = data[fieldName];
}
for (const modelName of relationalFields) {
if (typeof (data[modelName]) === 'undefined') {
continue;
}
if (Array.isArray(data[modelName])) {
output[modelName] = data[modelName]
.map(value => {
if (value === null) { return null; }
if (value.hasOwnProperty('id')) { return value.id; }
if (typeof (value) === 'number' || typeof (value) === 'string') { return value; }
});
} else {
const value = data[modelName];
if (value === null) { output[modelName] = null; }
else if (value.hasOwnProperty('id')) { output[modelName] = value.id; }
else if (typeof (value) === 'number' || typeof (value) === 'string') { output[modelName] = value; }
}
}
return output;
}