From ea9ab8ae3ce307589919d15d62f45816e01f9b21 Mon Sep 17 00:00:00 2001 From: adrien2p Date: Mon, 14 Feb 2022 14:03:48 +0100 Subject: [PATCH] feat: Improve middleware/router API --- docs/classes/Medusa.Medusa-1.md | 8 ++-- ...tityEvent_decorator.OnMedusaEntityEvent.md | 26 +++++----- ...nt_emmiter.Internals.CustomEventEmmiter.md | 12 ++--- .../metadata_reader.Internals.CustomMap.md | 2 +- docs/classes/utils.Utils.md | 12 ++--- docs/interfaces/types.MedusaMiddleware.md | 18 ++----- docs/interfaces/types.Type.md | 2 +- docs/modules/constants.md | 6 +-- .../decorators_components_decorator.md | 14 +++--- docs/modules/decorators_module_decorator.md | 2 +- ...ecorators_onMedusaEntityEvent_decorator.md | 10 ++-- docs/modules/decorators_utils.md | 2 +- docs/modules/event_emmiter.Internals.md | 2 +- docs/modules/event_emmiter.md | 2 +- docs/modules/index.md | 2 +- docs/modules/loaders_api_loader.md | 2 +- docs/modules/loaders_database_loader.md | 2 +- docs/modules/loaders_entities_loader.md | 8 ++-- docs/modules/loaders_middlewares_loader.md | 48 ++++++++++++++++++- docs/modules/loaders_migrations_loader.md | 2 +- docs/modules/loaders_plugins_loader.md | 2 +- docs/modules/loaders_repository_loader.md | 8 ++-- docs/modules/loaders_routes_loader.md | 4 +- docs/modules/loaders_services_loader.md | 6 +-- docs/modules/loaders_validators_loader.md | 2 +- docs/modules/metadata_reader.md | 4 +- docs/modules/types.md | 38 +++++++-------- src/loaders/middlewares.loader.ts | 33 ++++++++++--- src/loaders/routes.loader.ts | 16 +++---- src/loaders/tests/middlewares.loader.spec.ts | 18 +++++-- src/types.ts | 10 ++-- 31 files changed, 193 insertions(+), 130 deletions(-) diff --git a/docs/classes/Medusa.Medusa-1.md b/docs/classes/Medusa.Medusa-1.md index f376c3e0..e5bfa381 100644 --- a/docs/classes/Medusa.Medusa-1.md +++ b/docs/classes/Medusa.Medusa-1.md @@ -37,7 +37,7 @@ internal container and database connection. #### Defined in -[src/Medusa.ts:37](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/Medusa.ts#L37) +[src/Medusa.ts:37](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/Medusa.ts#L37) ## Properties @@ -47,7 +47,7 @@ internal container and database connection. #### Defined in -[src/Medusa.ts:30](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/Medusa.ts#L30) +[src/Medusa.ts:30](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/Medusa.ts#L30) ___ @@ -57,7 +57,7 @@ ___ #### Defined in -[src/Medusa.ts:31](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/Medusa.ts#L31) +[src/Medusa.ts:31](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/Medusa.ts#L31) ## Methods @@ -77,4 +77,4 @@ ___ #### Defined in -[src/Medusa.ts:45](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/Medusa.ts#L45) +[src/Medusa.ts:45](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/Medusa.ts#L45) diff --git a/docs/classes/decorators_onMedusaEntityEvent_decorator.OnMedusaEntityEvent.md b/docs/classes/decorators_onMedusaEntityEvent_decorator.OnMedusaEntityEvent.md index 5a861106..6b3472fa 100644 --- a/docs/classes/decorators_onMedusaEntityEvent_decorator.OnMedusaEntityEvent.md +++ b/docs/classes/decorators_onMedusaEntityEvent_decorator.OnMedusaEntityEvent.md @@ -45,7 +45,7 @@ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:42](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L42) +[src/decorators/onMedusaEntityEvent.decorator.ts:42](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L42) ## Properties @@ -55,7 +55,7 @@ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:40](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L40) +[src/decorators/onMedusaEntityEvent.decorator.ts:40](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L40) ___ @@ -65,7 +65,7 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:39](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L39) +[src/decorators/onMedusaEntityEvent.decorator.ts:39](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L39) ## Accessors @@ -79,7 +79,7 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:50](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L50) +[src/decorators/onMedusaEntityEvent.decorator.ts:50](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L50) ___ @@ -93,7 +93,7 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:46](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L46) +[src/decorators/onMedusaEntityEvent.decorator.ts:46](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L46) ## Methods @@ -120,7 +120,7 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:70](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L70) +[src/decorators/onMedusaEntityEvent.decorator.ts:70](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L70) ___ @@ -146,7 +146,7 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:58](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L58) +[src/decorators/onMedusaEntityEvent.decorator.ts:58](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L58) ___ @@ -173,7 +173,7 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:84](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L84) +[src/decorators/onMedusaEntityEvent.decorator.ts:84](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L84) ___ @@ -199,7 +199,7 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:66](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L66) +[src/decorators/onMedusaEntityEvent.decorator.ts:66](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L66) ___ @@ -226,7 +226,7 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:77](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L77) +[src/decorators/onMedusaEntityEvent.decorator.ts:77](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L77) ___ @@ -252,7 +252,7 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:62](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L62) +[src/decorators/onMedusaEntityEvent.decorator.ts:62](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L62) ___ @@ -280,7 +280,7 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:91](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L91) +[src/decorators/onMedusaEntityEvent.decorator.ts:91](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L91) ___ @@ -300,4 +300,4 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:54](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L54) +[src/decorators/onMedusaEntityEvent.decorator.ts:54](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L54) diff --git a/docs/classes/event_emmiter.Internals.CustomEventEmmiter.md b/docs/classes/event_emmiter.Internals.CustomEventEmmiter.md index d3e89413..ceb1d439 100644 --- a/docs/classes/event_emmiter.Internals.CustomEventEmmiter.md +++ b/docs/classes/event_emmiter.Internals.CustomEventEmmiter.md @@ -41,7 +41,7 @@ EventEmitter.constructor #### Defined in -[src/event-emmiter.ts:21](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/event-emmiter.ts#L21) +[src/event-emmiter.ts:21](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/event-emmiter.ts#L21) ## Properties @@ -51,7 +51,7 @@ EventEmitter.constructor #### Defined in -[src/event-emmiter.ts:19](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/event-emmiter.ts#L19) +[src/event-emmiter.ts:19](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/event-emmiter.ts#L19) ## Methods @@ -80,7 +80,7 @@ Emit an asynchrone event entity based and wait for the result. #### Defined in -[src/event-emmiter.ts:80](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/event-emmiter.ts#L80) +[src/event-emmiter.ts:80](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/event-emmiter.ts#L80) ___ @@ -110,7 +110,7 @@ Register a new event handler. #### Defined in -[src/event-emmiter.ts:31](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/event-emmiter.ts#L31) +[src/event-emmiter.ts:31](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/event-emmiter.ts#L31) ___ @@ -132,7 +132,7 @@ Apply all event handlers hold by the `listenerDescriptor`. #### Defined in -[src/event-emmiter.ts:43](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/event-emmiter.ts#L43) +[src/event-emmiter.ts:43](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/event-emmiter.ts#L43) ___ @@ -146,4 +146,4 @@ ___ #### Defined in -[src/event-emmiter.ts:68](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/event-emmiter.ts#L68) +[src/event-emmiter.ts:68](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/event-emmiter.ts#L68) diff --git a/docs/classes/metadata_reader.Internals.CustomMap.md b/docs/classes/metadata_reader.Internals.CustomMap.md index f0c86a70..0ed663de 100644 --- a/docs/classes/metadata_reader.Internals.CustomMap.md +++ b/docs/classes/metadata_reader.Internals.CustomMap.md @@ -84,4 +84,4 @@ Map.get #### Defined in -[src/metadata-reader.ts:5](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/metadata-reader.ts#L5) +[src/metadata-reader.ts:5](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/metadata-reader.ts#L5) diff --git a/docs/classes/utils.Utils.md b/docs/classes/utils.Utils.md index c4125e5e..ffd107d7 100644 --- a/docs/classes/utils.Utils.md +++ b/docs/classes/utils.Utils.md @@ -56,7 +56,7 @@ Attach a new subscriber to a specific entities. #### Defined in -[src/utils.ts:37](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/utils.ts#L37) +[src/utils.ts:37](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/utils.ts#L37) ___ @@ -80,7 +80,7 @@ Prepare the log to be shown to be consistent everywhere. #### Defined in -[src/utils.ts:68](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/utils.ts#L68) +[src/utils.ts:68](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/utils.ts#L68) ___ @@ -102,7 +102,7 @@ Log all the routes from the express instance. #### Defined in -[src/utils.ts:58](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/utils.ts#L58) +[src/utils.ts:58](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/utils.ts#L58) ___ @@ -123,7 +123,7 @@ ___ #### Defined in -[src/utils.ts:82](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/utils.ts#L82) +[src/utils.ts:82](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/utils.ts#L82) ___ @@ -153,7 +153,7 @@ Since it is not possible to use multiple extend, you can use that utilities to a #### Defined in -[src/utils.ts:17](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/utils.ts#L17) +[src/utils.ts:17](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/utils.ts#L17) ___ @@ -173,4 +173,4 @@ ___ #### Defined in -[src/utils.ts:102](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/utils.ts#L102) +[src/utils.ts:102](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/utils.ts#L102) diff --git a/docs/interfaces/types.MedusaMiddleware.md b/docs/interfaces/types.MedusaMiddleware.md index 516302c7..e4cab050 100644 --- a/docs/interfaces/types.MedusaMiddleware.md +++ b/docs/interfaces/types.MedusaMiddleware.md @@ -17,22 +17,10 @@ Describe a custom middleware instance. ### consume -▸ **consume**(`__namedParameters`): (`err`: `unknown`, `req`: [`MedusaAuthenticatedRequest`](../modules/types.md#medusaauthenticatedrequest) \| `Request`<`ParamsDictionary`, `any`, `any`, `ParsedQs`, `Record`<`string`, `any`\>\>, `res`: `Response`<`any`, `Record`<`string`, `any`\>\>, `next`: `NextFunction`) => `void` \| `Promise`<`void`\> +▸ **consume**(`err`, `req`, `res`, `next`): `void` \| `Promise`<`void`\> #### Parameters -| Name | Type | -| :------ | :------ | -| `__namedParameters` | `Object` | - -#### Returns - -`fn` - -▸ (`err`, `req`, `res`, `next`): `void` \| `Promise`<`void`\> - -##### Parameters - | Name | Type | | :------ | :------ | | `err` | `unknown` | @@ -40,10 +28,10 @@ Describe a custom middleware instance. | `res` | `Response`<`any`, `Record`<`string`, `any`\>\> | | `next` | `NextFunction` | -##### Returns +#### Returns `void` \| `Promise`<`void`\> #### Defined in -[src/types.ts:152](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L152) +[src/types.ts:152](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L152) diff --git a/docs/interfaces/types.Type.md b/docs/interfaces/types.Type.md index 7d7c2aeb..3072e104 100644 --- a/docs/interfaces/types.Type.md +++ b/docs/interfaces/types.Type.md @@ -40,4 +40,4 @@ Function.constructor #### Defined in -[src/types.ts:6](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L6) +[src/types.ts:6](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L6) diff --git a/docs/modules/constants.md b/docs/modules/constants.md index c4f5a62c..5a7397e5 100644 --- a/docs/modules/constants.md +++ b/docs/modules/constants.md @@ -20,7 +20,7 @@ Metadata key to store config from decorated injectables. #### Defined in -[src/constants.ts:9](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/constants.ts#L9) +[src/constants.ts:9](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/constants.ts#L9) ___ @@ -66,7 +66,7 @@ Hold the name of the medusa services registered into the container for later on #### Defined in -[src/constants.ts:14](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/constants.ts#L14) +[src/constants.ts:14](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/constants.ts#L14) ___ @@ -78,4 +78,4 @@ Metadata key to store config from decorated injectables. #### Defined in -[src/constants.ts:4](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/constants.ts#L4) +[src/constants.ts:4](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/constants.ts#L4) diff --git a/docs/modules/decorators_components_decorator.md b/docs/modules/decorators_components_decorator.md index b3e0950c..7b7fdab4 100644 --- a/docs/modules/decorators_components_decorator.md +++ b/docs/modules/decorators_components_decorator.md @@ -40,7 +40,7 @@ Mark a class as an entity to be used by the loader. #### Defined in -[src/decorators/components.decorator.ts:16](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/components.decorator.ts#L16) +[src/decorators/components.decorator.ts:16](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/components.decorator.ts#L16) ___ @@ -62,7 +62,7 @@ Mark a class as a middleware to be used by the loader. #### Defined in -[src/decorators/components.decorator.ts:43](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/components.decorator.ts#L43) +[src/decorators/components.decorator.ts:43](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/components.decorator.ts#L43) ___ @@ -78,7 +78,7 @@ Mark a class as a migration to be used by the loader. #### Defined in -[src/decorators/components.decorator.ts:51](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/components.decorator.ts#L51) +[src/decorators/components.decorator.ts:51](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/components.decorator.ts#L51) ___ @@ -106,7 +106,7 @@ Mark a class as a repository to be used by the loader. #### Defined in -[src/decorators/components.decorator.ts:34](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/components.decorator.ts#L34) +[src/decorators/components.decorator.ts:34](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/components.decorator.ts#L34) ___ @@ -128,7 +128,7 @@ Mark a class as a router to be used by the loader. #### Defined in -[src/decorators/components.decorator.ts:60](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/components.decorator.ts#L60) +[src/decorators/components.decorator.ts:60](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/components.decorator.ts#L60) ___ @@ -156,7 +156,7 @@ Mark a class as a service to be used by the loader. #### Defined in -[src/decorators/components.decorator.ts:25](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/components.decorator.ts#L25) +[src/decorators/components.decorator.ts:25](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/components.decorator.ts#L25) ___ @@ -178,4 +178,4 @@ Mark a class as a validator to be used by the validator utility. #### Defined in -[src/decorators/components.decorator.ts:69](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/components.decorator.ts#L69) +[src/decorators/components.decorator.ts:69](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/components.decorator.ts#L69) diff --git a/docs/modules/decorators_module_decorator.md b/docs/modules/decorators_module_decorator.md index e2649f6c..a453c1bf 100644 --- a/docs/modules/decorators_module_decorator.md +++ b/docs/modules/decorators_module_decorator.md @@ -29,4 +29,4 @@ Mark a class as aa module and store all injectable on it. #### Defined in -[src/decorators/module.decorator.ts:9](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/module.decorator.ts#L9) +[src/decorators/module.decorator.ts:9](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/module.decorator.ts#L9) diff --git a/docs/modules/decorators_onMedusaEntityEvent_decorator.md b/docs/modules/decorators_onMedusaEntityEvent_decorator.md index 3dedc9d4..80644e91 100644 --- a/docs/modules/decorators_onMedusaEntityEvent_decorator.md +++ b/docs/modules/decorators_onMedusaEntityEvent_decorator.md @@ -24,7 +24,7 @@ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:11](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L11) +[src/decorators/onMedusaEntityEvent.decorator.ts:11](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L11) ___ @@ -41,7 +41,7 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:6](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L6) +[src/decorators/onMedusaEntityEvent.decorator.ts:6](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L6) ___ @@ -60,7 +60,7 @@ Event types that can be emitted. #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:16](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L16) +[src/decorators/onMedusaEntityEvent.decorator.ts:16](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L16) ___ @@ -86,7 +86,7 @@ The arguments expected by the [OnMedusaEntityEvent](../classes/decorators_onMedu #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:25](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L25) +[src/decorators/onMedusaEntityEvent.decorator.ts:25](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L25) ___ @@ -112,4 +112,4 @@ The arguments expected by the event handler. #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:33](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/onMedusaEntityEvent.decorator.ts#L33) +[src/decorators/onMedusaEntityEvent.decorator.ts:33](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/onMedusaEntityEvent.decorator.ts#L33) diff --git a/docs/modules/decorators_utils.md b/docs/modules/decorators_utils.md index 24ed786f..2f93a7dd 100644 --- a/docs/modules/decorators_utils.md +++ b/docs/modules/decorators_utils.md @@ -32,4 +32,4 @@ #### Defined in -[src/decorators/utils.ts:4](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/decorators/utils.ts#L4) +[src/decorators/utils.ts:4](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/decorators/utils.ts#L4) diff --git a/docs/modules/event_emmiter.Internals.md b/docs/modules/event_emmiter.Internals.md index 9837133f..ff77b3a3 100644 --- a/docs/modules/event_emmiter.Internals.md +++ b/docs/modules/event_emmiter.Internals.md @@ -38,4 +38,4 @@ A listener descriptor. #### Defined in -[src/event-emmiter.ts:9](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/event-emmiter.ts#L9) +[src/event-emmiter.ts:9](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/event-emmiter.ts#L9) diff --git a/docs/modules/event_emmiter.md b/docs/modules/event_emmiter.md index 855a749e..81835044 100644 --- a/docs/modules/event_emmiter.md +++ b/docs/modules/event_emmiter.md @@ -23,4 +23,4 @@ Export the instance of the event emmiter. #### Defined in -[src/event-emmiter.ts:102](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/event-emmiter.ts#L102) +[src/event-emmiter.ts:102](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/event-emmiter.ts#L102) diff --git a/docs/modules/index.md b/docs/modules/index.md index 4289f1a8..6dfddabf 100644 --- a/docs/modules/index.md +++ b/docs/modules/index.md @@ -285,4 +285,4 @@ Renames and re-exports [customEventEmitter](event_emmiter.md#customeventemitter) #### Defined in -[src/index.ts:25](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/index.ts#L25) +[src/index.ts:25](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/index.ts#L25) diff --git a/docs/modules/loaders_api_loader.md b/docs/modules/loaders_api_loader.md index c2a5d30c..ae40a68e 100644 --- a/docs/modules/loaders_api_loader.md +++ b/docs/modules/loaders_api_loader.md @@ -30,4 +30,4 @@ Decorate the original api loader to apply our custom middleware first. #### Defined in -[src/loaders/api.loader.ts:12](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/api.loader.ts#L12) +[src/loaders/api.loader.ts:12](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/api.loader.ts#L12) diff --git a/docs/modules/loaders_database_loader.md b/docs/modules/loaders_database_loader.md index 626e8142..b3486b77 100644 --- a/docs/modules/loaders_database_loader.md +++ b/docs/modules/loaders_database_loader.md @@ -27,4 +27,4 @@ #### Defined in -[src/loaders/database.loader.ts:5](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/database.loader.ts#L5) +[src/loaders/database.loader.ts:5](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/database.loader.ts#L5) diff --git a/docs/modules/loaders_entities_loader.md b/docs/modules/loaders_entities_loader.md index c851bd0f..02ce7bdc 100644 --- a/docs/modules/loaders_entities_loader.md +++ b/docs/modules/loaders_entities_loader.md @@ -30,7 +30,7 @@ #### Defined in -[src/loaders/entities.loader.ts:5](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/entities.loader.ts#L5) +[src/loaders/entities.loader.ts:5](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/entities.loader.ts#L5) ___ @@ -50,7 +50,7 @@ ___ #### Defined in -[src/loaders/entities.loader.ts:16](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/entities.loader.ts#L16) +[src/loaders/entities.loader.ts:16](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/entities.loader.ts#L16) ___ @@ -73,7 +73,7 @@ Load custom entity and override existing ones. #### Defined in -[src/loaders/entities.loader.ts:49](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/entities.loader.ts#L49) +[src/loaders/entities.loader.ts:49](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/entities.loader.ts#L49) ___ @@ -97,4 +97,4 @@ Load custom entity into the container. #### Defined in -[src/loaders/entities.loader.ts:30](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/entities.loader.ts#L30) +[src/loaders/entities.loader.ts:30](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/entities.loader.ts#L30) diff --git a/docs/modules/loaders_middlewares_loader.md b/docs/modules/loaders_middlewares_loader.md index 7d1d73b6..356ac667 100644 --- a/docs/modules/loaders_middlewares_loader.md +++ b/docs/modules/loaders_middlewares_loader.md @@ -7,6 +7,7 @@ ### Functions - [middlewaresLoader](loaders_middlewares_loader.md#middlewaresloader) +- [wrapMiddleware](loaders_middlewares_loader.md#wrapmiddleware) ## Functions @@ -31,4 +32,49 @@ Register custom middlewares into medusa middleware service to be applied at the #### Defined in -[src/loaders/middlewares.loader.ts:13](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/middlewares.loader.ts#L13) +[src/loaders/middlewares.loader.ts:13](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/middlewares.loader.ts#L13) + +___ + +### wrapMiddleware + +▸ **wrapMiddleware**(`middleware`): (`options`: { `app`: `Express` }) => (...`args`: `unknown`[]) => `void` + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `middleware` | [`GetInjectableOption`](types.md#getinjectableoption)<``"middleware"``\> | + +#### Returns + +`fn` + +▸ (`options`): (...`args`: `unknown`[]) => `void` + +##### Parameters + +| Name | Type | +| :------ | :------ | +| `options` | `Object` | +| `options.app` | `Express` | + +##### Returns + +`fn` + +▸ (...`args`): `void` + +##### Parameters + +| Name | Type | +| :------ | :------ | +| `...args` | `unknown`[] | + +##### Returns + +`void` + +#### Defined in + +[src/loaders/middlewares.loader.ts:39](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/middlewares.loader.ts#L39) diff --git a/docs/modules/loaders_migrations_loader.md b/docs/modules/loaders_migrations_loader.md index 9d3be64e..32c55eda 100644 --- a/docs/modules/loaders_migrations_loader.md +++ b/docs/modules/loaders_migrations_loader.md @@ -30,4 +30,4 @@ Run custom migrations that are find from the provided directory and stored in a #### Defined in -[src/loaders/migrations.loader.ts:10](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/migrations.loader.ts#L10) +[src/loaders/migrations.loader.ts:10](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/migrations.loader.ts#L10) diff --git a/docs/modules/loaders_plugins_loader.md b/docs/modules/loaders_plugins_loader.md index e0137494..8149c934 100644 --- a/docs/modules/loaders_plugins_loader.md +++ b/docs/modules/loaders_plugins_loader.md @@ -29,4 +29,4 @@ Register all listeners before the plugins are loaded to be sure that the scope m #### Defined in -[src/loaders/plugins.loader.ts:11](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/plugins.loader.ts#L11) +[src/loaders/plugins.loader.ts:11](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/plugins.loader.ts#L11) diff --git a/docs/modules/loaders_repository_loader.md b/docs/modules/loaders_repository_loader.md index 361763bf..5ffe4e07 100644 --- a/docs/modules/loaders_repository_loader.md +++ b/docs/modules/loaders_repository_loader.md @@ -29,7 +29,7 @@ #### Defined in -[src/loaders/repository.loader.ts:16](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/repository.loader.ts#L16) +[src/loaders/repository.loader.ts:16](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/repository.loader.ts#L16) ___ @@ -52,7 +52,7 @@ Load custom repositories and override existing ones. #### Defined in -[src/loaders/repository.loader.ts:48](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/repository.loader.ts#L48) +[src/loaders/repository.loader.ts:48](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/repository.loader.ts#L48) ___ @@ -76,7 +76,7 @@ Load custom repository into the container. #### Defined in -[src/loaders/repository.loader.ts:30](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/repository.loader.ts#L30) +[src/loaders/repository.loader.ts:30](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/repository.loader.ts#L30) ___ @@ -97,4 +97,4 @@ ___ #### Defined in -[src/loaders/repository.loader.ts:5](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/repository.loader.ts#L5) +[src/loaders/repository.loader.ts:5](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/repository.loader.ts#L5) diff --git a/docs/modules/loaders_routes_loader.md b/docs/modules/loaders_routes_loader.md index fbe31f1a..f5ee2403 100644 --- a/docs/modules/loaders_routes_loader.md +++ b/docs/modules/loaders_routes_loader.md @@ -28,7 +28,7 @@ #### Defined in -[src/loaders/routes.loader.ts:4](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/routes.loader.ts#L4) +[src/loaders/routes.loader.ts:4](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/routes.loader.ts#L4) ___ @@ -49,4 +49,4 @@ ___ #### Defined in -[src/loaders/routes.loader.ts:14](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/routes.loader.ts#L14) +[src/loaders/routes.loader.ts:14](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/routes.loader.ts#L14) diff --git a/docs/modules/loaders_services_loader.md b/docs/modules/loaders_services_loader.md index 1fc69c7c..1d582450 100644 --- a/docs/modules/loaders_services_loader.md +++ b/docs/modules/loaders_services_loader.md @@ -33,7 +33,7 @@ Load custom service and override existing ones. #### Defined in -[src/loaders/services.loader.ts:55](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/services.loader.ts#L55) +[src/loaders/services.loader.ts:55](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/services.loader.ts#L55) ___ @@ -58,7 +58,7 @@ Load custom service into the container. #### Defined in -[src/loaders/services.loader.ts:31](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/services.loader.ts#L31) +[src/loaders/services.loader.ts:31](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/services.loader.ts#L31) ___ @@ -81,4 +81,4 @@ Load custom services from the rootDir. #### Defined in -[src/loaders/services.loader.ts:9](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/services.loader.ts#L9) +[src/loaders/services.loader.ts:9](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/services.loader.ts#L9) diff --git a/docs/modules/loaders_validators_loader.md b/docs/modules/loaders_validators_loader.md index bdc9f703..045a90e5 100644 --- a/docs/modules/loaders_validators_loader.md +++ b/docs/modules/loaders_validators_loader.md @@ -26,4 +26,4 @@ #### Defined in -[src/loaders/validators.loader.ts:4](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/loaders/validators.loader.ts#L4) +[src/loaders/validators.loader.ts:4](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/loaders/validators.loader.ts#L4) diff --git a/docs/modules/metadata_reader.md b/docs/modules/metadata_reader.md index 8f0fc16c..623c7739 100644 --- a/docs/modules/metadata_reader.md +++ b/docs/modules/metadata_reader.md @@ -40,7 +40,7 @@ Return the options from components. #### Defined in -[src/metadata-reader.ts:36](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/metadata-reader.ts#L36) +[src/metadata-reader.ts:36](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/metadata-reader.ts#L36) ___ @@ -62,4 +62,4 @@ Read all metadata from the imported modules and extract components that will be #### Defined in -[src/metadata-reader.ts:14](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/metadata-reader.ts#L14) +[src/metadata-reader.ts:14](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/metadata-reader.ts#L14) diff --git a/docs/modules/types.md b/docs/modules/types.md index f4571e23..16c0481e 100644 --- a/docs/modules/types.md +++ b/docs/modules/types.md @@ -53,7 +53,7 @@ #### Defined in -[src/types.ts:9](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L9) +[src/types.ts:9](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L9) ___ @@ -78,7 +78,7 @@ Defines the injection options for entities. #### Defined in -[src/types.ts:26](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L26) +[src/types.ts:26](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L26) ___ @@ -96,7 +96,7 @@ Determine which options type it actually is depending on the component type. #### Defined in -[src/types.ts:94](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L94) +[src/types.ts:94](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L94) ___ @@ -114,7 +114,7 @@ Determine which options type it actually is depending on the component type. #### Defined in -[src/types.ts:117](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L117) +[src/types.ts:117](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L117) ___ @@ -126,7 +126,7 @@ Components that does not required any other options that Type. #### Defined in -[src/types.ts:14](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L14) +[src/types.ts:14](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L14) ___ @@ -144,7 +144,7 @@ Union of all options type possible for injectable. #### Defined in -[src/types.ts:83](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L83) +[src/types.ts:83](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L83) ___ @@ -156,7 +156,7 @@ Describe the expected request from a middleware [MedusaMiddleware](../interfaces #### Defined in -[src/types.ts:130](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L130) +[src/types.ts:130](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L130) ___ @@ -175,7 +175,7 @@ Medusa request extended. #### Defined in -[src/types.ts:123](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L123) +[src/types.ts:123](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L123) ___ @@ -185,7 +185,7 @@ ___ #### Defined in -[src/types.ts:137](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L137) +[src/types.ts:137](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L137) ___ @@ -204,7 +204,7 @@ Describe all the available options for the Route [MedusaMiddleware](../interface #### Defined in -[src/types.ts:142](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L142) +[src/types.ts:142](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L142) ___ @@ -219,11 +219,11 @@ Defines the injection options for middlewares. | Name | Type | | :------ | :------ | | `requireAuth` | `boolean` | -| `routerOptions` | [`MedusaRouteOptions`](types.md#medusarouteoptions)[] | +| `routes` | [`MedusaRouteOptions`](types.md#medusarouteoptions)[] | #### Defined in -[src/types.ts:68](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L68) +[src/types.ts:68](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L68) ___ @@ -242,7 +242,7 @@ Mix the return type of two objects. #### Defined in -[src/types.ts:165](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L165) +[src/types.ts:163](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L163) ___ @@ -267,7 +267,7 @@ Defines the injection options for services, entities, repositories. #### Defined in -[src/types.ts:43](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L43) +[src/types.ts:43](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L43) ___ @@ -281,11 +281,11 @@ Defines the injection options for routes. | Name | Type | | :------ | :------ | -| `router` | [`RoutesInjectionRouterConfiguration`](types.md#routesinjectionrouterconfiguration)[] | +| `routes` | [`RoutesInjectionRouterConfiguration`](types.md#routesinjectionrouterconfiguration)[] | #### Defined in -[src/types.ts:61](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L61) +[src/types.ts:61](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L61) ___ @@ -306,7 +306,7 @@ Define a router and its configuration. #### Defined in -[src/types.ts:51](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L51) +[src/types.ts:51](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L51) ___ @@ -332,7 +332,7 @@ Defines the injection options for service. #### Defined in -[src/types.ts:34](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L34) +[src/types.ts:34](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L34) ___ @@ -356,4 +356,4 @@ Defines the injection options for routes. #### Defined in -[src/types.ts:76](https://github.com/adrien2p/medusa-extender/blob/9d6ee37/src/types.ts#L76) +[src/types.ts:76](https://github.com/adrien2p/medusa-extender/blob/00dfd85/src/types.ts#L76) diff --git a/src/loaders/middlewares.loader.ts b/src/loaders/middlewares.loader.ts index 56a66a79..0bb658fc 100644 --- a/src/loaders/middlewares.loader.ts +++ b/src/loaders/middlewares.loader.ts @@ -1,7 +1,7 @@ -import { Express } from 'express'; +import { Express, NextFunction, Response } from 'express'; import { AwilixContainer } from 'awilix'; import MiddlewareService from '@medusajs/medusa/dist/services/middleware'; -import { GetInjectableOptions, Utils } from './'; +import { GetInjectableOption, GetInjectableOptions, MedusaAuthenticatedRequest, MedusaRequest, Utils } from './'; /** * @internal @@ -18,16 +18,16 @@ export function middlewaresLoader( const medusaMiddlewareService = container.resolve('middlewareService') as MiddlewareService; for (const middlewareOptions of middlewares) { - const { requireAuth, metatype } = middlewareOptions; - const middleware = new metatype().consume; + const { requireAuth } = middlewareOptions; + const wrappedMiddleware = wrapMiddleware(middlewareOptions); if (!requireAuth) { - medusaMiddlewareService.addPreAuthentication(middleware, { app }); + medusaMiddlewareService.addPreAuthentication(wrappedMiddleware, {}); Utils.log( 'MedusaLoader#middlewaresLoader', `Middleware registered before auth - ${middlewareOptions.metatype.name}` ); } else { - medusaMiddlewareService.addPostAuthentication(middleware, { app }); + medusaMiddlewareService.addPostAuthentication(wrappedMiddleware, {}); Utils.log( 'MedusaLoader#middlewaresLoader', `Middleware registered after auth - ${middlewareOptions.metatype.name}` @@ -35,3 +35,24 @@ export function middlewaresLoader( } } } + +export function wrapMiddleware( + middleware: GetInjectableOption<'middleware'> +): (options: { app: Express }) => (...args: unknown[]) => void { + return (): (( + err: unknown, + req: MedusaRequest | MedusaAuthenticatedRequest, + res: Response, + next: NextFunction + ) => void) => { + return (err: unknown, req: MedusaRequest | MedusaAuthenticatedRequest, res: Response, next: NextFunction) => { + const shouldHandle = middleware.routes.some((route) => { + return req.method.toLowerCase() === route.method.toLowerCase() && req.path === route.path; + }); + if (shouldHandle) { + return new middleware.metatype().consume(err, req, res, next); + } + return next(); + }; + }; +} diff --git a/src/loaders/routes.loader.ts b/src/loaders/routes.loader.ts index 7113804f..f7a57870 100644 --- a/src/loaders/routes.loader.ts +++ b/src/loaders/routes.loader.ts @@ -3,9 +3,9 @@ import { Express, NextFunction, Request, Response } from 'express'; export function authenticatedRoutesLoader(routesOptions: GetInjectableOptions<'router'>, app: Express): void { for (const routeOptions of routesOptions) { - routeOptions.router.forEach((router) => { - if (router.requiredAuth) { - registerRoute(app, router); + routeOptions.routes.forEach((route) => { + if (route.requiredAuth) { + registerRoute(app, route); } }); } @@ -13,16 +13,16 @@ export function authenticatedRoutesLoader(routesOptions: GetInjectableOptions<'r export function unauthenticatedRoutesLoader(routesOptions: GetInjectableOptions<'router'>, app: Express): void { for (const routeOptions of routesOptions) { - routeOptions.router.forEach((router) => { - if (!router.requiredAuth) { - registerRoute(app, router); + routeOptions.routes.forEach((route) => { + if (!route.requiredAuth) { + registerRoute(app, route); } }); } } -function registerRoute(app: Express, router: RoutesInjectionRouterConfiguration): void { - const { method, path, handler } = router; +function registerRoute(app: Express, route: RoutesInjectionRouterConfiguration): void { + const { method, path, handler } = route; app[method.toLowerCase()]( path, async (req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction) => { diff --git a/src/loaders/tests/middlewares.loader.spec.ts b/src/loaders/tests/middlewares.loader.spec.ts index 12948a1f..18b95aeb 100644 --- a/src/loaders/tests/middlewares.loader.spec.ts +++ b/src/loaders/tests/middlewares.loader.spec.ts @@ -16,18 +16,28 @@ const MiddlewareServiceMock = { addPostAuthentication: jest.fn(), }; -@Middleware({ requireAuth: false, routerOptions: [{ path: '/admin/test', method: 'get' }] }) +@Middleware({ requireAuth: false, routes: [{ path: '/admin/test', method: 'get' }] }) class PreAuthUserMiddleware implements MedusaMiddleware { /* eslint-disable @typescript-eslint/no-unused-vars */ - consume(req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction): void | Promise { + consume( + err: unknown, + req: MedusaAuthenticatedRequest | Request, + res: Response, + next: NextFunction + ): void | Promise { return; } } -@Middleware({ requireAuth: true, routerOptions: [{ path: '/admin/test', method: 'get' }] }) +@Middleware({ requireAuth: true, routes: [{ path: '/admin/test', method: 'get' }] }) class PostAuthUserMiddleware implements MedusaMiddleware { /* eslint-disable @typescript-eslint/no-unused-vars */ - consume(req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction): void | Promise { + consume( + err: unknown, + req: MedusaAuthenticatedRequest | Request, + res: Response, + next: NextFunction + ): void | Promise { return; } } diff --git a/src/types.ts b/src/types.ts index 342bbead..91689b1b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -59,7 +59,7 @@ export type RoutesInjectionRouterConfiguration = { * Defines the injection options for routes. */ export type RouterInjectionOptions = { - router: RoutesInjectionRouterConfiguration[]; + routes: RoutesInjectionRouterConfiguration[]; }; /** @@ -67,7 +67,7 @@ export type RouterInjectionOptions = { */ export type MiddlewareInjectionOptions = { requireAuth: boolean; - routerOptions: MedusaRouteOptions[]; + routes: MedusaRouteOptions[]; }; /** @@ -149,14 +149,12 @@ export type MedusaRouteOptions = { * Describe a custom middleware instance. */ export interface MedusaMiddleware { - consume({ - app: Express, - }): ( + consume( err: unknown, req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction - ) => void | Promise; + ): void | Promise; } /**