-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatagateway.js
More file actions
120 lines (108 loc) · 3.42 KB
/
datagateway.js
File metadata and controls
120 lines (108 loc) · 3.42 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
/**
* DataGateway Client
* Een universele JavaScript wrapper voor de Python MongoDB API Gateway.
* Aangepast voor App-prefixing en geoptimaliseerd voor gebruik met OfflineManager.
*/
export default class DataGateway {
/**
* Initialiseert de gateway verbinding.
* @param {string} baseUrl - De volledige URL naar de API.
* @param {string} clientId - De unieke identificatie van de client.
* @param {string} appName - De naam van de applicatie (voor collectie-prefixing).
*/
constructor(baseUrl, clientId, appName) {
this.baseUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
this.clientId = clientId;
this.appName = appName;
}
/**
* Hulpmethode om de collectienaam te voorzien van de app-prefix.
* @private
*/
_getPrefixedName(collectionName) {
return `${this.appName}_${collectionName}`;
}
/**
* Genereert de standaard headers voor elk verzoek.
* @private
*/
_getHeaders() {
return {
'Content-Type': 'application/json',
'x-client-id': this.clientId
};
}
/**
* Verwerkt de API response en gooit een foutmelding bij problemen.
* @private
*/
async _handleResponse(response) {
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.error || `API Fout: ${response.status}`);
}
return response.json();
}
/**
* Haalt alle documenten op uit een collectie.
*/
async getCollection(collectionName) {
const fullPath = this._getPrefixedName(collectionName);
const response = await fetch(`${this.baseUrl}/api/${fullPath}`, {
method: 'GET',
headers: this._getHeaders()
});
return this._handleResponse(response);
}
/**
* Haalt één specifiek document op via ID.
*/
async getDocument(collectionName, docId) {
const fullPath = this._getPrefixedName(collectionName);
const response = await fetch(`${this.baseUrl}/api/${fullPath}/${docId}`, {
method: 'GET',
headers: this._getHeaders()
});
return this._handleResponse(response);
}
/**
* Slimme bewaarfunctie: Kiest automatisch tussen POST (nieuw) of PUT (update).
* Controleert op aanwezigheid van '_id' of 'id'.
*/
async saveDocument(collectionName, data) {
const fullPath = this._getPrefixedName(collectionName);
const docId = data._id || data.id;
const url = docId
? `${this.baseUrl}/api/${fullPath}/${docId}`
: `${this.baseUrl}/api/${fullPath}`;
const response = await fetch(url, {
method: docId ? 'PUT' : 'POST',
headers: this._getHeaders(),
body: JSON.stringify(data)
});
return this._handleResponse(response);
}
/**
* Verwijdert een document permanent uit de database.
*/
async deleteDocument(collectionName, docId) {
const fullPath = this._getPrefixedName(collectionName);
const response = await fetch(`${this.baseUrl}/api/${fullPath}/${docId}`, {
method: 'DELETE',
headers: this._getHeaders()
});
return this._handleResponse(response);
}
/**
* NIEUW: Wist de gehele collectie voor deze client.
* Stuurt een DELETE verzoek naar het collectie-endpoint zonder ID.
*/
async clearCollection(collectionName) {
const fullPath = this._getPrefixedName(collectionName);
const response = await fetch(`${this.baseUrl}/api/${fullPath}`, {
method: 'DELETE',
headers: this._getHeaders()
});
return this._handleResponse(response);
}
}