@fjell/express-router 4.4.3 → 4.4.5

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/dist/index.cjs CHANGED
@@ -64,22 +64,100 @@ class ItemRouter {
64
64
  _define_property$2(this, "getPkParam", ()=>{
65
65
  return `${this.getPkType()}Pk`;
66
66
  });
67
+ _define_property$2(this, "postAllAction", async (req, res)=>{
68
+ const libOptions = this.lib.definition.options;
69
+ const libOperations = this.lib.operations;
70
+ this.logger.debug('Posting All Action', {
71
+ query: req === null || req === void 0 ? void 0 : req.query,
72
+ params: req === null || req === void 0 ? void 0 : req.params,
73
+ locals: res === null || res === void 0 ? void 0 : res.locals
74
+ });
75
+ const allActionKey = req.path.substring(req.path.lastIndexOf('/') + 1);
76
+ if (!libOptions.allActions) {
77
+ this.logger.error('Item Actions are not configured');
78
+ res.status(500).json({
79
+ error: 'Item Actions are not configured'
80
+ });
81
+ return;
82
+ }
83
+ const allAction = libOptions.allActions[allActionKey];
84
+ if (!allAction) {
85
+ this.logger.error('All Action is not configured', {
86
+ allActionKey
87
+ });
88
+ res.status(500).json({
89
+ error: 'Item Action is not configured'
90
+ });
91
+ return;
92
+ }
93
+ try {
94
+ res.json(await libOperations.allAction(allActionKey, req.body));
95
+ } catch (err) {
96
+ this.logger.error('Error in All Action', {
97
+ message: err === null || err === void 0 ? void 0 : err.message,
98
+ stack: err === null || err === void 0 ? void 0 : err.stack
99
+ });
100
+ res.status(500).json(err);
101
+ }
102
+ });
103
+ _define_property$2(this, "getAllFacet", async (req, res)=>{
104
+ const libOptions = this.lib.definition.options;
105
+ const libOperations = this.lib.operations;
106
+ this.logger.debug('Getting All Facet', {
107
+ query: req === null || req === void 0 ? void 0 : req.query,
108
+ params: req === null || req === void 0 ? void 0 : req.params,
109
+ locals: res === null || res === void 0 ? void 0 : res.locals
110
+ });
111
+ const facetKey = req.path.substring(req.path.lastIndexOf('/') + 1);
112
+ if (!libOptions.allFacets) {
113
+ this.logger.error('Item Facets are not configured');
114
+ res.status(500).json({
115
+ error: 'Item Facets are not configured'
116
+ });
117
+ return;
118
+ }
119
+ const facet = libOptions.allFacets[facetKey];
120
+ if (!facet) {
121
+ this.logger.error('Item Facet is not configured', {
122
+ facetKey
123
+ });
124
+ res.status(500).json({
125
+ error: 'Item Facet is not configured'
126
+ });
127
+ return;
128
+ }
129
+ try {
130
+ const combinedQueryParams = {
131
+ ...req.query,
132
+ ...req.params
133
+ };
134
+ res.json(await libOperations.allFacet(facetKey, combinedQueryParams));
135
+ } catch (err) {
136
+ this.logger.error('Error in All Facet', {
137
+ message: err === null || err === void 0 ? void 0 : err.message,
138
+ stack: err === null || err === void 0 ? void 0 : err.stack
139
+ });
140
+ res.status(500).json(err);
141
+ }
142
+ });
67
143
  _define_property$2(this, "postItemAction", async (req, res)=>{
68
- this.logger.default('Getting Item', {
144
+ const libOptions = this.lib.definition.options;
145
+ const libOperations = this.lib.operations;
146
+ this.logger.debug('Getting Item', {
69
147
  query: req === null || req === void 0 ? void 0 : req.query,
70
148
  params: req === null || req === void 0 ? void 0 : req.params,
71
149
  locals: res === null || res === void 0 ? void 0 : res.locals
72
150
  });
73
151
  const ik = this.getIk(res);
74
152
  const actionKey = req.path.substring(req.path.lastIndexOf('/') + 1);
75
- if (!this.lib.actions) {
153
+ if (!libOptions.actions) {
76
154
  this.logger.error('Item Actions are not configured');
77
155
  res.status(500).json({
78
156
  error: 'Item Actions are not configured'
79
157
  });
80
158
  return;
81
159
  }
82
- const action = this.lib.actions[actionKey];
160
+ const action = libOptions.actions[actionKey];
83
161
  if (!action) {
84
162
  this.logger.error('Item Action is not configured', {
85
163
  actionKey
@@ -90,7 +168,7 @@ class ItemRouter {
90
168
  return;
91
169
  }
92
170
  try {
93
- res.json(await this.lib.action(ik, actionKey, req.body));
171
+ res.json(await libOperations.action(ik, actionKey, req.body));
94
172
  } catch (err) {
95
173
  this.logger.error('Error in Item Action', {
96
174
  message: err === null || err === void 0 ? void 0 : err.message,
@@ -100,21 +178,23 @@ class ItemRouter {
100
178
  }
101
179
  });
102
180
  _define_property$2(this, "getItemFacet", async (req, res)=>{
103
- this.logger.default('Getting Item', {
181
+ const libOptions = this.lib.definition.options;
182
+ const libOperations = this.lib.operations;
183
+ this.logger.debug('Getting Item', {
104
184
  query: req === null || req === void 0 ? void 0 : req.query,
105
185
  params: req === null || req === void 0 ? void 0 : req.params,
106
186
  locals: res === null || res === void 0 ? void 0 : res.locals
107
187
  });
108
188
  const ik = this.getIk(res);
109
189
  const facetKey = req.path.substring(req.path.lastIndexOf('/') + 1);
110
- if (!this.lib.facets) {
190
+ if (!libOptions.facets) {
111
191
  this.logger.error('Item Facets are not configured');
112
192
  res.status(500).json({
113
193
  error: 'Item Facets are not configured'
114
194
  });
115
195
  return;
116
196
  }
117
- const facet = this.lib.facets[facetKey];
197
+ const facet = libOptions.facets[facetKey];
118
198
  if (!facet) {
119
199
  this.logger.error('Item Facet is not configured', {
120
200
  facetKey
@@ -125,7 +205,11 @@ class ItemRouter {
125
205
  return;
126
206
  }
127
207
  try {
128
- res.json(await this.lib.facet(ik, facetKey, req.params));
208
+ const combinedQueryParams = {
209
+ ...req.query,
210
+ ...req.params
211
+ };
212
+ res.json(await libOperations.facet(ik, facetKey, combinedQueryParams));
129
213
  } catch (err) {
130
214
  this.logger.error('Error in Item Facet', {
131
215
  message: err === null || err === void 0 ? void 0 : err.message,
@@ -135,51 +219,57 @@ class ItemRouter {
135
219
  }
136
220
  });
137
221
  _define_property$2(this, "configure", (router)=>{
138
- this.logger.default('Configuring Router', {
222
+ const libOptions = this.lib.definition.options;
223
+ this.logger.debug('Configuring Router', {
139
224
  pkType: this.getPkType()
140
225
  });
141
226
  router.get('/', this.findItems);
142
227
  router.post('/', this.createItem);
143
- // const allActions = this.configureAllActions();
144
- // this.logger.debug('All Actions supplied to Router', { allActions });
145
- // if (allActions) {
146
- // Object.keys(allActions).forEach((actionKey) => {
147
- // this.logger.default('Configuring All Action', { actionKey });
148
- // // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
149
- // router.post(`/${actionKey}`, ...allActions[actionKey]);
150
- // });
151
- // }
228
+ this.logger.default('All Actions supplied to Router', {
229
+ allActions: libOptions.allActions
230
+ });
231
+ if (libOptions.allActions) {
232
+ Object.keys(libOptions.allActions).forEach((actionKey)=>{
233
+ this.logger.debug('Configuring All Action %s', actionKey);
234
+ // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
235
+ router.post(`/${actionKey}`, this.postAllAction);
236
+ });
237
+ }
238
+ this.logger.default('All Facets supplied to Router', {
239
+ allFacets: libOptions.allFacets
240
+ });
241
+ if (libOptions.allFacets) {
242
+ Object.keys(libOptions.allFacets).forEach((facetKey)=>{
243
+ this.logger.debug('Configuring All Facet %s', facetKey);
244
+ // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
245
+ router.get(`/${facetKey}`, this.getAllFacet);
246
+ });
247
+ }
152
248
  const itemRouter = express.Router();
153
249
  itemRouter.get('/', this.getItem);
154
250
  itemRouter.put('/', this.updateItem);
155
251
  itemRouter.delete('/', this.deleteItem);
156
- this.logger.debug('Item Actions supplied to Router', {
157
- itemActions: this.lib.actions
252
+ this.logger.default('Item Actions supplied to Router', {
253
+ itemActions: libOptions.actions
158
254
  });
159
- if (this.lib.actions) {
160
- Object.keys(this.lib.actions).forEach((actionKey)=>{
161
- this.logger.default('Configuring Item Action', {
162
- actionKey
163
- });
255
+ if (libOptions.actions) {
256
+ Object.keys(libOptions.actions).forEach((actionKey)=>{
257
+ this.logger.debug('Configuring Item Action %s', actionKey);
164
258
  // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
165
259
  itemRouter.post(`/${actionKey}`, this.postItemAction);
166
260
  });
167
261
  }
168
- this.logger.debug('Item Facets supplied to Router', {
169
- itemFacets: this.lib.facets
262
+ this.logger.default('Item Facets supplied to Router', {
263
+ itemFacets: libOptions.facets
170
264
  });
171
- if (this.lib.facets) {
172
- Object.keys(this.lib.facets).forEach((facetKey)=>{
173
- this.logger.default('Configuring Item Facet', {
174
- facetKey
175
- });
265
+ if (libOptions.facets) {
266
+ Object.keys(libOptions.facets).forEach((facetKey)=>{
267
+ this.logger.debug('Configuring Item Facet %s', facetKey);
176
268
  // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
177
269
  itemRouter.get(`/${facetKey}`, this.getItemFacet);
178
270
  });
179
271
  }
180
- this.logger.default('Configuring Item Operations under PK Param', {
181
- pkParam: this.getPkParam()
182
- });
272
+ this.logger.debug('Configuring Item Operations under PK Param %s', this.getPkParam());
183
273
  router.use(`/:${this.getPkParam()}`, this.validatePrimaryKeyValue, itemRouter);
184
274
  if (this.childRouters) {
185
275
  this.configureChildRouters(itemRouter, this.childRouters);
@@ -194,19 +284,17 @@ class ItemRouter {
194
284
  } else {
195
285
  this.logger.error('Invalid Primary Key', {
196
286
  pkParamValue,
197
- path: req === null || req === void 0 ? void 0 : req.path
287
+ path: req === null || req === void 0 ? void 0 : req.originalUrl
198
288
  });
199
289
  res.status(500).json({
200
290
  error: 'Invalid Primary Key',
201
- path: req === null || req === void 0 ? void 0 : req.path
291
+ path: req === null || req === void 0 ? void 0 : req.originalUrl
202
292
  });
203
293
  }
204
294
  });
205
295
  _define_property$2(this, "configureChildRouters", (router, childRouters)=>{
206
296
  for(const path in childRouters){
207
- this.logger.default('Configuring Child Router at Path', {
208
- path
209
- });
297
+ this.logger.debug('Configuring Child Router at Path %s', path);
210
298
  router.use(`/${path}`, childRouters[path]);
211
299
  }
212
300
  return router;
@@ -220,19 +308,20 @@ class ItemRouter {
220
308
  });
221
309
  // TODO: Probably a better way to do this, but this postCreate hook only needs the item.
222
310
  /* istanbul ignore next */ _define_property$2(this, "postCreateItem", async (item)=>{
223
- this.logger.default('Post Create Item', {
311
+ this.logger.debug('Post Create Item', {
224
312
  item
225
313
  });
226
314
  return item;
227
315
  });
228
316
  _define_property$2(this, "deleteItem", async (req, res)=>{
229
- this.logger.default('Deleting Item', {
317
+ const libOperations = this.lib.operations;
318
+ this.logger.debug('Deleting Item', {
230
319
  query: req.query,
231
320
  params: req.params,
232
321
  locals: res.locals
233
322
  });
234
323
  const ik = this.getIk(res);
235
- const removedItem = await this.lib.remove(ik);
324
+ const removedItem = await libOperations.remove(ik);
236
325
  const item = core.validatePK(removedItem, this.getPkType());
237
326
  res.json(item);
238
327
  });
@@ -240,7 +329,8 @@ class ItemRouter {
240
329
  throw new Error('Method not implemented in an abstract router');
241
330
  });
242
331
  /* eslint-enable */ _define_property$2(this, "getItem", async (req, res)=>{
243
- this.logger.default('Getting Item', {
332
+ const libOperations = this.lib.operations;
333
+ this.logger.debug('Getting Item', {
244
334
  query: req.query,
245
335
  params: req.params,
246
336
  locals: res.locals
@@ -248,7 +338,7 @@ class ItemRouter {
248
338
  const ik = this.getIk(res);
249
339
  try {
250
340
  // TODO: What error does validate PK throw, when can that fail?
251
- const item = core.validatePK(await this.lib.get(ik), this.getPkType());
341
+ const item = core.validatePK(await libOperations.get(ik), this.getPkType());
252
342
  res.json(item);
253
343
  } catch (err) {
254
344
  if (err instanceof lib.NotFoundError) {
@@ -275,7 +365,8 @@ class ItemRouter {
275
365
  }
276
366
  });
277
367
  _define_property$2(this, "updateItem", async (req, res)=>{
278
- this.logger.default('Updating Item', {
368
+ const libOperations = this.lib.operations;
369
+ this.logger.debug('Updating Item', {
279
370
  body: req === null || req === void 0 ? void 0 : req.body,
280
371
  query: req === null || req === void 0 ? void 0 : req.query,
281
372
  params: req === null || req === void 0 ? void 0 : req.params,
@@ -283,12 +374,12 @@ class ItemRouter {
283
374
  });
284
375
  const ik = this.getIk(res);
285
376
  const itemToUpdate = this.convertDates(req.body);
286
- const retItem = core.validatePK(await this.lib.update(ik, itemToUpdate), this.getPkType());
377
+ const retItem = core.validatePK(await libOperations.update(ik, itemToUpdate), this.getPkType());
287
378
  res.json(retItem);
288
379
  });
289
380
  _define_property$2(this, "convertDates", (item)=>{
290
381
  const events = item.events;
291
- this.logger.default('Converting Dates', {
382
+ this.logger.debug('Converting Dates', {
292
383
  item
293
384
  });
294
385
  if (events) {
@@ -347,7 +438,6 @@ function _define_property$1(obj, key, value) {
347
438
  }
348
439
  return obj;
349
440
  }
350
- const logger$1 = LibLogger.get('CItemRouter');
351
441
  class CItemRouter extends ItemRouter {
352
442
  hasParent() {
353
443
  return !!this.parentRoute;
@@ -377,24 +467,22 @@ class CItemRouter extends ItemRouter {
377
467
  }
378
468
  constructor(lib, type, parentRoute, options = {}){
379
469
  super(lib, type, options), _define_property$1(this, "parentRoute", void 0), _define_property$1(this, "createItem", async (req, res)=>{
380
- logger$1.trace('Creating Item 2', {
470
+ const libOperations = this.lib.operations;
471
+ this.logger.default('Creating Item', {
381
472
  body: req === null || req === void 0 ? void 0 : req.body,
382
473
  query: req === null || req === void 0 ? void 0 : req.query,
383
474
  params: req === null || req === void 0 ? void 0 : req.params,
384
475
  locals: res === null || res === void 0 ? void 0 : res.locals
385
476
  });
386
477
  const itemToCreate = this.convertDates(req.body);
387
- let item = core.validatePK(await this.lib.create(itemToCreate, {
478
+ let item = core.validatePK(await libOperations.create(itemToCreate, {
388
479
  locations: this.getLocations(res)
389
480
  }), this.getPkType());
390
481
  item = await this.postCreateItem(item);
482
+ this.logger.default('Created Item %j', item);
391
483
  res.json(item);
392
484
  }), _define_property$1(this, "findItems", async (req, res)=>{
393
- logger$1.trace('Finding Items', {
394
- query: req.query,
395
- params: req.params,
396
- locals: res.locals
397
- });
485
+ const libOperations = this.lib.operations;
398
486
  const query = req.query;
399
487
  const finder = query['finder'];
400
488
  const finderParams = query['finderParams'];
@@ -402,7 +490,7 @@ class CItemRouter extends ItemRouter {
402
490
  let items = [];
403
491
  if (finder) {
404
492
  // If finder is defined? Call a finder.
405
- logger$1.trace('Finding Items with a finder', {
493
+ this.logger.default('Finding Items with Finder', {
406
494
  finder,
407
495
  finderParams,
408
496
  one
@@ -413,15 +501,14 @@ class CItemRouter extends ItemRouter {
413
501
  item
414
502
  ] : [];
415
503
  } else {
416
- items = await this.lib.find(finder, JSON.parse(finderParams), this.getLocations(res));
504
+ items = await libOperations.find(finder, JSON.parse(finderParams), this.getLocations(res));
417
505
  }
418
506
  } else {
419
- logger$1.trace('Finding Items with a query', {
420
- query: req.query
421
- });
422
507
  // TODO: This is once of the more important places to perform some validaation and feedback
423
508
  const itemQuery = core.paramsToQuery(req.query);
424
- items = await this.lib.all(itemQuery, this.getLocations(res));
509
+ this.logger.default('Finding Items with Query: %j', itemQuery);
510
+ items = await libOperations.all(itemQuery, this.getLocations(res));
511
+ this.logger.default('Found %d Items with Query', items.length);
425
512
  }
426
513
  res.json(items.map((item)=>core.validatePK(item, this.getPkType())));
427
514
  });
@@ -442,7 +529,6 @@ function _define_property(obj, key, value) {
442
529
  }
443
530
  return obj;
444
531
  }
445
- const logger = LibLogger.get('PItemRouter');
446
532
  class PItemRouter extends ItemRouter {
447
533
  getIk(res) {
448
534
  const pri = this.getPk(res);
@@ -450,18 +536,21 @@ class PItemRouter extends ItemRouter {
450
536
  }
451
537
  constructor(lib, keyType, options = {}){
452
538
  super(lib, keyType, options), _define_property(this, "createItem", async (req, res)=>{
453
- logger.default('Creating Item 2', {
539
+ const libOperations = this.lib.operations;
540
+ this.logger.default('Creating Item', {
454
541
  body: req.body,
455
542
  query: req.query,
456
543
  params: req.params,
457
544
  locals: res.locals
458
545
  });
459
546
  const itemToCreate = this.convertDates(req.body);
460
- let item = core.validatePK(await this.lib.create(itemToCreate), this.getPkType());
547
+ let item = core.validatePK(await libOperations.create(itemToCreate), this.getPkType());
461
548
  item = await this.postCreateItem(item);
549
+ this.logger.default('Created Item %j', item);
462
550
  res.json(item);
463
551
  }), _define_property(this, "findItems", async (req, res)=>{
464
- logger.default('Finding Items', {
552
+ const libOperations = this.lib.operations;
553
+ this.logger.default('Finding Items', {
465
554
  query: req.query,
466
555
  params: req.params,
467
556
  locals: res.locals
@@ -473,26 +562,20 @@ class PItemRouter extends ItemRouter {
473
562
  const one = query['one'];
474
563
  if (finder) {
475
564
  // If finder is defined? Call a finder.
476
- logger.default('Finding Items with a finder', {
477
- finder,
478
- finderParams,
479
- one
480
- });
565
+ this.logger.default('Finding Items with Finder %s %j one:%s', finder, finderParams, one);
481
566
  if (one === 'true') {
482
567
  const item = await this.lib.findOne(finder, JSON.parse(finderParams));
483
568
  items = item ? [
484
569
  item
485
570
  ] : [];
486
571
  } else {
487
- items = await this.lib.find(finder, JSON.parse(finderParams));
572
+ items = await libOperations.find(finder, JSON.parse(finderParams));
488
573
  }
489
574
  } else {
490
- logger.default('Finding Items with a query', {
491
- query: req.query
492
- });
493
575
  // TODO: This is once of the more important places to perform some validaation and feedback
494
576
  const itemQuery = core.paramsToQuery(req.query);
495
- items = await this.lib.all(itemQuery);
577
+ this.logger.default('Finding Items with a query %j', itemQuery);
578
+ items = await libOperations.all(itemQuery);
496
579
  }
497
580
  res.json(items.map((item)=>core.validatePK(item, this.getPkType())));
498
581
  });