@fjell/express-router 4.4.14 → 4.4.19
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/README.md +2 -2
- package/build.js +4 -0
- package/dist/CItemRouter.d.ts +5 -4
- package/dist/CItemRouter.d.ts.map +1 -0
- package/dist/CItemRouter.js +63 -92
- package/dist/CItemRouter.js.map +7 -1
- package/dist/Instance.d.ts +5 -4
- package/dist/Instance.d.ts.map +1 -0
- package/dist/Instance.js +12 -22
- package/dist/Instance.js.map +7 -1
- package/dist/InstanceFactory.d.ts +5 -4
- package/dist/InstanceFactory.d.ts.map +1 -0
- package/dist/InstanceFactory.js +10 -18
- package/dist/InstanceFactory.js.map +7 -1
- package/dist/ItemRouter.d.ts +5 -4
- package/dist/ItemRouter.d.ts.map +1 -0
- package/dist/ItemRouter.js +290 -405
- package/dist/ItemRouter.js.map +7 -1
- package/dist/PItemRouter.d.ts +5 -4
- package/dist/PItemRouter.d.ts.map +1 -0
- package/dist/PItemRouter.js +43 -67
- package/dist/PItemRouter.js.map +7 -1
- package/dist/Registry.d.ts +1 -0
- package/dist/Registry.d.ts.map +1 -0
- package/dist/Registry.js +22 -27
- package/dist/Registry.js.map +7 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -6
- package/dist/index.js.map +7 -1
- package/dist/logger.d.ts +2 -1
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +6 -5
- package/dist/logger.js.map +7 -1
- package/dist/util/general.d.ts +1 -0
- package/dist/util/general.d.ts.map +1 -0
- package/dist/util/general.js +50 -0
- package/dist/util/general.js.map +7 -0
- package/docs/docs.config.ts +1 -32
- package/docs/package.json +4 -4
- package/docs/public/README.md +2 -2
- package/docs/tsconfig.node.json +2 -11
- package/package.json +25 -28
- package/vitest.config.ts +45 -0
- package/dist/CItemRouter.cjs +0 -100
- package/dist/CItemRouter.cjs.map +0 -1
- package/dist/Instance.cjs +0 -31
- package/dist/Instance.cjs.map +0 -1
- package/dist/InstanceFactory.cjs +0 -25
- package/dist/InstanceFactory.cjs.map +0 -1
- package/dist/ItemRouter.cjs +0 -427
- package/dist/ItemRouter.cjs.map +0 -1
- package/dist/PItemRouter.cjs +0 -75
- package/dist/PItemRouter.cjs.map +0 -1
- package/dist/Registry.cjs +0 -36
- package/dist/Registry.cjs.map +0 -1
- package/dist/index.cjs +0 -657
- package/dist/index.cjs.map +0 -1
- package/dist/logger.cjs +0 -10
- package/dist/logger.cjs.map +0 -1
- package/docs/package-lock.json +0 -5129
- package/docs/src/index.css +0 -21
- package/docs/src/main.tsx +0 -12
- package/docs/src/test/setup.ts +0 -1
- package/docs/src/types.d.ts +0 -4
package/dist/index.cjs
DELETED
|
@@ -1,657 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
|
|
5
|
-
const core = require('@fjell/core');
|
|
6
|
-
const lib = require('@fjell/lib');
|
|
7
|
-
const deepmerge = require('deepmerge');
|
|
8
|
-
const express = require('express');
|
|
9
|
-
const Logging = require('@fjell/logging');
|
|
10
|
-
const registry = require('@fjell/registry');
|
|
11
|
-
|
|
12
|
-
const LibLogger = Logging.getLogger('@fjell/express-router');
|
|
13
|
-
|
|
14
|
-
function _define_property$2(obj, key, value) {
|
|
15
|
-
if (key in obj) {
|
|
16
|
-
Object.defineProperty(obj, key, {
|
|
17
|
-
value: value,
|
|
18
|
-
enumerable: true,
|
|
19
|
-
configurable: true,
|
|
20
|
-
writable: true
|
|
21
|
-
});
|
|
22
|
-
} else {
|
|
23
|
-
obj[key] = value;
|
|
24
|
-
}
|
|
25
|
-
return obj;
|
|
26
|
-
}
|
|
27
|
-
class ItemRouter {
|
|
28
|
-
getLk(res) {
|
|
29
|
-
return {
|
|
30
|
-
kt: this.keyType,
|
|
31
|
-
lk: res.locals[this.getPkParam()]
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
// this is meant to be consumed by children routers
|
|
35
|
-
getLKA(res) {
|
|
36
|
-
return [
|
|
37
|
-
this.getLk(res)
|
|
38
|
-
];
|
|
39
|
-
}
|
|
40
|
-
getPk(res) {
|
|
41
|
-
return core.cPK(res.locals[this.getPkParam()], this.getPkType());
|
|
42
|
-
}
|
|
43
|
-
// Unless this is a contained router, the locations will always be an empty array.
|
|
44
|
-
/* eslint-disable */ getLocations(res) {
|
|
45
|
-
throw new Error('Method not implemented in an abstract router');
|
|
46
|
-
}
|
|
47
|
-
/* eslint-enable */ // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
48
|
-
getIk(res) {
|
|
49
|
-
throw new Error('Method not implemented in an abstract router');
|
|
50
|
-
}
|
|
51
|
-
/* istanbul ignore next */ getRouter() {
|
|
52
|
-
const router = express.Router();
|
|
53
|
-
this.configure(router);
|
|
54
|
-
return router;
|
|
55
|
-
}
|
|
56
|
-
constructor(lib$1, keyType, options = {}){
|
|
57
|
-
_define_property$2(this, "lib", void 0);
|
|
58
|
-
_define_property$2(this, "keyType", void 0);
|
|
59
|
-
_define_property$2(this, "options", void 0);
|
|
60
|
-
_define_property$2(this, "childRouters", {});
|
|
61
|
-
_define_property$2(this, "logger", void 0);
|
|
62
|
-
_define_property$2(this, "getPkType", ()=>{
|
|
63
|
-
return this.keyType;
|
|
64
|
-
});
|
|
65
|
-
_define_property$2(this, "getPkParam", ()=>{
|
|
66
|
-
return `${this.getPkType()}Pk`;
|
|
67
|
-
});
|
|
68
|
-
_define_property$2(this, "postAllAction", async (req, res)=>{
|
|
69
|
-
const libOptions = this.lib.options;
|
|
70
|
-
const libOperations = this.lib.operations;
|
|
71
|
-
this.logger.debug('Posting All Action', {
|
|
72
|
-
query: req === null || req === void 0 ? void 0 : req.query,
|
|
73
|
-
params: req === null || req === void 0 ? void 0 : req.params,
|
|
74
|
-
locals: res === null || res === void 0 ? void 0 : res.locals
|
|
75
|
-
});
|
|
76
|
-
const allActionKey = req.path.substring(req.path.lastIndexOf('/') + 1);
|
|
77
|
-
if (!libOptions.allActions) {
|
|
78
|
-
this.logger.error('Item Actions are not configured');
|
|
79
|
-
res.status(500).json({
|
|
80
|
-
error: 'Item Actions are not configured'
|
|
81
|
-
});
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
const allAction = libOptions.allActions[allActionKey];
|
|
85
|
-
if (!allAction) {
|
|
86
|
-
this.logger.error('All Action is not configured', {
|
|
87
|
-
allActionKey
|
|
88
|
-
});
|
|
89
|
-
res.status(500).json({
|
|
90
|
-
error: 'Item Action is not configured'
|
|
91
|
-
});
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
try {
|
|
95
|
-
res.json(await libOperations.allAction(allActionKey, req.body));
|
|
96
|
-
} catch (err) {
|
|
97
|
-
this.logger.error('Error in All Action', {
|
|
98
|
-
message: err === null || err === void 0 ? void 0 : err.message,
|
|
99
|
-
stack: err === null || err === void 0 ? void 0 : err.stack
|
|
100
|
-
});
|
|
101
|
-
res.status(500).json(err);
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
_define_property$2(this, "getAllFacet", async (req, res)=>{
|
|
105
|
-
const libOptions = this.lib.options;
|
|
106
|
-
const libOperations = this.lib.operations;
|
|
107
|
-
this.logger.debug('Getting All Facet', {
|
|
108
|
-
query: req === null || req === void 0 ? void 0 : req.query,
|
|
109
|
-
params: req === null || req === void 0 ? void 0 : req.params,
|
|
110
|
-
locals: res === null || res === void 0 ? void 0 : res.locals
|
|
111
|
-
});
|
|
112
|
-
const facetKey = req.path.substring(req.path.lastIndexOf('/') + 1);
|
|
113
|
-
if (!libOptions.allFacets) {
|
|
114
|
-
this.logger.error('Item Facets are not configured');
|
|
115
|
-
res.status(500).json({
|
|
116
|
-
error: 'Item Facets are not configured'
|
|
117
|
-
});
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
const facet = libOptions.allFacets[facetKey];
|
|
121
|
-
if (!facet) {
|
|
122
|
-
this.logger.error('Item Facet is not configured', {
|
|
123
|
-
facetKey
|
|
124
|
-
});
|
|
125
|
-
res.status(500).json({
|
|
126
|
-
error: 'Item Facet is not configured'
|
|
127
|
-
});
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
try {
|
|
131
|
-
const combinedQueryParams = {
|
|
132
|
-
...req.query,
|
|
133
|
-
...req.params
|
|
134
|
-
};
|
|
135
|
-
res.json(await libOperations.allFacet(facetKey, combinedQueryParams));
|
|
136
|
-
} catch (err) {
|
|
137
|
-
this.logger.error('Error in All Facet', {
|
|
138
|
-
message: err === null || err === void 0 ? void 0 : err.message,
|
|
139
|
-
stack: err === null || err === void 0 ? void 0 : err.stack
|
|
140
|
-
});
|
|
141
|
-
res.status(500).json(err);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
_define_property$2(this, "postItemAction", async (req, res)=>{
|
|
145
|
-
const libOptions = this.lib.options;
|
|
146
|
-
const libOperations = this.lib.operations;
|
|
147
|
-
this.logger.debug('Getting Item', {
|
|
148
|
-
query: req === null || req === void 0 ? void 0 : req.query,
|
|
149
|
-
params: req === null || req === void 0 ? void 0 : req.params,
|
|
150
|
-
locals: res === null || res === void 0 ? void 0 : res.locals
|
|
151
|
-
});
|
|
152
|
-
const ik = this.getIk(res);
|
|
153
|
-
const actionKey = req.path.substring(req.path.lastIndexOf('/') + 1);
|
|
154
|
-
if (!libOptions.actions) {
|
|
155
|
-
this.logger.error('Item Actions are not configured');
|
|
156
|
-
res.status(500).json({
|
|
157
|
-
error: 'Item Actions are not configured'
|
|
158
|
-
});
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
const action = libOptions.actions[actionKey];
|
|
162
|
-
if (!action) {
|
|
163
|
-
this.logger.error('Item Action is not configured', {
|
|
164
|
-
actionKey
|
|
165
|
-
});
|
|
166
|
-
res.status(500).json({
|
|
167
|
-
error: 'Item Action is not configured'
|
|
168
|
-
});
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
try {
|
|
172
|
-
res.json(await libOperations.action(ik, actionKey, req.body));
|
|
173
|
-
} catch (err) {
|
|
174
|
-
this.logger.error('Error in Item Action', {
|
|
175
|
-
message: err === null || err === void 0 ? void 0 : err.message,
|
|
176
|
-
stack: err === null || err === void 0 ? void 0 : err.stack
|
|
177
|
-
});
|
|
178
|
-
res.status(500).json(err);
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
_define_property$2(this, "getItemFacet", async (req, res)=>{
|
|
182
|
-
const libOptions = this.lib.options;
|
|
183
|
-
const libOperations = this.lib.operations;
|
|
184
|
-
this.logger.debug('Getting Item', {
|
|
185
|
-
query: req === null || req === void 0 ? void 0 : req.query,
|
|
186
|
-
params: req === null || req === void 0 ? void 0 : req.params,
|
|
187
|
-
locals: res === null || res === void 0 ? void 0 : res.locals
|
|
188
|
-
});
|
|
189
|
-
const ik = this.getIk(res);
|
|
190
|
-
const facetKey = req.path.substring(req.path.lastIndexOf('/') + 1);
|
|
191
|
-
if (!libOptions.facets) {
|
|
192
|
-
this.logger.error('Item Facets are not configured');
|
|
193
|
-
res.status(500).json({
|
|
194
|
-
error: 'Item Facets are not configured'
|
|
195
|
-
});
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
const facet = libOptions.facets[facetKey];
|
|
199
|
-
if (!facet) {
|
|
200
|
-
this.logger.error('Item Facet is not configured', {
|
|
201
|
-
facetKey
|
|
202
|
-
});
|
|
203
|
-
res.status(500).json({
|
|
204
|
-
error: 'Item Facet is not configured'
|
|
205
|
-
});
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
try {
|
|
209
|
-
const combinedQueryParams = {
|
|
210
|
-
...req.query,
|
|
211
|
-
...req.params
|
|
212
|
-
};
|
|
213
|
-
res.json(await libOperations.facet(ik, facetKey, combinedQueryParams));
|
|
214
|
-
} catch (err) {
|
|
215
|
-
this.logger.error('Error in Item Facet', {
|
|
216
|
-
message: err === null || err === void 0 ? void 0 : err.message,
|
|
217
|
-
stack: err === null || err === void 0 ? void 0 : err.stack
|
|
218
|
-
});
|
|
219
|
-
res.status(500).json(err);
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
_define_property$2(this, "configure", (router)=>{
|
|
223
|
-
const libOptions = this.lib.options;
|
|
224
|
-
this.logger.debug('Configuring Router', {
|
|
225
|
-
pkType: this.getPkType()
|
|
226
|
-
});
|
|
227
|
-
router.get('/', this.findItems);
|
|
228
|
-
router.post('/', this.createItem);
|
|
229
|
-
this.logger.default('All Actions supplied to Router', {
|
|
230
|
-
allActions: libOptions.allActions
|
|
231
|
-
});
|
|
232
|
-
if (libOptions.allActions) {
|
|
233
|
-
Object.keys(libOptions.allActions).forEach((actionKey)=>{
|
|
234
|
-
this.logger.debug('Configuring All Action %s', actionKey);
|
|
235
|
-
// TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
|
|
236
|
-
router.post(`/${actionKey}`, this.postAllAction);
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
this.logger.default('All Facets supplied to Router', {
|
|
240
|
-
allFacets: libOptions.allFacets
|
|
241
|
-
});
|
|
242
|
-
if (libOptions.allFacets) {
|
|
243
|
-
Object.keys(libOptions.allFacets).forEach((facetKey)=>{
|
|
244
|
-
this.logger.debug('Configuring All Facet %s', facetKey);
|
|
245
|
-
// TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
|
|
246
|
-
router.get(`/${facetKey}`, this.getAllFacet);
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
const itemRouter = express.Router();
|
|
250
|
-
itemRouter.get('/', this.getItem);
|
|
251
|
-
itemRouter.put('/', this.updateItem);
|
|
252
|
-
itemRouter.delete('/', this.deleteItem);
|
|
253
|
-
this.logger.default('Item Actions supplied to Router', {
|
|
254
|
-
itemActions: libOptions.actions
|
|
255
|
-
});
|
|
256
|
-
if (libOptions.actions) {
|
|
257
|
-
Object.keys(libOptions.actions).forEach((actionKey)=>{
|
|
258
|
-
this.logger.debug('Configuring Item Action %s', actionKey);
|
|
259
|
-
// TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
|
|
260
|
-
itemRouter.post(`/${actionKey}`, this.postItemAction);
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
this.logger.default('Item Facets supplied to Router', {
|
|
264
|
-
itemFacets: libOptions.facets
|
|
265
|
-
});
|
|
266
|
-
if (libOptions.facets) {
|
|
267
|
-
Object.keys(libOptions.facets).forEach((facetKey)=>{
|
|
268
|
-
this.logger.debug('Configuring Item Facet %s', facetKey);
|
|
269
|
-
// TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
|
|
270
|
-
itemRouter.get(`/${facetKey}`, this.getItemFacet);
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
this.logger.debug('Configuring Item Operations under PK Param %s', this.getPkParam());
|
|
274
|
-
router.use(`/:${this.getPkParam()}`, this.validatePrimaryKeyValue, itemRouter);
|
|
275
|
-
if (this.childRouters) {
|
|
276
|
-
this.configureChildRouters(itemRouter, this.childRouters);
|
|
277
|
-
}
|
|
278
|
-
return router;
|
|
279
|
-
});
|
|
280
|
-
_define_property$2(this, "validatePrimaryKeyValue", (req, res, next)=>{
|
|
281
|
-
const pkParamValue = req.params[this.getPkParam()];
|
|
282
|
-
if (this.validatePKParam(pkParamValue)) {
|
|
283
|
-
res.locals[this.getPkParam()] = pkParamValue;
|
|
284
|
-
next();
|
|
285
|
-
} else {
|
|
286
|
-
this.logger.error('Invalid Primary Key', {
|
|
287
|
-
pkParamValue,
|
|
288
|
-
path: req === null || req === void 0 ? void 0 : req.originalUrl
|
|
289
|
-
});
|
|
290
|
-
res.status(500).json({
|
|
291
|
-
error: 'Invalid Primary Key',
|
|
292
|
-
path: req === null || req === void 0 ? void 0 : req.originalUrl
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
});
|
|
296
|
-
_define_property$2(this, "configureChildRouters", (router, childRouters)=>{
|
|
297
|
-
for(const path in childRouters){
|
|
298
|
-
this.logger.debug('Configuring Child Router at Path %s', path);
|
|
299
|
-
router.use(`/${path}`, childRouters[path]);
|
|
300
|
-
}
|
|
301
|
-
return router;
|
|
302
|
-
});
|
|
303
|
-
_define_property$2(this, "addChildRouter", (path, router)=>{
|
|
304
|
-
this.childRouters[path] = router;
|
|
305
|
-
});
|
|
306
|
-
/* istanbul ignore next */ // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
307
|
-
_define_property$2(this, "createItem", async (req, res)=>{
|
|
308
|
-
throw new Error('Method not implemented in an abstract router');
|
|
309
|
-
});
|
|
310
|
-
// TODO: Probably a better way to do this, but this postCreate hook only needs the item.
|
|
311
|
-
/* istanbul ignore next */ _define_property$2(this, "postCreateItem", async (item)=>{
|
|
312
|
-
this.logger.debug('Post Create Item', {
|
|
313
|
-
item
|
|
314
|
-
});
|
|
315
|
-
return item;
|
|
316
|
-
});
|
|
317
|
-
_define_property$2(this, "deleteItem", async (req, res)=>{
|
|
318
|
-
const libOperations = this.lib.operations;
|
|
319
|
-
this.logger.debug('Deleting Item', {
|
|
320
|
-
query: req.query,
|
|
321
|
-
params: req.params,
|
|
322
|
-
locals: res.locals
|
|
323
|
-
});
|
|
324
|
-
const ik = this.getIk(res);
|
|
325
|
-
const removedItem = await libOperations.remove(ik);
|
|
326
|
-
const item = core.validatePK(removedItem, this.getPkType());
|
|
327
|
-
res.json(item);
|
|
328
|
-
});
|
|
329
|
-
/* eslint-disable */ /* istanbul ignore next */ _define_property$2(this, "findItems", async (req, res)=>{
|
|
330
|
-
throw new Error('Method not implemented in an abstract router');
|
|
331
|
-
});
|
|
332
|
-
/* eslint-enable */ _define_property$2(this, "getItem", async (req, res)=>{
|
|
333
|
-
const libOperations = this.lib.operations;
|
|
334
|
-
this.logger.debug('Getting Item', {
|
|
335
|
-
query: req.query,
|
|
336
|
-
params: req.params,
|
|
337
|
-
locals: res.locals
|
|
338
|
-
});
|
|
339
|
-
const ik = this.getIk(res);
|
|
340
|
-
try {
|
|
341
|
-
// TODO: What error does validate PK throw, when can that fail?
|
|
342
|
-
const item = core.validatePK(await libOperations.get(ik), this.getPkType());
|
|
343
|
-
res.json(item);
|
|
344
|
-
} catch (err) {
|
|
345
|
-
if (err instanceof lib.NotFoundError) {
|
|
346
|
-
this.logger.error('Item Not Found', {
|
|
347
|
-
ik,
|
|
348
|
-
message: err === null || err === void 0 ? void 0 : err.message,
|
|
349
|
-
stack: err === null || err === void 0 ? void 0 : err.stack
|
|
350
|
-
});
|
|
351
|
-
res.status(404).json({
|
|
352
|
-
ik,
|
|
353
|
-
message: "Item Not Found"
|
|
354
|
-
});
|
|
355
|
-
} else {
|
|
356
|
-
this.logger.error('General Error', {
|
|
357
|
-
ik,
|
|
358
|
-
message: err === null || err === void 0 ? void 0 : err.message,
|
|
359
|
-
stack: err === null || err === void 0 ? void 0 : err.stack
|
|
360
|
-
});
|
|
361
|
-
res.status(500).json({
|
|
362
|
-
ik,
|
|
363
|
-
message: "General Error"
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
});
|
|
368
|
-
_define_property$2(this, "updateItem", async (req, res)=>{
|
|
369
|
-
const libOperations = this.lib.operations;
|
|
370
|
-
this.logger.debug('Updating Item', {
|
|
371
|
-
body: req === null || req === void 0 ? void 0 : req.body,
|
|
372
|
-
query: req === null || req === void 0 ? void 0 : req.query,
|
|
373
|
-
params: req === null || req === void 0 ? void 0 : req.params,
|
|
374
|
-
locals: res === null || res === void 0 ? void 0 : res.locals
|
|
375
|
-
});
|
|
376
|
-
const ik = this.getIk(res);
|
|
377
|
-
const itemToUpdate = this.convertDates(req.body);
|
|
378
|
-
const retItem = core.validatePK(await libOperations.update(ik, itemToUpdate), this.getPkType());
|
|
379
|
-
res.json(retItem);
|
|
380
|
-
});
|
|
381
|
-
_define_property$2(this, "convertDates", (item)=>{
|
|
382
|
-
const events = item.events;
|
|
383
|
-
this.logger.debug('Converting Dates', {
|
|
384
|
-
item
|
|
385
|
-
});
|
|
386
|
-
if (events) {
|
|
387
|
-
Object.keys(events).forEach((key)=>{
|
|
388
|
-
Object.assign(events, {
|
|
389
|
-
[key]: deepmerge(events[key], {
|
|
390
|
-
at: events[key].at ? new Date(events[key].at) : null
|
|
391
|
-
})
|
|
392
|
-
});
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
Object.assign(item, {
|
|
396
|
-
events
|
|
397
|
-
});
|
|
398
|
-
return item;
|
|
399
|
-
});
|
|
400
|
-
// TODO: Maybe just simplify this and require that everything is a UUID?
|
|
401
|
-
/**
|
|
402
|
-
* This method might be an annoyance, but we need to capture a few cases where someone passes
|
|
403
|
-
* a PK parameter that has an odd string in it.
|
|
404
|
-
*
|
|
405
|
-
* @param pkParamValue The value of the primary key parameter
|
|
406
|
-
* @returns if the value is valid.
|
|
407
|
-
*/ _define_property$2(this, "validatePKParam", (pkParamValue)=>{
|
|
408
|
-
let validPkParam = true;
|
|
409
|
-
if (pkParamValue.length <= 0) {
|
|
410
|
-
this.logger.error('Primary Key is an Empty String', {
|
|
411
|
-
pkParamValue
|
|
412
|
-
});
|
|
413
|
-
validPkParam = false;
|
|
414
|
-
} else if (pkParamValue === 'undefined') {
|
|
415
|
-
this.logger.error('Primary Key is the string \'undefined\'', {
|
|
416
|
-
pkParamValue
|
|
417
|
-
});
|
|
418
|
-
validPkParam = false;
|
|
419
|
-
}
|
|
420
|
-
return validPkParam;
|
|
421
|
-
});
|
|
422
|
-
this.lib = lib$1;
|
|
423
|
-
this.keyType = keyType;
|
|
424
|
-
this.options = options;
|
|
425
|
-
this.logger = LibLogger.get("ItemRouter", keyType);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
function _define_property$1(obj, key, value) {
|
|
430
|
-
if (key in obj) {
|
|
431
|
-
Object.defineProperty(obj, key, {
|
|
432
|
-
value: value,
|
|
433
|
-
enumerable: true,
|
|
434
|
-
configurable: true,
|
|
435
|
-
writable: true
|
|
436
|
-
});
|
|
437
|
-
} else {
|
|
438
|
-
obj[key] = value;
|
|
439
|
-
}
|
|
440
|
-
return obj;
|
|
441
|
-
}
|
|
442
|
-
class CItemRouter extends ItemRouter {
|
|
443
|
-
hasParent() {
|
|
444
|
-
return !!this.parentRoute;
|
|
445
|
-
}
|
|
446
|
-
getIk(res) {
|
|
447
|
-
const pri = this.getPk(res);
|
|
448
|
-
const loc = this.getLocations(res);
|
|
449
|
-
return {
|
|
450
|
-
kt: pri.kt,
|
|
451
|
-
pk: pri.pk,
|
|
452
|
-
loc
|
|
453
|
-
};
|
|
454
|
-
}
|
|
455
|
-
getLKA(res) {
|
|
456
|
-
/**
|
|
457
|
-
* A location key array is passed to a child router to provide contextfor the items it will
|
|
458
|
-
* be working with. It is always a concatenation of "My LKA" + "Parent LKA" which will
|
|
459
|
-
* bubble all the way up to the root Primary.
|
|
460
|
-
*/ let lka = [
|
|
461
|
-
this.getLk(res)
|
|
462
|
-
];
|
|
463
|
-
lka = lka.concat(this.parentRoute.getLKA(res));
|
|
464
|
-
return lka;
|
|
465
|
-
}
|
|
466
|
-
getLocations(res) {
|
|
467
|
-
return this.parentRoute.getLKA(res);
|
|
468
|
-
}
|
|
469
|
-
constructor(lib, type, parentRoute, options = {}){
|
|
470
|
-
super(lib, type, options), _define_property$1(this, "parentRoute", void 0), _define_property$1(this, "createItem", async (req, res)=>{
|
|
471
|
-
const libOperations = this.lib.operations;
|
|
472
|
-
this.logger.default('Creating Item', {
|
|
473
|
-
body: req === null || req === void 0 ? void 0 : req.body,
|
|
474
|
-
query: req === null || req === void 0 ? void 0 : req.query,
|
|
475
|
-
params: req === null || req === void 0 ? void 0 : req.params,
|
|
476
|
-
locals: res === null || res === void 0 ? void 0 : res.locals
|
|
477
|
-
});
|
|
478
|
-
const itemToCreate = this.convertDates(req.body);
|
|
479
|
-
let item = core.validatePK(await libOperations.create(itemToCreate, {
|
|
480
|
-
locations: this.getLocations(res)
|
|
481
|
-
}), this.getPkType());
|
|
482
|
-
item = await this.postCreateItem(item);
|
|
483
|
-
this.logger.default('Created Item %j', item);
|
|
484
|
-
res.json(item);
|
|
485
|
-
}), _define_property$1(this, "findItems", async (req, res)=>{
|
|
486
|
-
const libOperations = this.lib.operations;
|
|
487
|
-
const query = req.query;
|
|
488
|
-
const finder = query['finder'];
|
|
489
|
-
const finderParams = query['finderParams'];
|
|
490
|
-
const one = query['one'];
|
|
491
|
-
let items = [];
|
|
492
|
-
if (finder) {
|
|
493
|
-
// If finder is defined? Call a finder.
|
|
494
|
-
this.logger.default('Finding Items with Finder', {
|
|
495
|
-
finder,
|
|
496
|
-
finderParams,
|
|
497
|
-
one
|
|
498
|
-
});
|
|
499
|
-
if (one === 'true') {
|
|
500
|
-
const item = await this.lib.findOne(finder, JSON.parse(finderParams), this.getLocations(res));
|
|
501
|
-
items = item ? [
|
|
502
|
-
item
|
|
503
|
-
] : [];
|
|
504
|
-
} else {
|
|
505
|
-
items = await libOperations.find(finder, JSON.parse(finderParams), this.getLocations(res));
|
|
506
|
-
}
|
|
507
|
-
} else {
|
|
508
|
-
// TODO: This is once of the more important places to perform some validaation and feedback
|
|
509
|
-
const itemQuery = core.paramsToQuery(req.query);
|
|
510
|
-
this.logger.default('Finding Items with Query: %j', itemQuery);
|
|
511
|
-
items = await libOperations.all(itemQuery, this.getLocations(res));
|
|
512
|
-
this.logger.default('Found %d Items with Query', items.length);
|
|
513
|
-
}
|
|
514
|
-
res.json(items.map((item)=>core.validatePK(item, this.getPkType())));
|
|
515
|
-
});
|
|
516
|
-
this.parentRoute = parentRoute;
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
function _define_property(obj, key, value) {
|
|
521
|
-
if (key in obj) {
|
|
522
|
-
Object.defineProperty(obj, key, {
|
|
523
|
-
value: value,
|
|
524
|
-
enumerable: true,
|
|
525
|
-
configurable: true,
|
|
526
|
-
writable: true
|
|
527
|
-
});
|
|
528
|
-
} else {
|
|
529
|
-
obj[key] = value;
|
|
530
|
-
}
|
|
531
|
-
return obj;
|
|
532
|
-
}
|
|
533
|
-
class PItemRouter extends ItemRouter {
|
|
534
|
-
getIk(res) {
|
|
535
|
-
const pri = this.getPk(res);
|
|
536
|
-
return pri;
|
|
537
|
-
}
|
|
538
|
-
constructor(lib, keyType, options = {}){
|
|
539
|
-
super(lib, keyType, options), _define_property(this, "createItem", async (req, res)=>{
|
|
540
|
-
const libOperations = this.lib.operations;
|
|
541
|
-
this.logger.default('Creating Item', {
|
|
542
|
-
body: req.body,
|
|
543
|
-
query: req.query,
|
|
544
|
-
params: req.params,
|
|
545
|
-
locals: res.locals
|
|
546
|
-
});
|
|
547
|
-
const itemToCreate = this.convertDates(req.body);
|
|
548
|
-
let item = core.validatePK(await libOperations.create(itemToCreate), this.getPkType());
|
|
549
|
-
item = await this.postCreateItem(item);
|
|
550
|
-
this.logger.default('Created Item %j', item);
|
|
551
|
-
res.json(item);
|
|
552
|
-
}), _define_property(this, "findItems", async (req, res)=>{
|
|
553
|
-
const libOperations = this.lib.operations;
|
|
554
|
-
this.logger.default('Finding Items', {
|
|
555
|
-
query: req.query,
|
|
556
|
-
params: req.params,
|
|
557
|
-
locals: res.locals
|
|
558
|
-
});
|
|
559
|
-
let items = [];
|
|
560
|
-
const query = req.query;
|
|
561
|
-
const finder = query['finder'];
|
|
562
|
-
const finderParams = query['finderParams'];
|
|
563
|
-
const one = query['one'];
|
|
564
|
-
if (finder) {
|
|
565
|
-
// If finder is defined? Call a finder.
|
|
566
|
-
this.logger.default('Finding Items with Finder %s %j one:%s', finder, finderParams, one);
|
|
567
|
-
if (one === 'true') {
|
|
568
|
-
const item = await this.lib.findOne(finder, JSON.parse(finderParams));
|
|
569
|
-
items = item ? [
|
|
570
|
-
item
|
|
571
|
-
] : [];
|
|
572
|
-
} else {
|
|
573
|
-
items = await libOperations.find(finder, JSON.parse(finderParams));
|
|
574
|
-
}
|
|
575
|
-
} else {
|
|
576
|
-
// TODO: This is once of the more important places to perform some validaation and feedback
|
|
577
|
-
const itemQuery = core.paramsToQuery(req.query);
|
|
578
|
-
this.logger.default('Finding Items with a query %j', itemQuery);
|
|
579
|
-
items = await libOperations.all(itemQuery);
|
|
580
|
-
}
|
|
581
|
-
res.json(items.map((item)=>core.validatePK(item, this.getPkType())));
|
|
582
|
-
});
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
const logger$2 = LibLogger.get("Instance");
|
|
587
|
-
const createInstance = (registry$1, coordinate, router, operations, options)=>{
|
|
588
|
-
logger$2.debug("createInstance", {
|
|
589
|
-
coordinate,
|
|
590
|
-
router,
|
|
591
|
-
registry: registry$1,
|
|
592
|
-
operations,
|
|
593
|
-
options
|
|
594
|
-
});
|
|
595
|
-
const baseInstance = registry.createInstance(registry$1, coordinate);
|
|
596
|
-
return {
|
|
597
|
-
...baseInstance,
|
|
598
|
-
router,
|
|
599
|
-
operations,
|
|
600
|
-
options: options || {}
|
|
601
|
-
};
|
|
602
|
-
};
|
|
603
|
-
const isInstance = (instance)=>{
|
|
604
|
-
return instance != null && typeof instance === 'object' && instance.coordinate != null && instance.router != null && instance.registry != null && instance.operations != null && instance.options != null;
|
|
605
|
-
};
|
|
606
|
-
|
|
607
|
-
const logger$1 = LibLogger.get("InstanceFactory");
|
|
608
|
-
/**
|
|
609
|
-
* Factory function for creating express-router instances
|
|
610
|
-
*/ const createInstanceFactory = (router, operations, options)=>{
|
|
611
|
-
return (coordinate, context)=>{
|
|
612
|
-
logger$1.debug("Creating express-router instance", {
|
|
613
|
-
coordinate,
|
|
614
|
-
registry: context.registry,
|
|
615
|
-
router,
|
|
616
|
-
operations,
|
|
617
|
-
options
|
|
618
|
-
});
|
|
619
|
-
return createInstance(context.registry, coordinate, router, operations, options);
|
|
620
|
-
};
|
|
621
|
-
};
|
|
622
|
-
|
|
623
|
-
const logger = LibLogger.get("Registry");
|
|
624
|
-
/**
|
|
625
|
-
* Factory function for creating express-router registries
|
|
626
|
-
*/ const createRegistryFactory = ()=>{
|
|
627
|
-
return (type, registryHub)=>{
|
|
628
|
-
if (type !== 'express-router') {
|
|
629
|
-
throw new Error(`Express Router registry factory can only create 'express-router' type registries, got: ${type}`);
|
|
630
|
-
}
|
|
631
|
-
logger.debug("Creating express-router registry", {
|
|
632
|
-
type,
|
|
633
|
-
registryHub
|
|
634
|
-
});
|
|
635
|
-
const baseRegistry = registry.createRegistry(type, registryHub);
|
|
636
|
-
// Cast to Registry for type safety
|
|
637
|
-
return baseRegistry;
|
|
638
|
-
};
|
|
639
|
-
};
|
|
640
|
-
/**
|
|
641
|
-
* Creates a new express-router registry instance
|
|
642
|
-
*/ const createRegistry = (registryHub)=>{
|
|
643
|
-
const baseRegistry = registry.createRegistry('express-router', registryHub);
|
|
644
|
-
return {
|
|
645
|
-
...baseRegistry
|
|
646
|
-
};
|
|
647
|
-
};
|
|
648
|
-
|
|
649
|
-
exports.CItemRouter = CItemRouter;
|
|
650
|
-
exports.ItemRouter = ItemRouter;
|
|
651
|
-
exports.PItemRouter = PItemRouter;
|
|
652
|
-
exports.createInstance = createInstance;
|
|
653
|
-
exports.createInstanceFactory = createInstanceFactory;
|
|
654
|
-
exports.createRegistry = createRegistry;
|
|
655
|
-
exports.createRegistryFactory = createRegistryFactory;
|
|
656
|
-
exports.isInstance = isInstance;
|
|
657
|
-
//# sourceMappingURL=index.cjs.map
|