@nymphjs/server 1.0.0-alpha.3 → 1.0.0-alpha.30

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 CHANGED
@@ -3,6 +3,131 @@
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-alpha.30](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.29...v1.0.0-alpha.30) (2022-01-07)
7
+
8
+ **Note:** Version bump only for package @nymphjs/server
9
+
10
+ # [1.0.0-alpha.29](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.28...v1.0.0-alpha.29) (2022-01-07)
11
+
12
+ **Note:** Version bump only for package @nymphjs/server
13
+
14
+ # [1.0.0-alpha.28](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.27...v1.0.0-alpha.28) (2022-01-07)
15
+
16
+ **Note:** Version bump only for package @nymphjs/server
17
+
18
+ # [1.0.0-alpha.27](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.26...v1.0.0-alpha.27) (2022-01-05)
19
+
20
+ ### Features
21
+
22
+ - update smui to latest versions ([7ed7bd3](https://github.com/sciactive/nymphjs/commit/7ed7bd34d01a155c7001a2671de25ef2f3363682))
23
+
24
+ # [1.0.0-alpha.26](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.25...v1.0.0-alpha.26) (2021-12-30)
25
+
26
+ ### Features
27
+
28
+ - add json options rest server option ([35cbff4](https://github.com/sciactive/nymphjs/commit/35cbff4b8dbc3e112604119efe05b813f5948dff))
29
+
30
+ # [1.0.0-alpha.25](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.24...v1.0.0-alpha.25) (2021-12-30)
31
+
32
+ **Note:** Version bump only for package @nymphjs/server
33
+
34
+ # [1.0.0-alpha.24](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.23...v1.0.0-alpha.24) (2021-12-30)
35
+
36
+ **Note:** Version bump only for package @nymphjs/server
37
+
38
+ # [1.0.0-alpha.23](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.22...v1.0.0-alpha.23) (2021-12-30)
39
+
40
+ ### Features
41
+
42
+ - add optional weakly referenced cache to client lib ([fbb184c](https://github.com/sciactive/nymphjs/commit/fbb184c6a721968bb92cc9b05c328594618554ed))
43
+
44
+ # [1.0.0-alpha.22](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.21...v1.0.0-alpha.22) (2021-12-14)
45
+
46
+ ### Features
47
+
48
+ - update smui to latest versions ([912873b](https://github.com/sciactive/nymphjs/commit/912873b863d1ae5d51c359a3c0558bff38ce85cd))
49
+
50
+ # [1.0.0-alpha.21](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.20...v1.0.0-alpha.21) (2021-11-29)
51
+
52
+ **Note:** Version bump only for package @nymphjs/server
53
+
54
+ # [1.0.0-alpha.20](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.19...v1.0.0-alpha.20) (2021-11-24)
55
+
56
+ **Note:** Version bump only for package @nymphjs/server
57
+
58
+ # [1.0.0-alpha.19](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.18...v1.0.0-alpha.19) (2021-11-18)
59
+
60
+ ### Features
61
+
62
+ - update smui and other packages to latest versions ([2465340](https://github.com/sciactive/nymphjs/commit/24653400d887bc04c41c3c4ee0c73ce2f2289e0d))
63
+
64
+ # [1.0.0-alpha.18](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.17...v1.0.0-alpha.18) (2021-11-09)
65
+
66
+ **Note:** Version bump only for package @nymphjs/server
67
+
68
+ # [1.0.0-alpha.17](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.16...v1.0.0-alpha.17) (2021-10-14)
69
+
70
+ **Note:** Version bump only for package @nymphjs/server
71
+
72
+ # [1.0.0-alpha.16](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.15...v1.0.0-alpha.16) (2021-10-14)
73
+
74
+ ### Bug Fixes
75
+
76
+ - don't ignore built files in npm packages ([7d688db](https://github.com/sciactive/nymphjs/commit/7d688dbec362f1f71fb451a1d0dbcaecc15d99fc))
77
+
78
+ # [1.0.0-alpha.15](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.14...v1.0.0-alpha.15) (2021-10-14)
79
+
80
+ **Note:** Version bump only for package @nymphjs/server
81
+
82
+ # [1.0.0-alpha.14](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.13...v1.0.0-alpha.14) (2021-10-14)
83
+
84
+ **Note:** Version bump only for package @nymphjs/server
85
+
86
+ # [1.0.0-alpha.13](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.12...v1.0.0-alpha.13) (2021-10-14)
87
+
88
+ **Note:** Version bump only for package @nymphjs/server
89
+
90
+ # [1.0.0-alpha.12](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.11...v1.0.0-alpha.12) (2021-10-13)
91
+
92
+ **Note:** Version bump only for package @nymphjs/server
93
+
94
+ # [1.0.0-alpha.11](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.10...v1.0.0-alpha.11) (2021-10-13)
95
+
96
+ **Note:** Version bump only for package @nymphjs/server
97
+
98
+ # [1.0.0-alpha.10](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.9...v1.0.0-alpha.10) (2021-10-12)
99
+
100
+ **Note:** Version bump only for package @nymphjs/server
101
+
102
+ # [1.0.0-alpha.9](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.8...v1.0.0-alpha.9) (2021-10-12)
103
+
104
+ **Note:** Version bump only for package @nymphjs/server
105
+
106
+ # [1.0.0-alpha.8](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.7...v1.0.0-alpha.8) (2021-10-12)
107
+
108
+ ### Bug Fixes
109
+
110
+ - server not running potentially important delete functions ([ce7521d](https://github.com/sciactive/nymphjs/commit/ce7521d89ca655f72f6842abf25c2817ce7b1513))
111
+
112
+ # [1.0.0-alpha.7](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.6...v1.0.0-alpha.7) (2021-10-05)
113
+
114
+ **Note:** Version bump only for package @nymphjs/server
115
+
116
+ # [1.0.0-alpha.6](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.5...v1.0.0-alpha.6) (2021-10-05)
117
+
118
+ **Note:** Version bump only for package @nymphjs/server
119
+
120
+ # [1.0.0-alpha.5](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.4...v1.0.0-alpha.5) (2021-09-30)
121
+
122
+ **Note:** Version bump only for package @nymphjs/server
123
+
124
+ # [1.0.0-alpha.4](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.3...v1.0.0-alpha.4) (2021-09-27)
125
+
126
+ ### Features
127
+
128
+ - gate UIDs on Tilmeld with config and abilities ([99a9141](https://github.com/sciactive/nymphjs/commit/99a9141cc92fe3d1ad68d21e42de4e9b5493e4d9))
129
+ - use new instances of nymph for server and pubsub requests ([0c18fab](https://github.com/sciactive/nymphjs/commit/0c18faba2b55fe82c16806d221fc54d2cd42c992))
130
+
6
131
  # [1.0.0-alpha.3](https://github.com/sciactive/nymphjs/compare/v1.0.0-alpha.2...v1.0.0-alpha.3) (2021-09-22)
7
132
 
8
133
  ### Features
package/README.md CHANGED
@@ -30,7 +30,7 @@ const nymph = new Nymph(
30
30
  filename: ':memory:',
31
31
  })
32
32
  );
33
- nymph.setEntityClass(MyEntity.class, MyEntity);
33
+ nymph.addEntityClass(MyEntity);
34
34
 
35
35
  // Create your Express app.
36
36
  const app = express();
@@ -58,7 +58,7 @@ const nymph = new Nymph({
58
58
  // use HTTPS, but you don't have to.
59
59
  restUrl: 'https://mydomain.tld/rest',
60
60
  });
61
- nymph.setEntityClass(MyEntity.class, MyEntity);
61
+ nymph.addEntityClass(MyEntity);
62
62
  ```
63
63
 
64
64
  # License
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,75 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const express_1 = __importDefault(require("express"));
16
+ const driver_sqlite3_1 = __importDefault(require("@nymphjs/driver-sqlite3"));
17
+ const nymph_1 = require("@nymphjs/nymph");
18
+ const client_node_1 = require("@nymphjs/client-node");
19
+ const index_1 = __importDefault(require("./index"));
20
+ const testArtifacts_1 = require("./testArtifacts");
21
+ const sqliteConfig = {
22
+ filename: ':memory:',
23
+ };
24
+ const nymphServer = new nymph_1.Nymph({}, new driver_sqlite3_1.default(sqliteConfig));
25
+ nymphServer.addEntityClass(testArtifacts_1.EmployeeModel);
26
+ const app = (0, express_1.default)();
27
+ app.use('/test', (0, index_1.default)(nymphServer));
28
+ const server = app.listen(5081);
29
+ const nymph = new client_node_1.Nymph({
30
+ restUrl: 'http://localhost:5081/test/',
31
+ weakCache: true,
32
+ });
33
+ nymph.addEntityClass(testArtifacts_1.Employee);
34
+ describe('Nymph REST Server and Client with Client Weak Ref Cache', () => {
35
+ function createJane() {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ const jane = yield testArtifacts_1.Employee.factory();
38
+ jane.name = 'Jane Doe';
39
+ jane.current = true;
40
+ jane.salary = 8000000;
41
+ jane.startDate = Date.now();
42
+ jane.subordinates = [];
43
+ jane.title = 'Seniorer Person';
44
+ try {
45
+ yield jane.$save();
46
+ }
47
+ catch (e) {
48
+ console.error('Error creating entity: ', e);
49
+ throw e;
50
+ }
51
+ return jane;
52
+ });
53
+ }
54
+ it('change an entity and check weakCache', () => __awaiter(void 0, void 0, void 0, function* () {
55
+ if (typeof WeakRef === 'undefined') {
56
+ throw new Error('You must run this test in an environment that includes WeakRef.');
57
+ }
58
+ const employee = yield createJane();
59
+ if (employee.guid == null) {
60
+ throw new Error('Entity is null.');
61
+ }
62
+ const checkA = yield testArtifacts_1.Employee.factory(employee.guid);
63
+ const checkB = yield nymph.getEntity({ class: testArtifacts_1.Employee }, { type: '&', guid: employee.guid });
64
+ if (!checkB) {
65
+ throw new Error("Couldn't fetch entity.");
66
+ }
67
+ employee.current = false;
68
+ expect(checkA.current).toEqual(false);
69
+ expect(checkB.current).toEqual(false);
70
+ }));
71
+ afterAll(() => {
72
+ server.close();
73
+ });
74
+ });
75
+ //# sourceMappingURL=cache.test.js.map
@@ -0,0 +1 @@
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,SAAe,UAAU;;YACvB,MAAM,IAAI,GAAG,MAAM,wBAAQ,CAAC,OAAO,EAAE,CAAC;YACtC,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;YACvB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;YACpB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC;YACtB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YAC5B,IAAI,CAAC,YAAY,GAAG,EAAE,CAAC;YACvB,IAAI,CAAC,KAAK,GAAG,iBAAiB,CAAC;YAC/B,IAAI;gBACF,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;aACpB;YAAC,OAAO,CAAM,EAAE;gBACf,OAAO,CAAC,KAAK,CAAC,yBAAyB,EAAE,CAAC,CAAC,CAAC;gBAC5C,MAAM,CAAC,CAAC;aACT;YACD,OAAO,IAAI,CAAC;QACd,CAAC;KAAA;IAED,EAAE,CAAC,sCAAsC,EAAE,GAAS,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,CAAA,CAAC,CAAC;IAEH,QAAQ,CAAC,GAAG,EAAE;QACZ,MAAM,CAAC,KAAK,EAAE,CAAC;IACjB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,2 +1,5 @@
1
+ import type { OptionsJson } from 'body-parser';
1
2
  import { Nymph } from '@nymphjs/nymph';
2
- export default function createServer(nymph: Nymph): import("express-serve-static-core").Express;
3
+ export default function createServer(nymph: Nymph, { jsonOptions }?: {
4
+ jsonOptions?: OptionsJson;
5
+ }): import("express-serve-static-core").Express;
package/dist/index.js CHANGED
@@ -16,16 +16,21 @@ const express_1 = __importDefault(require("express"));
16
16
  const cookie_parser_1 = __importDefault(require("cookie-parser"));
17
17
  const nymph_1 = require("@nymphjs/nymph");
18
18
  const nymph_2 = require("@nymphjs/nymph");
19
- function createServer(nymph) {
19
+ const NOT_FOUND_ERROR = 'Entity is not found.';
20
+ function createServer(nymph, { jsonOptions = {} } = {}) {
20
21
  const rest = (0, express_1.default)();
21
22
  rest.use((0, cookie_parser_1.default)());
22
- rest.use(express_1.default.json());
23
+ rest.use(express_1.default.json(jsonOptions || {}));
24
+ function instantiateNymph(_request, response, next) {
25
+ response.locals.nymph = nymph.clone();
26
+ next();
27
+ }
23
28
  function authenticateTilmeld(request, response, next) {
24
- if (nymph.tilmeld) {
25
- nymph.tilmeld.request = request;
26
- nymph.tilmeld.response = response;
29
+ if (response.locals.nymph.tilmeld) {
30
+ response.locals.nymph.tilmeld.request = request;
31
+ response.locals.nymph.tilmeld.response = response;
27
32
  try {
28
- nymph.tilmeld.authenticate();
33
+ response.locals.nymph.tilmeld.authenticate();
29
34
  }
30
35
  catch (e) {
31
36
  httpError(response, 500, 'Internal Server Error', e);
@@ -35,11 +40,11 @@ function createServer(nymph) {
35
40
  next();
36
41
  }
37
42
  function unauthenticateTilmeld(_request, response, next) {
38
- if (nymph.tilmeld) {
39
- nymph.tilmeld.request = null;
40
- nymph.tilmeld.response = null;
43
+ if (response.locals.nymph.tilmeld) {
44
+ response.locals.nymph.tilmeld.request = null;
45
+ response.locals.nymph.tilmeld.response = null;
41
46
  try {
42
- nymph.tilmeld.clearSession();
47
+ response.locals.nymph.tilmeld.clearSession();
43
48
  }
44
49
  catch (e) {
45
50
  httpError(response, 500, 'Internal Server Error', e);
@@ -70,6 +75,7 @@ function createServer(nymph) {
70
75
  };
71
76
  }
72
77
  }
78
+ rest.use(instantiateNymph);
73
79
  rest.use(authenticateTilmeld);
74
80
  rest.get('/', (request, response) => __awaiter(this, void 0, void 0, function* () {
75
81
  try {
@@ -94,7 +100,7 @@ function createServer(nymph) {
94
100
  }
95
101
  let EntityClass;
96
102
  try {
97
- EntityClass = nymph.getEntityClass(data[0].class);
103
+ EntityClass = response.locals.nymph.getEntityClass(data[0].class);
98
104
  }
99
105
  catch (e) {
100
106
  httpError(response, 400, 'Bad Request', e);
@@ -106,10 +112,10 @@ function createServer(nymph) {
106
112
  let result;
107
113
  try {
108
114
  if (action === 'entity') {
109
- result = yield nymph.getEntity(data[0], ...data.slice(1));
115
+ result = yield response.locals.nymph.getEntity(data[0], ...data.slice(1));
110
116
  }
111
117
  else {
112
- result = yield nymph.getEntities(...data);
118
+ result = yield response.locals.nymph.getEntities(...data);
113
119
  }
114
120
  }
115
121
  catch (e) {
@@ -117,7 +123,8 @@ function createServer(nymph) {
117
123
  return;
118
124
  }
119
125
  if (result === [] || result == null) {
120
- if (action === 'entity' || nymph.config.emptyListError) {
126
+ if (action === 'entity' ||
127
+ response.locals.nymph.config.emptyListError) {
121
128
  httpError(response, 404, 'Not Found');
122
129
  return;
123
130
  }
@@ -126,9 +133,19 @@ function createServer(nymph) {
126
133
  response.send(JSON.stringify(result));
127
134
  }
128
135
  else {
136
+ if (typeof data !== 'string') {
137
+ httpError(response, 400, 'Bad Request');
138
+ return;
139
+ }
140
+ if (response.locals.nymph.tilmeld) {
141
+ if (!response.locals.nymph.tilmeld.checkClientUIDPermissions(data, nymph_1.TilmeldAccessLevels.READ_ACCESS)) {
142
+ httpError(response, 403, 'Forbidden');
143
+ return;
144
+ }
145
+ }
129
146
  let result;
130
147
  try {
131
- result = yield nymph.getUID(`${data}`);
148
+ result = yield response.locals.nymph.getUID(data);
132
149
  }
133
150
  catch (e) {
134
151
  httpError(response, 500, 'Internal Server Error', e);
@@ -167,6 +184,7 @@ function createServer(nymph) {
167
184
  let hadSuccess = false;
168
185
  let invalidRequest = false;
169
186
  let conflict = false;
187
+ let notfound = false;
170
188
  let lastException = null;
171
189
  for (let entData of data) {
172
190
  if (entData.guid) {
@@ -176,12 +194,15 @@ function createServer(nymph) {
176
194
  }
177
195
  let entity;
178
196
  try {
179
- entity = yield loadEntity(entData);
197
+ entity = yield loadEntity(entData, response.locals.nymph);
180
198
  }
181
199
  catch (e) {
182
200
  if (e instanceof nymph_1.EntityConflictError) {
183
201
  conflict = true;
184
202
  }
203
+ else if (e.message === NOT_FOUND_ERROR) {
204
+ notfound = true;
205
+ }
185
206
  else if (e instanceof nymph_1.InvalidParametersError) {
186
207
  invalidRequest = true;
187
208
  lastException = e;
@@ -225,6 +246,10 @@ function createServer(nymph) {
225
246
  httpError(response, 409, 'Conflict');
226
247
  return;
227
248
  }
249
+ else if (notfound) {
250
+ httpError(response, 404, 'Not Found');
251
+ return;
252
+ }
228
253
  else {
229
254
  httpError(response, 500, 'Internal Server Error', lastException);
230
255
  return;
@@ -244,11 +269,11 @@ function createServer(nymph) {
244
269
  httpError(response, 400, 'Bad Request');
245
270
  return;
246
271
  }
247
- const params = referencesToEntities([...data.params]);
272
+ const params = referencesToEntities([...data.params], response.locals.nymph);
248
273
  if (data.static) {
249
274
  let EntityClass;
250
275
  try {
251
- EntityClass = nymph.getEntityClass(data.class);
276
+ EntityClass = response.locals.nymph.getEntityClass(data.class);
252
277
  }
253
278
  catch (e) {
254
279
  httpError(response, 400, 'Bad Request');
@@ -285,12 +310,15 @@ function createServer(nymph) {
285
310
  else {
286
311
  let entity;
287
312
  try {
288
- entity = yield loadEntity(data.entity);
313
+ entity = yield loadEntity(data.entity, response.locals.nymph);
289
314
  }
290
315
  catch (e) {
291
316
  if (e instanceof nymph_1.EntityConflictError) {
292
317
  httpError(response, 409, 'Conflict');
293
318
  }
319
+ else if (e.message === NOT_FOUND_ERROR) {
320
+ httpError(response, 404, 'Not Found', e);
321
+ }
294
322
  else if (e instanceof nymph_1.InvalidParametersError) {
295
323
  httpError(response, 400, 'Bad Request', e);
296
324
  }
@@ -334,9 +362,19 @@ function createServer(nymph) {
334
362
  }
335
363
  }
336
364
  else {
365
+ if (typeof data !== 'string') {
366
+ httpError(response, 400, 'Bad Request');
367
+ return;
368
+ }
369
+ if (response.locals.nymph.tilmeld) {
370
+ if (!response.locals.nymph.tilmeld.checkClientUIDPermissions(data, nymph_1.TilmeldAccessLevels.WRITE_ACCESS)) {
371
+ httpError(response, 403, 'Forbidden');
372
+ return;
373
+ }
374
+ }
337
375
  let result;
338
376
  try {
339
- result = yield nymph.newUID(`${data}`);
377
+ result = yield response.locals.nymph.newUID(data);
340
378
  }
341
379
  catch (e) {
342
380
  httpError(response, 500, 'Internal Server Error', e);
@@ -391,9 +429,15 @@ function createServer(nymph) {
391
429
  httpError(response, 400, 'Bad Request');
392
430
  return;
393
431
  }
432
+ if (response.locals.nymph.tilmeld) {
433
+ if (!response.locals.nymph.tilmeld.checkClientUIDPermissions(data.name, nymph_1.TilmeldAccessLevels.FULL_ACCESS)) {
434
+ httpError(response, 403, 'Forbidden');
435
+ return;
436
+ }
437
+ }
394
438
  let result;
395
439
  try {
396
- result = yield nymph.setUID(data.name, data.value);
440
+ result = yield response.locals.nymph.setUID(data.name, data.value);
397
441
  }
398
442
  catch (e) {
399
443
  httpError(response, 500, 'Internal Server Error', e);
@@ -425,16 +469,22 @@ function createServer(nymph) {
425
469
  }
426
470
  let entity;
427
471
  try {
428
- entity = yield loadEntity(entData, patch);
472
+ entity = yield loadEntity(entData, response.locals.nymph, patch);
429
473
  }
430
474
  catch (e) {
431
475
  if (e instanceof nymph_1.EntityConflictError) {
432
476
  conflict = true;
433
477
  }
434
- if (e instanceof nymph_1.InvalidParametersError) {
478
+ else if (e.message === NOT_FOUND_ERROR) {
479
+ notfound = true;
480
+ }
481
+ else if (e instanceof nymph_1.InvalidParametersError) {
435
482
  invalidRequest = true;
436
483
  lastException = e;
437
484
  }
485
+ else {
486
+ lastException = e;
487
+ }
438
488
  saved.push(null);
439
489
  continue;
440
490
  }
@@ -502,30 +552,58 @@ function createServer(nymph) {
502
552
  }
503
553
  const deleted = [];
504
554
  let failures = false;
555
+ let hadSuccess = false;
505
556
  let invalidRequest = false;
557
+ let notfound = false;
506
558
  let lastException = null;
507
- for (let delEnt of data) {
559
+ for (let entData of data) {
560
+ if (entData.guid && entData.guid.length != 24) {
561
+ invalidRequest = true;
562
+ continue;
563
+ }
564
+ let EntityClass;
508
565
  try {
509
- const guid = delEnt.guid;
510
- if (!delEnt.guid) {
511
- invalidRequest = true;
512
- continue;
513
- }
514
- if (yield nymph.deleteEntityByID(guid, delEnt.class)) {
515
- deleted.push(guid);
566
+ EntityClass = response.locals.nymph.getEntityClass(entData.class);
567
+ }
568
+ catch (e) {
569
+ invalidRequest = true;
570
+ failures = true;
571
+ continue;
572
+ }
573
+ let entity;
574
+ try {
575
+ entity = yield response.locals.nymph.getEntity({ class: EntityClass }, { type: '&', guid: entData.guid });
576
+ }
577
+ catch (e) {
578
+ lastException = e;
579
+ failures = true;
580
+ continue;
581
+ }
582
+ if (!entity) {
583
+ notfound = true;
584
+ failures = true;
585
+ continue;
586
+ }
587
+ try {
588
+ if (yield entity.$delete()) {
589
+ deleted.push(entData.guid);
590
+ hadSuccess = true;
516
591
  }
517
592
  else {
518
593
  failures = true;
519
594
  }
520
595
  }
521
596
  catch (e) {
522
- failures = true;
523
597
  lastException = e;
598
+ failures = true;
524
599
  }
525
600
  }
526
601
  if (deleted.length === 0) {
527
602
  if (invalidRequest || !failures) {
528
- httpError(response, 400, 'Bad Request');
603
+ httpError(response, 400, 'Bad Request', lastException);
604
+ }
605
+ else if (notfound) {
606
+ httpError(response, 404, 'Not Found');
529
607
  }
530
608
  else {
531
609
  httpError(response, 500, 'Internal Server Error', lastException);
@@ -546,9 +624,15 @@ function createServer(nymph) {
546
624
  httpError(response, 400, 'Bad Request');
547
625
  return;
548
626
  }
627
+ if (response.locals.nymph.tilmeld) {
628
+ if (!response.locals.nymph.tilmeld.checkClientUIDPermissions(data, nymph_1.TilmeldAccessLevels.FULL_ACCESS)) {
629
+ httpError(response, 403, 'Forbidden');
630
+ return;
631
+ }
632
+ }
549
633
  let result;
550
634
  try {
551
- result = yield nymph.deleteUID(data);
635
+ result = yield response.locals.nymph.deleteUID(data);
552
636
  }
553
637
  catch (e) {
554
638
  httpError(response, 500, 'Internal Server Error', e);
@@ -569,7 +653,7 @@ function createServer(nymph) {
569
653
  }
570
654
  }));
571
655
  rest.use(unauthenticateTilmeld);
572
- function loadEntity(entityData, patch = false, allowConflict = false) {
656
+ function loadEntity(entityData, nymph, patch = false, allowConflict = false) {
573
657
  return __awaiter(this, void 0, void 0, function* () {
574
658
  if (entityData.class === 'Entity') {
575
659
  throw new nymph_1.InvalidParametersError("Can't use Entity class directly from the front end.");
@@ -582,7 +666,7 @@ function createServer(nymph) {
582
666
  guid: `${entityData['guid']}`,
583
667
  });
584
668
  if (entity === null) {
585
- throw new Error('Entity is not found.');
669
+ throw new Error(NOT_FOUND_ERROR);
586
670
  }
587
671
  }
588
672
  else {
@@ -597,7 +681,7 @@ function createServer(nymph) {
597
681
  return entity;
598
682
  });
599
683
  }
600
- function referencesToEntities(item) {
684
+ function referencesToEntities(item, nymph) {
601
685
  if (Array.isArray(item)) {
602
686
  if (item.length === 3 && item[0] === 'nymph_entity_reference') {
603
687
  try {
@@ -608,12 +692,12 @@ function createServer(nymph) {
608
692
  return item;
609
693
  }
610
694
  }
611
- return item.map((entry) => referencesToEntities(entry));
695
+ return item.map((entry) => referencesToEntities(entry, nymph));
612
696
  }
613
697
  else if (typeof item === 'object' && !(item instanceof nymph_1.Entity)) {
614
698
  const newItem = {};
615
699
  for (let curProperty in item) {
616
- newItem[curProperty] = referencesToEntities(item[curProperty]);
700
+ newItem[curProperty] = referencesToEntities(item[curProperty], nymph);
617
701
  }
618
702
  return newItem;
619
703
  }