@nymphjs/server 1.0.0-beta.0 → 1.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/dist/cache.test.js +21 -32
- package/dist/cache.test.js.map +1 -1
- package/dist/index.js +152 -161
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +171 -185
- package/dist/index.test.js.map +1 -1
- package/dist/testArtifacts.js +22 -47
- package/dist/testArtifacts.js.map +1 -1
- package/package.json +6 -6
- package/tsconfig.json +2 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [1.0.0-beta.2](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.1...v1.0.0-beta.2) (2022-11-21)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @nymphjs/server
|
|
9
|
+
|
|
10
|
+
# [1.0.0-beta.1](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.0...v1.0.0-beta.1) (2022-11-21)
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
- adjust typescript targets to output node 16 code ([36f15a6](https://github.com/sciactive/nymphjs/commit/36f15a601362ed54f4465ef6527402c026bbcf61))
|
|
15
|
+
|
|
6
16
|
# [1.0.0-beta.0](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.43...v1.0.0-beta.0) (2022-11-16)
|
|
7
17
|
|
|
8
18
|
### Features
|
package/dist/cache.test.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -32,42 +23,40 @@ const nymph = new client_node_1.Nymph({
|
|
|
32
23
|
});
|
|
33
24
|
nymph.addEntityClass(testArtifacts_1.Employee);
|
|
34
25
|
describe('Nymph REST Server and Client with Client Weak Ref Cache', () => {
|
|
35
|
-
function createJane() {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return jane;
|
|
52
|
-
});
|
|
26
|
+
async function createJane() {
|
|
27
|
+
const jane = await testArtifacts_1.Employee.factory();
|
|
28
|
+
jane.name = 'Jane Doe';
|
|
29
|
+
jane.current = true;
|
|
30
|
+
jane.salary = 8000000;
|
|
31
|
+
jane.startDate = Date.now();
|
|
32
|
+
jane.subordinates = [];
|
|
33
|
+
jane.title = 'Seniorer Person';
|
|
34
|
+
try {
|
|
35
|
+
await jane.$save();
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
console.error('Error creating entity: ', e);
|
|
39
|
+
throw e;
|
|
40
|
+
}
|
|
41
|
+
return jane;
|
|
53
42
|
}
|
|
54
|
-
it('change an entity and check weakCache', () =>
|
|
43
|
+
it('change an entity and check weakCache', async () => {
|
|
55
44
|
if (typeof WeakRef === 'undefined') {
|
|
56
45
|
throw new Error('You must run this test in an environment that includes WeakRef.');
|
|
57
46
|
}
|
|
58
|
-
const employee =
|
|
47
|
+
const employee = await createJane();
|
|
59
48
|
if (employee.guid == null) {
|
|
60
49
|
throw new Error('Entity is null.');
|
|
61
50
|
}
|
|
62
|
-
const checkA =
|
|
63
|
-
const checkB =
|
|
51
|
+
const checkA = await testArtifacts_1.Employee.factory(employee.guid);
|
|
52
|
+
const checkB = await nymph.getEntity({ class: testArtifacts_1.Employee }, { type: '&', guid: employee.guid });
|
|
64
53
|
if (!checkB) {
|
|
65
54
|
throw new Error("Couldn't fetch entity.");
|
|
66
55
|
}
|
|
67
56
|
employee.current = false;
|
|
68
57
|
expect(checkA.current).toEqual(false);
|
|
69
58
|
expect(checkB.current).toEqual(false);
|
|
70
|
-
})
|
|
59
|
+
});
|
|
71
60
|
afterAll(() => {
|
|
72
61
|
server.close();
|
|
73
62
|
});
|
package/dist/cache.test.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.test.js","sourceRoot":"","sources":["../src/cache.test.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cache.test.js","sourceRoot":"","sources":["../src/cache.test.ts"],"names":[],"mappings":";;;;;AAAA,sDAA8B;AAC9B,6EAAoD;AACpD,0CAAsD;AACtD,sDAA6C;AAE7C,oDAAmC;AACnC,mDAA0D;AAE1D,MAAM,YAAY,GAAG;IACnB,QAAQ,EAAE,UAAU;CACrB,CAAC;AAEF,MAAM,WAAW,GAAG,IAAI,aAAW,CAAC,EAAE,EAAE,IAAI,wBAAa,CAAC,YAAY,CAAC,CAAC,CAAC;AACzE,WAAW,CAAC,cAAc,CAAC,6BAAa,CAAC,CAAC;AAE1C,MAAM,GAAG,GAAG,IAAA,iBAAO,GAAE,CAAC;AACtB,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,IAAA,eAAY,EAAC,WAAW,CAAC,CAAC,CAAC;AAC5C,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AAEhC,MAAM,KAAK,GAAG,IAAI,mBAAK,CAAC;IACtB,OAAO,EAAE,6BAA6B;IACtC,SAAS,EAAE,IAAI;CAChB,CAAC,CAAC;AACH,KAAK,CAAC,cAAc,CAAC,wBAAQ,CAAC,CAAC;AAE/B,QAAQ,CAAC,yDAAyD,EAAE,GAAG,EAAE;IACvE,KAAK,UAAU,UAAU;QACvB,MAAM,IAAI,GAAG,MAAM,wBAAQ,CAAC,OAAO,EAAE,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC;QACtB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAC5B,IAAI,CAAC,YAAY,GAAG,EAAE,CAAC;QACvB,IAAI,CAAC,KAAK,GAAG,iBAAiB,CAAC;QAC/B,IAAI;YACF,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;SACpB;QAAC,OAAO,CAAM,EAAE;YACf,OAAO,CAAC,KAAK,CAAC,yBAAyB,EAAE,CAAC,CAAC,CAAC;YAC5C,MAAM,CAAC,CAAC;SACT;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,EAAE,CAAC,sCAAsC,EAAE,KAAK,IAAI,EAAE;QAEpD,IAAI,OAAO,OAAO,KAAK,WAAW,EAAE;YAClC,MAAM,IAAI,KAAK,CACb,iEAAiE,CAClE,CAAC;SACH;QAED,MAAM,QAAQ,GAAG,MAAM,UAAU,EAAE,CAAC;QAEpC,IAAI,QAAQ,CAAC,IAAI,IAAI,IAAI,EAAE;YACzB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;SACpC;QAED,MAAM,MAAM,GAAG,MAAM,wBAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACrD,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,SAAS,CAClC,EAAE,KAAK,EAAE,wBAAQ,EAAE,EACnB,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE,CACnC,CAAC;QAEF,IAAI,CAAC,MAAM,EAAE;YACX,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;SAC3C;QAED,QAAQ,CAAC,OAAO,GAAG,KAAK,CAAC;QAEzB,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACtC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,GAAG,EAAE;QACZ,MAAM,CAAC,KAAK,EAAE,CAAC;IACjB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -54,30 +45,29 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
54
45
|
next();
|
|
55
46
|
}
|
|
56
47
|
function getActionData(request) {
|
|
57
|
-
var _a, _b, _c, _d;
|
|
58
48
|
if (request.method === 'GET') {
|
|
59
|
-
if (typeof
|
|
60
|
-
typeof
|
|
49
|
+
if (typeof request.query?.action !== 'string' ||
|
|
50
|
+
typeof request.query?.data !== 'string') {
|
|
61
51
|
return {
|
|
62
52
|
action: '',
|
|
63
53
|
data: {},
|
|
64
54
|
};
|
|
65
55
|
}
|
|
66
56
|
return {
|
|
67
|
-
action:
|
|
57
|
+
action: JSON.parse(request.query.action) ?? '',
|
|
68
58
|
data: JSON.parse(request.query.data),
|
|
69
59
|
};
|
|
70
60
|
}
|
|
71
61
|
else {
|
|
72
62
|
return {
|
|
73
|
-
action:
|
|
63
|
+
action: request.body.action ?? '',
|
|
74
64
|
data: request.body.data,
|
|
75
65
|
};
|
|
76
66
|
}
|
|
77
67
|
}
|
|
78
68
|
rest.use(instantiateNymph);
|
|
79
69
|
rest.use(authenticateTilmeld);
|
|
80
|
-
rest.get('/', (request, response) =>
|
|
70
|
+
rest.get('/', async (request, response) => {
|
|
81
71
|
try {
|
|
82
72
|
const { action, data } = getActionData(request);
|
|
83
73
|
if (['entity', 'entities', 'uid'].indexOf(action) === -1) {
|
|
@@ -114,10 +104,10 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
114
104
|
let result;
|
|
115
105
|
try {
|
|
116
106
|
if (action === 'entity') {
|
|
117
|
-
result =
|
|
107
|
+
result = await response.locals.nymph.getEntity(options, ...selectors);
|
|
118
108
|
}
|
|
119
109
|
else {
|
|
120
|
-
result =
|
|
110
|
+
result = await response.locals.nymph.getEntities(options, ...selectors);
|
|
121
111
|
}
|
|
122
112
|
}
|
|
123
113
|
catch (e) {
|
|
@@ -147,7 +137,7 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
147
137
|
}
|
|
148
138
|
let result;
|
|
149
139
|
try {
|
|
150
|
-
result =
|
|
140
|
+
result = await response.locals.nymph.getUID(data);
|
|
151
141
|
}
|
|
152
142
|
catch (e) {
|
|
153
143
|
httpError(response, 500, 'Internal Server Error', e);
|
|
@@ -169,8 +159,8 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
169
159
|
httpError(response, 500, 'Internal Server Error', e);
|
|
170
160
|
return;
|
|
171
161
|
}
|
|
172
|
-
})
|
|
173
|
-
rest.post('/', (request, response) =>
|
|
162
|
+
});
|
|
163
|
+
rest.post('/', async (request, response) => {
|
|
174
164
|
try {
|
|
175
165
|
const { action, data: dataConst } = getActionData(request);
|
|
176
166
|
let data = dataConst;
|
|
@@ -196,7 +186,7 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
196
186
|
}
|
|
197
187
|
let entity;
|
|
198
188
|
try {
|
|
199
|
-
entity =
|
|
189
|
+
entity = await loadEntity(entData, response.locals.nymph);
|
|
200
190
|
}
|
|
201
191
|
catch (e) {
|
|
202
192
|
if (e instanceof nymph_1.EntityConflictError) {
|
|
@@ -221,7 +211,7 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
221
211
|
continue;
|
|
222
212
|
}
|
|
223
213
|
try {
|
|
224
|
-
if (
|
|
214
|
+
if (await entity.$save()) {
|
|
225
215
|
created.push(entity);
|
|
226
216
|
hadSuccess = true;
|
|
227
217
|
}
|
|
@@ -298,7 +288,7 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
298
288
|
const result = method.call(EntityClass, ...params);
|
|
299
289
|
let ret = result;
|
|
300
290
|
if (result instanceof Promise) {
|
|
301
|
-
ret =
|
|
291
|
+
ret = await result;
|
|
302
292
|
}
|
|
303
293
|
response.status(200);
|
|
304
294
|
response.setHeader('Content-Type', 'application/json');
|
|
@@ -312,7 +302,7 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
312
302
|
else {
|
|
313
303
|
let entity;
|
|
314
304
|
try {
|
|
315
|
-
entity =
|
|
305
|
+
entity = await loadEntity(data.entity, response.locals.nymph);
|
|
316
306
|
}
|
|
317
307
|
catch (e) {
|
|
318
308
|
if (e instanceof nymph_1.EntityConflictError) {
|
|
@@ -346,7 +336,7 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
346
336
|
const result = entity[data.method](...params);
|
|
347
337
|
let ret = result;
|
|
348
338
|
if (result instanceof Promise) {
|
|
349
|
-
ret =
|
|
339
|
+
ret = await result;
|
|
350
340
|
}
|
|
351
341
|
response.status(200);
|
|
352
342
|
response.setHeader('Content-Type', 'application/json');
|
|
@@ -376,7 +366,7 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
376
366
|
}
|
|
377
367
|
let result;
|
|
378
368
|
try {
|
|
379
|
-
result =
|
|
369
|
+
result = await response.locals.nymph.newUID(data);
|
|
380
370
|
}
|
|
381
371
|
catch (e) {
|
|
382
372
|
httpError(response, 500, 'Internal Server Error', e);
|
|
@@ -395,152 +385,150 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
395
385
|
httpError(response, 500, 'Internal Server Error', e);
|
|
396
386
|
return;
|
|
397
387
|
}
|
|
398
|
-
})
|
|
399
|
-
rest.put('/', (request, response) =>
|
|
388
|
+
});
|
|
389
|
+
rest.put('/', async (request, response) => {
|
|
400
390
|
try {
|
|
401
391
|
const { action, data } = getActionData(request);
|
|
402
392
|
if (['entity', 'entities', 'uid'].indexOf(action) === -1) {
|
|
403
393
|
httpError(response, 400, 'Bad Request');
|
|
404
394
|
return;
|
|
405
395
|
}
|
|
406
|
-
|
|
396
|
+
await doPutOrPatch(response, action, data, false);
|
|
407
397
|
}
|
|
408
398
|
catch (e) {
|
|
409
399
|
httpError(response, 500, 'Internal Server Error', e);
|
|
410
400
|
return;
|
|
411
401
|
}
|
|
412
|
-
})
|
|
413
|
-
rest.patch('/', (request, response) =>
|
|
402
|
+
});
|
|
403
|
+
rest.patch('/', async (request, response) => {
|
|
414
404
|
try {
|
|
415
405
|
const { action, data } = getActionData(request);
|
|
416
406
|
if (['entity', 'entities'].indexOf(action) === -1) {
|
|
417
407
|
httpError(response, 400, 'Bad Request');
|
|
418
408
|
return;
|
|
419
409
|
}
|
|
420
|
-
|
|
410
|
+
await doPutOrPatch(response, action, data, true);
|
|
421
411
|
}
|
|
422
412
|
catch (e) {
|
|
423
413
|
httpError(response, 500, 'Internal Server Error', e);
|
|
424
414
|
return;
|
|
425
415
|
}
|
|
426
|
-
})
|
|
427
|
-
function doPutOrPatch(response, action, data, patch) {
|
|
428
|
-
|
|
429
|
-
if (
|
|
430
|
-
|
|
431
|
-
|
|
416
|
+
});
|
|
417
|
+
async function doPutOrPatch(response, action, data, patch) {
|
|
418
|
+
if (action === 'uid') {
|
|
419
|
+
if (typeof data.name !== 'string' || typeof data.value !== 'number') {
|
|
420
|
+
httpError(response, 400, 'Bad Request');
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
if (response.locals.nymph.tilmeld) {
|
|
424
|
+
if (!response.locals.nymph.tilmeld.checkClientUIDPermissions(data.name, nymph_1.TilmeldAccessLevels.FULL_ACCESS)) {
|
|
425
|
+
httpError(response, 403, 'Forbidden');
|
|
432
426
|
return;
|
|
433
427
|
}
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
428
|
+
}
|
|
429
|
+
let result;
|
|
430
|
+
try {
|
|
431
|
+
result = await response.locals.nymph.setUID(data.name, data.value);
|
|
432
|
+
}
|
|
433
|
+
catch (e) {
|
|
434
|
+
httpError(response, 500, 'Internal Server Error', e);
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (!result) {
|
|
438
|
+
httpError(response, 500, 'Internal Server Error');
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
response.status(200);
|
|
442
|
+
response.setHeader('Content-Type', 'text/plain');
|
|
443
|
+
response.send(`${result}`);
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
if (action === 'entity') {
|
|
447
|
+
data = [data];
|
|
448
|
+
}
|
|
449
|
+
const saved = [];
|
|
450
|
+
let hadSuccess = false;
|
|
451
|
+
let invalidRequest = false;
|
|
452
|
+
let conflict = false;
|
|
453
|
+
let notfound = false;
|
|
454
|
+
let lastException = null;
|
|
455
|
+
for (let entData of data) {
|
|
456
|
+
if (entData.guid && entData.guid.length != 24) {
|
|
457
|
+
invalidRequest = true;
|
|
458
|
+
saved.push(null);
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
let entity;
|
|
441
462
|
try {
|
|
442
|
-
|
|
463
|
+
entity = await loadEntity(entData, response.locals.nymph, patch);
|
|
443
464
|
}
|
|
444
465
|
catch (e) {
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
}
|
|
448
|
-
if (!result) {
|
|
449
|
-
httpError(response, 500, 'Internal Server Error');
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
response.status(200);
|
|
453
|
-
response.setHeader('Content-Type', 'text/plain');
|
|
454
|
-
response.send(`${result}`);
|
|
455
|
-
}
|
|
456
|
-
else {
|
|
457
|
-
if (action === 'entity') {
|
|
458
|
-
data = [data];
|
|
459
|
-
}
|
|
460
|
-
const saved = [];
|
|
461
|
-
let hadSuccess = false;
|
|
462
|
-
let invalidRequest = false;
|
|
463
|
-
let conflict = false;
|
|
464
|
-
let notfound = false;
|
|
465
|
-
let lastException = null;
|
|
466
|
-
for (let entData of data) {
|
|
467
|
-
if (entData.guid && entData.guid.length != 24) {
|
|
468
|
-
invalidRequest = true;
|
|
469
|
-
saved.push(null);
|
|
470
|
-
continue;
|
|
471
|
-
}
|
|
472
|
-
let entity;
|
|
473
|
-
try {
|
|
474
|
-
entity = yield loadEntity(entData, response.locals.nymph, patch);
|
|
466
|
+
if (e instanceof nymph_1.EntityConflictError) {
|
|
467
|
+
conflict = true;
|
|
475
468
|
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
conflict = true;
|
|
479
|
-
}
|
|
480
|
-
else if (e.message === NOT_FOUND_ERROR) {
|
|
481
|
-
notfound = true;
|
|
482
|
-
}
|
|
483
|
-
else if (e instanceof nymph_1.InvalidParametersError) {
|
|
484
|
-
invalidRequest = true;
|
|
485
|
-
lastException = e;
|
|
486
|
-
}
|
|
487
|
-
else {
|
|
488
|
-
lastException = e;
|
|
489
|
-
}
|
|
490
|
-
saved.push(null);
|
|
491
|
-
continue;
|
|
469
|
+
else if (e.message === NOT_FOUND_ERROR) {
|
|
470
|
+
notfound = true;
|
|
492
471
|
}
|
|
493
|
-
if (
|
|
472
|
+
else if (e instanceof nymph_1.InvalidParametersError) {
|
|
494
473
|
invalidRequest = true;
|
|
495
|
-
|
|
496
|
-
continue;
|
|
497
|
-
}
|
|
498
|
-
try {
|
|
499
|
-
if (yield entity.$save()) {
|
|
500
|
-
saved.push(entity);
|
|
501
|
-
hadSuccess = true;
|
|
502
|
-
}
|
|
503
|
-
else {
|
|
504
|
-
saved.push(false);
|
|
505
|
-
}
|
|
474
|
+
lastException = e;
|
|
506
475
|
}
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
invalidRequest = true;
|
|
510
|
-
}
|
|
511
|
-
else {
|
|
512
|
-
lastException = e;
|
|
513
|
-
}
|
|
514
|
-
saved.push(null);
|
|
476
|
+
else {
|
|
477
|
+
lastException = e;
|
|
515
478
|
}
|
|
479
|
+
saved.push(null);
|
|
480
|
+
continue;
|
|
516
481
|
}
|
|
517
|
-
if (!
|
|
518
|
-
|
|
519
|
-
|
|
482
|
+
if (!entity) {
|
|
483
|
+
invalidRequest = true;
|
|
484
|
+
saved.push(null);
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
try {
|
|
488
|
+
if (await entity.$save()) {
|
|
489
|
+
saved.push(entity);
|
|
490
|
+
hadSuccess = true;
|
|
520
491
|
}
|
|
521
|
-
else
|
|
522
|
-
|
|
492
|
+
else {
|
|
493
|
+
saved.push(false);
|
|
523
494
|
}
|
|
524
|
-
|
|
525
|
-
|
|
495
|
+
}
|
|
496
|
+
catch (e) {
|
|
497
|
+
if (e instanceof nymph_2.EntityInvalidDataError) {
|
|
498
|
+
invalidRequest = true;
|
|
526
499
|
}
|
|
527
500
|
else {
|
|
528
|
-
|
|
501
|
+
lastException = e;
|
|
529
502
|
}
|
|
530
|
-
|
|
503
|
+
saved.push(null);
|
|
531
504
|
}
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
if (
|
|
535
|
-
response
|
|
505
|
+
}
|
|
506
|
+
if (!hadSuccess) {
|
|
507
|
+
if (invalidRequest) {
|
|
508
|
+
httpError(response, 400, 'Bad Request', lastException);
|
|
509
|
+
}
|
|
510
|
+
else if (conflict) {
|
|
511
|
+
httpError(response, 409, 'Conflict');
|
|
512
|
+
}
|
|
513
|
+
else if (notfound) {
|
|
514
|
+
httpError(response, 404, 'Not Found');
|
|
536
515
|
}
|
|
537
516
|
else {
|
|
538
|
-
response
|
|
517
|
+
httpError(response, 500, 'Internal Server Error', lastException);
|
|
539
518
|
}
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
response.status(200);
|
|
522
|
+
response.setHeader('Content-Type', 'application/json');
|
|
523
|
+
if (action === 'entity') {
|
|
524
|
+
response.send(JSON.stringify(saved[0]));
|
|
525
|
+
}
|
|
526
|
+
else {
|
|
527
|
+
response.send(saved);
|
|
540
528
|
}
|
|
541
|
-
}
|
|
529
|
+
}
|
|
542
530
|
}
|
|
543
|
-
rest.delete('/', (request, response) =>
|
|
531
|
+
rest.delete('/', async (request, response) => {
|
|
544
532
|
try {
|
|
545
533
|
const { action, data: dataConst } = getActionData(request);
|
|
546
534
|
let data = dataConst;
|
|
@@ -574,7 +562,7 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
574
562
|
}
|
|
575
563
|
let entity;
|
|
576
564
|
try {
|
|
577
|
-
entity =
|
|
565
|
+
entity = await response.locals.nymph.getEntity({ class: EntityClass }, { type: '&', guid: entData.guid });
|
|
578
566
|
}
|
|
579
567
|
catch (e) {
|
|
580
568
|
lastException = e;
|
|
@@ -587,7 +575,7 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
587
575
|
continue;
|
|
588
576
|
}
|
|
589
577
|
try {
|
|
590
|
-
if (
|
|
578
|
+
if (await entity.$delete()) {
|
|
591
579
|
deleted.push(entData.guid);
|
|
592
580
|
hadSuccess = true;
|
|
593
581
|
}
|
|
@@ -634,7 +622,7 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
634
622
|
}
|
|
635
623
|
let result;
|
|
636
624
|
try {
|
|
637
|
-
result =
|
|
625
|
+
result = await response.locals.nymph.deleteUID(data);
|
|
638
626
|
}
|
|
639
627
|
catch (e) {
|
|
640
628
|
httpError(response, 500, 'Internal Server Error', e);
|
|
@@ -653,35 +641,33 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
653
641
|
httpError(response, 500, 'Internal Server Error', e);
|
|
654
642
|
return;
|
|
655
643
|
}
|
|
656
|
-
})
|
|
644
|
+
});
|
|
657
645
|
rest.use(unauthenticateTilmeld);
|
|
658
|
-
function loadEntity(entityData, nymph, patch = false, allowConflict = false) {
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
throw new Error(NOT_FOUND_ERROR);
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
else {
|
|
675
|
-
entity = yield EntityClass.factory();
|
|
676
|
-
}
|
|
677
|
-
if (patch) {
|
|
678
|
-
entity.$jsonAcceptPatch(entityData, allowConflict);
|
|
679
|
-
}
|
|
680
|
-
else {
|
|
681
|
-
entity.$jsonAcceptData(entityData, allowConflict);
|
|
646
|
+
async function loadEntity(entityData, nymph, patch = false, allowConflict = false) {
|
|
647
|
+
if (entityData.class === 'Entity') {
|
|
648
|
+
throw new nymph_1.InvalidParametersError("Can't use Entity class directly from the front end.");
|
|
649
|
+
}
|
|
650
|
+
let EntityClass = nymph.getEntityClass(entityData.class);
|
|
651
|
+
let entity;
|
|
652
|
+
if (entityData.guid) {
|
|
653
|
+
entity = await nymph.getEntity({ class: EntityClass, source: 'client' }, {
|
|
654
|
+
type: '&',
|
|
655
|
+
guid: `${entityData['guid']}`,
|
|
656
|
+
});
|
|
657
|
+
if (entity === null) {
|
|
658
|
+
throw new Error(NOT_FOUND_ERROR);
|
|
682
659
|
}
|
|
683
|
-
|
|
684
|
-
|
|
660
|
+
}
|
|
661
|
+
else {
|
|
662
|
+
entity = await EntityClass.factory();
|
|
663
|
+
}
|
|
664
|
+
if (patch) {
|
|
665
|
+
entity.$jsonAcceptPatch(entityData, allowConflict);
|
|
666
|
+
}
|
|
667
|
+
else {
|
|
668
|
+
entity.$jsonAcceptData(entityData, allowConflict);
|
|
669
|
+
}
|
|
670
|
+
return entity;
|
|
685
671
|
}
|
|
686
672
|
function referencesToEntities(item, nymph) {
|
|
687
673
|
if (Array.isArray(item)) {
|
|
@@ -711,9 +697,14 @@ function createServer(nymph, { jsonOptions = {} } = {}) {
|
|
|
711
697
|
res.setHeader('Content-Type', 'application/json');
|
|
712
698
|
}
|
|
713
699
|
if (error) {
|
|
714
|
-
res.send(
|
|
715
|
-
|
|
716
|
-
:
|
|
700
|
+
res.send({
|
|
701
|
+
textStatus: `${errorCode} ${message}`,
|
|
702
|
+
message: error.message,
|
|
703
|
+
error,
|
|
704
|
+
...(process.env.NODE_ENV !== 'production'
|
|
705
|
+
? { stack: error.stack }
|
|
706
|
+
: {}),
|
|
707
|
+
});
|
|
717
708
|
}
|
|
718
709
|
else {
|
|
719
710
|
res.send({ textStatus: `${errorCode} ${message}` });
|