@fjell/express-router 4.3.9 → 4.4.0
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/{src/CItemRouter.d.ts → CItemRouter.d.ts} +4 -4
- package/dist/CItemRouter.js +85 -0
- package/dist/{src/ItemRouter.d.ts → ItemRouter.d.ts} +4 -4
- package/dist/ItemRouter.js +289 -0
- package/dist/{src/PItemRouter.d.ts → PItemRouter.d.ts} +4 -4
- package/dist/PItemRouter.js +67 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +4 -0
- package/dist/logger.d.ts +2 -0
- package/dist/logger.js +6 -0
- package/package.json +26 -17
- package/src/ItemRouter.ts +3 -3
- package/dist/src/CItemRouter.js +0 -58
- package/dist/src/CItemRouter.js.map +0 -1
- package/dist/src/ItemRouter.js +0 -222
- package/dist/src/ItemRouter.js.map +0 -1
- package/dist/src/PItemRouter.js +0 -41
- package/dist/src/PItemRouter.js.map +0 -1
- package/dist/src/index.d.ts +0 -3
- package/dist/src/index.js +0 -4
- package/dist/src/index.js.map +0 -1
- package/dist/src/logger.d.ts +0 -2
- package/dist/src/logger.js +0 -4
- package/dist/src/logger.js.map +0 -1
- package/eslint.config.mjs +0 -70
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ComKey, Item, LocKeyArray } from
|
|
2
|
-
import { Request, Response } from
|
|
3
|
-
import { ItemRouter, ItemRouterOptions } from
|
|
4
|
-
import { Contained } from
|
|
1
|
+
import { ComKey, Item, LocKeyArray } from '@fjell/core';
|
|
2
|
+
import { Request, Response } from 'express';
|
|
3
|
+
import { ItemRouter, ItemRouterOptions } from './ItemRouter';
|
|
4
|
+
import { Contained } from '@fjell/lib';
|
|
5
5
|
export declare class CItemRouter<T extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> extends ItemRouter<S, L1, L2, L3, L4, L5> {
|
|
6
6
|
private parentRoute;
|
|
7
7
|
constructor(lib: Contained.Operations<T, S, L1, L2, L3, L4, L5>, type: S, parentRoute: ItemRouter<L1, L2, L3, L4, L5, never>, options?: ItemRouterOptions);
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { validatePK, paramsToQuery } from '@fjell/core';
|
|
2
|
+
import { ItemRouter } from './ItemRouter.js';
|
|
3
|
+
import LibLogger from './logger.js';
|
|
4
|
+
|
|
5
|
+
function _define_property(obj, key, value) {
|
|
6
|
+
if (key in obj) {
|
|
7
|
+
Object.defineProperty(obj, key, {
|
|
8
|
+
value: value,
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true
|
|
12
|
+
});
|
|
13
|
+
} else {
|
|
14
|
+
obj[key] = value;
|
|
15
|
+
}
|
|
16
|
+
return obj;
|
|
17
|
+
}
|
|
18
|
+
const logger = LibLogger.get('CItemRouter');
|
|
19
|
+
class CItemRouter extends ItemRouter {
|
|
20
|
+
hasParent() {
|
|
21
|
+
return !!this.parentRoute;
|
|
22
|
+
}
|
|
23
|
+
getIk(res) {
|
|
24
|
+
const pri = this.getPk(res);
|
|
25
|
+
const loc = this.getLocations(res);
|
|
26
|
+
return {
|
|
27
|
+
kt: pri.kt,
|
|
28
|
+
pk: pri.pk,
|
|
29
|
+
loc
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
getLKA(res) {
|
|
33
|
+
/**
|
|
34
|
+
* A location key array is passed to a child router to provide contextfor the items it will
|
|
35
|
+
* be working with. It is always a concatenation of "My LKA" + "Parent LKA" which will
|
|
36
|
+
* bubble all the way up to the root Primary.
|
|
37
|
+
*/ let lka = [
|
|
38
|
+
this.getLk(res)
|
|
39
|
+
];
|
|
40
|
+
lka = lka.concat(this.parentRoute.getLKA(res));
|
|
41
|
+
return lka;
|
|
42
|
+
}
|
|
43
|
+
getLocations(res) {
|
|
44
|
+
return this.parentRoute.getLKA(res);
|
|
45
|
+
}
|
|
46
|
+
constructor(lib, type, parentRoute, options = {}){
|
|
47
|
+
super(lib, type, options), _define_property(this, "parentRoute", void 0), _define_property(this, "createItem", async (req, res)=>{
|
|
48
|
+
logger.trace('Creating Item 2', {
|
|
49
|
+
body: req === null || req === void 0 ? void 0 : req.body,
|
|
50
|
+
query: req === null || req === void 0 ? void 0 : req.query,
|
|
51
|
+
params: req === null || req === void 0 ? void 0 : req.params,
|
|
52
|
+
locals: res === null || res === void 0 ? void 0 : res.locals
|
|
53
|
+
});
|
|
54
|
+
const itemToCreate = this.convertDates(req.body);
|
|
55
|
+
let item = validatePK(await this.lib.create(itemToCreate, {
|
|
56
|
+
locations: this.getLocations(res)
|
|
57
|
+
}), this.getPkType());
|
|
58
|
+
item = await this.postCreateItem(item);
|
|
59
|
+
return res.json(item);
|
|
60
|
+
}), /* eslint-disable */ _define_property(this, "findItems", async (req, res)=>{
|
|
61
|
+
logger.trace('Finding Items', {
|
|
62
|
+
query: req.query,
|
|
63
|
+
params: req.params,
|
|
64
|
+
locals: res.locals
|
|
65
|
+
});
|
|
66
|
+
const query = req.query;
|
|
67
|
+
const finder = query['finder'];
|
|
68
|
+
const finderParams = query['finderParams'];
|
|
69
|
+
let items = [];
|
|
70
|
+
if (finder) {
|
|
71
|
+
// If finder is defined? Call a finder.
|
|
72
|
+
items = await this.lib.find(finder, JSON.parse(finderParams), this.getLocations(res));
|
|
73
|
+
} else {
|
|
74
|
+
// TODO: This is once of the more important places to perform some validaation and feedback
|
|
75
|
+
const itemQuery = paramsToQuery(req.query);
|
|
76
|
+
items = await this.lib.all(itemQuery, this.getLocations(res));
|
|
77
|
+
}
|
|
78
|
+
return res.json(items.map((item)=>validatePK(item, this.getPkType())));
|
|
79
|
+
});
|
|
80
|
+
this.parentRoute = parentRoute;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export { CItemRouter };
|
|
85
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ0l0ZW1Sb3V0ZXIuanMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ComKey, Item, ItemProperties, LocKey, LocKeyArray, PriKey } from
|
|
2
|
-
import { Operations } from
|
|
3
|
-
import { Request, RequestHandler, Response, Router } from
|
|
1
|
+
import { ComKey, Item, ItemProperties, LocKey, LocKeyArray, PriKey } from '@fjell/core';
|
|
2
|
+
import { Operations } from '@fjell/lib';
|
|
3
|
+
import { Request, RequestHandler, Response, Router } from 'express';
|
|
4
4
|
export type ItemRouterOptions = Record<string, never>;
|
|
5
5
|
export type ActionMethod = <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(req: Request, res: Response, item: Item<S, L1, L2, L3, L4, L5>, params: any, body: any) => Promise<Item<S, L1, L2, L3, L4, L5>>;
|
|
6
6
|
export type AllActionMethods = Array<RequestHandler>;
|
|
@@ -26,7 +26,7 @@ export declare class ItemRouter<S extends string, L1 extends string = never, L2
|
|
|
26
26
|
addChildRouter: (path: string, router: Router) => void;
|
|
27
27
|
protected configureItemActions(): Record<string, ActionMethod>;
|
|
28
28
|
protected configureAllActions(): Record<string, AllActionMethods>;
|
|
29
|
-
getRouter():
|
|
29
|
+
getRouter(): Router;
|
|
30
30
|
protected createItem: (req: Request, res: Response) => Promise<Response<any, Record<string, any>>>;
|
|
31
31
|
postCreateItem: (item: Item<S, L1, L2, L3, L4, L5>) => Promise<Item<S, L1, L2, L3, L4, L5>>;
|
|
32
32
|
protected deleteItem: (req: Request, res: Response) => Promise<Response<any, Record<string, any>>>;
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { cPK, validatePK } from '@fjell/core';
|
|
2
|
+
import { NotFoundError } from '@fjell/lib';
|
|
3
|
+
import deepmerge from 'deepmerge';
|
|
4
|
+
import { Router } from 'express';
|
|
5
|
+
import LibLogger from './logger.js';
|
|
6
|
+
|
|
7
|
+
function _define_property(obj, key, value) {
|
|
8
|
+
if (key in obj) {
|
|
9
|
+
Object.defineProperty(obj, key, {
|
|
10
|
+
value: value,
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
writable: true
|
|
14
|
+
});
|
|
15
|
+
} else {
|
|
16
|
+
obj[key] = value;
|
|
17
|
+
}
|
|
18
|
+
return obj;
|
|
19
|
+
}
|
|
20
|
+
class ItemRouter {
|
|
21
|
+
getLk(res) {
|
|
22
|
+
return {
|
|
23
|
+
kt: this.keyType,
|
|
24
|
+
lk: res.locals[this.getPkParam()]
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
// this is meant to be consumed by children routers
|
|
28
|
+
getLKA(res) {
|
|
29
|
+
return [
|
|
30
|
+
this.getLk(res)
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
getPk(res) {
|
|
34
|
+
return cPK(res.locals[this.getPkParam()], this.getPkType());
|
|
35
|
+
}
|
|
36
|
+
// Unless this is a contained router, the locations will always be an empty array.
|
|
37
|
+
/* eslint-disable */ getLocations(res) {
|
|
38
|
+
throw new Error('Method not implemented in an abstract router');
|
|
39
|
+
}
|
|
40
|
+
/* eslint-enable */ // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
41
|
+
getIk(res) {
|
|
42
|
+
throw new Error('Method not implemented in an abstract router');
|
|
43
|
+
}
|
|
44
|
+
/* istanbul ignore next */ configureItemActions() {
|
|
45
|
+
this.logger.debug('ARouter - No Item Actions Configured');
|
|
46
|
+
return {};
|
|
47
|
+
}
|
|
48
|
+
/* istanbul ignore next */ configureAllActions() {
|
|
49
|
+
this.logger.debug('ARouter - No All Actions Configured');
|
|
50
|
+
return {};
|
|
51
|
+
}
|
|
52
|
+
/* istanbul ignore next */ getRouter() {
|
|
53
|
+
const router = Router();
|
|
54
|
+
this.configure(router);
|
|
55
|
+
return router;
|
|
56
|
+
}
|
|
57
|
+
constructor(lib, keyType, options = {}){
|
|
58
|
+
_define_property(this, "lib", void 0);
|
|
59
|
+
_define_property(this, "keyType", void 0);
|
|
60
|
+
_define_property(this, "options", void 0);
|
|
61
|
+
_define_property(this, "childRouters", {});
|
|
62
|
+
_define_property(this, "logger", void 0);
|
|
63
|
+
_define_property(this, "itemActions", void 0);
|
|
64
|
+
_define_property(this, "getPkType", ()=>{
|
|
65
|
+
return this.keyType;
|
|
66
|
+
});
|
|
67
|
+
_define_property(this, "getPkParam", ()=>{
|
|
68
|
+
return `${this.getPkType()}Pk`;
|
|
69
|
+
});
|
|
70
|
+
_define_property(this, "postItemAction", async (req, res)=>{
|
|
71
|
+
this.logger.default('Getting Item', {
|
|
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 ik = this.getIk(res);
|
|
77
|
+
const actionKey = req.path.substring(req.path.lastIndexOf('/') + 1);
|
|
78
|
+
if (!this.itemActions) {
|
|
79
|
+
this.logger.error('Item Actions are not configured');
|
|
80
|
+
return res.status(500).json({
|
|
81
|
+
error: 'Item Actions are not configured'
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
const item = validatePK(await this.lib.get(ik), this.getPkType());
|
|
86
|
+
return res.json(await this.itemActions[actionKey](req, res, item, req.params, req.body));
|
|
87
|
+
} catch (err) {
|
|
88
|
+
this.logger.error('Error in Item Action', {
|
|
89
|
+
message: err === null || err === void 0 ? void 0 : err.message,
|
|
90
|
+
stack: err === null || err === void 0 ? void 0 : err.stack
|
|
91
|
+
});
|
|
92
|
+
return res.status(500).json(err);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
_define_property(this, "configure", (router)=>{
|
|
96
|
+
this.logger.default('Configuring Router', {
|
|
97
|
+
pkType: this.getPkType()
|
|
98
|
+
});
|
|
99
|
+
router.get('/', this.findItems);
|
|
100
|
+
router.post('/', this.createItem);
|
|
101
|
+
const allActions = this.configureAllActions();
|
|
102
|
+
this.logger.debug('All Actions supplied to Router', {
|
|
103
|
+
allActions
|
|
104
|
+
});
|
|
105
|
+
if (allActions) {
|
|
106
|
+
Object.keys(allActions).forEach((actionKey)=>{
|
|
107
|
+
this.logger.default('Configuring All Action', {
|
|
108
|
+
actionKey
|
|
109
|
+
});
|
|
110
|
+
// TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
|
|
111
|
+
router.post(`/${actionKey}`, ...allActions[actionKey]);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
const itemRouter = Router();
|
|
115
|
+
itemRouter.get('/', this.getItem);
|
|
116
|
+
itemRouter.put('/', this.updateItem);
|
|
117
|
+
itemRouter.delete('/', this.deleteItem);
|
|
118
|
+
this.itemActions = this.configureItemActions();
|
|
119
|
+
this.logger.debug('Item Actions supplied to Router', {
|
|
120
|
+
itemActions: this.itemActions
|
|
121
|
+
});
|
|
122
|
+
if (this.itemActions) {
|
|
123
|
+
Object.keys(this.itemActions).forEach((actionKey)=>{
|
|
124
|
+
this.logger.default('Configuring Item Action', {
|
|
125
|
+
actionKey
|
|
126
|
+
});
|
|
127
|
+
// TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
|
|
128
|
+
itemRouter.post(`/${actionKey}`, this.postItemAction);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
this.logger.default('Configuring Item Operations under PK Param', {
|
|
132
|
+
pkParam: this.getPkParam()
|
|
133
|
+
});
|
|
134
|
+
router.use(`/:${this.getPkParam()}`, this.validatePrimaryKeyValue, itemRouter);
|
|
135
|
+
if (this.childRouters) {
|
|
136
|
+
this.configureChildRouters(itemRouter, this.childRouters);
|
|
137
|
+
}
|
|
138
|
+
return router;
|
|
139
|
+
});
|
|
140
|
+
_define_property(this, "validatePrimaryKeyValue", (req, res, next)=>{
|
|
141
|
+
const pkParamValue = req.params[this.getPkParam()];
|
|
142
|
+
if (this.validatePKParam(pkParamValue)) {
|
|
143
|
+
res.locals[this.getPkParam()] = pkParamValue;
|
|
144
|
+
next();
|
|
145
|
+
} else {
|
|
146
|
+
this.logger.error('Invalid Primary Key', {
|
|
147
|
+
pkParamValue,
|
|
148
|
+
path: req === null || req === void 0 ? void 0 : req.path
|
|
149
|
+
});
|
|
150
|
+
res.status(500).json({
|
|
151
|
+
error: 'Invalid Primary Key',
|
|
152
|
+
path: req === null || req === void 0 ? void 0 : req.path
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
_define_property(this, "configureChildRouters", (router, childRouters)=>{
|
|
157
|
+
for(const path in childRouters){
|
|
158
|
+
this.logger.default('Configuring Child Router at Path', {
|
|
159
|
+
path
|
|
160
|
+
});
|
|
161
|
+
router.use(`/${path}`, childRouters[path]);
|
|
162
|
+
}
|
|
163
|
+
return router;
|
|
164
|
+
});
|
|
165
|
+
_define_property(this, "addChildRouter", (path, router)=>{
|
|
166
|
+
this.childRouters[path] = router;
|
|
167
|
+
});
|
|
168
|
+
/* istanbul ignore next */ // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
169
|
+
_define_property(this, "createItem", async (req, res)=>{
|
|
170
|
+
throw new Error('Method not implemented in an abstract router');
|
|
171
|
+
});
|
|
172
|
+
// TODO: Probably a better way to do this, but this postCreate hook only needs the item.
|
|
173
|
+
/* istanbul ignore next */ _define_property(this, "postCreateItem", async (item)=>{
|
|
174
|
+
this.logger.default('Post Create Item', {
|
|
175
|
+
item
|
|
176
|
+
});
|
|
177
|
+
return item;
|
|
178
|
+
});
|
|
179
|
+
_define_property(this, "deleteItem", async (req, res)=>{
|
|
180
|
+
this.logger.default('Deleting Item', {
|
|
181
|
+
query: req.query,
|
|
182
|
+
params: req.params,
|
|
183
|
+
locals: res.locals
|
|
184
|
+
});
|
|
185
|
+
const ik = this.getIk(res);
|
|
186
|
+
const removedItem = await this.lib.remove(ik);
|
|
187
|
+
const item = validatePK(removedItem, this.getPkType());
|
|
188
|
+
return res.json(item);
|
|
189
|
+
});
|
|
190
|
+
/* eslint-disable */ /* istanbul ignore next */ _define_property(this, "findItems", async (req, res)=>{
|
|
191
|
+
throw new Error('Method not implemented in an abstract router');
|
|
192
|
+
});
|
|
193
|
+
/* eslint-enable */ _define_property(this, "getItem", async (req, res)=>{
|
|
194
|
+
this.logger.default('Getting Item', {
|
|
195
|
+
query: req.query,
|
|
196
|
+
params: req.params,
|
|
197
|
+
locals: res.locals
|
|
198
|
+
});
|
|
199
|
+
const ik = this.getIk(res);
|
|
200
|
+
try {
|
|
201
|
+
// TODO: What error does validate PK throw, when can that fail?
|
|
202
|
+
const item = validatePK(await this.lib.get(ik), this.getPkType());
|
|
203
|
+
return res.json(item);
|
|
204
|
+
} catch (err) {
|
|
205
|
+
if (err instanceof NotFoundError) {
|
|
206
|
+
this.logger.error('Item Not Found', {
|
|
207
|
+
ik,
|
|
208
|
+
message: err === null || err === void 0 ? void 0 : err.message,
|
|
209
|
+
stack: err === null || err === void 0 ? void 0 : err.stack
|
|
210
|
+
});
|
|
211
|
+
return res.status(404).json({
|
|
212
|
+
ik,
|
|
213
|
+
message: "Item Not Found"
|
|
214
|
+
});
|
|
215
|
+
} else {
|
|
216
|
+
this.logger.error('General Error', {
|
|
217
|
+
ik,
|
|
218
|
+
message: err === null || err === void 0 ? void 0 : err.message,
|
|
219
|
+
stack: err === null || err === void 0 ? void 0 : err.stack
|
|
220
|
+
});
|
|
221
|
+
return res.status(500).json({
|
|
222
|
+
ik,
|
|
223
|
+
message: "General Error"
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
_define_property(this, "updateItem", async (req, res)=>{
|
|
229
|
+
this.logger.default('Updating Item', {
|
|
230
|
+
body: req === null || req === void 0 ? void 0 : req.body,
|
|
231
|
+
query: req === null || req === void 0 ? void 0 : req.query,
|
|
232
|
+
params: req === null || req === void 0 ? void 0 : req.params,
|
|
233
|
+
locals: res === null || res === void 0 ? void 0 : res.locals
|
|
234
|
+
});
|
|
235
|
+
const ik = this.getIk(res);
|
|
236
|
+
const itemToUpdate = this.convertDates(req.body);
|
|
237
|
+
const retItem = validatePK(await this.lib.update(ik, itemToUpdate), this.getPkType());
|
|
238
|
+
return res.json(retItem);
|
|
239
|
+
});
|
|
240
|
+
_define_property(this, "convertDates", (item)=>{
|
|
241
|
+
const events = item.events;
|
|
242
|
+
this.logger.default('Converting Dates', {
|
|
243
|
+
item
|
|
244
|
+
});
|
|
245
|
+
if (events) {
|
|
246
|
+
Object.keys(events).forEach((key)=>{
|
|
247
|
+
Object.assign(events, {
|
|
248
|
+
[key]: deepmerge(events[key], {
|
|
249
|
+
at: events[key].at ? new Date(events[key].at) : null
|
|
250
|
+
})
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
Object.assign(item, {
|
|
255
|
+
events
|
|
256
|
+
});
|
|
257
|
+
return item;
|
|
258
|
+
});
|
|
259
|
+
// TODO: Maybe just simplify this and require that everything is a UUID?
|
|
260
|
+
/**
|
|
261
|
+
* This method might be an annoyance, but we need to capture a few cases where someone passes
|
|
262
|
+
* a PK parameter that has an odd string in it.
|
|
263
|
+
*
|
|
264
|
+
* @param pkParamValue The value of the primary key parameter
|
|
265
|
+
* @returns if the value is valid.
|
|
266
|
+
*/ _define_property(this, "validatePKParam", (pkParamValue)=>{
|
|
267
|
+
let validPkParam = true;
|
|
268
|
+
if (pkParamValue.length <= 0) {
|
|
269
|
+
this.logger.error('Primary Key is an Empty String', {
|
|
270
|
+
pkParamValue
|
|
271
|
+
});
|
|
272
|
+
validPkParam = false;
|
|
273
|
+
} else if (pkParamValue === 'undefined') {
|
|
274
|
+
this.logger.error('Primary Key is the string \'undefined\'', {
|
|
275
|
+
pkParamValue
|
|
276
|
+
});
|
|
277
|
+
validPkParam = false;
|
|
278
|
+
}
|
|
279
|
+
return validPkParam;
|
|
280
|
+
});
|
|
281
|
+
this.lib = lib;
|
|
282
|
+
this.keyType = keyType;
|
|
283
|
+
this.options = options;
|
|
284
|
+
this.logger = LibLogger.get("ItemRouter", keyType);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export { ItemRouter };
|
|
289
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiSXRlbVJvdXRlci5qcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Item, PriKey } from
|
|
2
|
-
import { Primary } from
|
|
3
|
-
import { ItemRouter, ItemRouterOptions } from
|
|
4
|
-
import { Request, Response } from
|
|
1
|
+
import { Item, PriKey } from '@fjell/core';
|
|
2
|
+
import { Primary } from '@fjell/lib';
|
|
3
|
+
import { ItemRouter, ItemRouterOptions } from './ItemRouter';
|
|
4
|
+
import { Request, Response } from 'express';
|
|
5
5
|
export declare class PItemRouter<T extends Item<S>, S extends string> extends ItemRouter<S> {
|
|
6
6
|
constructor(lib: Primary.Operations<T, S>, keyType: S, options?: ItemRouterOptions);
|
|
7
7
|
getIk(res: Response): PriKey<S>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { validatePK, paramsToQuery } from '@fjell/core';
|
|
2
|
+
import { ItemRouter } from './ItemRouter.js';
|
|
3
|
+
import LibLogger from './logger.js';
|
|
4
|
+
|
|
5
|
+
function _define_property(obj, key, value) {
|
|
6
|
+
if (key in obj) {
|
|
7
|
+
Object.defineProperty(obj, key, {
|
|
8
|
+
value: value,
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true
|
|
12
|
+
});
|
|
13
|
+
} else {
|
|
14
|
+
obj[key] = value;
|
|
15
|
+
}
|
|
16
|
+
return obj;
|
|
17
|
+
}
|
|
18
|
+
const logger = LibLogger.get('PItemRouter');
|
|
19
|
+
class PItemRouter extends ItemRouter {
|
|
20
|
+
getIk(res) {
|
|
21
|
+
const pri = this.getPk(res);
|
|
22
|
+
return pri;
|
|
23
|
+
}
|
|
24
|
+
constructor(lib, keyType, options = {}){
|
|
25
|
+
super(lib, keyType, options), _define_property(this, "createItem", async (req, res)=>{
|
|
26
|
+
logger.default('Creating Item 2', {
|
|
27
|
+
body: req.body,
|
|
28
|
+
query: req.query,
|
|
29
|
+
params: req.params,
|
|
30
|
+
locals: res.locals
|
|
31
|
+
});
|
|
32
|
+
const itemToCreate = this.convertDates(req.body);
|
|
33
|
+
let item = validatePK(await this.lib.create(itemToCreate), this.getPkType());
|
|
34
|
+
item = await this.postCreateItem(item);
|
|
35
|
+
return res.json(item);
|
|
36
|
+
}), /* eslint-disable */ _define_property(this, "findItems", async (req, res)=>{
|
|
37
|
+
logger.default('Finding Items', {
|
|
38
|
+
query: req.query,
|
|
39
|
+
params: req.params,
|
|
40
|
+
locals: res.locals
|
|
41
|
+
});
|
|
42
|
+
let items = [];
|
|
43
|
+
const query = req.query;
|
|
44
|
+
const finder = query['finder'];
|
|
45
|
+
const finderParams = query['finderParams'];
|
|
46
|
+
if (finder) {
|
|
47
|
+
// If finder is defined? Call a finder.
|
|
48
|
+
logger.default('Finding Items with a finder', {
|
|
49
|
+
finder,
|
|
50
|
+
finderParams
|
|
51
|
+
});
|
|
52
|
+
items = await this.lib.find(finder, JSON.parse(finderParams));
|
|
53
|
+
} else {
|
|
54
|
+
logger.default('Finding Items with a query', {
|
|
55
|
+
query: req.query
|
|
56
|
+
});
|
|
57
|
+
// TODO: This is once of the more important places to perform some validaation and feedback
|
|
58
|
+
const itemQuery = paramsToQuery(req.query);
|
|
59
|
+
items = await this.lib.all(itemQuery);
|
|
60
|
+
}
|
|
61
|
+
return res.json(items.map((item)=>validatePK(item, this.getPkType())));
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export { PItemRouter };
|
|
67
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiUEl0ZW1Sb3V0ZXIuanMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { ItemRouter } from './ItemRouter.js';
|
|
2
|
+
export { CItemRouter } from './CItemRouter.js';
|
|
3
|
+
export { PItemRouter } from './PItemRouter.js';
|
|
4
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzsifQ==
|
package/dist/logger.d.ts
ADDED
package/dist/logger.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import Logging from '@fjell/logging';
|
|
2
|
+
|
|
3
|
+
const LibLogger = Logging.getLogger('@fjell/express-router');
|
|
4
|
+
|
|
5
|
+
export { LibLogger as default };
|
|
6
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9nZ2VyLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7OyJ9
|
package/package.json
CHANGED
|
@@ -1,30 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fjell/express-router",
|
|
3
|
-
"version": "4.
|
|
4
|
-
"license": "
|
|
3
|
+
"version": "4.4.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
5
|
"description": "Express Router for Fjell",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=21"
|
|
8
8
|
},
|
|
9
|
-
"main": "dist/
|
|
9
|
+
"main": "dist/index.js",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"require": "./dist/index.js",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"type": "module",
|
|
18
|
-
"scripts": {
|
|
19
|
-
"build": "yarn run lint && tsc && tsc-alias -p tsconfig.json",
|
|
20
|
-
"dev": "concurrently \"tsc -w\" \"tsc-alias -w\"",
|
|
21
|
-
"lint": "eslint .",
|
|
22
|
-
"clean": "rimraf dist",
|
|
23
|
-
"test": "yarn run lint && NODE_OPTIONS='--experimental-require-module' yarn run jest --coverage"
|
|
24
|
-
},
|
|
25
18
|
"dependencies": {
|
|
26
|
-
"@fjell/
|
|
27
|
-
"@fjell/
|
|
19
|
+
"@fjell/core": "^4.4",
|
|
20
|
+
"@fjell/lib": "^4.4",
|
|
21
|
+
"@fjell/logging": "^4.4",
|
|
28
22
|
"@google-cloud/firestore": "^7.11.0",
|
|
29
23
|
"cors": "^2.8.5",
|
|
30
24
|
"dayjs": "^1.11.13",
|
|
@@ -42,6 +36,10 @@
|
|
|
42
36
|
"@babel/preset-env": "^7.26.9",
|
|
43
37
|
"@babel/preset-react": "^7.26.3",
|
|
44
38
|
"@babel/preset-typescript": "^7.26.0",
|
|
39
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
40
|
+
"@eslint/js": "^9.27.0",
|
|
41
|
+
"@jest/globals": "^29.7.0",
|
|
42
|
+
"@swc/core": "^1.11.24",
|
|
45
43
|
"@tsconfig/recommended": "^1.0.8",
|
|
46
44
|
"@types/cors": "^2.8.17",
|
|
47
45
|
"@types/express": "^4.17.21",
|
|
@@ -53,6 +51,8 @@
|
|
|
53
51
|
"@types/response-time": "^2.3.8",
|
|
54
52
|
"@typescript-eslint/eslint-plugin": "^8.24.1",
|
|
55
53
|
"@typescript-eslint/parser": "^8.24.1",
|
|
54
|
+
"@vitest/coverage-v8": "^3.1.4",
|
|
55
|
+
"@vitest/ui": "^3.1.4",
|
|
56
56
|
"concurrently": "^9.1.2",
|
|
57
57
|
"eslint": "^9.21.0",
|
|
58
58
|
"jest": "^29.7.0",
|
|
@@ -61,11 +61,20 @@
|
|
|
61
61
|
"ts-jest": "^29.2.5",
|
|
62
62
|
"ts-node": "^10.9.2",
|
|
63
63
|
"tsc-alias": "^1.8.10",
|
|
64
|
-
"typescript": "^5.7.3"
|
|
64
|
+
"typescript": "^5.7.3",
|
|
65
|
+
"vite": "^6.3.5",
|
|
66
|
+
"vite-plugin-dts": "^4.5.4",
|
|
67
|
+
"vite-plugin-node": "^5.0.1",
|
|
68
|
+
"vitest": "^3.1.4"
|
|
65
69
|
},
|
|
66
|
-
"packageManager": "yarn@4.6.0",
|
|
67
70
|
"repository": {
|
|
68
71
|
"type": "git",
|
|
69
72
|
"url": "git+https://github.com/getfjell/express-router.git"
|
|
73
|
+
},
|
|
74
|
+
"scripts": {
|
|
75
|
+
"build": "tsc --noEmit && vite build",
|
|
76
|
+
"lint": "eslint . --ext .ts --fix",
|
|
77
|
+
"clean": "rimraf dist",
|
|
78
|
+
"test": "pnpm run lint && vitest run --coverage"
|
|
70
79
|
}
|
|
71
80
|
}
|
package/src/ItemRouter.ts
CHANGED
|
@@ -100,7 +100,7 @@ export class ItemRouter<
|
|
|
100
100
|
}
|
|
101
101
|
try {
|
|
102
102
|
const item =
|
|
103
|
-
|
|
103
|
+
validatePK(await this.lib.get(ik), this.getPkType()) as Item<S, L1, L2, L3, L4, L5>;
|
|
104
104
|
return res.json(await this.itemActions[actionKey](req, res, item, req.params, req.body));
|
|
105
105
|
} catch (err: any) {
|
|
106
106
|
this.logger.error('Error in Item Action', { message: err?.message, stack: err?.stack });
|
|
@@ -129,7 +129,7 @@ export class ItemRouter<
|
|
|
129
129
|
itemRouter.delete('/', this.deleteItem);
|
|
130
130
|
|
|
131
131
|
this.itemActions = this.configureItemActions();
|
|
132
|
-
this.logger.debug('Item Actions supplied to Router', { itemActions:this.itemActions });
|
|
132
|
+
this.logger.debug('Item Actions supplied to Router', { itemActions: this.itemActions });
|
|
133
133
|
if (this.itemActions) {
|
|
134
134
|
Object.keys(this.itemActions).forEach((actionKey) => {
|
|
135
135
|
this.logger.default('Configuring Item Action', { actionKey });
|
|
@@ -184,7 +184,7 @@ export class ItemRouter<
|
|
|
184
184
|
}
|
|
185
185
|
|
|
186
186
|
/* istanbul ignore next */
|
|
187
|
-
public getRouter() {
|
|
187
|
+
public getRouter(): Router {
|
|
188
188
|
const router = Router();
|
|
189
189
|
this.configure(router);
|
|
190
190
|
return router;
|
package/dist/src/CItemRouter.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { paramsToQuery, validatePK } from "@fjell/core";
|
|
2
|
-
import { ItemRouter } from "./ItemRouter";
|
|
3
|
-
import LibLogger from "./logger";
|
|
4
|
-
const logger = LibLogger.get('CItemRouter');
|
|
5
|
-
export class CItemRouter extends ItemRouter {
|
|
6
|
-
parentRoute;
|
|
7
|
-
constructor(lib, type, parentRoute, options = {}) {
|
|
8
|
-
super(lib, type, options);
|
|
9
|
-
this.parentRoute = parentRoute;
|
|
10
|
-
}
|
|
11
|
-
hasParent() {
|
|
12
|
-
return !!this.parentRoute;
|
|
13
|
-
}
|
|
14
|
-
getIk(res) {
|
|
15
|
-
const pri = this.getPk(res);
|
|
16
|
-
const loc = this.getLocations(res);
|
|
17
|
-
return { kt: pri.kt, pk: pri.pk, loc };
|
|
18
|
-
}
|
|
19
|
-
getLKA(res) {
|
|
20
|
-
/**
|
|
21
|
-
* A location key array is passed to a child router to provide contextfor the items it will
|
|
22
|
-
* be working with. It is always a concatenation of "My LKA" + "Parent LKA" which will
|
|
23
|
-
* bubble all the way up to the root Primary.
|
|
24
|
-
*/
|
|
25
|
-
let lka = [this.getLk(res)];
|
|
26
|
-
lka = lka.concat(this.parentRoute.getLKA(res));
|
|
27
|
-
return lka;
|
|
28
|
-
}
|
|
29
|
-
getLocations(res) {
|
|
30
|
-
return this.parentRoute.getLKA(res);
|
|
31
|
-
}
|
|
32
|
-
createItem = async (req, res) => {
|
|
33
|
-
logger.trace('Creating Item 2', { body: req?.body, query: req?.query, params: req?.params, locals: res?.locals });
|
|
34
|
-
const itemToCreate = this.convertDates(req.body);
|
|
35
|
-
let item = validatePK(await this.lib.create(itemToCreate, { locations: this.getLocations(res) }), this.getPkType());
|
|
36
|
-
item = await this.postCreateItem(item);
|
|
37
|
-
return res.json(item);
|
|
38
|
-
};
|
|
39
|
-
/* eslint-disable */
|
|
40
|
-
findItems = async (req, res) => {
|
|
41
|
-
logger.trace('Finding Items', { query: req.query, params: req.params, locals: res.locals });
|
|
42
|
-
const query = req.query;
|
|
43
|
-
const finder = query['finder'];
|
|
44
|
-
const finderParams = query['finderParams'];
|
|
45
|
-
let items = [];
|
|
46
|
-
if (finder) {
|
|
47
|
-
// If finder is defined? Call a finder.
|
|
48
|
-
items = await this.lib.find(finder, JSON.parse(finderParams), this.getLocations(res));
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
// TODO: This is once of the more important places to perform some validaation and feedback
|
|
52
|
-
const itemQuery = paramsToQuery(req.query);
|
|
53
|
-
items = await this.lib.all(itemQuery, this.getLocations(res));
|
|
54
|
-
}
|
|
55
|
-
return res.json(items.map((item) => validatePK(item, this.getPkType())));
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
//# sourceMappingURL=CItemRouter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CItemRouter.js","sourceRoot":"","sources":["../../src/CItemRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EACyC,aAAa,EAAuB,UAAU,EAC7F,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,UAAU,EAAqB,MAAM,cAAc,CAAC;AAC7D,OAAO,SAAS,MAAM,UAAU,CAAC;AAGjC,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;AAK5C,MAAM,OAAO,WAQX,SAAQ,UAAiC;IAEjC,WAAW,CAAwC;IAE3D,YACE,GAAmD,EACnD,IAAO,EACP,WAAkD,EAClD,UAA6B,EAAE;QAE/B,KAAK,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;QAC1B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IACjC,CAAC;IAEM,SAAS;QACd,OAAO,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC;IAC5B,CAAC;IAEM,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-
import { cPK, validatePK } from "@fjell/core";
|
|
2
|
-
import { NotFoundError } from "@fjell/lib";
|
|
3
|
-
import deepmerge from "deepmerge";
|
|
4
|
-
import { Router } from "express";
|
|
5
|
-
import LibLogger from "./logger";
|
|
6
|
-
export class ItemRouter {
|
|
7
|
-
lib;
|
|
8
|
-
keyType;
|
|
9
|
-
options;
|
|
10
|
-
childRouters = {};
|
|
11
|
-
logger;
|
|
12
|
-
itemActions;
|
|
13
|
-
constructor(lib, keyType, options = {}) {
|
|
14
|
-
this.lib = lib;
|
|
15
|
-
this.keyType = keyType;
|
|
16
|
-
this.options = options;
|
|
17
|
-
this.logger = LibLogger.get("ItemRouter", keyType);
|
|
18
|
-
}
|
|
19
|
-
getPkType = () => {
|
|
20
|
-
return this.keyType;
|
|
21
|
-
};
|
|
22
|
-
getPkParam = () => {
|
|
23
|
-
return `${this.getPkType()}Pk`;
|
|
24
|
-
};
|
|
25
|
-
getLk(res) {
|
|
26
|
-
return { kt: this.keyType, lk: res.locals[this.getPkParam()] };
|
|
27
|
-
}
|
|
28
|
-
// this is meant to be consumed by children routers
|
|
29
|
-
getLKA(res) {
|
|
30
|
-
return [this.getLk(res)];
|
|
31
|
-
}
|
|
32
|
-
getPk(res) {
|
|
33
|
-
return cPK(res.locals[this.getPkParam()], this.getPkType());
|
|
34
|
-
}
|
|
35
|
-
// Unless this is a contained router, the locations will always be an empty array.
|
|
36
|
-
/* eslint-disable */
|
|
37
|
-
getLocations(res) {
|
|
38
|
-
throw new Error('Method not implemented in an abstract router');
|
|
39
|
-
}
|
|
40
|
-
/* eslint-enable */
|
|
41
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
42
|
-
getIk(res) {
|
|
43
|
-
throw new Error('Method not implemented in an abstract router');
|
|
44
|
-
}
|
|
45
|
-
postItemAction = async (req, res) => {
|
|
46
|
-
this.logger.default('Getting Item', { query: req?.query, params: req?.params, locals: res?.locals });
|
|
47
|
-
const ik = this.getIk(res);
|
|
48
|
-
const actionKey = req.path.substring(req.path.lastIndexOf('/') + 1);
|
|
49
|
-
if (!this.itemActions) {
|
|
50
|
-
this.logger.error('Item Actions are not configured');
|
|
51
|
-
return res.status(500).json({ error: 'Item Actions are not configured' });
|
|
52
|
-
}
|
|
53
|
-
try {
|
|
54
|
-
const item = validatePK(await this.lib.get(ik), this.getPkType());
|
|
55
|
-
return res.json(await this.itemActions[actionKey](req, res, item, req.params, req.body));
|
|
56
|
-
}
|
|
57
|
-
catch (err) {
|
|
58
|
-
this.logger.error('Error in Item Action', { message: err?.message, stack: err?.stack });
|
|
59
|
-
return res.status(500).json(err);
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
configure = (router) => {
|
|
63
|
-
this.logger.default('Configuring Router', { pkType: this.getPkType() });
|
|
64
|
-
router.get('/', this.findItems);
|
|
65
|
-
router.post('/', this.createItem);
|
|
66
|
-
const allActions = this.configureAllActions();
|
|
67
|
-
this.logger.debug('All Actions supplied to Router', { allActions });
|
|
68
|
-
if (allActions) {
|
|
69
|
-
Object.keys(allActions).forEach((actionKey) => {
|
|
70
|
-
this.logger.default('Configuring All Action', { actionKey });
|
|
71
|
-
// TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
|
|
72
|
-
router.post(`/${actionKey}`, ...allActions[actionKey]);
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
const itemRouter = Router();
|
|
76
|
-
itemRouter.get('/', this.getItem);
|
|
77
|
-
itemRouter.put('/', this.updateItem);
|
|
78
|
-
itemRouter.delete('/', this.deleteItem);
|
|
79
|
-
this.itemActions = this.configureItemActions();
|
|
80
|
-
this.logger.debug('Item Actions supplied to Router', { itemActions: this.itemActions });
|
|
81
|
-
if (this.itemActions) {
|
|
82
|
-
Object.keys(this.itemActions).forEach((actionKey) => {
|
|
83
|
-
this.logger.default('Configuring Item Action', { actionKey });
|
|
84
|
-
// TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
|
|
85
|
-
itemRouter.post(`/${actionKey}`, this.postItemAction);
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
this.logger.default('Configuring Item Operations under PK Param', { pkParam: this.getPkParam() });
|
|
89
|
-
router.use(`/:${this.getPkParam()}`, this.validatePrimaryKeyValue, itemRouter);
|
|
90
|
-
if (this.childRouters) {
|
|
91
|
-
this.configureChildRouters(itemRouter, this.childRouters);
|
|
92
|
-
}
|
|
93
|
-
return router;
|
|
94
|
-
};
|
|
95
|
-
validatePrimaryKeyValue = (req, res, next) => {
|
|
96
|
-
const pkParamValue = req.params[this.getPkParam()];
|
|
97
|
-
if (this.validatePKParam(pkParamValue)) {
|
|
98
|
-
res.locals[this.getPkParam()] = pkParamValue;
|
|
99
|
-
next();
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
this.logger.error('Invalid Primary Key', { pkParamValue, path: req?.path });
|
|
103
|
-
res.status(500).json({ error: 'Invalid Primary Key', path: req?.path });
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
configureChildRouters = (router, childRouters) => {
|
|
107
|
-
for (const path in childRouters) {
|
|
108
|
-
this.logger.default('Configuring Child Router at Path', { path });
|
|
109
|
-
router.use(`/${path}`, childRouters[path]);
|
|
110
|
-
}
|
|
111
|
-
return router;
|
|
112
|
-
};
|
|
113
|
-
addChildRouter = (path, router) => {
|
|
114
|
-
this.childRouters[path] = router;
|
|
115
|
-
};
|
|
116
|
-
/* istanbul ignore next */
|
|
117
|
-
configureItemActions() {
|
|
118
|
-
this.logger.debug('ARouter - No Item Actions Configured');
|
|
119
|
-
return {};
|
|
120
|
-
}
|
|
121
|
-
/* istanbul ignore next */
|
|
122
|
-
configureAllActions() {
|
|
123
|
-
this.logger.debug('ARouter - No All Actions Configured');
|
|
124
|
-
return {};
|
|
125
|
-
}
|
|
126
|
-
/* istanbul ignore next */
|
|
127
|
-
getRouter() {
|
|
128
|
-
const router = Router();
|
|
129
|
-
this.configure(router);
|
|
130
|
-
return router;
|
|
131
|
-
}
|
|
132
|
-
/* istanbul ignore next */
|
|
133
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
134
|
-
createItem = async (req, res) => {
|
|
135
|
-
throw new Error('Method not implemented in an abstract router');
|
|
136
|
-
};
|
|
137
|
-
// TODO: Probably a better way to do this, but this postCreate hook only needs the item.
|
|
138
|
-
/* istanbul ignore next */
|
|
139
|
-
postCreateItem = async (item) => {
|
|
140
|
-
this.logger.default('Post Create Item', { item });
|
|
141
|
-
return item;
|
|
142
|
-
};
|
|
143
|
-
deleteItem = async (req, res) => {
|
|
144
|
-
this.logger.default('Deleting Item', { query: req.query, params: req.params, locals: res.locals });
|
|
145
|
-
const ik = this.getIk(res);
|
|
146
|
-
const removedItem = await this.lib.remove(ik);
|
|
147
|
-
const item = validatePK(removedItem, this.getPkType());
|
|
148
|
-
return res.json(item);
|
|
149
|
-
};
|
|
150
|
-
/* eslint-disable */
|
|
151
|
-
/* istanbul ignore next */
|
|
152
|
-
findItems = async (req, res) => {
|
|
153
|
-
throw new Error('Method not implemented in an abstract router');
|
|
154
|
-
};
|
|
155
|
-
/* eslint-enable */
|
|
156
|
-
getItem = async (req, res) => {
|
|
157
|
-
this.logger.default('Getting Item', { query: req.query, params: req.params, locals: res.locals });
|
|
158
|
-
const ik = this.getIk(res);
|
|
159
|
-
try {
|
|
160
|
-
// TODO: What error does validate PK throw, when can that fail?
|
|
161
|
-
const item = validatePK(await this.lib.get(ik), this.getPkType());
|
|
162
|
-
return res.json(item);
|
|
163
|
-
}
|
|
164
|
-
catch (err) {
|
|
165
|
-
if (err instanceof NotFoundError) {
|
|
166
|
-
this.logger.error('Item Not Found', { ik, message: err?.message, stack: err?.stack });
|
|
167
|
-
return res.status(404).json({
|
|
168
|
-
ik,
|
|
169
|
-
message: "Item Not Found",
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
this.logger.error('General Error', { ik, message: err?.message, stack: err?.stack });
|
|
174
|
-
return res.status(500).json({
|
|
175
|
-
ik,
|
|
176
|
-
message: "General Error",
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
updateItem = async (req, res) => {
|
|
182
|
-
this.logger.default('Updating Item', { body: req?.body, query: req?.query, params: req?.params, locals: res?.locals });
|
|
183
|
-
const ik = this.getIk(res);
|
|
184
|
-
const itemToUpdate = this.convertDates(req.body);
|
|
185
|
-
const retItem = validatePK(await this.lib.update(ik, itemToUpdate), this.getPkType());
|
|
186
|
-
return res.json(retItem);
|
|
187
|
-
};
|
|
188
|
-
convertDates = (item) => {
|
|
189
|
-
const events = item.events;
|
|
190
|
-
this.logger.default('Converting Dates', { item });
|
|
191
|
-
if (events) {
|
|
192
|
-
Object.keys(events).forEach((key) => {
|
|
193
|
-
Object.assign(events, {
|
|
194
|
-
[key]: deepmerge(events[key], { at: events[key].at ? new Date(events[key].at) : null })
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
Object.assign(item, { events });
|
|
199
|
-
return item;
|
|
200
|
-
};
|
|
201
|
-
// TODO: Maybe just simplify this and require that everything is a UUID?
|
|
202
|
-
/**
|
|
203
|
-
* This method might be an annoyance, but we need to capture a few cases where someone passes
|
|
204
|
-
* a PK parameter that has an odd string in it.
|
|
205
|
-
*
|
|
206
|
-
* @param pkParamValue The value of the primary key parameter
|
|
207
|
-
* @returns if the value is valid.
|
|
208
|
-
*/
|
|
209
|
-
validatePKParam = (pkParamValue) => {
|
|
210
|
-
let validPkParam = true;
|
|
211
|
-
if (pkParamValue.length <= 0) {
|
|
212
|
-
this.logger.error('Primary Key is an Empty String', { pkParamValue });
|
|
213
|
-
validPkParam = false;
|
|
214
|
-
}
|
|
215
|
-
else if (pkParamValue === 'undefined') {
|
|
216
|
-
this.logger.error('Primary Key is the string \'undefined\'', { pkParamValue });
|
|
217
|
-
validPkParam = false;
|
|
218
|
-
}
|
|
219
|
-
return validPkParam;
|
|
220
|
-
};
|
|
221
|
-
}
|
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,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;iBACxF,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;QACD,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;QAChC,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,wEAAwE;IACxE;;;;;;OAMG;IACO,eAAe,GAAG,CAAC,YAAoB,EAAW,EAAE;QAC5D,IAAI,YAAY,GAAG,IAAI,CAAC;QACxB,IAAI,YAAY,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;YAC7B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,gCAAgC,EAAE,EAAE,YAAY,EAAE,CAAC,CAAC;YACtE,YAAY,GAAG,KAAK,CAAC;QACvB,CAAC;aAAM,IAAI,YAAY,KAAK,WAAW,EAAE,CAAC;YACxC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,yCAAyC,EAAE,EAAE,YAAY,EAAE,CAAC,CAAC;YAC/E,YAAY,GAAG,KAAK,CAAC;QACvB,CAAC;QACD,OAAO,YAAY,CAAC;IACtB,CAAC,CAAA;CAEF"}
|
package/dist/src/PItemRouter.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { paramsToQuery, validatePK } from "@fjell/core";
|
|
2
|
-
import { ItemRouter } from "./ItemRouter";
|
|
3
|
-
import LibLogger from "./logger";
|
|
4
|
-
const logger = LibLogger.get('PItemRouter');
|
|
5
|
-
export class PItemRouter extends ItemRouter {
|
|
6
|
-
constructor(lib, keyType, options = {}) {
|
|
7
|
-
super(lib, keyType, options);
|
|
8
|
-
}
|
|
9
|
-
getIk(res) {
|
|
10
|
-
const pri = this.getPk(res);
|
|
11
|
-
return pri;
|
|
12
|
-
}
|
|
13
|
-
createItem = async (req, res) => {
|
|
14
|
-
logger.default('Creating Item 2', { body: req.body, query: req.query, params: req.params, locals: res.locals });
|
|
15
|
-
const itemToCreate = this.convertDates(req.body);
|
|
16
|
-
let item = validatePK(await this.lib.create(itemToCreate), this.getPkType());
|
|
17
|
-
item = await this.postCreateItem(item);
|
|
18
|
-
return res.json(item);
|
|
19
|
-
};
|
|
20
|
-
/* eslint-disable */
|
|
21
|
-
findItems = async (req, res) => {
|
|
22
|
-
logger.default('Finding Items', { query: req.query, params: req.params, locals: res.locals });
|
|
23
|
-
let items = [];
|
|
24
|
-
const query = req.query;
|
|
25
|
-
const finder = query['finder'];
|
|
26
|
-
const finderParams = query['finderParams'];
|
|
27
|
-
if (finder) {
|
|
28
|
-
// If finder is defined? Call a finder.
|
|
29
|
-
logger.default('Finding Items with a finder', { finder, finderParams });
|
|
30
|
-
items = await this.lib.find(finder, JSON.parse(finderParams));
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
logger.default('Finding Items with a query', { query: req.query });
|
|
34
|
-
// TODO: This is once of the more important places to perform some validaation and feedback
|
|
35
|
-
const itemQuery = paramsToQuery(req.query);
|
|
36
|
-
items = await this.lib.all(itemQuery);
|
|
37
|
-
}
|
|
38
|
-
return res.json(items.map((item) => validatePK(item, this.getPkType())));
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
//# sourceMappingURL=PItemRouter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"PItemRouter.js","sourceRoot":"","sources":["../../src/PItemRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,aAAa,EAAuB,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9F,OAAO,EAAE,UAAU,EAAqB,MAAM,cAAc,CAAC;AAE7D,OAAO,SAAS,MAAM,UAAU,CAAC;AAEjC,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;AAM5C,MAAM,OAAO,WA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MAAM,GAAG,KAAK,CAAC,QAAQ,CAAW,CAAC;QACzC,MAAM,YAAY,GAAG,KAAK,CAAC,cAAc,CAAW,CAAC;QAErD,IAAI,MAAM,EAAG,CAAC;YACZ,wCAAwC;YACxC,MAAM,CAAC,OAAO,CAAC,6BAA6B,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC,CAAC;YACxE,KAAK,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;QAChE,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,OAAO,CAAC,4BAA4B,EAAE,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC;YACnE,2FAA2F;YAC3F,MAAM,SAAS,GAAc,aAAa,CAAC,GAAG,CAAC,KAAoB,CAAC,CAAC;YACrE,KAAK,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACxC,CAAC;QAED,OAAO,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAa,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC;IACpF,CAAC,CAAC;CAEH"}
|
package/dist/src/index.d.ts
DELETED
package/dist/src/index.js
DELETED
package/dist/src/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC"}
|
package/dist/src/logger.d.ts
DELETED
package/dist/src/logger.js
DELETED
package/dist/src/logger.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,gBAAgB,CAAC;AAErC,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,uBAAuB,CAAC,CAAC;AAE7D,eAAe,SAAS,CAAC"}
|
package/eslint.config.mjs
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import typescriptEslint from "@typescript-eslint/eslint-plugin";
|
|
2
|
-
import tsParser from "@typescript-eslint/parser";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
5
|
-
import js from "@eslint/js";
|
|
6
|
-
import { FlatCompat } from "@eslint/eslintrc";
|
|
7
|
-
|
|
8
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
-
const __dirname = path.dirname(__filename);
|
|
10
|
-
const compat = new FlatCompat({
|
|
11
|
-
baseDirectory: __dirname,
|
|
12
|
-
recommendedConfig: js.configs.recommended,
|
|
13
|
-
allConfig: js.configs.all
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
export default [{
|
|
17
|
-
ignores: ["**/dist", "**/node_modules"],
|
|
18
|
-
}, ...compat.extends("plugin:@typescript-eslint/recommended"), {
|
|
19
|
-
plugins: {
|
|
20
|
-
"@typescript-eslint": typescriptEslint,
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
languageOptions: {
|
|
24
|
-
parser: tsParser,
|
|
25
|
-
},
|
|
26
|
-
|
|
27
|
-
rules: {
|
|
28
|
-
"@typescript-eslint/no-unused-expressions": "off",
|
|
29
|
-
"no-console": 0,
|
|
30
|
-
"no-unused-vars": "off",
|
|
31
|
-
|
|
32
|
-
"max-len": ["error", {
|
|
33
|
-
code: 120,
|
|
34
|
-
}],
|
|
35
|
-
|
|
36
|
-
"max-depth": ["error", 4],
|
|
37
|
-
"max-params": ["error", 4],
|
|
38
|
-
"max-lines": ["error", 500],
|
|
39
|
-
|
|
40
|
-
"no-multiple-empty-lines": ["error", {
|
|
41
|
-
max: 1,
|
|
42
|
-
maxEOF: 1,
|
|
43
|
-
}],
|
|
44
|
-
|
|
45
|
-
"no-trailing-spaces": ["error", {
|
|
46
|
-
skipBlankLines: true,
|
|
47
|
-
}],
|
|
48
|
-
|
|
49
|
-
indent: ["error", 2, {
|
|
50
|
-
SwitchCase: 1,
|
|
51
|
-
}],
|
|
52
|
-
|
|
53
|
-
"sort-imports": ["error", {
|
|
54
|
-
ignoreCase: true,
|
|
55
|
-
ignoreDeclarationSort: true,
|
|
56
|
-
ignoreMemberSort: false,
|
|
57
|
-
memberSyntaxSortOrder: ["none", "all", "multiple", "single"],
|
|
58
|
-
}],
|
|
59
|
-
|
|
60
|
-
"no-var": "error",
|
|
61
|
-
"no-undefined": "error",
|
|
62
|
-
"@typescript-eslint/no-unused-vars": "error",
|
|
63
|
-
"@typescript-eslint/ban-ts-comment": "off",
|
|
64
|
-
"@typescript-eslint/no-explicit-any": "off",
|
|
65
|
-
|
|
66
|
-
"no-restricted-imports": ["error", {
|
|
67
|
-
patterns: ["..*", "src/*"],
|
|
68
|
-
}],
|
|
69
|
-
},
|
|
70
|
-
}];
|