remult 0.14.31 → 0.14.32

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.
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JobsInQueueEntity = exports.EntityQueueStorage = exports.ExpressRequestBridgeToDataApiRequest = exports.SiteArea = exports.buildRemultServer = void 0;
3
+ exports.JobsInQueueEntity = exports.EntityQueueStorage = exports.ExpressRequestBridgeToDataApiRequest = exports.buildRemultServer = void 0;
4
4
  var tslib_1 = require("tslib");
5
- var register_actions_on_server_1 = require("./register-actions-on-server");
6
- var register_entities_on_server_1 = require("./register-entities-on-server");
7
5
  var JsonEntityFileStorage_1 = require("./JsonEntityFileStorage");
8
6
  var server_action_1 = require("../src/server-action");
9
7
  var data_api_1 = require("../src/data-api");
@@ -11,7 +9,7 @@ var context_1 = require("../src/context");
11
9
  var remult3_1 = require("../src/remult3");
12
10
  var json_data_provider_1 = require("../src/data-providers/json-data-provider");
13
11
  var id_entity_1 = require("../src/id-entity");
14
- function buildRemultServer(app, options) {
12
+ function buildRemultServer(options) {
15
13
  var _this = this;
16
14
  if (!options) {
17
15
  options = {};
@@ -49,37 +47,248 @@ function buildRemultServer(app, options) {
49
47
  });
50
48
  }); });
51
49
  }
52
- var bridge = new ExpressBridge(app, new inProcessQueueHandler(options.queueStorage), options, dataProvider);
53
- if (options.logApiEndPoints !== undefined)
54
- bridge.logApiEndPoints = options.logApiEndPoints;
55
- if (options.rootPath === undefined)
56
- options.rootPath = context_1.Remult.apiBaseUrl;
57
- var apiArea = bridge.addArea(options.rootPath);
58
- if (!options.disableAutoApi) {
59
- var actions = [];
50
+ {
51
+ var allControllers = [];
52
+ if (!options.entities)
53
+ options.entities = tslib_1.__spread(context_1.allEntities);
60
54
  if (options.entities)
61
- actions.push.apply(actions, tslib_1.__spread(options.entities));
55
+ allControllers.push.apply(allControllers, tslib_1.__spread(options.entities));
62
56
  if (options.controllers)
63
- actions.push.apply(actions, tslib_1.__spread(options.controllers));
64
- register_actions_on_server_1.registerActionsOnServer(apiArea, actions);
65
- register_entities_on_server_1.registerEntitiesOnServer(apiArea, options.entities);
57
+ allControllers.push.apply(allControllers, tslib_1.__spread(options.controllers));
58
+ options.controllers = allControllers;
66
59
  }
60
+ if (options.rootPath === undefined)
61
+ options.rootPath = context_1.Remult.apiBaseUrl;
62
+ server_action_1.actionInfo.runningOnServer = true;
63
+ var bridge = new RemultServerImplementation(new inProcessQueueHandler(options.queueStorage), options, dataProvider);
67
64
  return bridge;
68
65
  }
69
66
  exports.buildRemultServer = buildRemultServer;
70
67
  ;
