@fjell/express-router 4.4.2 → 4.4.3
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/CItemRouter.cjs.map +1 -1
- package/dist/CItemRouter.js.map +1 -1
- package/dist/ItemRouter.cjs +39 -41
- package/dist/ItemRouter.cjs.map +1 -1
- package/dist/ItemRouter.d.ts +1 -9
- package/dist/ItemRouter.js +39 -41
- package/dist/ItemRouter.js.map +1 -1
- package/dist/PItemRouter.cjs.map +1 -1
- package/dist/PItemRouter.js.map +1 -1
- package/dist/index.cjs +39 -41
- package/dist/index.cjs.map +1 -1
- package/dist/logger.cjs.map +1 -1
- package/dist/logger.js.map +1 -1
- package/package.json +2 -2
- package/src/CItemRouter.ts +1 -1
- package/src/ItemRouter.ts +32 -71
- package/src/PItemRouter.ts +1 -1
package/src/ItemRouter.ts
CHANGED
|
@@ -11,36 +11,11 @@ import {
|
|
|
11
11
|
} from "@fjell/core";
|
|
12
12
|
import { NotFoundError, Operations } from "@fjell/lib";
|
|
13
13
|
import deepmerge from "deepmerge";
|
|
14
|
-
import { Request,
|
|
14
|
+
import { Request, Response, Router } from "express";
|
|
15
15
|
import LibLogger from "./logger";
|
|
16
16
|
|
|
17
17
|
export type ItemRouterOptions = Record<string, never>;
|
|
18
18
|
|
|
19
|
-
// TODO: body is in the request, it's not needed in the parameters
|
|
20
|
-
export type ActionMethod = <
|
|
21
|
-
S extends string,
|
|
22
|
-
L1 extends string = never,
|
|
23
|
-
L2 extends string = never,
|
|
24
|
-
L3 extends string = never,
|
|
25
|
-
L4 extends string = never,
|
|
26
|
-
L5 extends string = never
|
|
27
|
-
>(req: Request, res: Response, item: Item<S, L1, L2, L3, L4, L5>, params: any, body: any) =>
|
|
28
|
-
Promise<Item<S, L1, L2, L3, L4, L5>>;
|
|
29
|
-
|
|
30
|
-
// TODO: body is in the request, it's not needed in the parameters
|
|
31
|
-
export type AllActionMethods = Array<RequestHandler>;
|
|
32
|
-
|
|
33
|
-
// TODO: body is in the request, it's not needed in the parameters
|
|
34
|
-
export type FacetMethod = <
|
|
35
|
-
S extends string,
|
|
36
|
-
L1 extends string = never,
|
|
37
|
-
L2 extends string = never,
|
|
38
|
-
L3 extends string = never,
|
|
39
|
-
L4 extends string = never,
|
|
40
|
-
L5 extends string = never
|
|
41
|
-
>(req: Request, res: Response, item: Item<S, L1, L2, L3, L4, L5>, params: any) =>
|
|
42
|
-
Promise<any>;
|
|
43
|
-
|
|
44
19
|
export class ItemRouter<
|
|
45
20
|
S extends string,
|
|
46
21
|
L1 extends string = never,
|
|
@@ -55,8 +30,6 @@ export class ItemRouter<
|
|
|
55
30
|
protected options: ItemRouterOptions;
|
|
56
31
|
private childRouters: Record<string, Router> = {};
|
|
57
32
|
private logger;
|
|
58
|
-
private itemActions: Record<string, ActionMethod> | undefined;
|
|
59
|
-
private itemFacets: Record<string, FacetMethod> | undefined;
|
|
60
33
|
|
|
61
34
|
constructor(
|
|
62
35
|
lib: Operations<Item<S, L1, L2, L3, L4, L5>, S, L1, L2, L3, L4, L5>,
|
|
@@ -106,15 +79,19 @@ export class ItemRouter<
|
|
|
106
79
|
this.logger.default('Getting Item', { query: req?.query, params: req?.params, locals: res?.locals });
|
|
107
80
|
const ik = this.getIk(res);
|
|
108
81
|
const actionKey = req.path.substring(req.path.lastIndexOf('/') + 1);
|
|
109
|
-
if (!this.
|
|
82
|
+
if (!this.lib.actions) {
|
|
110
83
|
this.logger.error('Item Actions are not configured');
|
|
111
84
|
res.status(500).json({ error: 'Item Actions are not configured' });
|
|
112
85
|
return;
|
|
113
86
|
}
|
|
87
|
+
const action = this.lib.actions[actionKey];
|
|
88
|
+
if (!action) {
|
|
89
|
+
this.logger.error('Item Action is not configured', { actionKey });
|
|
90
|
+
res.status(500).json({ error: 'Item Action is not configured' });
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
114
93
|
try {
|
|
115
|
-
|
|
116
|
-
validatePK(await this.lib.get(ik), this.getPkType()) as Item<S, L1, L2, L3, L4, L5>;
|
|
117
|
-
res.json(await this.itemActions[actionKey](req, res, item, req.params, req.body));
|
|
94
|
+
res.json(await this.lib.action(ik, actionKey, req.body));
|
|
118
95
|
} catch (err: any) {
|
|
119
96
|
this.logger.error('Error in Item Action', { message: err?.message, stack: err?.stack });
|
|
120
97
|
res.status(500).json(err);
|
|
@@ -125,15 +102,19 @@ export class ItemRouter<
|
|
|
125
102
|
this.logger.default('Getting Item', { query: req?.query, params: req?.params, locals: res?.locals });
|
|
126
103
|
const ik = this.getIk(res);
|
|
127
104
|
const facetKey = req.path.substring(req.path.lastIndexOf('/') + 1);
|
|
128
|
-
if (!this.
|
|
105
|
+
if (!this.lib.facets) {
|
|
129
106
|
this.logger.error('Item Facets are not configured');
|
|
130
107
|
res.status(500).json({ error: 'Item Facets are not configured' });
|
|
131
108
|
return;
|
|
132
109
|
}
|
|
110
|
+
const facet = this.lib.facets[facetKey];
|
|
111
|
+
if (!facet) {
|
|
112
|
+
this.logger.error('Item Facet is not configured', { facetKey });
|
|
113
|
+
res.status(500).json({ error: 'Item Facet is not configured' });
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
133
116
|
try {
|
|
134
|
-
|
|
135
|
-
validatePK(await this.lib.get(ik), this.getPkType()) as Item<S, L1, L2, L3, L4, L5>;
|
|
136
|
-
await this.itemFacets[facetKey](req, res, item, req.params);
|
|
117
|
+
res.json(await this.lib.facet(ik, facetKey, req.params));
|
|
137
118
|
} catch (err: any) {
|
|
138
119
|
this.logger.error('Error in Item Facet', { message: err?.message, stack: err?.stack });
|
|
139
120
|
res.status(500).json(err);
|
|
@@ -145,35 +126,33 @@ export class ItemRouter<
|
|
|
145
126
|
router.get('/', this.findItems);
|
|
146
127
|
router.post('/', this.createItem);
|
|
147
128
|
|
|
148
|
-
const allActions = this.configureAllActions();
|
|
149
|
-
this.logger.debug('All Actions supplied to Router', { allActions });
|
|
150
|
-
if (allActions) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
129
|
+
// const allActions = this.configureAllActions();
|
|
130
|
+
// this.logger.debug('All Actions supplied to Router', { allActions });
|
|
131
|
+
// if (allActions) {
|
|
132
|
+
// Object.keys(allActions).forEach((actionKey) => {
|
|
133
|
+
// this.logger.default('Configuring All Action', { actionKey });
|
|
134
|
+
// // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
|
|
135
|
+
// router.post(`/${actionKey}`, ...allActions[actionKey]);
|
|
136
|
+
// });
|
|
137
|
+
// }
|
|
157
138
|
|
|
158
139
|
const itemRouter = Router();
|
|
159
140
|
itemRouter.get('/', this.getItem);
|
|
160
141
|
itemRouter.put('/', this.updateItem);
|
|
161
142
|
itemRouter.delete('/', this.deleteItem);
|
|
162
143
|
|
|
163
|
-
this.itemActions
|
|
164
|
-
this.
|
|
165
|
-
|
|
166
|
-
Object.keys(this.itemActions).forEach((actionKey) => {
|
|
144
|
+
this.logger.debug('Item Actions supplied to Router', { itemActions: this.lib.actions });
|
|
145
|
+
if (this.lib.actions) {
|
|
146
|
+
Object.keys(this.lib.actions).forEach((actionKey) => {
|
|
167
147
|
this.logger.default('Configuring Item Action', { actionKey });
|
|
168
148
|
// TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
|
|
169
149
|
itemRouter.post(`/${actionKey}`, this.postItemAction)
|
|
170
150
|
});
|
|
171
151
|
}
|
|
172
152
|
|
|
173
|
-
this.itemFacets
|
|
174
|
-
this.
|
|
175
|
-
|
|
176
|
-
Object.keys(this.itemFacets).forEach((facetKey) => {
|
|
153
|
+
this.logger.debug('Item Facets supplied to Router', { itemFacets: this.lib.facets });
|
|
154
|
+
if (this.lib.facets) {
|
|
155
|
+
Object.keys(this.lib.facets).forEach((facetKey) => {
|
|
177
156
|
this.logger.default('Configuring Item Facet', { facetKey });
|
|
178
157
|
// TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers
|
|
179
158
|
itemRouter.get(`/${facetKey}`, this.getItemFacet)
|
|
@@ -213,24 +192,6 @@ export class ItemRouter<
|
|
|
213
192
|
this.childRouters[path] = router;
|
|
214
193
|
}
|
|
215
194
|
|
|
216
|
-
/* istanbul ignore next */
|
|
217
|
-
protected configureItemActions(): Record<string, ActionMethod> {
|
|
218
|
-
this.logger.debug('ARouter - No Item Actions Configured');
|
|
219
|
-
return {};
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/* istanbul ignore next */
|
|
223
|
-
protected configureItemFacets(): Record<string, FacetMethod> {
|
|
224
|
-
this.logger.debug('ARouter - No Item Facets Configured');
|
|
225
|
-
return {};
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/* istanbul ignore next */
|
|
229
|
-
protected configureAllActions(): Record<string, AllActionMethods> {
|
|
230
|
-
this.logger.debug('ARouter - No All Actions Configured');
|
|
231
|
-
return {};
|
|
232
|
-
}
|
|
233
|
-
|
|
234
195
|
/* istanbul ignore next */
|
|
235
196
|
public getRouter(): Router {
|
|
236
197
|
const router = Router();
|
package/src/PItemRouter.ts
CHANGED
|
@@ -13,7 +13,7 @@ interface ParsedQuery {
|
|
|
13
13
|
export class PItemRouter<T extends Item<S>, S extends string> extends ItemRouter<S> {
|
|
14
14
|
|
|
15
15
|
constructor(lib: Primary.Operations<T, S>, keyType: S, options: ItemRouterOptions = {}) {
|
|
16
|
-
super(lib, keyType, options);
|
|
16
|
+
super(lib as any, keyType, options);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
public getIk(res: Response): PriKey<S> {
|