@fjell/express-router 4.4.23 → 4.4.24
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 +205 -0
- package/dist/CItemRouter.d.ts +1 -1
- package/dist/CItemRouter.d.ts.map +1 -1
- package/dist/CItemRouter.js +23 -8
- package/dist/CItemRouter.js.map +2 -2
- package/dist/Instance.d.ts.map +1 -1
- package/dist/Instance.js.map +2 -2
- package/dist/InstanceFactory.d.ts.map +1 -1
- package/dist/InstanceFactory.js.map +2 -2
- package/dist/ItemRouter.d.ts +12 -3
- package/dist/ItemRouter.d.ts.map +1 -1
- package/dist/ItemRouter.js +91 -15
- package/dist/ItemRouter.js.map +2 -2
- package/dist/PItemRouter.d.ts +1 -1
- package/dist/PItemRouter.d.ts.map +1 -1
- package/dist/PItemRouter.js.map +2 -2
- package/dist/util/general.js.map +1 -1
- package/examples/README.md +47 -0
- package/examples/basic-router-example.ts +7 -6
- package/examples/full-application-example.ts +137 -12
- package/examples/nested-router-example.ts +154 -54
- package/examples/router-handlers-example.ts +370 -0
- package/package.json +2 -1
package/dist/ItemRouter.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/ItemRouter.ts"],
|
|
4
|
-
"sourcesContent": ["import {\n ComKey,\n cPK,\n Item,\n ItemEvent,\n LocKey,\n LocKeyArray,\n PriKey,\n validatePK\n} from \"@fjell/core\";\nimport { NotFoundError } from \"@fjell/lib\";\nimport { Instance } from \"./Instance.js\";\nimport deepmerge from \"deepmerge\";\nimport { Request, Response, Router } from \"express\";\nimport LibLogger from \"./logger.js\";\n\nexport type ItemRouterOptions = Record<string, never>;\n\nexport class ItemRouter<\n S extends string,\n L1 extends string = never,\n L2 extends string = never,\n L3 extends string = never,\n L4 extends string = never,\n L5 extends string = never\n> {\n\n protected lib: Instance<Item<S, L1, L2, L3, L4, L5>, S, L1, L2, L3, L4, L5>;\n private keyType: S;\n protected options: ItemRouterOptions;\n private childRouters: Record<string, Router> = {};\n protected logger;\n\n constructor(\n lib: Instance<Item<S, L1, L2, L3, L4, L5>, S, L1, L2, L3, L4, L5>,\n keyType: S,\n options: ItemRouterOptions = {}\n ) {\n this.lib = lib;\n this.keyType = keyType;\n this.options = options;\n this.logger = LibLogger.get(\"ItemRouter\", keyType);\n }\n\n public getPkType = (): S => {\n return this.keyType;\n }\n\n protected getPkParam = (): string => {\n return `${this.getPkType()}Pk`;\n }\n\n protected getLk(res: Response): LocKey<S> {\n return { kt: this.keyType, lk: res.locals[this.getPkParam()] };\n }\n\n // this is meant to be consumed by children routers\n public getLKA(res: Response): LocKeyArray<S, L1, L2, L3, L4> {\n return [this.getLk(res)] as LocKeyArray<S, L1, L2, L3, L4>;\n }\n\n public getPk(res: Response): PriKey<S> {\n return cPK<S>(res.locals[this.getPkParam()], this.getPkType());\n }\n\n // Unless this is a contained router, the locations will always be an empty array.\n /* eslint-disable */\n protected getLocations(res: Response): LocKeyArray<L1, L2, L3, L4, L5> | [] {\n throw new Error('Method not implemented in an abstract router');\n }\n /* eslint-enable */\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected getIk(res: Response): PriKey<S> | ComKey<S, L1, L2, L3, L4, L5> {\n throw new Error('Method not implemented in an abstract router');\n }\n\n protected postAllAction = async (req: Request, res: Response) => {\n const libOptions = this.lib.options;\n const libOperations = this.lib.operations;\n this.logger.debug('Posting All Action', { query: req?.query, params: req?.params, locals: res?.locals });\n const allActionKey = req.path.substring(req.path.lastIndexOf('/') + 1);\n if (!libOptions.allActions) {\n this.logger.error('Item Actions are not configured');\n res.status(500).json({ error: 'Item Actions are not configured' });\n return;\n }\n const allAction = libOptions.allActions[allActionKey];\n if (!allAction) {\n this.logger.error('All Action is not configured', { allActionKey });\n res.status(500).json({ error: 'Item Action is not configured' });\n return;\n }\n try {\n res.json(await libOperations.allAction(allActionKey, req.body));\n } catch (err: any) {\n this.logger.error('Error in All Action', { message: err?.message, stack: err?.stack });\n res.status(500).json(err);\n }\n }\n\n protected getAllFacet = async (req: Request, res: Response) => {\n const libOptions = this.lib.options;\n const libOperations = this.lib.operations;\n this.logger.debug('Getting All Facet', { query: req?.query, params: req?.params, locals: res?.locals });\n const facetKey = req.path.substring(req.path.lastIndexOf('/') + 1);\n if (!libOptions.allFacets) {\n this.logger.error('Item Facets are not configured');\n res.status(500).json({ error: 'Item Facets are not configured' });\n return;\n }\n const facet = libOptions.allFacets[facetKey];\n if (!facet) {\n this.logger.error('Item Facet is not configured', { facetKey });\n res.status(500).json({ error: 'Item Facet is not configured' });\n return;\n }\n try {\n const combinedQueryParams = { ...req.query, ...req.params } as Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>;\n res.json(await libOperations.allFacet(facetKey, combinedQueryParams));\n } catch (err: any) {\n this.logger.error('Error in All Facet', { message: err?.message, stack: err?.stack });\n res.status(500).json(err);\n }\n }\n\n protected postItemAction = async (req: Request, res: Response) => {\n const libOptions = this.lib.options;\n const libOperations = this.lib.operations;\n this.logger.debug('Getting Item', { query: req?.query, params: req?.params, locals: res?.locals });\n const ik = this.getIk(res);\n const actionKey = req.path.substring(req.path.lastIndexOf('/') + 1);\n if (!libOptions.actions) {\n this.logger.error('Item Actions are not configured');\n res.status(500).json({ error: 'Item Actions are not configured' });\n return;\n }\n const action = libOptions.actions[actionKey];\n if (!action) {\n this.logger.error('Item Action is not configured', { actionKey });\n res.status(500).json({ error: 'Item Action is not configured' });\n return;\n }\n try {\n res.json(await libOperations.action(ik, actionKey, req.body));\n } catch (err: any) {\n this.logger.error('Error in Item Action', { message: err?.message, stack: err?.stack });\n res.status(500).json(err);\n }\n }\n\n protected getItemFacet = async (req: Request, res: Response) => {\n const libOptions = this.lib.options;\n const libOperations = this.lib.operations;\n this.logger.debug('Getting Item', { query: req?.query, params: req?.params, locals: res?.locals });\n const ik = this.getIk(res);\n const facetKey = req.path.substring(req.path.lastIndexOf('/') + 1);\n if (!libOptions.facets) {\n this.logger.error('Item Facets are not configured');\n res.status(500).json({ error: 'Item Facets are not configured' });\n return;\n }\n const facet = libOptions.facets[facetKey];\n if (!facet) {\n this.logger.error('Item Facet is not configured', { facetKey });\n res.status(500).json({ error: 'Item Facet is not configured' });\n return;\n }\n try {\n const combinedQueryParams = { ...req.query, ...req.params } as Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>;\n res.json(await libOperations.facet(ik, facetKey, combinedQueryParams));\n } catch (err: any) {\n this.logger.error('Error in Item Facet', { message: err?.message, stack: err?.stack });\n res.status(500).json(err);\n }\n }\n\n private configure = (router: Router) => {\n const libOptions = this.lib.options;\n this.logger.debug('Configuring Router', { pkType: this.getPkType() });\n router.get('/', this.findItems);\n router.post('/', this.createItem);\n\n this.logger.default('All Actions supplied to Router', { allActions: libOptions.allActions });\n if (libOptions.allActions) {\n Object.keys(libOptions.allActions).forEach((actionKey) => {\n this.logger.debug('Configuring All Action %s', actionKey);\n // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers\n router.post(`/${actionKey}`, this.postAllAction);\n });\n }\n\n this.logger.default('All Facets supplied to Router', { allFacets: libOptions.allFacets });\n if (libOptions.allFacets) {\n Object.keys(libOptions.allFacets).forEach((facetKey) => {\n this.logger.debug('Configuring All Facet %s', facetKey);\n // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers\n router.get(`/${facetKey}`, this.getAllFacet);\n });\n }\n\n const itemRouter = Router();\n itemRouter.get('/', this.getItem);\n itemRouter.put('/', this.updateItem);\n itemRouter.delete('/', this.deleteItem);\n\n this.logger.default('Item Actions supplied to Router', { itemActions: libOptions.actions });\n if (libOptions.actions) {\n Object.keys(libOptions.actions).forEach((actionKey) => {\n this.logger.debug('Configuring Item Action %s', actionKey);\n // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers\n itemRouter.post(`/${actionKey}`, this.postItemAction)\n });\n }\n\n this.logger.default('Item Facets supplied to Router', { itemFacets: libOptions.facets });\n if (libOptions.facets) {\n Object.keys(libOptions.facets).forEach((facetKey) => {\n this.logger.debug('Configuring Item Facet %s', facetKey);\n // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers\n itemRouter.get(`/${facetKey}`, this.getItemFacet)\n });\n }\n\n this.logger.debug('Configuring Item Operations under PK Param %s', this.getPkParam());\n router.use(`/:${this.getPkParam()}`, this.validatePrimaryKeyValue, itemRouter);\n\n if (this.childRouters) {\n this.configureChildRouters(itemRouter, this.childRouters);\n }\n return router;\n }\n\n private validatePrimaryKeyValue = (req: Request, res: Response, next: any) => {\n const pkParamValue = req.params[this.getPkParam()];\n if (this.validatePKParam(pkParamValue)) {\n res.locals[this.getPkParam()] = pkParamValue;\n next();\n } else {\n this.logger.error('Invalid Primary Key', { pkParamValue, path: req?.originalUrl });\n res.status(500).json({ error: 'Invalid Primary Key', path: req?.originalUrl });\n }\n }\n\n private configureChildRouters = (router: Router, childRouters: Record<string, Router>) => {\n for (const path in childRouters) {\n this.logger.debug('Configuring Child Router at Path %s', path);\n\n router.use(`/${path}`, childRouters[path]);\n }\n return router;\n }\n\n public addChildRouter = (path: string, router: Router) => {\n this.childRouters[path] = router;\n }\n\n /* istanbul ignore next */\n public getRouter(): Router {\n const router = Router();\n this.configure(router);\n return router;\n }\n\n /* istanbul ignore next */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected createItem = async (req: Request, res: Response): Promise<void> => {\n throw new Error('Method not implemented in an abstract router');\n };\n\n // TODO: Probably a better way to do this, but this postCreate hook only needs the item.\n /* istanbul ignore next */\n public postCreateItem = async (item: Item<S, L1, L2, L3, L4, L5>): Promise<Item<S, L1, L2, L3, L4, L5>> => {\n this.logger.debug('Post Create Item', { item });\n return item;\n };\n\n protected deleteItem = async (req: Request, res: Response): Promise<void> => {\n const libOperations = this.lib.operations;\n\n this.logger.debug('Deleting Item', { query: req.query, params: req.params, locals: res.locals });\n const ik = this.getIk(res);\n const removedItem = await libOperations.remove(ik);\n const item = validatePK(removedItem, this.getPkType());\n res.json(item);\n };\n\n /* eslint-disable */\n /* istanbul ignore next */\n protected findItems = async (req: Request, res: Response): Promise<void> => {\n throw new Error('Method not implemented in an abstract router');\n };\n /* eslint-enable */\n\n protected getItem = async (req: Request, res: Response) => {\n const libOperations = this.lib.operations;\n this.logger.debug('Getting Item', { query: req.query, params: req.params, locals: res.locals });\n const ik = this.getIk(res);\n try {\n // TODO: What error does validate PK throw, when can that fail?\n const item = validatePK(await libOperations.get(ik), this.getPkType());\n res.json(item);\n } catch (err: any) {\n if (err instanceof NotFoundError) {\n this.logger.error('Item Not Found', { ik, message: err?.message, stack: err?.stack });\n res.status(404).json({\n ik,\n message: \"Item Not Found\",\n });\n } else {\n this.logger.error('General Error', { ik, message: err?.message, stack: err?.stack });\n res.status(500).json({\n ik,\n message: \"General Error\",\n });\n }\n }\n }\n\n protected updateItem = async (req: Request, res: Response) => {\n const libOperations = this.lib.operations;\n this.logger.debug('Updating Item',\n { body: req?.body, query: req?.query, params: req?.params, locals: res?.locals });\n const ik = this.getIk(res);\n const itemToUpdate = this.convertDates(req.body as Partial<Item<S, L1, L2, L3, L4, L5>>);\n const retItem = validatePK(await libOperations.update(ik, itemToUpdate), this.getPkType());\n res.json(retItem);\n };\n\n public convertDates = (item: Partial<Item<S, L1, L2, L3, L4, L5>>): Partial<Item<S, L1, L2, L3, L4, L5>> => {\n const events = item.events as Record<string, ItemEvent>;\n this.logger.debug('Converting Dates', { item });\n if (events) {\n Object.keys(events).forEach((key: string) => {\n Object.assign(events, {\n [key]: deepmerge(events[key], { at: events[key].at ? new Date(events[key].at) : null })\n });\n });\n }\n Object.assign(item, { events });\n return item;\n };\n\n // TODO: Maybe just simplify this and require that everything is a UUID?\n /**\n * This method might be an annoyance, but we need to capture a few cases where someone passes\n * a PK parameter that has an odd string in it.\n *\n * @param pkParamValue The value of the primary key parameter\n * @returns if the value is valid.\n */\n protected validatePKParam = (pkParamValue: string): boolean => {\n let validPkParam = true;\n if (pkParamValue.length <= 0) {\n this.logger.error('Primary Key is an Empty String', { pkParamValue });\n validPkParam = false;\n } else if (pkParamValue === 'undefined') {\n this.logger.error('Primary Key is the string \\'undefined\\'', { pkParamValue });\n validPkParam = false;\n }\n return validPkParam;\n }\n\n}\n"],
|
|
5
|
-
"mappings": "AAAA;AAAA,EAEE;AAAA,EAMA;AAAA,OACK;AACP,SAAS,qBAAqB;AAE9B,OAAO,eAAe;AACtB,SAA4B,cAAc;AAC1C,OAAO,eAAe;AAIf,MAAM,WAOX;AAAA,EAEU;AAAA,EACF;AAAA,EACE;AAAA,EACF,eAAuC,CAAC;AAAA,EACtC;AAAA,EAEV,YACE,KACA,SACA,UAA6B,CAAC,GAC9B;AACA,SAAK,MAAM;AACX,SAAK,UAAU;AACf,SAAK,UAAU;AACf,SAAK,SAAS,UAAU,IAAI,cAAc,OAAO;AAAA,EACnD;AAAA,EAEO,YAAY,MAAS;AAC1B,WAAO,KAAK;AAAA,EACd;AAAA,EAEU,aAAa,MAAc;AACnC,WAAO,GAAG,KAAK,UAAU,CAAC;AAAA,EAC5B;AAAA,EAEU,MAAM,KAA0B;AACxC,WAAO,EAAE,IAAI,KAAK,SAAS,IAAI,IAAI,OAAO,KAAK,WAAW,CAAC,EAAE;AAAA,EAC/D;AAAA;AAAA,EAGO,OAAO,KAA+C;AAC3D,WAAO,CAAC,KAAK,MAAM,GAAG,CAAC;AAAA,EACzB;AAAA,EAEO,MAAM,KAA0B;AACrC,WAAO,IAAO,IAAI,OAAO,KAAK,WAAW,CAAC,GAAG,KAAK,UAAU,CAAC;AAAA,EAC/D;AAAA;AAAA;AAAA,EAIU,aAAa,KAAqD;AAC1E,UAAM,IAAI,MAAM,8CAA8C;AAAA,EAChE;AAAA;AAAA;AAAA,EAIU,MAAM,KAA0D;AACxE,UAAM,IAAI,MAAM,8CAA8C;AAAA,EAChE;AAAA,EAEU,gBAAgB,OAAO,KAAc,QAAkB;AAC/D,UAAM,aAAa,KAAK,IAAI;AAC5B,UAAM,gBAAgB,KAAK,I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/lib\";\nimport { Instance } from \"./Instance.js\";\nimport deepmerge from \"deepmerge\";\nimport { Request, Response, Router } from \"express\";\nimport LibLogger from \"./logger.js\";\n\nexport type ItemRouterOptions<\n S extends string = string,\n L1 extends string = never,\n L2 extends string = never,\n L3 extends string = never,\n L4 extends string = never,\n L5 extends string = never\n> = {\n /** Handlers for item actions */\n actions?: Record<string, (req: Request, res: Response, ik: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>) => Promise<void>>;\n /** Handlers for item facets */\n facets?: Record<string, (req: Request, res: Response, ik: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>) => Promise<void>>;\n /** Handlers for all actions */\n allActions?: Record<string, (req: Request, res: Response) => Promise<void>>;\n /** Handlers for all facets */\n allFacets?: Record<string, (req: Request, res: Response) => Promise<void>>;\n};\n\nexport class ItemRouter<\n S extends string,\n L1 extends string = never,\n L2 extends string = never,\n L3 extends string = never,\n L4 extends string = never,\n L5 extends string = never\n> {\n\n protected lib: Instance<Item<S, L1, L2, L3, L4, L5>, S, L1, L2, L3, L4, L5>;\n private keyType: S;\n protected options: ItemRouterOptions<S, L1, L2, L3, L4, L5>;\n private childRouters: Record<string, Router> = {};\n protected logger;\n\n constructor(\n lib: Instance<Item<S, L1, L2, L3, L4, L5>, S, L1, L2, L3, L4, L5>,\n keyType: S,\n options: ItemRouterOptions<S, L1, L2, L3, L4, L5> = {}\n ) {\n this.lib = lib;\n this.keyType = keyType;\n this.options = options;\n this.logger = LibLogger.get(\"ItemRouter\", keyType);\n }\n\n public getPkType = (): S => {\n return this.keyType;\n }\n\n protected getPkParam = (): string => {\n return `${this.getPkType()}Pk`;\n }\n\n protected getLk(res: Response): LocKey<S> {\n return { kt: this.keyType, lk: res.locals[this.getPkParam()] };\n }\n\n // this is meant to be consumed by children routers\n public getLKA(res: Response): LocKeyArray<S, L1, L2, L3, L4> {\n return [this.getLk(res)] as LocKeyArray<S, L1, L2, L3, L4>;\n }\n\n public getPk(res: Response): PriKey<S> {\n return cPK<S>(res.locals[this.getPkParam()], this.getPkType());\n }\n\n // Unless this is a contained router, the locations will always be an empty array.\n /* eslint-disable */\n protected getLocations(res: Response): LocKeyArray<L1, L2, L3, L4, L5> | [] {\n throw new Error('Method not implemented in an abstract router');\n }\n /* eslint-enable */\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected getIk(res: Response): PriKey<S> | ComKey<S, L1, L2, L3, L4, L5> {\n throw new Error('Method not implemented in an abstract router');\n }\n\n protected postAllAction = async (req: Request, res: Response) => {\n const libOptions = this.lib.options;\n const libOperations = this.lib.operations;\n this.logger.debug('Posting All Action', { query: req?.query, params: req?.params, locals: res?.locals });\n const allActionKey = req.path.substring(req.path.lastIndexOf('/') + 1);\n\n // Check for router-level handler first\n if (this.options.allActions && this.options.allActions[allActionKey]) {\n this.logger.debug('Using router-level all action handler', { allActionKey });\n try {\n await this.options.allActions[allActionKey](req, res);\n return;\n } catch (err: any) {\n this.logger.error('Error in router-level all action', { message: err?.message, stack: err?.stack });\n res.status(500).json(err);\n return;\n }\n }\n\n // Fallback to library handler\n if (!libOptions.allActions) {\n this.logger.error('All Actions are not configured');\n res.status(500).json({ error: 'All Actions are not configured' });\n return;\n }\n const allAction = libOptions.allActions[allActionKey];\n if (!allAction) {\n this.logger.error('All Action is not configured', { allActionKey });\n res.status(500).json({ error: 'All Action is not configured' });\n return;\n }\n try {\n res.json(await libOperations.allAction(allActionKey, req.body));\n } catch (err: any) {\n this.logger.error('Error in All Action', { message: err?.message, stack: err?.stack });\n res.status(500).json(err);\n }\n }\n\n protected getAllFacet = async (req: Request, res: Response) => {\n const libOptions = this.lib.options;\n const libOperations = this.lib.operations;\n this.logger.debug('Getting All Facet', { query: req?.query, params: req?.params, locals: res?.locals });\n const facetKey = req.path.substring(req.path.lastIndexOf('/') + 1);\n\n // Check for router-level handler first\n if (this.options.allFacets && this.options.allFacets[facetKey]) {\n this.logger.debug('Using router-level all facet handler', { facetKey });\n try {\n await this.options.allFacets[facetKey](req, res);\n return;\n } catch (err: any) {\n this.logger.error('Error in router-level all facet', { message: err?.message, stack: err?.stack });\n res.status(500).json(err);\n return;\n }\n }\n\n // Fallback to library handler\n if (!libOptions.allFacets) {\n this.logger.error('All Facets are not configured');\n res.status(500).json({ error: 'All Facets are not configured' });\n return;\n }\n const facet = libOptions.allFacets[facetKey];\n if (!facet) {\n this.logger.error('All Facet is not configured', { facetKey });\n res.status(500).json({ error: 'All Facet is not configured' });\n return;\n }\n try {\n const combinedQueryParams = { ...req.query, ...req.params } as Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>;\n res.json(await libOperations.allFacet(facetKey, combinedQueryParams));\n } catch (err: any) {\n this.logger.error('Error in All Facet', { message: err?.message, stack: err?.stack });\n res.status(500).json(err);\n }\n }\n\n protected postItemAction = async (req: Request, res: Response) => {\n const libOptions = this.lib.options;\n const libOperations = this.lib.operations;\n this.logger.debug('Posting Item Action', { query: req?.query, params: req?.params, locals: res?.locals });\n const ik = this.getIk(res);\n const actionKey = req.path.substring(req.path.lastIndexOf('/') + 1);\n\n // Check for router-level handler first\n if (this.options.actions && this.options.actions[actionKey]) {\n this.logger.debug('Using router-level action handler', { actionKey });\n try {\n await this.options.actions[actionKey](req, res, ik);\n return;\n } catch (err: any) {\n this.logger.error('Error in router-level action', { message: err?.message, stack: err?.stack });\n res.status(500).json(err);\n return;\n }\n }\n\n // Fallback to library handler\n if (!libOptions.actions) {\n this.logger.error('Item Actions are not configured');\n res.status(500).json({ error: 'Item Actions are not configured' });\n return;\n }\n const action = libOptions.actions[actionKey];\n if (!action) {\n this.logger.error('Item Action is not configured', { actionKey });\n res.status(500).json({ error: 'Item Action is not configured' });\n return;\n }\n try {\n res.json(await libOperations.action(ik, actionKey, req.body));\n } catch (err: any) {\n this.logger.error('Error in Item Action', { message: err?.message, stack: err?.stack });\n res.status(500).json(err);\n }\n }\n\n protected getItemFacet = async (req: Request, res: Response) => {\n const libOptions = this.lib.options;\n const libOperations = this.lib.operations;\n this.logger.debug('Getting Item Facet', { query: req?.query, params: req?.params, locals: res?.locals });\n const ik = this.getIk(res);\n const facetKey = req.path.substring(req.path.lastIndexOf('/') + 1);\n\n // Check for router-level handler first\n if (this.options.facets && this.options.facets[facetKey]) {\n this.logger.debug('Using router-level facet handler', { facetKey });\n try {\n await this.options.facets[facetKey](req, res, ik);\n return;\n } catch (err: any) {\n this.logger.error('Error in router-level facet', { message: err?.message, stack: err?.stack });\n res.status(500).json(err);\n return;\n }\n }\n\n // Fallback to library handler\n if (!libOptions.facets) {\n this.logger.error('Item Facets are not configured');\n res.status(500).json({ error: 'Item Facets are not configured' });\n return;\n }\n const facet = libOptions.facets[facetKey];\n if (!facet) {\n this.logger.error('Item Facet is not configured', { facetKey });\n res.status(500).json({ error: 'Item Facet is not configured' });\n return;\n }\n try {\n const combinedQueryParams = { ...req.query, ...req.params } as Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>;\n res.json(await libOperations.facet(ik, facetKey, combinedQueryParams));\n } catch (err: any) {\n this.logger.error('Error in Item Facet', { message: err?.message, stack: err?.stack });\n res.status(500).json(err);\n }\n }\n\n private configure = (router: Router) => {\n const libOptions = this.lib.options;\n this.logger.debug('Configuring Router', { pkType: this.getPkType() });\n router.get('/', this.findItems);\n router.post('/', this.createItem);\n\n this.logger.default('All Actions supplied to Router', { allActions: libOptions.allActions });\n if (libOptions.allActions) {\n Object.keys(libOptions.allActions).forEach((actionKey) => {\n this.logger.debug('Configuring All Action %s', actionKey);\n // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers\n router.post(`/${actionKey}`, this.postAllAction);\n });\n }\n\n this.logger.default('All Facets supplied to Router', { allFacets: libOptions.allFacets });\n if (libOptions.allFacets) {\n Object.keys(libOptions.allFacets).forEach((facetKey) => {\n this.logger.debug('Configuring All Facet %s', facetKey);\n // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers\n router.get(`/${facetKey}`, this.getAllFacet);\n });\n }\n\n const itemRouter = Router();\n itemRouter.get('/', this.getItem);\n itemRouter.put('/', this.updateItem);\n itemRouter.delete('/', this.deleteItem);\n\n this.logger.default('Item Actions supplied to Router', { itemActions: libOptions.actions });\n if (libOptions.actions) {\n Object.keys(libOptions.actions).forEach((actionKey) => {\n this.logger.debug('Configuring Item Action %s', actionKey);\n // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers\n itemRouter.post(`/${actionKey}`, this.postItemAction)\n });\n }\n\n this.logger.default('Item Facets supplied to Router', { itemFacets: libOptions.facets });\n if (libOptions.facets) {\n Object.keys(libOptions.facets).forEach((facetKey) => {\n this.logger.debug('Configuring Item Facet %s', facetKey);\n // TODO: Ok, this is a bit of a hack, but we need to customize the types of the request handlers\n itemRouter.get(`/${facetKey}`, this.getItemFacet)\n });\n }\n\n this.logger.debug('Configuring Item Operations under PK Param %s', this.getPkParam());\n router.use(`/:${this.getPkParam()}`, this.validatePrimaryKeyValue, itemRouter);\n\n if (this.childRouters) {\n this.configureChildRouters(itemRouter, this.childRouters);\n }\n return router;\n }\n\n private validatePrimaryKeyValue = (req: Request, res: Response, next: any) => {\n const pkParamValue = req.params[this.getPkParam()];\n if (this.validatePKParam(pkParamValue)) {\n res.locals[this.getPkParam()] = pkParamValue;\n next();\n } else {\n this.logger.error('Invalid Primary Key', { pkParamValue, path: req?.originalUrl });\n res.status(500).json({ error: 'Invalid Primary Key', path: req?.originalUrl });\n }\n }\n\n private configureChildRouters = (router: Router, childRouters: Record<string, Router>) => {\n for (const path in childRouters) {\n this.logger.debug('Configuring Child Router at Path %s', path);\n\n router.use(`/${path}`, childRouters[path]);\n }\n return router;\n }\n\n public addChildRouter = (path: string, router: Router) => {\n this.childRouters[path] = router;\n }\n\n /* istanbul ignore next */\n public getRouter(): Router {\n const router = Router();\n this.configure(router);\n return router;\n }\n\n /* istanbul ignore next */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected createItem = async (req: Request, res: Response): Promise<void> => {\n throw new Error('Method not implemented in an abstract router');\n };\n\n // TODO: Probably a better way to do this, but this postCreate hook only needs the item.\n /* istanbul ignore next */\n public postCreateItem = async (item: Item<S, L1, L2, L3, L4, L5>): Promise<Item<S, L1, L2, L3, L4, L5>> => {\n this.logger.debug('Post Create Item', { item });\n return item;\n };\n\n protected deleteItem = async (req: Request, res: Response): Promise<void> => {\n const libOperations = this.lib.operations;\n\n this.logger.debug('Deleting Item', { query: req.query, params: req.params, locals: res.locals });\n const ik = this.getIk(res);\n try {\n const removedItem = await libOperations.remove(ik);\n const item = validatePK(removedItem, this.getPkType());\n res.json(item);\n } catch (err: any) {\n if (err instanceof NotFoundError) {\n this.logger.error('Item Not Found for Delete', { ik, message: err?.message, stack: err?.stack });\n res.status(404).json({\n ik,\n message: \"Item Not Found\",\n });\n } else {\n this.logger.error('General Error in Delete', { ik, message: err?.message, stack: err?.stack });\n res.status(500).json({\n ik,\n message: \"General Error\",\n });\n }\n }\n };\n\n /* eslint-disable */\n /* istanbul ignore next */\n protected findItems = async (req: Request, res: Response): Promise<void> => {\n throw new Error('Method not implemented in an abstract router');\n };\n /* eslint-enable */\n\n protected getItem = async (req: Request, res: Response) => {\n const libOperations = this.lib.operations;\n this.logger.debug('Getting Item', { query: req.query, params: req.params, locals: res.locals });\n const ik = this.getIk(res);\n try {\n // TODO: What error does validate PK throw, when can that fail?\n const item = validatePK(await libOperations.get(ik), this.getPkType());\n res.json(item);\n } catch (err: any) {\n if (err instanceof NotFoundError) {\n this.logger.error('Item Not Found', { ik, message: err?.message, stack: err?.stack });\n res.status(404).json({\n ik,\n message: \"Item Not Found\",\n });\n } else {\n this.logger.error('General Error', { ik, message: err?.message, stack: err?.stack });\n res.status(500).json({\n ik,\n message: \"General Error\",\n });\n }\n }\n }\n\n protected updateItem = async (req: Request, res: Response) => {\n const libOperations = this.lib.operations;\n this.logger.debug('Updating Item',\n { body: req?.body, query: req?.query, params: req?.params, locals: res?.locals });\n const ik = this.getIk(res);\n try {\n const itemToUpdate = this.convertDates(req.body as Partial<Item<S, L1, L2, L3, L4, L5>>);\n const retItem = validatePK(await libOperations.update(ik, itemToUpdate), this.getPkType());\n res.json(retItem);\n } catch (err: any) {\n if (err instanceof NotFoundError) {\n this.logger.error('Item Not Found for Update', { ik, message: err?.message, stack: err?.stack });\n res.status(404).json({\n ik,\n message: \"Item Not Found\",\n });\n } else {\n this.logger.error('General Error in Update', { ik, message: err?.message, stack: err?.stack });\n res.status(500).json({\n ik,\n message: \"General Error\",\n });\n }\n }\n };\n\n public convertDates = (item: Partial<Item<S, L1, L2, L3, L4, L5>>): Partial<Item<S, L1, L2, L3, L4, L5>> => {\n const events = item.events as Record<string, ItemEvent>;\n this.logger.debug('Converting Dates', { item });\n if (events) {\n Object.keys(events).forEach((key: string) => {\n Object.assign(events, {\n [key]: deepmerge(events[key], { at: events[key].at ? new Date(events[key].at) : null })\n });\n });\n }\n Object.assign(item, { events });\n return item;\n };\n\n // TODO: Maybe just simplify this and require that everything is a UUID?\n /**\n * This method might be an annoyance, but we need to capture a few cases where someone passes\n * a PK parameter that has an odd string in it.\n *\n * @param pkParamValue The value of the primary key parameter\n * @returns if the value is valid.\n */\n protected validatePKParam = (pkParamValue: string): boolean => {\n let validPkParam = true;\n if (pkParamValue.length <= 0) {\n this.logger.error('Primary Key is an Empty String', { pkParamValue });\n validPkParam = false;\n } else if (pkParamValue === 'undefined') {\n this.logger.error('Primary Key is the string \\'undefined\\'', { pkParamValue });\n validPkParam = false;\n }\n return validPkParam;\n }\n\n}\n"],
|
|
5
|
+
"mappings": "AAAA;AAAA,EAEE;AAAA,EAMA;AAAA,OACK;AACP,SAAS,qBAAqB;AAE9B,OAAO,eAAe;AACtB,SAA4B,cAAc;AAC1C,OAAO,eAAe;AAoBf,MAAM,WAOX;AAAA,EAEU;AAAA,EACF;AAAA,EACE;AAAA,EACF,eAAuC,CAAC;AAAA,EACtC;AAAA,EAEV,YACE,KACA,SACA,UAAoD,CAAC,GACrD;AACA,SAAK,MAAM;AACX,SAAK,UAAU;AACf,SAAK,UAAU;AACf,S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OAAO,GAAG,EAAE,EAAE,IAAI,KAAK,CAAC;AAAA,QACxF,CAAC;AAAA,MACH,CAAC;AAAA,IACH;AACA,WAAO,OAAO,MAAM,EAAE,OAAO,CAAC;AAC9B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUU,kBAAkB,CAAC,iBAAkC;AAC7D,QAAI,eAAe;AACnB,QAAI,aAAa,UAAU,GAAG;AAC5B,WAAK,OAAO,MAAM,kCAAkC,EAAE,aAAa,CAAC;AACpE,qBAAe;AAAA,IACjB,WAAW,iBAAiB,aAAa;AACvC,WAAK,OAAO,MAAM,yCAA2C,EAAE,aAAa,CAAC;AAC7E,qBAAe;AAAA,IACjB;AACA,WAAO;AAAA,EACT;AAEF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/PItemRouter.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { ItemRouter, ItemRouterOptions } from "./ItemRouter.js";
|
|
|
3
3
|
import { Instance } from "./Instance.js";
|
|
4
4
|
import { Request, Response } from "express";
|
|
5
5
|
export declare class PItemRouter<T extends Item<S>, S extends string> extends ItemRouter<S> {
|
|
6
|
-
constructor(lib: Instance<T, S>, keyType: S, options?: ItemRouterOptions);
|
|
6
|
+
constructor(lib: Instance<T, S>, keyType: S, options?: ItemRouterOptions<S, never, never, never, never, never>);
|
|
7
7
|
getIk(res: Response): PriKey<S>;
|
|
8
8
|
createItem: (req: Request, res: Response) => Promise<void>;
|
|
9
9
|
protected findItems: (req: Request, res: Response) => Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PItemRouter.d.ts","sourceRoot":"","sources":["../src/PItemRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAA4B,MAAM,EAA2B,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,QAA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"version": 3,
|
|
3
3
|
"sources": ["../src/PItemRouter.ts"],
|
|
4
|
-
"sourcesContent": ["import { Item, ItemQuery, paramsToQuery, PriKey, QueryParams, validatePK } from \"@fjell/core\";\nimport { ItemRouter, ItemRouterOptions } from \"./ItemRouter.js\";\nimport { Instance } from \"./Instance.js\";\nimport { Request, Response } from \"express\";\n\ninterface ParsedQuery {\n [key: string]: undefined | string | string[] | ParsedQuery | ParsedQuery[];\n}\n\nexport class PItemRouter<T extends Item<S>, S extends string> extends ItemRouter<S> {\n\n constructor(lib: Instance<T, S>, keyType: S, options: ItemRouterOptions = {}) {\n super(lib as any, keyType, options);\n }\n\n public getIk(res: Response): PriKey<S> {\n const pri = this.getPk(res) as PriKey<S>;\n return pri\n }\n\n public createItem = async (req: Request, res: Response) => {\n const libOperations = this.lib.operations;\n this.logger.default('Creating Item', { body: req.body, query: req.query, params: req.params, locals: res.locals });\n const itemToCreate = this.convertDates(req.body as Item<S>);\n let item = validatePK(await libOperations.create(itemToCreate), this.getPkType()) as Item<S>;\n item = await this.postCreateItem(item);\n this.logger.default('Created Item %j', item);\n res.json(item);\n };\n\n protected findItems = async (req: Request, res: Response) => {\n const libOperations = this.lib.operations;\n this.logger.default('Finding Items', { query: req.query, params: req.params, locals: res.locals });\n\n let items: Item<S>[] = [];\n\n const query: ParsedQuery = req.query as unknown as ParsedQuery;\n const finder = query['finder'] as string;\n const finderParams = query['finderParams'] as string;\n const one = query['one'] as string;\n\n if (finder) {\n // If finder is defined? Call a finder.\n this.logger.default('Finding Items with Finder %s %j one:%s', finder, finderParams, one);\n\n if (one === 'true') {\n const item = await (this.lib as any).findOne(finder, JSON.parse(finderParams));\n items = item ? [item] : [];\n } else {\n items = await libOperations.find(finder, JSON.parse(finderParams));\n }\n } else {\n // TODO: This is once of the more important places to perform some validaation and feedback\n const itemQuery: ItemQuery = paramsToQuery(req.query as QueryParams);\n this.logger.default('Finding Items with a query %j', itemQuery);\n items = await libOperations.all(itemQuery);\n }\n\n res.json(items.map((item: Item<S>) => validatePK(item, this.getPkType())));\n };\n\n}\n"],
|
|
5
|
-
"mappings": "AAAA,SAA0B,eAAoC,kBAAkB;AAChF,SAAS,kBAAqC;AAQvC,MAAM,oBAAyD,WAAc;AAAA,EAElF,YAAY,KAAqB,SAAY,
|
|
4
|
+
"sourcesContent": ["import { Item, ItemQuery, paramsToQuery, PriKey, QueryParams, validatePK } from \"@fjell/core\";\nimport { ItemRouter, ItemRouterOptions } from \"./ItemRouter.js\";\nimport { Instance } from \"./Instance.js\";\nimport { Request, Response } from \"express\";\n\ninterface ParsedQuery {\n [key: string]: undefined | string | string[] | ParsedQuery | ParsedQuery[];\n}\n\nexport class PItemRouter<T extends Item<S>, S extends string> extends ItemRouter<S> {\n\n constructor(lib: Instance<T, S>, keyType: S, options: ItemRouterOptions<S, never, never, never, never, never> = {}) {\n super(lib as any, keyType, options);\n }\n\n public getIk(res: Response): PriKey<S> {\n const pri = this.getPk(res) as PriKey<S>;\n return pri\n }\n\n public createItem = async (req: Request, res: Response) => {\n const libOperations = this.lib.operations;\n this.logger.default('Creating Item', { body: req.body, query: req.query, params: req.params, locals: res.locals });\n const itemToCreate = this.convertDates(req.body as Item<S>);\n let item = validatePK(await libOperations.create(itemToCreate), this.getPkType()) as Item<S>;\n item = await this.postCreateItem(item);\n this.logger.default('Created Item %j', item);\n res.json(item);\n };\n\n protected findItems = async (req: Request, res: Response) => {\n const libOperations = this.lib.operations;\n this.logger.default('Finding Items', { query: req.query, params: req.params, locals: res.locals });\n\n let items: Item<S>[] = [];\n\n const query: ParsedQuery = req.query as unknown as ParsedQuery;\n const finder = query['finder'] as string;\n const finderParams = query['finderParams'] as string;\n const one = query['one'] as string;\n\n if (finder) {\n // If finder is defined? Call a finder.\n this.logger.default('Finding Items with Finder %s %j one:%s', finder, finderParams, one);\n\n if (one === 'true') {\n const item = await (this.lib as any).findOne(finder, JSON.parse(finderParams));\n items = item ? [item] : [];\n } else {\n items = await libOperations.find(finder, JSON.parse(finderParams));\n }\n } else {\n // TODO: This is once of the more important places to perform some validaation and feedback\n const itemQuery: ItemQuery = paramsToQuery(req.query as QueryParams);\n this.logger.default('Finding Items with a query %j', itemQuery);\n items = await libOperations.all(itemQuery);\n }\n\n res.json(items.map((item: Item<S>) => validatePK(item, this.getPkType())));\n };\n\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAA0B,eAAoC,kBAAkB;AAChF,SAAS,kBAAqC;AAQvC,MAAM,oBAAyD,WAAc;AAAA,EAElF,YAAY,KAAqB,SAAY,UAAmE,CAAC,GAAG;AAClH,UAAM,KAAY,SAAS,OAAO;AAAA,EACpC;AAAA,EAEO,MAAM,KAA0B;AACrC,UAAM,MAAM,KAAK,MAAM,GAAG;AAC1B,WAAO;AAAA,EACT;AAAA,EAEO,aAAa,OAAO,KAAc,QAAkB;AACzD,UAAM,gBAAgB,KAAK,IAAI;AAC/B,SAAK,OAAO,QAAQ,iBAAiB,EAAE,MAAM,IAAI,MAAM,OAAO,IAAI,OAAO,QAAQ,IAAI,QAAQ,QAAQ,IAAI,OAAO,CAAC;AACjH,UAAM,eAAe,KAAK,aAAa,IAAI,IAAe;AAC1D,QAAI,OAAO,WAAW,MAAM,cAAc,OAAO,YAAY,GAAG,KAAK,UAAU,CAAC;AAChF,WAAO,MAAM,KAAK,eAAe,IAAI;AACrC,SAAK,OAAO,QAAQ,mBAAmB,IAAI;AAC3C,QAAI,KAAK,IAAI;AAAA,EACf;AAAA,EAEU,YAAY,OAAO,KAAc,QAAkB;AAC3D,UAAM,gBAAgB,KAAK,IAAI;AAC/B,SAAK,OAAO,QAAQ,iBAAiB,EAAE,OAAO,IAAI,OAAO,QAAQ,IAAI,QAAQ,QAAQ,IAAI,OAAO,CAAC;AAEjG,QAAI,QAAmB,CAAC;AAExB,UAAM,QAAqB,IAAI;AAC/B,UAAM,SAAS,MAAM,QAAQ;AAC7B,UAAM,eAAe,MAAM,cAAc;AACzC,UAAM,MAAM,MAAM,KAAK;AAEvB,QAAI,QAAQ;AAEV,WAAK,OAAO,QAAQ,0CAA0C,QAAQ,cAAc,GAAG;AAEvF,UAAI,QAAQ,QAAQ;AAClB,cAAM,OAAO,MAAO,KAAK,IAAY,QAAQ,QAAQ,KAAK,MAAM,YAAY,CAAC;AAC7E,gBAAQ,OAAO,CAAC,IAAI,IAAI,CAAC;AAAA,MAC3B,OAAO;AACL,gBAAQ,MAAM,cAAc,KAAK,QAAQ,KAAK,MAAM,YAAY,CAAC;AAAA,MACnE;AAAA,IACF,OAAO;AAEL,YAAM,YAAuB,cAAc,IAAI,KAAoB;AACnE,WAAK,OAAO,QAAQ,iCAAiC,SAAS;AAC9D,cAAQ,MAAM,cAAc,IAAI,SAAS;AAAA,IAC3C;AAEA,QAAI,KAAK,MAAM,IAAI,CAAC,SAAkB,WAAW,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC;AAAA,EAC3E;AAEF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/util/general.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/util/general.ts"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-unused-vars */\
|
|
4
|
+
"sourcesContent": ["\n/* eslint-disable no-undefined, @typescript-eslint/no-unused-vars */\nexport const clean = (obj: any) => {\n return Object.fromEntries(\n Object.entries(obj).filter(([_, v]) => v !== undefined)\n );\n}\n\n//Recursive implementation of jSON.stringify;\nexport const stringifyJSON = function (obj: any, visited: Set<any> = new Set()): string {\n const arrOfKeyVals: string[] = [];\n const arrVals: string[] = [];\n let objKeys: string[] = [];\n\n /*********CHECK FOR PRIMITIVE TYPES**********/\n if (typeof obj === 'number' || typeof obj === 'boolean' || obj === null)\n return '' + obj;\n else if (typeof obj === 'string')\n return '\"' + obj + '\"';\n\n /*********DETECT CIRCULAR REFERENCES**********/\n if (obj instanceof Object && visited.has(obj)) {\n return '\"(circular)\"';\n }\n\n /*********CHECK FOR ARRAY**********/\n else if (Array.isArray(obj)) {\n //check for empty array\n if (obj[0] === undefined)\n return '[]';\n else {\n // Add array to visited before processing its elements\n visited.add(obj);\n obj.forEach(function (el) {\n arrVals.push(stringifyJSON(el, visited));\n });\n return '[' + arrVals + ']';\n }\n }\n /*********CHECK FOR OBJECT**********/\n else if (obj instanceof Object) {\n // Add object to visited before processing its properties\n visited.add(obj);\n //get object keys\n objKeys = Object.keys(obj);\n //set key output;\n objKeys.forEach(function (key) {\n const keyOut = '\"' + key + '\":';\n const keyValOut = obj[key];\n //skip functions and undefined properties\n if (keyValOut instanceof Function || keyValOut === undefined)\n return; // Skip this entry entirely instead of pushing an empty string\n else if (typeof keyValOut === 'string')\n arrOfKeyVals.push(keyOut + '\"' + keyValOut + '\"');\n else if (typeof keyValOut === 'boolean' || typeof keyValOut === 'number' || keyValOut === null)\n arrOfKeyVals.push(keyOut + keyValOut);\n //check for nested objects, call recursively until no more objects\n else if (keyValOut instanceof Object) {\n arrOfKeyVals.push(keyOut + stringifyJSON(keyValOut, visited));\n }\n });\n return '{' + arrOfKeyVals + '}';\n }\n return '';\n};\n"],
|
|
5
5
|
"mappings": "AAEO,MAAM,QAAQ,CAAC,QAAa;AACjC,SAAO,OAAO;AAAA,IACZ,OAAO,QAAQ,GAAG,EAAE,OAAO,CAAC,CAAC,GAAG,CAAC,MAAM,MAAM,MAAS;AAAA,EACxD;AACF;AAGO,MAAM,gBAAgB,SAAU,KAAU,UAAoB,oBAAI,IAAI,GAAW;AACtF,QAAM,eAAyB,CAAC;AAChC,QAAM,UAAoB,CAAC;AAC3B,MAAI,UAAoB,CAAC;AAGzB,MAAI,OAAO,QAAQ,YAAY,OAAO,QAAQ,aAAa,QAAQ;AACjE,WAAO,KAAK;AAAA,WACL,OAAO,QAAQ;AACtB,WAAO,MAAM,MAAM;AAGrB,MAAI,eAAe,UAAU,QAAQ,IAAI,GAAG,GAAG;AAC7C,WAAO;AAAA,EACT,WAGS,MAAM,QAAQ,GAAG,GAAG;AAE3B,QAAI,IAAI,CAAC,MAAM;AACb,aAAO;AAAA,SACJ;AAEH,cAAQ,IAAI,GAAG;AACf,UAAI,QAAQ,SAAU,IAAI;AACxB,gBAAQ,KAAK,cAAc,IAAI,OAAO,CAAC;AAAA,MACzC,CAAC;AACD,aAAO,MAAM,UAAU;AAAA,IACzB;AAAA,EACF,WAES,eAAe,QAAQ;AAE9B,YAAQ,IAAI,GAAG;AAEf,cAAU,OAAO,KAAK,GAAG;AAEzB,YAAQ,QAAQ,SAAU,KAAK;AAC7B,YAAM,SAAS,MAAM,MAAM;AAC3B,YAAM,YAAY,IAAI,GAAG;AAEzB,UAAI,qBAAqB,YAAY,cAAc;AACjD;AAAA,eACO,OAAO,cAAc;AAC5B,qBAAa,KAAK,SAAS,MAAM,YAAY,GAAG;AAAA,eACzC,OAAO,cAAc,aAAa,OAAO,cAAc,YAAY,cAAc;AACxF,qBAAa,KAAK,SAAS,SAAS;AAAA,eAE7B,qBAAqB,QAAQ;AACpC,qBAAa,KAAK,SAAS,cAAc,WAAW,OAAO,CAAC;AAAA,MAC9D;AAAA,IACF,CAAC;AACD,WAAO,MAAM,eAAe;AAAA,EAC9B;AACA,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/examples/README.md
CHANGED
|
@@ -34,6 +34,16 @@ Shows how fjell-express-router handles enterprise organizational data patterns w
|
|
|
34
34
|
|
|
35
35
|
Perfect for understanding how to build production-ready applications with fjell-express-router.
|
|
36
36
|
|
|
37
|
+
### 4. `router-handlers-example.ts` 🎯 **Router-Level Handler Precedence**
|
|
38
|
+
**Advanced router customization!** Demonstrates how to define custom handlers at the router level that take precedence over library-level handlers:
|
|
39
|
+
- **Router-level handlers**: Define custom actions, facets, allActions, and allFacets directly in router options
|
|
40
|
+
- **Handler precedence**: Router-level handlers are called first, with fallback to library handlers
|
|
41
|
+
- **Cross-system integration**: Perfect for aggregating data from multiple services or external APIs
|
|
42
|
+
- **Business logic separation**: Keep complex integrations at the router layer while maintaining library simplicity
|
|
43
|
+
- **Flexible architecture**: Supports both PItemRouter and CItemRouter with appropriate type safety
|
|
44
|
+
|
|
45
|
+
Ideal for scenarios requiring external service calls, data aggregation, or complex business logic that shouldn't be in the core library layer.
|
|
46
|
+
|
|
37
47
|
## Key Concepts Demonstrated
|
|
38
48
|
|
|
39
49
|
### Basic Router Setup (basic-router-example.ts)
|
|
@@ -170,6 +180,12 @@ npx tsx examples/full-application-example.ts
|
|
|
170
180
|
# Server runs on http://localhost:3003
|
|
171
181
|
```
|
|
172
182
|
|
|
183
|
+
**Router Handlers Example:**
|
|
184
|
+
```bash
|
|
185
|
+
npx tsx examples/router-handlers-example.ts
|
|
186
|
+
# Server runs on http://localhost:3004
|
|
187
|
+
```
|
|
188
|
+
|
|
173
189
|
## Testing the Examples
|
|
174
190
|
|
|
175
191
|
### Basic Router Example (Port 3001)
|
|
@@ -258,6 +274,37 @@ curl -X POST http://localhost:3003/api/customers \
|
|
|
258
274
|
}'
|
|
259
275
|
```
|
|
260
276
|
|
|
277
|
+
### Router Handlers Example (Port 3004)
|
|
278
|
+
```bash
|
|
279
|
+
# Test router-level user actions
|
|
280
|
+
curl -X POST http://localhost:3004/api/users/user-1/activate
|
|
281
|
+
curl -X POST http://localhost:3004/api/users/user-1/deactivate
|
|
282
|
+
|
|
283
|
+
# Test router-level user facets
|
|
284
|
+
curl http://localhost:3004/api/users/user-1/profile
|
|
285
|
+
curl http://localhost:3004/api/users/user-1/activity
|
|
286
|
+
|
|
287
|
+
# Test router-level all actions
|
|
288
|
+
curl -X POST http://localhost:3004/api/users/bulk-activate
|
|
289
|
+
curl -X POST http://localhost:3004/api/users/send-notifications
|
|
290
|
+
|
|
291
|
+
# Test router-level all facets
|
|
292
|
+
curl http://localhost:3004/api/users/statistics
|
|
293
|
+
curl http://localhost:3004/api/users/analytics
|
|
294
|
+
|
|
295
|
+
# Test router-level post actions (nested under users)
|
|
296
|
+
curl -X POST http://localhost:3004/api/users/user-1/posts/post-1/publish
|
|
297
|
+
curl -X POST http://localhost:3004/api/users/user-1/posts/post-1/archive
|
|
298
|
+
|
|
299
|
+
# Test router-level post facets
|
|
300
|
+
curl http://localhost:3004/api/users/user-1/posts/post-1/analytics
|
|
301
|
+
curl http://localhost:3004/api/users/user-1/posts/post-1/comments
|
|
302
|
+
|
|
303
|
+
# Test fallback to library handlers for undefined router handlers
|
|
304
|
+
curl -X POST http://localhost:3004/api/users/user-1/someOtherAction
|
|
305
|
+
curl http://localhost:3004/api/users/user-1/someOtherFacet
|
|
306
|
+
```
|
|
307
|
+
|
|
261
308
|
## Integration Patterns
|
|
262
309
|
|
|
263
310
|
### Mock Operations
|
|
@@ -12,11 +12,12 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import { Item, PriKey, UUID } from '@fjell/core';
|
|
15
|
+
import { NotFoundError } from '@fjell/lib';
|
|
15
16
|
import express, { Application } from 'express';
|
|
16
17
|
import { createRegistry, PItemRouter } from '../src';
|
|
17
18
|
|
|
18
19
|
// Define our data models
|
|
19
|
-
interface User extends Item<'user'> {
|
|
20
|
+
export interface User extends Item<'user'> {
|
|
20
21
|
id: string;
|
|
21
22
|
name: string;
|
|
22
23
|
email: string;
|
|
@@ -24,7 +25,7 @@ interface User extends Item<'user'> {
|
|
|
24
25
|
lastLogin?: Date;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
|
-
interface Task extends Item<'task'> {
|
|
28
|
+
export interface Task extends Item<'task'> {
|
|
28
29
|
id: string;
|
|
29
30
|
title: string;
|
|
30
31
|
description: string;
|
|
@@ -122,7 +123,7 @@ const createUserOperations = () => {
|
|
|
122
123
|
console.log(`🔍 UserOperations.get(${key.pk}) - Fetching user...`);
|
|
123
124
|
const user = mockUserStorage.get(String(key.pk));
|
|
124
125
|
if (!user) {
|
|
125
|
-
throw new
|
|
126
|
+
throw new NotFoundError('get', { kta: ['user', '', '', '', '', ''], scopes: [] }, key);
|
|
126
127
|
}
|
|
127
128
|
return user;
|
|
128
129
|
},
|
|
@@ -148,7 +149,7 @@ const createUserOperations = () => {
|
|
|
148
149
|
console.log(`🔄 UserOperations.update(${key.pk}) - Updating user...`);
|
|
149
150
|
const existing = mockUserStorage.get(String(key.pk));
|
|
150
151
|
if (!existing) {
|
|
151
|
-
throw new
|
|
152
|
+
throw new NotFoundError('update', { kta: ['user', '', '', '', '', ''], scopes: [] }, key);
|
|
152
153
|
}
|
|
153
154
|
const updated: User = {
|
|
154
155
|
...existing,
|
|
@@ -199,7 +200,7 @@ const createTaskOperations = () => {
|
|
|
199
200
|
console.log(`🔍 TaskOperations.get(${key.pk}) - Fetching task...`);
|
|
200
201
|
const task = mockTaskStorage.get(String(key.pk));
|
|
201
202
|
if (!task) {
|
|
202
|
-
throw new
|
|
203
|
+
throw new NotFoundError('get', { kta: ['task', '', '', '', '', ''], scopes: [] }, key);
|
|
203
204
|
}
|
|
204
205
|
return task;
|
|
205
206
|
},
|
|
@@ -225,7 +226,7 @@ const createTaskOperations = () => {
|
|
|
225
226
|
console.log(`🔄 TaskOperations.update(${key.pk}) - Updating task...`);
|
|
226
227
|
const existing = mockTaskStorage.get(String(key.pk));
|
|
227
228
|
if (!existing) {
|
|
228
|
-
throw new
|
|
229
|
+
throw new NotFoundError('update', { kta: ['task', '', '', '', '', ''], scopes: [] }, key);
|
|
229
230
|
}
|
|
230
231
|
const updated: Task = {
|
|
231
232
|
...existing,
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import express, { Application, NextFunction, Request, Response } from 'express';
|
|
15
|
-
import { ComKey, Item, PriKey, UUID } from '@fjell/core';
|
|
15
|
+
import { ComKey, Item, LocKey, PriKey, UUID } from '@fjell/core';
|
|
16
16
|
import { CItemRouter, createRegistry, PItemRouter } from '../src';
|
|
17
17
|
|
|
18
18
|
// ===== Data Models =====
|
|
@@ -280,12 +280,23 @@ const createCustomerOperations = () => ({
|
|
|
280
280
|
async update(key: PriKey<'customer'>, updates: Partial<Customer>) {
|
|
281
281
|
const existing = mockCustomerStorage.get(String(key.pk));
|
|
282
282
|
if (!existing) throw new Error(`Customer not found: ${key.pk}`);
|
|
283
|
-
const updated = {
|
|
283
|
+
const updated = {
|
|
284
|
+
...existing,
|
|
285
|
+
...updates,
|
|
286
|
+
key: existing.key, // Preserve the key
|
|
287
|
+
events: {
|
|
288
|
+
...existing.events,
|
|
289
|
+
updated: { at: new Date() }
|
|
290
|
+
}
|
|
291
|
+
};
|
|
284
292
|
mockCustomerStorage.set(String(key.pk), updated);
|
|
285
293
|
return updated;
|
|
286
294
|
},
|
|
287
295
|
async remove(key: PriKey<'customer'>) {
|
|
288
|
-
|
|
296
|
+
const customer = mockCustomerStorage.get(String(key.pk));
|
|
297
|
+
if (!customer) throw new Error(`Customer not found: ${key.pk}`);
|
|
298
|
+
mockCustomerStorage.delete(String(key.pk));
|
|
299
|
+
return customer;
|
|
289
300
|
},
|
|
290
301
|
async find(finder: string, params: any) {
|
|
291
302
|
const customers = Array.from(mockCustomerStorage.values());
|
|
@@ -318,12 +329,23 @@ const createProductOperations = () => ({
|
|
|
318
329
|
async update(key: PriKey<'product'>, updates: Partial<Product>) {
|
|
319
330
|
const existing = mockProductStorage.get(String(key.pk));
|
|
320
331
|
if (!existing) throw new Error(`Product not found: ${key.pk}`);
|
|
321
|
-
const updated = {
|
|
332
|
+
const updated = {
|
|
333
|
+
...existing,
|
|
334
|
+
...updates,
|
|
335
|
+
key: existing.key, // Preserve the key
|
|
336
|
+
events: {
|
|
337
|
+
...existing.events,
|
|
338
|
+
updated: { at: new Date() }
|
|
339
|
+
}
|
|
340
|
+
};
|
|
322
341
|
mockProductStorage.set(String(key.pk), updated);
|
|
323
342
|
return updated;
|
|
324
343
|
},
|
|
325
344
|
async remove(key: PriKey<'product'>) {
|
|
326
|
-
|
|
345
|
+
const product = mockProductStorage.get(String(key.pk));
|
|
346
|
+
if (!product) throw new Error(`Product not found: ${key.pk}`);
|
|
347
|
+
mockProductStorage.delete(String(key.pk));
|
|
348
|
+
return product;
|
|
327
349
|
},
|
|
328
350
|
async find(finder: string, params: any) {
|
|
329
351
|
const products = Array.from(mockProductStorage.values());
|
|
@@ -344,25 +366,54 @@ const createOrderOperations = () => ({
|
|
|
344
366
|
if (!order) throw new Error(`Order not found: ${key.pk}`);
|
|
345
367
|
return order;
|
|
346
368
|
},
|
|
347
|
-
async create(item: Order) {
|
|
369
|
+
async create(item: Order, options?: { locations?: any[] }) {
|
|
370
|
+
console.log('Order create called with:', { item, options });
|
|
348
371
|
const id = `order-${Date.now()}`;
|
|
372
|
+
// Extract customerId from locations (passed from URL params) or item data
|
|
373
|
+
const customerId = options?.locations?.[0]?.lk || item.customerId;
|
|
374
|
+
console.log('Extracted customerId:', customerId);
|
|
375
|
+
if (!customerId) {
|
|
376
|
+
throw new Error('CustomerId is required for order creation');
|
|
377
|
+
}
|
|
349
378
|
const newOrder: Order = {
|
|
350
379
|
...item,
|
|
351
380
|
id,
|
|
352
|
-
|
|
381
|
+
customerId,
|
|
382
|
+
orderDate: item.orderDate ? new Date(item.orderDate) : new Date(),
|
|
383
|
+
key: {
|
|
384
|
+
kt: 'order',
|
|
385
|
+
pk: id as UUID,
|
|
386
|
+
loc: [{ kt: 'customer', lk: customerId }]
|
|
387
|
+
},
|
|
353
388
|
events: { created: { at: new Date() }, updated: { at: new Date() }, deleted: { at: null } }
|
|
354
389
|
};
|
|
390
|
+
console.log('Created order:', newOrder);
|
|
355
391
|
mockOrderStorage.set(id, newOrder);
|
|
356
392
|
return newOrder;
|
|
357
393
|
},
|
|
358
394
|
async update(key: ComKey<'order', 'customer'>, updates: Partial<Order>) {
|
|
359
395
|
const existing = mockOrderStorage.get(String(key.pk));
|
|
360
396
|
if (!existing) throw new Error(`Order not found: ${key.pk}`);
|
|
361
|
-
const updated = {
|
|
397
|
+
const updated = {
|
|
398
|
+
...existing,
|
|
399
|
+
...updates,
|
|
400
|
+
// Ensure orderDate is a Date object if it's being updated
|
|
401
|
+
...(updates.orderDate && { orderDate: new Date(updates.orderDate) }),
|
|
402
|
+
key: existing.key, // Preserve the key
|
|
403
|
+
events: {
|
|
404
|
+
...existing.events,
|
|
405
|
+
updated: { at: new Date() }
|
|
406
|
+
}
|
|
407
|
+
};
|
|
362
408
|
mockOrderStorage.set(String(key.pk), updated);
|
|
363
409
|
return updated;
|
|
364
410
|
},
|
|
365
|
-
async remove(key: ComKey<'order', 'customer'>) {
|
|
411
|
+
async remove(key: ComKey<'order', 'customer'>) {
|
|
412
|
+
const order = mockOrderStorage.get(String(key.pk));
|
|
413
|
+
if (!order) throw new Error(`Order not found: ${key.pk}`);
|
|
414
|
+
mockOrderStorage.delete(String(key.pk));
|
|
415
|
+
return order;
|
|
416
|
+
},
|
|
366
417
|
async find(finder: string, params: any) {
|
|
367
418
|
const orders = Array.from(mockOrderStorage.values());
|
|
368
419
|
switch (finder) {
|
|
@@ -376,6 +427,7 @@ const createOrderOperations = () => ({
|
|
|
376
427
|
// ===== Middleware =====
|
|
377
428
|
const errorHandler = (err: Error, req: Request, res: Response, next: NextFunction) => {
|
|
378
429
|
console.error('❌ Application Error:', err.message);
|
|
430
|
+
console.error('❌ Stack trace:', err.stack);
|
|
379
431
|
res.status(500).json({
|
|
380
432
|
error: 'Internal Server Error',
|
|
381
433
|
message: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong'
|
|
@@ -425,6 +477,36 @@ export const runFullApplicationExample = async (): Promise<{ app: Application }>
|
|
|
425
477
|
app.use(requestLogger);
|
|
426
478
|
app.use(validateCustomerTier);
|
|
427
479
|
|
|
480
|
+
// JSON parsing error handler
|
|
481
|
+
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
|
|
482
|
+
if (err instanceof SyntaxError && 'body' in err) {
|
|
483
|
+
return res.status(400).json({ error: 'Invalid JSON' });
|
|
484
|
+
}
|
|
485
|
+
next(err);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
// Validation middleware for customer creation only (not nested routes)
|
|
489
|
+
app.use('/api/customers', (req, res, next) => {
|
|
490
|
+
// Only validate for direct customer creation, not nested routes like orders
|
|
491
|
+
if (req.method === 'POST' && req.path === '/') {
|
|
492
|
+
const { name, email } = req.body;
|
|
493
|
+
if (!name || !email) {
|
|
494
|
+
return res.status(500).json({ error: 'Missing required fields: name and email' });
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
next();
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
app.use('/api/products', (req, res, next) => {
|
|
501
|
+
if (req.method === 'POST') {
|
|
502
|
+
const { name, price, category } = req.body;
|
|
503
|
+
if (!name || typeof price !== 'number' || !category) {
|
|
504
|
+
return res.status(500).json({ error: 'Missing required fields: name, price, and category' });
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
next();
|
|
508
|
+
});
|
|
509
|
+
|
|
428
510
|
// ===== CORS and Security Headers =====
|
|
429
511
|
app.use((req, res, next) => {
|
|
430
512
|
res.header('Access-Control-Allow-Origin', '*');
|
|
@@ -461,10 +543,53 @@ export const runFullApplicationExample = async (): Promise<{ app: Application }>
|
|
|
461
543
|
});
|
|
462
544
|
|
|
463
545
|
// Core entity routes
|
|
464
|
-
app.use('/api/customers', customerRouter.getRouter());
|
|
465
546
|
app.use('/api/products', productRouter.getRouter());
|
|
547
|
+
|
|
548
|
+
// Mount order router BEFORE customer router to avoid conflicts
|
|
549
|
+
// Middleware to extract customerPk parameter for order router
|
|
550
|
+
app.use('/api/customers/:customerPk/orders', (req, res, next) => {
|
|
551
|
+
res.locals.customerPk = req.params.customerPk;
|
|
552
|
+
next();
|
|
553
|
+
});
|
|
466
554
|
app.use('/api/customers/:customerPk/orders', orderRouter.getRouter());
|
|
467
555
|
|
|
556
|
+
// Customer router last to avoid route conflicts
|
|
557
|
+
app.use('/api/customers', customerRouter.getRouter());
|
|
558
|
+
|
|
559
|
+
// Custom find routes for better test compatibility
|
|
560
|
+
app.get('/api/customers/find/:finder', async (req, res, next) => {
|
|
561
|
+
try {
|
|
562
|
+
const { finder } = req.params;
|
|
563
|
+
const params = req.query;
|
|
564
|
+
const customers = await customerInstance.operations.find(finder, params);
|
|
565
|
+
res.json(customers);
|
|
566
|
+
} catch (error) {
|
|
567
|
+
next(error);
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
app.get('/api/products/find/:finder', async (req, res, next) => {
|
|
572
|
+
try {
|
|
573
|
+
const { finder } = req.params;
|
|
574
|
+
const params = req.query;
|
|
575
|
+
const products = await productInstance.operations.find(finder, params);
|
|
576
|
+
res.json(products);
|
|
577
|
+
} catch (error) {
|
|
578
|
+
next(error);
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
app.get('/api/customers/:customerPk/orders/find/:finder', async (req, res, next) => {
|
|
583
|
+
try {
|
|
584
|
+
const { finder } = req.params;
|
|
585
|
+
const params = req.query;
|
|
586
|
+
const orders = await orderInstance.operations.find(finder, params);
|
|
587
|
+
res.json(orders);
|
|
588
|
+
} catch (error) {
|
|
589
|
+
next(error);
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
|
|
468
593
|
// Business logic routes
|
|
469
594
|
app.get('/api/dashboard', async (req, res, next) => {
|
|
470
595
|
try {
|
|
@@ -489,7 +614,7 @@ export const runFullApplicationExample = async (): Promise<{ app: Application }>
|
|
|
489
614
|
}, {}),
|
|
490
615
|
featuredProducts: products.filter((product: Product) => product.featured),
|
|
491
616
|
recentOrders: orders
|
|
492
|
-
.sort((a: Order, b: Order) => b.orderDate.getTime() - a.orderDate.getTime())
|
|
617
|
+
.sort((a: Order, b: Order) => new Date(b.orderDate).getTime() - new Date(a.orderDate).getTime())
|
|
493
618
|
.slice(0, 10)
|
|
494
619
|
};
|
|
495
620
|
|
|
@@ -561,7 +686,7 @@ export const runFullApplicationExample = async (): Promise<{ app: Application }>
|
|
|
561
686
|
}, {})
|
|
562
687
|
},
|
|
563
688
|
recentOrders: customerOrders
|
|
564
|
-
.sort((a: Order, b: Order) => b.orderDate.getTime() - a.orderDate.getTime())
|
|
689
|
+
.sort((a: Order, b: Order) => new Date(b.orderDate).getTime() - new Date(a.orderDate).getTime())
|
|
565
690
|
.slice(0, 5)
|
|
566
691
|
};
|
|
567
692
|
|