71
- var ExpressBridge = /** @class */ (function () {
72
- function ExpressBridge(app, queue, options, dataProvider) {
73
- this.app = app;
68
+ var RemultServerImplementation = /** @class */ (function () {
69
+ function RemultServerImplementation(queue, options, dataProvider) {
74
70
  this.queue = queue;
75
71
  this.options = options;
76
72
  this.dataProvider = dataProvider;
73
+ this.registeredRouter = false;
74
+ this.hasQueue = false;
77
75
  /* internal */
78
76
  this.backendMethodsOpenApi = [];
79
- this.logApiEndPoints = true;
80
77
  }
81
- ExpressBridge.prototype.openApiDoc = function (options) {
78
+ RemultServerImplementation.prototype.handle = function (req, gRes) {
79
+ if (!this.routeImpl) {
80
+ this.routeImpl = new RouteImplementation();
81
+ this.registerRouter(this.routeImpl);
82
+ }
83
+ return this.routeImpl.handle(req, gRes);
84
+ };
85
+ RemultServerImplementation.prototype.registerRouter = function (r) {
82
86
  var e_1, _a, e_2, _b;
87
+ var _this = this;
88
+ if (this.registeredRouter)
89
+ throw "Router already registered";
90
+ this.registeredRouter = true;
91
+ {
92
+ try {
93
+ for (var _c = tslib_1.__values(this.options.controllers), _d = _c.next(); !_d.done; _d = _c.next()) {
94
+ var c = _d.value;
95
+ var z = c[server_action_1.classBackendMethodsArray];
96
+ if (z)
97
+ try {
98
+ for (var z_1 = (e_2 = void 0, tslib_1.__values(z)), z_1_1 = z_1.next(); !z_1_1.done; z_1_1 = z_1.next()) {
99
+ var a = z_1_1.value;
100
+ var x = a[server_action_1.serverActionField];
101
+ if (!x) {
102
+ throw 'failed to set server action, did you forget the BackendMethod Decorator?';
103
+ }
104
+ this.addAction(x, r);
105
+ }
106
+ }
107
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
108
+ finally {
109
+ try {
110
+ if (z_1_1 && !z_1_1.done && (_b = z_1.return)) _b.call(z_1);
111
+ }
112
+ finally { if (e_2) throw e_2.error; }
113
+ }
114
+ }
115
+ }
116
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
117
+ finally {
118
+ try {
119
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
120
+ }
121
+ finally { if (e_1) throw e_1.error; }
122
+ }
123
+ if (this.hasQueue)
124
+ this.addAction({
125
+ __register: function (x) {
126
+ x(server_action_1.Action.apiUrlForJobStatus, false, function () { return true; }, function (data, req, res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
127
+ var job, userId;
128
+ return tslib_1.__generator(this, function (_a) {
129
+ switch (_a.label) {
130
+ case 0: return [4 /*yield*/, this.queue.getJobInfo(data.queuedJobId)];
131
+ case 1:
132
+ job = _a.sent();
133
+ userId = undefined;
134
+ if (req === null || req === void 0 ? void 0 : req.user)
135
+ userId = req.user.id;
136
+ if (job.userId == '')
137
+ job.userId = undefined;
138
+ if (userId != job.userId)
139
+ res.forbidden();
140
+ else
141
+ res.success(job.info);
142
+ return [2 /*return*/];
143
+ }
144
+ });
145
+ }); });
146
+ }
147
+ }, r);
148
+ }
149
+ this.options.entities.forEach(function (e) {
150
+ var key = remult3_1.getEntityKey(e);
151
+ if (key != undefined)
152
+ _this.add(key, function (c) {
153
+ return new data_api_1.DataApi(c.repo(e), c);
154
+ }, r);
155
+ });
156
+ };
157
+ RemultServerImplementation.prototype.add = function (key, dataApiFactory, r) {
158
+ var _this = this;
159
+ var myRoute = this.options.rootPath + '/' + key;
160
+ if (this.options.logApiEndPoints)
161
+ console.log(myRoute);
162
+ r.route(myRoute)
163
+ .get(this.process(function (c, req, res) {
164
+ return dataApiFactory(c).httpGet(res, req);
165
+ })).put(this.process(function (c, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
166
+ return [2 /*return*/, dataApiFactory(c).put(res, '', orig.body)];
167
+ }); }); }))
168
+ .delete(this.process(function (c, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
169
+ return [2 /*return*/, dataApiFactory(c).delete(res, '')];
170
+ }); }); }))
171
+ .post(this.process(function (c, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
172
+ return [2 /*return*/, dataApiFactory(c).httpPost(res, req, orig.body)];
173
+ }); }); }));
174
+ r.route(myRoute + '/:id')
175
+ //@ts-ignore
176
+ .get(this.process(function (c, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
177
+ return [2 /*return*/, dataApiFactory(c).get(res, orig.params.id)];
178
+ }); }); }))
179
+ //@ts-ignore
180
+ .put(this.process(function (c, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
181
+ return [2 /*return*/, dataApiFactory(c).put(res, orig.params.id, orig.body)];
182
+ }); }); }))
183
+ //@ts-ignore
184
+ .delete(this.process(function (c, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
185
+ return [2 /*return*/, dataApiFactory(c).delete(res, orig.params.id)];
186
+ }); }); }));
187
+ };
188
+ RemultServerImplementation.prototype.process = function (what) {
189
+ var _this = this;
190
+ return function (req, res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
191
+ var myReq, myRes, remult, _a, _b, user;
192
+ return tslib_1.__generator(this, function (_c) {
193
+ switch (_c.label) {
194
+ case 0:
195
+ myReq = new ExpressRequestBridgeToDataApiRequest(req);
196
+ myRes = new ExpressResponseBridgeToDataApiResponse(res, req);
197
+ remult = new context_1.Remult();
198
+ _b = (_a = remult).setDataProvider;
199
+ return [4 /*yield*/, this.dataProvider];
200
+ case 1:
201
+ _b.apply(_a, [_c.sent()]);
202
+ if (!req) return [3 /*break*/, 5];
203
+ user = void 0;
204
+ if (!this.options.getUser) return [3 /*break*/, 3];
205
+ return [4 /*yield*/, this.options.getUser(req)];
206
+ case 2:
207
+ user = _c.sent();
208
+ return [3 /*break*/, 4];
209
+ case 3:
210
+ user = req['user'];
211
+ if (!user)
212
+ user = req['auth'];
213
+ _c.label = 4;
214
+ case 4:
215
+ if (user)
216
+ remult.setUser(user);
217
+ _c.label = 5;
218
+ case 5:
219
+ if (!this.options.initRequest) return [3 /*break*/, 7];
220
+ return [4 /*yield*/, this.options.initRequest(remult, req)];
221
+ case 6:
222
+ _c.sent();
223
+ _c.label = 7;
224
+ case 7:
225
+ what(remult, myReq, myRes, req);
226
+ return [2 /*return*/];
227
+ }
228
+ });
229
+ }); };
230
+ };
231
+ ;
232
+ RemultServerImplementation.prototype.getRemult = function (req) {
233
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
234
+ var remult;
235
+ var _this = this;
236
+ return tslib_1.__generator(this, function (_a) {
237
+ switch (_a.label) {
238
+ case 0: return [4 /*yield*/, this.process(function (c) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
239
+ return tslib_1.__generator(this, function (_a) {
240
+ remult = c;
241
+ return [2 /*return*/];
242
+ });
243
+ }); })(req, undefined)];
244
+ case 1:
245
+ _a.sent();
246
+ return [2 /*return*/, remult];
247
+ }
248
+ });
249
+ });
250
+ };
251
+ RemultServerImplementation.prototype.addAction = function (action, r) {
252
+ var _this = this;
253
+ action.__register(function (url, queue, allowed, what) {
254
+ var myUrl = _this.options.rootPath + '/' + url;
255
+ var tag = (function () {
256
+ var split = url.split('/');
257
+ if (split.length == 1)
258
+ return 'Static Backend Methods';
259
+ else
260
+ return split[0];
261
+ })();
262
+ _this.backendMethodsOpenApi.push({ path: myUrl, allowed: allowed, tag: tag });
263
+ if (_this.options.logApiEndPoints)
264
+ console.log(myUrl);
265
+ if (queue) {
266
+ _this.hasQueue = true;
267
+ _this.queue.mapQueuedAction(myUrl, what);
268
+ }
269
+ r.route(myUrl).post(_this.process(function (remult, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
270
+ var r_1;
271
+ var _a;
272
+ return tslib_1.__generator(this, function (_b) {
273
+ switch (_b.label) {
274
+ case 0:
275
+ if (!queue) return [3 /*break*/, 2];
276
+ _a = {};
277
+ return [4 /*yield*/, this.queue.submitJob(myUrl, remult, orig.body)];
278
+ case 1:
279
+ r_1 = (_a.queuedJobId = _b.sent(),
280
+ _a);
281
+ res.success(r_1);
282
+ return [3 /*break*/, 3];
283
+ case 2: return [2 /*return*/, what(orig.body, remult, res)];
284
+ case 3: return [2 /*return*/];
285
+ }
286
+ });
287
+ }); }));
288
+ });
289
+ };
290
+ RemultServerImplementation.prototype.openApiDoc = function (options) {
291
+ var e_3, _a, e_4, _b;
83
292
  var r = new context_1.Remult();
84
293
  if (!options.version)
85
294
  options.version = "1.0.0";
@@ -130,14 +339,14 @@ var ExpressBridge = /** @class */ (function () {
130
339
  }
131
340
  };
132
341
  var _loop_1 = function (e) {
133
- var e_3, _a;
342
+ var e_5, _a;
134
343
  var meta = r.repo(e).metadata;
135
344
  var key = remult3_1.getEntityKey(e);
136
345
  var parameters = [];
137
346
  if (key) {
138
347
  var properties = {};
139
348
  try {
140
- for (var _b = (e_3 = void 0, tslib_1.__values(meta.fields)), _c = _b.next(); !_c.done; _c = _b.next()) {
349
+ for (var _b = (e_5 = void 0, tslib_1.__values(meta.fields)), _c = _b.next(); !_c.done; _c = _b.next()) {
141
350
  var f = _c.value;
142
351
  var type = f.valueType == String ? "string" :
143
352
  f.valueType == Boolean ? "boolean" :
@@ -165,12 +374,12 @@ var ExpressBridge = /** @class */ (function () {
165
374
  });
166
375
  }
167
376
  }
168
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
377
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
169
378
  finally {
170
379
  try {
171
380
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
172
381
  }
173
- finally { if (e_3) throw e_3.error; }
382
+ finally { if (e_5) throw e_5.error; }
174
383
  }
175
384
  spec.components.schemas[key] = {
176
385
  type: "object",
@@ -308,21 +517,21 @@ var ExpressBridge = /** @class */ (function () {
308
517
  }
309
518
  };
310
519
  try {
311
- for (var allEntities_1 = tslib_1.__values(context_1.allEntities), allEntities_1_1 = allEntities_1.next(); !allEntities_1_1.done; allEntities_1_1 = allEntities_1.next()) {
312
- var e = allEntities_1_1.value;
520
+ for (var _c = tslib_1.__values(this.options.entities), _d = _c.next(); !_d.done; _d = _c.next()) {
521
+ var e = _d.value;
313
522
  _loop_1(e);
314
523
  }
315
524
  }
316
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
525
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
317
526
  finally {
318
527
  try {
319
- if (allEntities_1_1 && !allEntities_1_1.done && (_a = allEntities_1.return)) _a.call(allEntities_1);
528
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
320
529
  }
321
- finally { if (e_1) throw e_1.error; }
530
+ finally { if (e_3) throw e_3.error; }
322
531
  }
323
532
  try {
324
- for (var _c = tslib_1.__values(this.backendMethodsOpenApi), _d = _c.next(); !_d.done; _d = _c.next()) {
325
- var b = _d.value;
533
+ for (var _e = tslib_1.__values(this.backendMethodsOpenApi), _f = _e.next(); !_f.done; _f = _e.next()) {
534
+ var b = _f.value;
326
535
  spec.paths[b.path] = {
327
536
  post: secureBase(b.allowed, false, {
328
537
  "produces": ["application/json"],
@@ -361,12 +570,12 @@ var ExpressBridge = /** @class */ (function () {
361
570
  };
362
571
  }
363
572
  }
364
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
573
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
365
574
  finally {
366
575
  try {
367
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
576
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
368
577
  }
369
- finally { if (e_2) throw e_2.error; }
578
+ finally { if (e_4) throw e_4.error; }
370
579
  }
371
580
  spec.components.schemas["InvalidResponse"] = {
372
581
  "type": "object",
@@ -381,193 +590,8 @@ var ExpressBridge = /** @class */ (function () {
381
590
  };
382
591
  return spec;
383
592
  };
384
- ExpressBridge.prototype.addArea = function (rootUrl) {
385
- var r = new SiteArea(this, this.app, rootUrl, this.logApiEndPoints);
386
- if (!this.firstArea) {
387
- this.firstArea = r;
388
- }
389
- return r;
390
- };
391
- ExpressBridge.prototype.getRemult = function (req) {
392
- return tslib_1.__awaiter(this, void 0, void 0, function () {
393
- return tslib_1.__generator(this, function (_a) {
394
- return [2 /*return*/, this.firstArea.getRemult(req)];
395
- });
396
- });
397
- };
398
- return ExpressBridge;
593
+ return RemultServerImplementation;
399
594
  }());
400
- var SiteArea = /** @class */ (function () {
401
- function SiteArea(bridge, app, rootUrl, logApiEndpoints) {
402
- this.bridge = bridge;
403
- this.app = app;
404
- this.rootUrl = rootUrl;
405
- this.logApiEndpoints = logApiEndpoints;
406
- }
407
- SiteArea.prototype.add = function (key, dataApiFactory) {
408
- var _this = this;
409
- var myRoute = this.rootUrl + '/' + key;
410
- if (this.logApiEndpoints)
411
- console.log(myRoute);
412
- this.app.route(myRoute)
413
- .get(this.process(function (c, req, res) {
414
- return dataApiFactory(c).httpGet(res, req);
415
- })).put(this.process(function (c, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
416
- return [2 /*return*/, dataApiFactory(c).put(res, '', orig.body)];
417
- }); }); }))
418
- .delete(this.process(function (c, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
419
- return [2 /*return*/, dataApiFactory(c).delete(res, '')];
420
- }); }); }))
421
- .post(this.process(function (c, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
422
- return [2 /*return*/, dataApiFactory(c).httpPost(res, req, orig.body)];
423
- }); }); }));
424
- this.app.route(myRoute + '/:id')
425
- //@ts-ignore
426
- .get(this.process(function (c, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
427
- return [2 /*return*/, dataApiFactory(c).get(res, orig.params.id)];
428
- }); }); }))
429
- //@ts-ignore
430
- .put(this.process(function (c, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
431
- return [2 /*return*/, dataApiFactory(c).put(res, orig.params.id, orig.body)];
432
- }); }); }))
433
- //@ts-ignore
434
- .delete(this.process(function (c, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
435
- return [2 /*return*/, dataApiFactory(c).delete(res, orig.params.id)];
436
- }); }); }));
437
- };
438
- SiteArea.prototype.process = function (what) {
439
- var _this = this;
440
- return function (req, res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
441
- var myReq, myRes, remult, _a, _b, user;
442
- return tslib_1.__generator(this, function (_c) {
443
- switch (_c.label) {
444
- case 0:
445
- myReq = new ExpressRequestBridgeToDataApiRequest(req);
446
- myRes = new ExpressResponseBridgeToDataApiResponse(res, req);
447
- remult = new context_1.Remult();
448
- _b = (_a = remult).setDataProvider;
449
- return [4 /*yield*/, this.bridge.dataProvider];
450
- case 1:
451
- _b.apply(_a, [_c.sent()]);
452
- if (!req) return [3 /*break*/, 5];
453
- user = void 0;
454
- if (!this.bridge.options.getUser) return [3 /*break*/, 3];
455
- return [4 /*yield*/, this.bridge.options.getUser(req)];
456
- case 2:
457
- user = _c.sent();
458
- return [3 /*break*/, 4];
459
- case 3:
460
- user = req['user'];
461
- if (!user)
462
- user = req['auth'];
463
- _c.label = 4;
464
- case 4:
465
- if (user)
466
- remult.setUser(user);
467
- _c.label = 5;
468
- case 5:
469
- if (!this.bridge.options.initRequest) return [3 /*break*/, 7];
470
- return [4 /*yield*/, this.bridge.options.initRequest(remult, req)];
471
- case 6:
472
- _c.sent();
473
- _c.label = 7;
474
- case 7:
475
- what(remult, myReq, myRes, req);
476
- return [2 /*return*/];
477
- }
478
- });
479
- }); };
480
- };
481
- ;
482
- SiteArea.prototype.getRemult = function (req) {
483
- return tslib_1.__awaiter(this, void 0, void 0, function () {
484
- var remult;
485
- var _this = this;
486
- return tslib_1.__generator(this, function (_a) {
487
- switch (_a.label) {
488
- case 0: return [4 /*yield*/, this.process(function (c) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
489
- return tslib_1.__generator(this, function (_a) {
490
- remult = c;
491
- return [2 /*return*/];
492
- });
493
- }); })(req, undefined)];
494
- case 1:
495
- _a.sent();
496
- return [2 /*return*/, remult];
497
- }
498
- });
499
- });
500
- };
501
- SiteArea.prototype.initQueue = function () {
502
- var _this = this;
503
- this.addAction({
504
- __register: function (x) {
505
- x(server_action_1.Action.apiUrlForJobStatus, false, function () { return true; }, function (data, req, res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
506
- var job, userId;
507
- return tslib_1.__generator(this, function (_a) {
508
- switch (_a.label) {
509
- case 0: return [4 /*yield*/, this.bridge.queue.getJobInfo(data.queuedJobId)];
510
- case 1:
511
- job = _a.sent();
512
- userId = undefined;
513
- if (req === null || req === void 0 ? void 0 : req.user)
514
- userId = req.user.id;
515
- if (job.userId == '')
516
- job.userId = undefined;
517
- if (userId != job.userId)
518
- res.forbidden();
519
- else
520
- res.success(job.info);
521
- return [2 /*return*/];
522
- }
523
- });
524
- }); });
525
- }
526
- });
527
- this.initQueue = function () { };
528
- };
529
- SiteArea.prototype.addAction = function (action) {
530
- var _this = this;
531
- action.__register(function (url, queue, allowed, what) {
532
- var myUrl = _this.rootUrl + '/' + url;
533
- var tag = (function () {
534
- var split = url.split('/');
535
- if (split.length == 1)
536
- return 'Static Backend Methods';
537
- else
538
- return split[0];
539
- })();
540
- _this.bridge.backendMethodsOpenApi.push({ path: myUrl, allowed: allowed, tag: tag });
541
- if (_this.logApiEndpoints)
542
- console.log(myUrl);
543
- if (queue) {
544
- _this.initQueue();
545
- _this.bridge.queue.mapQueuedAction(myUrl, what);
546
- }
547
- _this.app.route(myUrl).post(_this.process(function (remult, req, res, orig) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
548
- var r;
549
- var _a;
550
- return tslib_1.__generator(this, function (_b) {
551
- switch (_b.label) {
552
- case 0:
553
- if (!queue) return [3 /*break*/, 2];
554
- _a = {};
555
- return [4 /*yield*/, this.bridge.queue.submitJob(myUrl, remult, orig.body)];
556
- case 1:
557
- r = (_a.queuedJobId = _b.sent(),
558
- _a);
559
- res.success(r);
560
- return [3 /*break*/, 3];
561
- case 2: return [2 /*return*/, what(orig.body, remult, res)];
562
- case 3: return [2 /*return*/];
563
- }
564
- });
565
- }); }));
566
- });
567
- };
568
- return SiteArea;
569
- }());
570
- exports.SiteArea = SiteArea;
571
595
  var ExpressRequestBridgeToDataApiRequest = /** @class */ (function () {
572
596
  function ExpressRequestBridgeToDataApiRequest(r) {
573
597
  this.r = r;
@@ -815,6 +839,121 @@ var EntityQueueStorage = /** @class */ (function () {
815
839
  return EntityQueueStorage;
816
840
  }());
817
841
  exports.EntityQueueStorage = EntityQueueStorage;
842
+ var RouteImplementation = /** @class */ (function () {
843
+ function RouteImplementation() {
844
+ this.map = new Map();
845
+ }
846
+ RouteImplementation.prototype.route = function (path) {
847
+ //consider using:
848
+ //* https://raw.githubusercontent.com/cmorten/opine/main/src/utils/pathToRegex.ts
849
+ //* https://github.com/pillarjs/path-to-regexp
850
+ var r = path.toLowerCase();
851
+ var m = new Map();
852
+ this.map.set(r, m);
853
+ var route = {
854
+ get: function (h) {
855
+ m.set("get", h);
856
+ return route;
857
+ },
858
+ put: function (h) {
859
+ m.set("put", h);
860
+ return route;
861
+ },
862
+ post: function (h) {
863
+ m.set("post", h);
864
+ return route;
865
+ },
866
+ delete: function (h) {
867
+ m.set("delete", h);
868
+ return route;
869
+ }
870
+ };
871
+ return route;
872
+ };
873
+ RouteImplementation.prototype.handle = function (req, gRes) {
874
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
875
+ var _this = this;
876
+ return tslib_1.__generator(this, function (_a) {
877
+ return [2 /*return*/, new Promise(function (res) {
878
+ var response = new /** @class */ (function () {
879
+ function class_1() {
880
+ this.statusCode = 200;
881
+ }
882
+ class_1.prototype.json = function (data) {
883
+ if (gRes !== undefined)
884
+ gRes.json(data);
885
+ res({ statusCode: this.statusCode, data: data });
886
+ };
887
+ class_1.prototype.status = function (statusCode) {
888
+ if (gRes !== undefined)
889
+ gRes.status(statusCode);
890
+ this.statusCode = statusCode;
891
+ return this;
892
+ };
893
+ class_1.prototype.end = function () {
894
+ if (gRes !== undefined)
895
+ gRes.end();
896
+ res({
897
+ statusCode: this.statusCode
898
+ });
899
+ };
900
+ return class_1;
901
+ }());
902
+ _this.middleware(req, response, function () { return res(undefined); });
903
+ })];
904
+ });
905
+ });
906
+ };
907
+ RouteImplementation.prototype.middleware = function (req, res, next) {
908
+ var theUrl = req.url;
909
+ if (theUrl.startsWith('/')) //next sends a partial url '/api/tasks' and not the full url
910
+ theUrl = 'http://stam' + theUrl;
911
+ var url = new URL(theUrl);
912
+ var path = url.pathname;
913
+ if (!req.query) {
914
+ var query_1 = {};
915
+ url.searchParams.forEach(function (val, key) {
916
+ var current = query_1[key];
917
+ if (!current) {
918
+ query_1[key] = val;
919
+ return;
920
+ }
921
+ if (Array.isArray(current)) {
922
+ current.push(val);
923
+ return;
924
+ }
925
+ query_1[key] = [current, val];
926
+ });
927
+ req.query = query_1;
928
+ }
929
+ var lowerPath = path.toLowerCase();
930
+ var m = this.map.get(lowerPath);
931
+ if (m) {
932
+ var h = m.get(req.method.toLowerCase());
933
+ if (h) {
934
+ h(req, res, next);
935
+ return;
936
+ }
937
+ }
938
+ var idPosition = path.lastIndexOf('/');
939
+ if (idPosition >= 0) {
940
+ lowerPath = path.substring(0, idPosition) + '/:id';
941
+ m = this.map.get(lowerPath);
942
+ if (m) {
943
+ var h = m.get(req.method.toLowerCase());
944
+ if (h) {
945
+ if (!req.params)
946
+ req.params = {};
947
+ req.params.id = path.substring(idPosition + 1);
948
+ h(req, res, next);
949
+ return;
950
+ }
951
+ }
952
+ }
953
+ next();
954
+ };
955
+ return RouteImplementation;
956
+ }());
818
957
  var JobsInQueueEntity = /** @class */ (function (_super) {
819
958
  tslib_1.__extends(JobsInQueueEntity, _super);
820
959
  function JobsInQueueEntity() {
package/server/index.d.ts CHANGED
@@ -1,4 +1,2 @@
1
1
  export { JsonEntityFileStorage } from './JsonEntityFileStorage';
2
- export { SiteArea, queuedJobInfo, QueueStorage } from './expressBridge';
3
- export { registerEntitiesOnServer } from './register-entities-on-server';
4
- export { registerActionsOnServer } from './register-actions-on-server';
2
+ export { queuedJobInfo, QueueStorage, GenericRequest, GenericRequestHandler, GenericResponse, GenericRouter, buildRemultServer, RemultServerOptions, SpecificRoute, RemultServer } from './expressBridge';