-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAPIBackendAdapter.ts
More file actions
242 lines (207 loc) · 10.9 KB
/
APIBackendAdapter.ts
File metadata and controls
242 lines (207 loc) · 10.9 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* Copyright 2018 - 2021 Swiss Federal Institute of Technology Lausanne (EPFL)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This open source software code was developed in part or in whole in the
* Human Brain Project, funded from the European Union's Horizon 2020
* Framework Programme for Research and Innovation under
* Specific Grant Agreements No. 720270, No. 785907, and No. 945539
* (Human Brain Project SGA1, SGA2 and SGA3).
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import type API from './API';
import type { UUID, Stage, Settings, UserProfile, KGCoreResult, StructureOfType, InstanceFull, InstanceSummary, SuggestionStructure, Neighbor, Scope, UserSummary, IncomingLink, InstanceRawStructure, InstancesData, InstanceLabel } from '../types';
import type { AxiosInstance } from 'axios';
const RELATIVE_ROOT_PATH = '/editor/api';
declare global {
interface Window {
rootPath?: string
}
}
const getStage = (stage?: Stage) => {
if(stage) {
return `?stage=${stage}`;
}
return '';
};
const endpoints = {
settings: () => `${RELATIVE_ROOT_PATH}/settings`,
user: () => `${RELATIVE_ROOT_PATH}/users/me`,
usersForReview: (search: string) => `${RELATIVE_ROOT_PATH}/users/search?search=${search}`,
invitedUsers: (instanceId: UUID) => `${RELATIVE_ROOT_PATH}/instances/${instanceId}/invitedUsers`,
inviteUser: (instanceId: UUID, userId: UUID) => `${RELATIVE_ROOT_PATH}/instances/${instanceId}/users/${userId}/invite`,
instancesList: (stage?: Stage) => `${RELATIVE_ROOT_PATH}/instancesBulk/list${getStage(stage)}`,
instancesSummary: (stage?: Stage) => `${RELATIVE_ROOT_PATH}/instancesBulk/summary${getStage(stage)}`,
instancesLabel: (stage?: Stage) => `${RELATIVE_ROOT_PATH}/instancesBulk/label${getStage(stage)}`,
searchInstancesByType: (space: string, type: string, from: number, size: number, search: string) => `${RELATIVE_ROOT_PATH}/summary?space=${space}&type=${encodeURIComponent(type)}&from=${from}&size=${size}&searchByLabel=${encodeURIComponent(search)}`,
suggestions: (instanceId: UUID, field: string, sourceType: string|undefined, targetType: string|undefined, start:number|undefined, size:number|undefined, search: string|undefined) => {
const params = [];
if(sourceType) {
params.push(`sourceType=${encodeURIComponent(sourceType)}`);
}
if(targetType) {
params.push(`targetType=${encodeURIComponent(targetType)}`);
}
if(start) {
params.push(`start=${start}`);
}
if(size) {
params.push(`size=${size}`);
}
if(search) {
params.push(`search=${encodeURIComponent(search)}`);
}
return `${RELATIVE_ROOT_PATH}/instances/${instanceId}/suggestions?field=${encodeURIComponent(field)}${params.length>0?('&' + params.join('&')):''}`;
},
instance: (instanceId: UUID) => `${RELATIVE_ROOT_PATH}/instances/${instanceId}`,
rawInstance: (instanceId: UUID) => `${RELATIVE_ROOT_PATH}/instances/${instanceId}/raw`,
instanceScope: (instanceId: UUID) => `${RELATIVE_ROOT_PATH}/instances/${instanceId}/scope`,
createInstance: (space: string, instanceId?: UUID) => `${RELATIVE_ROOT_PATH}/instances${instanceId?('/' + instanceId):''}?space=${space}`,
moveInstance: (instanceId: UUID, space: string) => `${RELATIVE_ROOT_PATH}/instances/${instanceId}/spaces/${space}`,
release: (instanceId: UUID) => `${RELATIVE_ROOT_PATH}/releases/${instanceId}/release`,
releaseStatusTopInstance: () => `${RELATIVE_ROOT_PATH}/releases/status?releaseTreeScope=TOP_INSTANCE_ONLY`,
releaseStatusChildren: () => `${RELATIVE_ROOT_PATH}/releases/status?releaseTreeScope=CHILDREN_ONLY`,
neighbors: (instanceId: UUID) => `${RELATIVE_ROOT_PATH}/instances/${instanceId}/neighbors`,
spaceTypes: (space: string) => `${RELATIVE_ROOT_PATH}/spaces/${space}/types`,
removeTypeFromSpace: (type: string, space: string) => `${RELATIVE_ROOT_PATH}/spaces/${space}/types?type=${encodeURIComponent(type)}`,
types: (space: string) => `${RELATIVE_ROOT_PATH}/types?space=${space}`,
incomingLinks: (instanceId: UUID, property: string, type: string, from: number, size: number) => `${RELATIVE_ROOT_PATH}/instances/${instanceId}/incomingLinks?property=${encodeURIComponent(property)}&type=${encodeURIComponent(type)}&from=${from}&size=${size}`
};
class APIBackendAdapter implements API {
private _axios: AxiosInstance;
constructor(axios: AxiosInstance) {
this._axios = axios;
}
async getSettings(): Promise<Settings> {
const { data } = await this._axios.get(endpoints.settings());
return data?.data as Settings;
}
async getUserProfile(): Promise<UserProfile> {
const { data } = await this._axios.get(endpoints.user());
return data?.data as UserProfile;
}
async getSpaceTypes(space: string): Promise<KGCoreResult<StructureOfType[]>> {
const { data } = await this._axios.get(endpoints.spaceTypes(space));
return data;
}
async addTypesToSpace(types: string[], space: string): Promise<KGCoreResult<StructureOfType[]>> {
const { data } = await this._axios.post(endpoints.spaceTypes(space), types);
return data;
}
async removeTypeFromSpace(type: string, space: string): Promise<void> {
await this._axios.delete(endpoints.removeTypeFromSpace(type, space));
}
async getSpaceAvailableTypes(space: string): Promise<KGCoreResult<StructureOfType[]>> {
const { data } = await this._axios.get(endpoints.types(space));
return data;
}
async getInstance(instanceId: UUID): Promise<KGCoreResult<InstanceFull>> {
const { data } = await this._axios.get(endpoints.instance(instanceId));
return data;
}
async getRawInstance(instanceId: UUID): Promise<InstanceRawStructure> {
const { data } = await this._axios.get(endpoints.rawInstance(instanceId));
return data;
}
async deleteInstance(instanceId: UUID): Promise<void> {
await this._axios.delete(endpoints.instance(instanceId));
}
async createInstance(space: string, instanceId: UUID|undefined, payload: object): Promise<KGCoreResult<InstanceFull>> {
const { data } = await this._axios.post(endpoints.createInstance(space, instanceId), payload);
return data;
}
async moveInstance(instanceId: UUID, space: string): Promise<void> {
await this._axios.put(endpoints.moveInstance(instanceId, space));
}
async patchInstance(instanceId: UUID, payload: object): Promise<KGCoreResult<InstanceFull>> {
const { data } = await this._axios.patch(endpoints.instance(instanceId), payload);
return data;
}
async searchInstancesByType(space: string, type: string, from: number, size: number, search: string): Promise<KGCoreResult<InstanceSummary[]>> {
const { data } = await this._axios.get(endpoints.searchInstancesByType(space, type, from, size, search));
return data;
}
async getSuggestions(instanceId: UUID, field: string, sourceType: string|undefined, targetType: string|undefined, from: number|undefined, size: number|undefined, search: string|undefined, payload: object): Promise<KGCoreResult<SuggestionStructure>> { //NOSONAR
const { data } = await this._axios.post(endpoints.suggestions(instanceId, field, sourceType, targetType, from, size, search), payload);
return data;
}
async getInstanceNeighbors(instanceId: UUID): Promise<KGCoreResult<Neighbor>> {
const { data } = await this._axios.get(endpoints.neighbors(instanceId));
return data;
}
async getInstanceScope(instanceId: UUID): Promise<KGCoreResult<Scope>> {
const { data } = await this._axios.get(endpoints.instanceScope(instanceId));
return data;
}
async getInstancesLabel(stage: Stage, instanceIds: UUID[]): Promise<KGCoreResult<InstancesData<InstanceLabel>>> {
const { data } = await this._axios.post(endpoints.instancesLabel(stage), instanceIds);
return data;
}
async getInstancesSummary(stage: Stage | undefined, instanceIds: UUID[]): Promise<KGCoreResult<InstancesData<InstanceSummary>>> {
const { data } = await this._axios.post(endpoints.instancesSummary(stage), instanceIds);
return data;
}
async getInstancesList(stage: Stage, instanceIds: UUID[]): Promise<KGCoreResult<InstancesData<InstanceFull>>> {
const { data } = await this._axios.post(endpoints.instancesList(stage), instanceIds);
return data;
}
async getInvitedUsers(instanceId: UUID): Promise<KGCoreResult<UserSummary[]>> {
const { data } = await this._axios.get(endpoints.invitedUsers(instanceId));
return data;
}
async getUsersForReview(search: string): Promise<KGCoreResult<UserSummary[]>> {
const { data } = await this._axios.get(endpoints.usersForReview(search));
return data;
}
async inviteUser(instanceId: UUID, userId: UUID): Promise<KGCoreResult<UserSummary[]>> {
const { data } = await this._axios.put(endpoints.inviteUser(instanceId, userId));
return data;
}
async removeUserInvitation(instanceId: UUID, userId: UUID): Promise<KGCoreResult<UserSummary[]>> {
const { data } = await this._axios.delete(endpoints.inviteUser(instanceId, userId));
return data;
}
async releaseInstance(instanceId: UUID): Promise<void> {
await this._axios.put(endpoints.release(instanceId));
}
async unreleaseInstance(instanceId: UUID): Promise<void> {
await this._axios.delete(endpoints.release(instanceId));
}
async getReleaseStatusTopInstance(instanceIds: UUID[]): Promise<KGCoreResult<Map<string, KGCoreResult<string>>>> {
const { data } = await this._axios.post(endpoints.releaseStatusTopInstance(), instanceIds);
return data;
}
async getReleaseStatusChildren(instanceIds: UUID[]): Promise<KGCoreResult<Map<string, KGCoreResult<string>>>> {
const { data } = await this._axios.post(endpoints.releaseStatusChildren(), instanceIds);
return data;
}
async getMoreIncomingLinks(instanceId: UUID, property: string, type: string, from: number, size: number): Promise<KGCoreResult<IncomingLink[]>> {
const { data } = await this._axios.get(endpoints.incomingLinks(instanceId, property, type, from, size));
return data;
}
}
export default APIBackendAdapter;