From 66eeb0d07c8b6988996f38b06f324f81a4989289 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Fri, 28 Jan 2022 16:53:00 +0100 Subject: [PATCH] Feat() split Injectable into multiple decorators in order to improve DX (#7) Feat() split Injectable into multiple decorator in order to improve DX --- README.md | 72 +++++--- docs/README.md | 72 +++++--- ...tityEvent_decorator.OnMedusaEntityEvent.md | 26 +-- ...nt_emmiter.Internals.CustomEventEmmiter.md | 12 +- docs/classes/loader.Loader.md | 2 +- docs/classes/medusa.Medusa.md | 8 +- .../metadata_reader.Internals.CustomMap.md | 2 +- docs/classes/utils.Utils.md | 12 +- docs/interfaces/types.MedusaMiddleware.md | 2 +- docs/interfaces/types.Type.md | 2 +- docs/modules.md | 3 +- docs/modules/contants.md | 6 +- docs/modules/decorators.md | 71 ++++++-- .../decorators_components_decorator.md | 158 ++++++++++++++++++ .../decorators_injectable_decorator.md | 37 ---- docs/modules/decorators_module_decorator.md | 2 +- ...ecorators_onMedusaEntityEvent_decorator.md | 10 +- docs/modules/decorators_utils.md | 35 ++++ docs/modules/event_emmiter.Internals.md | 2 +- docs/modules/event_emmiter.md | 2 +- docs/modules/index.md | 73 +++++--- docs/modules/loaders.md | 30 ++-- 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 | 2 +- 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 | 8 +- docs/modules/loaders_services_loader.md | 6 +- docs/modules/metadata_reader.md | 8 +- docs/modules/types.md | 135 +++++++-------- src/decorators/components.decorator.ts | 61 +++++++ src/decorators/index.ts | 2 +- src/decorators/injectable.decorator.ts | 14 -- src/decorators/module.decorator.ts | 1 - .../onMedusaEntityEvent.decorator.ts | 1 + src/decorators/utils.ts | 11 ++ src/index.ts | 2 +- src/loader.ts | 4 +- src/loaders/routes.loader.ts | 4 +- src/loaders/tests/entities.loader.spec.ts | 6 +- src/loaders/tests/middlewares.loader.spec.ts | 6 +- src/loaders/tests/repositories.loader.spec.ts | 15 +- src/metadata-reader.ts | 6 +- src/types.ts | 48 +++--- 47 files changed, 672 insertions(+), 331 deletions(-) create mode 100644 docs/modules/decorators_components_decorator.md delete mode 100644 docs/modules/decorators_injectable_decorator.md create mode 100644 docs/modules/decorators_utils.md create mode 100644 src/decorators/components.decorator.ts delete mode 100644 src/decorators/injectable.decorator.ts create mode 100644 src/decorators/utils.ts diff --git a/README.md b/README.md index d2de7c32..f044fca7 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ - [Create you server](#create-your-server) - [Create you first module](#create-your-first-module-rocket) - [Entity](#entity) + - [Migration](#migration) - [Repository](#repository) - [Service](#service) - [Middleware](#middleware) @@ -126,10 +127,10 @@ Let say that you want to add a new field on the `Product` entity. import { Product as MedusaProduct } from '@medusa/medusa/dist'; import { Column, Entity } from "typeorm"; -import { Injectable } from "medusa-extender"; +import { Entity as MedusaEntity } from "medusa-extender"; //... -@Injectable({ type: 'entity', override: MedusaProduct }) +@MedusaEntity({ override: MedusaProduct }) @Entity() class Product extends MedusaProduct { @Column() @@ -137,6 +138,28 @@ class Product extends MedusaProduct { } ``` +### Migration + +After have updated your entity, you will have to migrate the database in order to reflect the new fields. + +```typescript +// modules/product/20211126000001-add-field-to-product + +import { MigrationInterface, QueryRunner } from 'typeorm'; +import { Migration } from 'medusa-extender'; + +@Migration() +export default class AddFieldToProduct1611063162649 implements MigrationInterface { + name = 'addFieldToProduct1611063162649'; + + public async up(queryRunner: QueryRunner): Promise { + } + + public async down(queryRunner: QueryRunner): Promise { + } +} +``` + ### Repository We will then create a new repository to reflect our custom entity. @@ -146,11 +169,11 @@ We will then create a new repository to reflect our custom entity. import { ProductRepository as MedusaProductRepository } from '@medusa/medusa/dist/repositories/product'; import { EntityRepository, Repository } from "typeorm"; -import { Injectable, Utils } from "medusa-extender"; +import { Repository as MedusaRepository, Utils } from "medusa-extender"; import { Product } from "./product.entity"; //... -@Injectable({ type: 'repository', override: MedusaProductRepository }) +@MedusaRepository({ override: MedusaProductRepository }) @EntityRepository() class ProductRepository extends Repository { } @@ -169,7 +192,7 @@ We want now to add a custom service to implement our custom logic for our new fi ```typescript // modules/product/product.service.ts -import { Injectable } from 'medusa-extender'; +import { Service } from 'medusa-extender'; //... interface ConstructorParams { @@ -186,7 +209,7 @@ interface ConstructorParams { + } + + public async down(queryRunner: QueryRunner): Promise { + } +} +``` + ### Repository We will then create a new repository to reflect our custom entity. @@ -148,11 +171,11 @@ We will then create a new repository to reflect our custom entity. import { ProductRepository as MedusaProductRepository } from '@medusa/medusa/dist/repositories/product'; import { EntityRepository, Repository } from "typeorm"; -import { Injectable, Utils } from "medusa-extender"; +import { Repository as MedusaRepository, Utils } from "medusa-extender"; import { Product } from "./product.entity"; //... -@Injectable({ type: 'repository', override: MedusaProductRepository }) +@MedusaRepository({ override: MedusaProductRepository }) @EntityRepository() class ProductRepository extends Repository { } @@ -171,7 +194,7 @@ We want now to add a custom service to implement our custom logic for our new fi ```typescript // modules/product/product.service.ts -import { Injectable } from 'medusa-extender'; +import { Service } from 'medusa-extender'; //... interface ConstructorParams { @@ -188,7 +211,7 @@ interface ConstructorParams(`options`): `ClassDecorator` + +Mark a class as an entity to be used by the loader. + +#### Type parameters + +| Name | Type | +| :------ | :------ | +| `T` | `unknown` | + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `options` | [`EntityInjectableOptions`](types.md#entityinjectableoptions)<`T`\> | + +#### Returns + +`ClassDecorator` + +#### Defined in + +[src/decorators/components.decorator.ts:15](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/components.decorator.ts#L15) + +___ + +### Middleware + +▸ **Middleware**(`options`): `ClassDecorator` + +Mark a class as a middleware to be used by the loader. + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `options` | [`MiddlewareInjectionOptions`](types.md#middlewareinjectionoptions) | + +#### Returns + +`ClassDecorator` + +#### Defined in + +[src/decorators/components.decorator.ts:42](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/components.decorator.ts#L42) + +___ + +### Migration + +▸ **Migration**(): `ClassDecorator` + +Mark a class as a migration to be used by the loader. + +#### Returns + +`ClassDecorator` + +#### Defined in + +[src/decorators/components.decorator.ts:50](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/components.decorator.ts#L50) + +___ + +### Repository + +▸ **Repository**<`T`\>(`options`): `ClassDecorator` + +Mark a class as a repository to be used by the loader. + +#### Type parameters + +| Name | Type | +| :------ | :------ | +| `T` | `unknown` | + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `options` | [`RepositoryInjectableOptions`](types.md#repositoryinjectableoptions)<`T`\> | + +#### Returns + +`ClassDecorator` + +#### Defined in + +[src/decorators/components.decorator.ts:33](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/components.decorator.ts#L33) + +___ + +### Router + +▸ **Router**(`options`): `ClassDecorator` + +Mark a class as a router to be used by the loader. + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `options` | [`RouterInjectionOptions`](types.md#routerinjectionoptions) | + +#### Returns + +`ClassDecorator` + +#### Defined in + +[src/decorators/components.decorator.ts:59](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/components.decorator.ts#L59) + +___ + +### Service + +▸ **Service**<`T`\>(`options`): `ClassDecorator` + +Mark a class as a service to be used by the loader. + +#### Type parameters + +| Name | Type | +| :------ | :------ | +| `T` | `unknown` | + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `options` | [`ServiceInjectableOptions`](types.md#serviceinjectableoptions)<`T`\> | + +#### Returns + +`ClassDecorator` + +#### Defined in + +[src/decorators/components.decorator.ts:24](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/components.decorator.ts#L24) diff --git a/docs/modules/decorators_injectable_decorator.md b/docs/modules/decorators_injectable_decorator.md deleted file mode 100644 index 369484c8..00000000 --- a/docs/modules/decorators_injectable_decorator.md +++ /dev/null @@ -1,37 +0,0 @@ -[medusa-extender](../README.md) / [Exports](../modules.md) / decorators/injectable.decorator - -# Module: decorators/injectable.decorator - -## Table of contents - -### Functions - -- [Injectable](decorators_injectable_decorator.md#injectable) - -## Functions - -### Injectable - -▸ **Injectable**<`T`\>(`options`): `ClassDecorator` - -Mark a class as an injectable to be used by the loader. - -#### Type parameters - -| Name | Type | -| :------ | :------ | -| `T` | `unknown` | - -#### Parameters - -| Name | Type | -| :------ | :------ | -| `options` | [`InjectableOptions`](types.md#injectableoptions)<`T`\> | - -#### Returns - -`ClassDecorator` - -#### Defined in - -[src/decorators/injectable.decorator.ts:10](https://github.com/adrien2p/medusa-extender/blob/8756338/src/decorators/injectable.decorator.ts#L10) diff --git a/docs/modules/decorators_module_decorator.md b/docs/modules/decorators_module_decorator.md index d53e5291..9afb23cd 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:10](https://github.com/adrien2p/medusa-extender/blob/8756338/src/decorators/module.decorator.ts#L10) +[src/decorators/module.decorator.ts:9](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/module.decorator.ts#L9) diff --git a/docs/modules/decorators_onMedusaEntityEvent_decorator.md b/docs/modules/decorators_onMedusaEntityEvent_decorator.md index 4980974e..3ccaef56 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:10](https://github.com/adrien2p/medusa-extender/blob/8756338/src/decorators/onMedusaEntityEvent.decorator.ts#L10) +[src/decorators/onMedusaEntityEvent.decorator.ts:11](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/onMedusaEntityEvent.decorator.ts#L11) ___ @@ -47,7 +47,7 @@ ___ #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:5](https://github.com/adrien2p/medusa-extender/blob/8756338/src/decorators/onMedusaEntityEvent.decorator.ts#L5) +[src/decorators/onMedusaEntityEvent.decorator.ts:6](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/onMedusaEntityEvent.decorator.ts#L6) ___ @@ -66,7 +66,7 @@ Event types that can be emitted. #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:15](https://github.com/adrien2p/medusa-extender/blob/8756338/src/decorators/onMedusaEntityEvent.decorator.ts#L15) +[src/decorators/onMedusaEntityEvent.decorator.ts:16](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/onMedusaEntityEvent.decorator.ts#L16) ___ @@ -92,7 +92,7 @@ The arguments expected by the [OnMedusaEntityEvent](../classes/decorators_onMedu #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:24](https://github.com/adrien2p/medusa-extender/blob/8756338/src/decorators/onMedusaEntityEvent.decorator.ts#L24) +[src/decorators/onMedusaEntityEvent.decorator.ts:25](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/onMedusaEntityEvent.decorator.ts#L25) ___ @@ -118,4 +118,4 @@ The arguments expected by the event handler. #### Defined in -[src/decorators/onMedusaEntityEvent.decorator.ts:32](https://github.com/adrien2p/medusa-extender/blob/8756338/src/decorators/onMedusaEntityEvent.decorator.ts#L32) +[src/decorators/onMedusaEntityEvent.decorator.ts:33](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/onMedusaEntityEvent.decorator.ts#L33) diff --git a/docs/modules/decorators_utils.md b/docs/modules/decorators_utils.md new file mode 100644 index 00000000..ae575e72 --- /dev/null +++ b/docs/modules/decorators_utils.md @@ -0,0 +1,35 @@ +[medusa-extender](../README.md) / [Exports](../modules.md) / decorators/utils + +# Module: decorators/utils + +## Table of contents + +### Functions + +- [buildComponentDecorator](decorators_utils.md#buildcomponentdecorator) + +## Functions + +### buildComponentDecorator + +▸ **buildComponentDecorator**<`T`\>(`options`): `ClassDecorator` + +#### Type parameters + +| Name | +| :------ | +| `T` | + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `options` | [`MiddlewareInjectionOptions`](types.md#middlewareinjectionoptions) & { `type`: [`InjectableComponentTypes`](types.md#injectablecomponenttypes) } & [`RouterInjectionOptions`](types.md#routerinjectionoptions) & { `type`: [`InjectableComponentTypes`](types.md#injectablecomponenttypes) } & [`EntityInjectableOptions`](types.md#entityinjectableoptions)<`T`\> & { `type`: [`InjectableComponentTypes`](types.md#injectablecomponenttypes) } & [`ServiceInjectableOptions`](types.md#serviceinjectableoptions)<`T`\> & { `type`: [`InjectableComponentTypes`](types.md#injectablecomponenttypes) } & [`RepositoryInjectableOptions`](types.md#repositoryinjectableoptions)<`T`\> & { `type`: [`InjectableComponentTypes`](types.md#injectablecomponenttypes) } | + +#### Returns + +`ClassDecorator` + +#### Defined in + +[src/decorators/utils.ts:5](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/decorators/utils.ts#L5) diff --git a/docs/modules/event_emmiter.Internals.md b/docs/modules/event_emmiter.Internals.md index 2db29d65..bc6426e2 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/8756338/src/event-emmiter.ts#L9) +[src/event-emmiter.ts:9](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/event-emmiter.ts#L9) diff --git a/docs/modules/event_emmiter.md b/docs/modules/event_emmiter.md index 40a11abf..0ed19628 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:103](https://github.com/adrien2p/medusa-extender/blob/8756338/src/event-emmiter.ts#L103) +[src/event-emmiter.ts:103](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/event-emmiter.ts#L103) diff --git a/docs/modules/index.md b/docs/modules/index.md index a5a50684..e766c5d6 100644 --- a/docs/modules/index.md +++ b/docs/modules/index.md @@ -6,15 +6,15 @@ ### References -- [ComplexInjectableOptions](index.md#complexinjectableoptions) - [Constructor](index.md#constructor) +- [Entity](index.md#entity) - [EntityActions](index.md#entityactions) - [EntityEventActionOptions](index.md#entityeventactionoptions) - [EntityEventType](index.md#entityeventtype) +- [EntityInjectableOptions](index.md#entityinjectableoptions) - [GetInjectableOption](index.md#getinjectableoption) - [GetInjectableOptions](index.md#getinjectableoptions) - [INJECTABLE\_OPTIONS\_KEY](index.md#injectable_options_key) -- [Injectable](index.md#injectable) - [InjectableComponentTypes](index.md#injectablecomponenttypes) - [InjectableOptions](index.md#injectableoptions) - [MEDUSA\_RESOLVER\_KEYS](index.md#medusa_resolver_keys) @@ -27,13 +27,18 @@ - [MedusaRequest](index.md#medusarequest) - [MedusaRouteMethods](index.md#medusaroutemethods) - [MedusaRouteOptions](index.md#medusarouteoptions) +- [Middleware](index.md#middleware) - [MiddlewareInjectionOptions](index.md#middlewareinjectionoptions) -- [MigrationInjectionOptions](index.md#migrationinjectionoptions) +- [Migration](index.md#migration) - [Module](index.md#module) - [OnMedusaEntityEvent](index.md#onmedusaentityevent) +- [Repository](index.md#repository) - [RepositoryInjectableOptions](index.md#repositoryinjectableoptions) -- [RoutesInjectionOptions](index.md#routesinjectionoptions) +- [Router](index.md#router) +- [RouterInjectionOptions](index.md#routerinjectionoptions) - [RoutesInjectionRouterConfiguration](index.md#routesinjectionrouterconfiguration) +- [Service](index.md#service) +- [ServiceInjectableOptions](index.md#serviceinjectableoptions) - [Type](index.md#type) - [eventEmitter](index.md#eventemitter) @@ -43,15 +48,15 @@ ## References -### ComplexInjectableOptions +### Constructor -Re-exports [ComplexInjectableOptions](types.md#complexinjectableoptions) +Re-exports [Constructor](types.md#constructor) ___ -### Constructor +### Entity -Re-exports [Constructor](types.md#constructor) +Re-exports [Entity](decorators_components_decorator.md#entity) ___ @@ -73,6 +78,12 @@ Re-exports [EntityEventType](decorators_onMedusaEntityEvent_decorator.md#entitye ___ +### EntityInjectableOptions + +Re-exports [EntityInjectableOptions](types.md#entityinjectableoptions) + +___ + ### GetInjectableOption Re-exports [GetInjectableOption](types.md#getinjectableoption) @@ -91,12 +102,6 @@ Re-exports [INJECTABLE_OPTIONS_KEY](contants.md#injectable_options_key) ___ -### Injectable - -Re-exports [Injectable](decorators_injectable_decorator.md#injectable) - -___ - ### InjectableComponentTypes Re-exports [InjectableComponentTypes](types.md#injectablecomponenttypes) @@ -169,15 +174,21 @@ Re-exports [MedusaRouteOptions](types.md#medusarouteoptions) ___ +### Middleware + +Re-exports [Middleware](decorators_components_decorator.md#middleware) + +___ + ### MiddlewareInjectionOptions Re-exports [MiddlewareInjectionOptions](types.md#middlewareinjectionoptions) ___ -### MigrationInjectionOptions +### Migration -Re-exports [MigrationInjectionOptions](types.md#migrationinjectionoptions) +Re-exports [Migration](decorators_components_decorator.md#migration) ___ @@ -193,15 +204,27 @@ Re-exports [OnMedusaEntityEvent](../classes/decorators_onMedusaEntityEvent_decor ___ +### Repository + +Re-exports [Repository](decorators_components_decorator.md#repository) + +___ + ### RepositoryInjectableOptions Re-exports [RepositoryInjectableOptions](types.md#repositoryinjectableoptions) ___ -### RoutesInjectionOptions +### Router + +Re-exports [Router](decorators_components_decorator.md#router) + +___ + +### RouterInjectionOptions -Re-exports [RoutesInjectionOptions](types.md#routesinjectionoptions) +Re-exports [RouterInjectionOptions](types.md#routerinjectionoptions) ___ @@ -211,6 +234,18 @@ Re-exports [RoutesInjectionRouterConfiguration](types.md#routesinjectionrouterco ___ +### Service + +Re-exports [Service](decorators_components_decorator.md#service) + +___ + +### ServiceInjectableOptions + +Re-exports [ServiceInjectableOptions](types.md#serviceinjectableoptions) + +___ + ### Type Re-exports [Type](../interfaces/types.Type.md) @@ -236,4 +271,4 @@ Renames and re-exports [customEventEmitter](event_emmiter.md#customeventemitter) #### Defined in -[src/index.ts:20](https://github.com/adrien2p/medusa-extender/blob/8756338/src/index.ts#L20) +[src/index.ts:20](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/index.ts#L20) diff --git a/docs/modules/loaders.md b/docs/modules/loaders.md index ace887f1..19d47ce7 100644 --- a/docs/modules/loaders.md +++ b/docs/modules/loaders.md @@ -6,8 +6,8 @@ ### References -- [ComplexInjectableOptions](loaders.md#complexinjectableoptions) - [Constructor](loaders.md#constructor) +- [EntityInjectableOptions](loaders.md#entityinjectableoptions) - [GetInjectableOption](loaders.md#getinjectableoption) - [GetInjectableOptions](loaders.md#getinjectableoptions) - [InjectableComponentTypes](loaders.md#injectablecomponenttypes) @@ -18,10 +18,10 @@ - [MedusaRouteMethods](loaders.md#medusaroutemethods) - [MedusaRouteOptions](loaders.md#medusarouteoptions) - [MiddlewareInjectionOptions](loaders.md#middlewareinjectionoptions) -- [MigrationInjectionOptions](loaders.md#migrationinjectionoptions) - [RepositoryInjectableOptions](loaders.md#repositoryinjectableoptions) -- [RoutesInjectionOptions](loaders.md#routesinjectionoptions) +- [RouterInjectionOptions](loaders.md#routerinjectionoptions) - [RoutesInjectionRouterConfiguration](loaders.md#routesinjectionrouterconfiguration) +- [ServiceInjectableOptions](loaders.md#serviceinjectableoptions) - [Type](loaders.md#type) - [Utils](loaders.md#utils) - [apiLoader](loaders.md#apiloader) @@ -44,15 +44,15 @@ ## References -### ComplexInjectableOptions +### Constructor -Re-exports [ComplexInjectableOptions](types.md#complexinjectableoptions) +Re-exports [Constructor](types.md#constructor) ___ -### Constructor +### EntityInjectableOptions -Re-exports [Constructor](types.md#constructor) +Re-exports [EntityInjectableOptions](types.md#entityinjectableoptions) ___ @@ -116,21 +116,15 @@ Re-exports [MiddlewareInjectionOptions](types.md#middlewareinjectionoptions) ___ -### MigrationInjectionOptions - -Re-exports [MigrationInjectionOptions](types.md#migrationinjectionoptions) - -___ - ### RepositoryInjectableOptions Re-exports [RepositoryInjectableOptions](types.md#repositoryinjectableoptions) ___ -### RoutesInjectionOptions +### RouterInjectionOptions -Re-exports [RoutesInjectionOptions](types.md#routesinjectionoptions) +Re-exports [RouterInjectionOptions](types.md#routerinjectionoptions) ___ @@ -140,6 +134,12 @@ Re-exports [RoutesInjectionRouterConfiguration](types.md#routesinjectionrouterco ___ +### ServiceInjectableOptions + +Re-exports [ServiceInjectableOptions](types.md#serviceinjectableoptions) + +___ + ### Type Re-exports [Type](../interfaces/types.Type.md) diff --git a/docs/modules/loaders_api_loader.md b/docs/modules/loaders_api_loader.md index 1da4c8a3..f28059d1 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/8756338/src/loaders/api.loader.ts#L12) +[src/loaders/api.loader.ts:12](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/api.loader.ts#L12) diff --git a/docs/modules/loaders_database_loader.md b/docs/modules/loaders_database_loader.md index 62da5752..ad3df9aa 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/8756338/src/loaders/database.loader.ts#L5) +[src/loaders/database.loader.ts:5](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/database.loader.ts#L5) diff --git a/docs/modules/loaders_entities_loader.md b/docs/modules/loaders_entities_loader.md index 2f5092a2..2977322d 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/8756338/src/loaders/entities.loader.ts#L5) +[src/loaders/entities.loader.ts:5](https://github.com/adrien2p/medusa-extender/blob/1127b65/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/8756338/src/loaders/entities.loader.ts#L16) +[src/loaders/entities.loader.ts:16](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/entities.loader.ts#L16) ___ @@ -73,7 +73,7 @@ Load custom entity and override existing ones. #### Defined in -[src/loaders/entities.loader.ts:50](https://github.com/adrien2p/medusa-extender/blob/8756338/src/loaders/entities.loader.ts#L50) +[src/loaders/entities.loader.ts:50](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/entities.loader.ts#L50) ___ @@ -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/8756338/src/loaders/entities.loader.ts#L30) +[src/loaders/entities.loader.ts:30](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/entities.loader.ts#L30) diff --git a/docs/modules/loaders_middlewares_loader.md b/docs/modules/loaders_middlewares_loader.md index ff22302f..5b2459a0 100644 --- a/docs/modules/loaders_middlewares_loader.md +++ b/docs/modules/loaders_middlewares_loader.md @@ -31,4 +31,4 @@ Register custom middlewares into the middlewareRegistery. #### Defined in -[src/loaders/middlewares.loader.ts:13](https://github.com/adrien2p/medusa-extender/blob/8756338/src/loaders/middlewares.loader.ts#L13) +[src/loaders/middlewares.loader.ts:13](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/middlewares.loader.ts#L13) diff --git a/docs/modules/loaders_migrations_loader.md b/docs/modules/loaders_migrations_loader.md index fceddc6a..f094c9a2 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/8756338/src/loaders/migrations.loader.ts#L10) +[src/loaders/migrations.loader.ts:10](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/migrations.loader.ts#L10) diff --git a/docs/modules/loaders_plugins_loader.md b/docs/modules/loaders_plugins_loader.md index 5889ab95..3fd74c98 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/8756338/src/loaders/plugins.loader.ts#L11) +[src/loaders/plugins.loader.ts:11](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/plugins.loader.ts#L11) diff --git a/docs/modules/loaders_repository_loader.md b/docs/modules/loaders_repository_loader.md index c49f65ee..a95161d6 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/8756338/src/loaders/repository.loader.ts#L16) +[src/loaders/repository.loader.ts:16](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/repository.loader.ts#L16) ___ @@ -52,7 +52,7 @@ Load custom repositories and override existing ones. #### Defined in -[src/loaders/repository.loader.ts:49](https://github.com/adrien2p/medusa-extender/blob/8756338/src/loaders/repository.loader.ts#L49) +[src/loaders/repository.loader.ts:49](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/repository.loader.ts#L49) ___ @@ -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/8756338/src/loaders/repository.loader.ts#L30) +[src/loaders/repository.loader.ts:30](https://github.com/adrien2p/medusa-extender/blob/1127b65/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/8756338/src/loaders/repository.loader.ts#L5) +[src/loaders/repository.loader.ts:5](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/repository.loader.ts#L5) diff --git a/docs/modules/loaders_routes_loader.md b/docs/modules/loaders_routes_loader.md index 9eac34fa..0bef4cd3 100644 --- a/docs/modules/loaders_routes_loader.md +++ b/docs/modules/loaders_routes_loader.md @@ -19,7 +19,7 @@ | Name | Type | | :------ | :------ | -| `routesOptions` | [`GetInjectableOptions`](types.md#getinjectableoptions)<``"route"``\> | +| `routesOptions` | [`GetInjectableOptions`](types.md#getinjectableoptions)<``"router"``\> | | `app` | `Express` | #### Returns @@ -28,7 +28,7 @@ #### Defined in -[src/loaders/routes.loader.ts:4](https://github.com/adrien2p/medusa-extender/blob/8756338/src/loaders/routes.loader.ts#L4) +[src/loaders/routes.loader.ts:4](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/routes.loader.ts#L4) ___ @@ -40,7 +40,7 @@ ___ | Name | Type | | :------ | :------ | -| `routesOptions` | [`GetInjectableOptions`](types.md#getinjectableoptions)<``"route"``\> | +| `routesOptions` | [`GetInjectableOptions`](types.md#getinjectableoptions)<``"router"``\> | | `app` | `Express` | #### Returns @@ -49,4 +49,4 @@ ___ #### Defined in -[src/loaders/routes.loader.ts:14](https://github.com/adrien2p/medusa-extender/blob/8756338/src/loaders/routes.loader.ts#L14) +[src/loaders/routes.loader.ts:14](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/routes.loader.ts#L14) diff --git a/docs/modules/loaders_services_loader.md b/docs/modules/loaders_services_loader.md index 4d6cd2d5..2fbb7bec 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:56](https://github.com/adrien2p/medusa-extender/blob/8756338/src/loaders/services.loader.ts#L56) +[src/loaders/services.loader.ts:56](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/services.loader.ts#L56) ___ @@ -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/8756338/src/loaders/services.loader.ts#L31) +[src/loaders/services.loader.ts:31](https://github.com/adrien2p/medusa-extender/blob/1127b65/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/8756338/src/loaders/services.loader.ts#L9) +[src/loaders/services.loader.ts:9](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/loaders/services.loader.ts#L9) diff --git a/docs/modules/metadata_reader.md b/docs/modules/metadata_reader.md index ddbfcad8..8984dfb6 100644 --- a/docs/modules/metadata_reader.md +++ b/docs/modules/metadata_reader.md @@ -17,7 +17,7 @@ ### componentsMetadataReader -▸ **componentsMetadataReader**<`TComponentType`\>(`component`): [`InjectableOptions`](types.md#injectableoptions)<`TComponentType`\> +▸ **componentsMetadataReader**<`TComponentType`\>(`component`): [`InjectableOptions`](types.md#injectableoptions)<`TComponentType`\> & { `type`: [`InjectableComponentTypes`](types.md#injectablecomponenttypes) } **`internal`** Return the options from components. @@ -36,11 +36,11 @@ Return the options from components. #### Returns -[`InjectableOptions`](types.md#injectableoptions)<`TComponentType`\> +[`InjectableOptions`](types.md#injectableoptions)<`TComponentType`\> & { `type`: [`InjectableComponentTypes`](types.md#injectablecomponenttypes) } #### Defined in -[src/metadata-reader.ts:43](https://github.com/adrien2p/medusa-extender/blob/8756338/src/metadata-reader.ts#L43) +[src/metadata-reader.ts:43](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/metadata-reader.ts#L43) ___ @@ -62,4 +62,4 @@ Read all metadata from the imported modules and extract components that will be #### Defined in -[src/metadata-reader.ts:15](https://github.com/adrien2p/medusa-extender/blob/8756338/src/metadata-reader.ts#L15) +[src/metadata-reader.ts:15](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/metadata-reader.ts#L15) diff --git a/docs/modules/types.md b/docs/modules/types.md index e759680b..48a37faa 100644 --- a/docs/modules/types.md +++ b/docs/modules/types.md @@ -11,8 +11,8 @@ ### Type aliases -- [ComplexInjectableOptions](types.md#complexinjectableoptions) - [Constructor](types.md#constructor) +- [EntityInjectableOptions](types.md#entityinjectableoptions) - [GetInjectableOption](types.md#getinjectableoption) - [GetInjectableOptions](types.md#getinjectableoptions) - [InjectableComponentTypes](types.md#injectablecomponenttypes) @@ -22,69 +22,68 @@ - [MedusaRouteMethods](types.md#medusaroutemethods) - [MedusaRouteOptions](types.md#medusarouteoptions) - [MiddlewareInjectionOptions](types.md#middlewareinjectionoptions) -- [MigrationInjectionOptions](types.md#migrationinjectionoptions) - [RepositoryInjectableOptions](types.md#repositoryinjectableoptions) -- [RoutesInjectionOptions](types.md#routesinjectionoptions) +- [RouterInjectionOptions](types.md#routerinjectionoptions) - [RoutesInjectionRouterConfiguration](types.md#routesinjectionrouterconfiguration) +- [ServiceInjectableOptions](types.md#serviceinjectableoptions) ## Type aliases -### ComplexInjectableOptions - -Ƭ **ComplexInjectableOptions**<`TOverride`\>: `Object` +### Constructor -Defines the injection options for services, entities. +Ƭ **Constructor**<`T`\>: (...`args`: `unknown`[]) => `T` #### Type parameters -| Name | Type | -| :------ | :------ | -| `TOverride` | `unknown` | +| Name | +| :------ | +| `T` | #### Type declaration +• (...`args`) + +##### Parameters + | Name | Type | | :------ | :------ | -| `override?` | [`Type`](../interfaces/types.Type.md)<`TOverride`\> | -| `resolutionKey?` | `string` | -| `scope?` | `LifetimeType` | -| `type` | `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"entity"`` \| ``"service"``\> | +| `...args` | `unknown`[] | #### Defined in -[src/types.ts:18](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L18) +[src/types.ts:8](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L8) ___ -### Constructor +### EntityInjectableOptions -Ƭ **Constructor**<`T`\>: (...`args`: `unknown`[]) => `T` +Ƭ **EntityInjectableOptions**<`TOverride`\>: `Object` + +Defines the injection options for entities. #### Type parameters -| Name | -| :------ | -| `T` | +| Name | Type | +| :------ | :------ | +| `TOverride` | `unknown` | #### Type declaration -• (...`args`) - -##### Parameters - | Name | Type | | :------ | :------ | -| `...args` | `unknown`[] | +| `override?` | [`Type`](../interfaces/types.Type.md)<`TOverride`\> | +| `resolutionKey?` | `string` | +| `scope?` | `LifetimeType` | #### Defined in -[src/types.ts:8](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L8) +[src/types.ts:18](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L18) ___ ### GetInjectableOption -Ƭ **GetInjectableOption**<`TComponentType`\>: `TComponentType` extends `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"entity"`` \| ``"service"``\> ? [`ComplexInjectableOptions`](types.md#complexinjectableoptions) : `TComponentType` extends `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"repository"``\> ? [`RepositoryInjectableOptions`](types.md#repositoryinjectableoptions) : `TComponentType` extends `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"migration"``\> ? [`MigrationInjectionOptions`](types.md#migrationinjectionoptions) : `TComponentType` extends `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"route"``\> ? [`RoutesInjectionOptions`](types.md#routesinjectionoptions) : `TComponentType` extends `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"middleware"``\> ? [`MiddlewareInjectionOptions`](types.md#middlewareinjectionoptions) : `never` & { `metatype`: `TComponentType` extends ``"middleware"`` ? [`Type`](../interfaces/types.Type.md)<[`MedusaMiddleware`](../interfaces/types.MedusaMiddleware.md)\> : [`Type`](../interfaces/types.Type.md) } +Ƭ **GetInjectableOption**<`TComponentType`\>: `TComponentType` extends `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"entity"``\> ? [`EntityInjectableOptions`](types.md#entityinjectableoptions) : `TComponentType` extends `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"service"``\> ? [`ServiceInjectableOptions`](types.md#serviceinjectableoptions) : `TComponentType` extends `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"repository"``\> ? [`RepositoryInjectableOptions`](types.md#repositoryinjectableoptions) : `TComponentType` extends `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"migration"``\> ? `any` : `TComponentType` extends `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"router"``\> ? [`RouterInjectionOptions`](types.md#routerinjectionoptions) : `TComponentType` extends `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"middleware"``\> ? [`MiddlewareInjectionOptions`](types.md#middlewareinjectionoptions) : `never` & { `metatype`: `TComponentType` extends ``"middleware"`` ? [`Type`](../interfaces/types.Type.md)<[`MedusaMiddleware`](../interfaces/types.MedusaMiddleware.md)\> : [`Type`](../interfaces/types.Type.md) ; `type`: [`InjectableComponentTypes`](types.md#injectablecomponenttypes) } Determine which options type it actually is depending on the component type. @@ -96,7 +95,7 @@ Determine which options type it actually is depending on the component type. #### Defined in -[src/types.ts:82](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L82) +[src/types.ts:79](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L79) ___ @@ -114,25 +113,25 @@ Determine which options type it actually is depending on the component type. #### Defined in -[src/types.ts:98](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L98) +[src/types.ts:100](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L100) ___ ### InjectableComponentTypes -Ƭ **InjectableComponentTypes**: ``"entity"`` \| ``"repository"`` \| ``"service"`` \| ``"migration"`` \| ``"route"`` \| ``"middleware"`` +Ƭ **InjectableComponentTypes**: ``"entity"`` \| ``"repository"`` \| ``"service"`` \| ``"migration"`` \| ``"router"`` \| ``"middleware"`` Components that does not required any other options that Type. #### Defined in -[src/types.ts:13](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L13) +[src/types.ts:13](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L13) ___ ### InjectableOptions -Ƭ **InjectableOptions**<`T`\>: [`ComplexInjectableOptions`](types.md#complexinjectableoptions)<`T`\> \| [`RepositoryInjectableOptions`](types.md#repositoryinjectableoptions)<`T`\> \| [`MigrationInjectionOptions`](types.md#migrationinjectionoptions) \| [`MiddlewareInjectionOptions`](types.md#middlewareinjectionoptions) \| [`RoutesInjectionOptions`](types.md#routesinjectionoptions) +Ƭ **InjectableOptions**<`T`\>: [`EntityInjectableOptions`](types.md#entityinjectableoptions)<`T`\> \| [`ServiceInjectableOptions`](types.md#serviceinjectableoptions)<`T`\> \| [`RepositoryInjectableOptions`](types.md#repositoryinjectableoptions)<`T`\> \| [`MiddlewareInjectionOptions`](types.md#middlewareinjectionoptions) \| [`RouterInjectionOptions`](types.md#routerinjectionoptions) Union of all options type possible for injectable. @@ -144,7 +143,7 @@ Union of all options type possible for injectable. #### Defined in -[src/types.ts:72](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L72) +[src/types.ts:69](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L69) ___ @@ -156,7 +155,7 @@ Describe the expected request from a middleware [MedusaMiddleware](../interfaces #### Defined in -[src/types.ts:111](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L111) +[src/types.ts:113](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L113) ___ @@ -175,7 +174,7 @@ Medusa request extended. #### Defined in -[src/types.ts:104](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L104) +[src/types.ts:106](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L106) ___ @@ -185,7 +184,7 @@ ___ #### Defined in -[src/types.ts:118](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L118) +[src/types.ts:120](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L120) ___ @@ -204,7 +203,7 @@ Describe all the available options for the Route [MedusaMiddleware](../interface #### Defined in -[src/types.ts:123](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L123) +[src/types.ts:125](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L125) ___ @@ -220,29 +219,10 @@ Defines the injection options for middlewares. | :------ | :------ | | `requireAuth` | `boolean` | | `routerOptions` | [`MedusaRouteOptions`](types.md#medusarouteoptions)[] | -| `type` | `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"middleware"``\> | - -#### Defined in - -[src/types.ts:63](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L63) - -___ - -### MigrationInjectionOptions - -Ƭ **MigrationInjectionOptions**: `Object` - -Defines the injection options for migrations. - -#### Type declaration - -| Name | Type | -| :------ | :------ | -| `type` | `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"migration"``\> | #### Defined in -[src/types.ts:38](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L38) +[src/types.ts:61](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L61) ___ @@ -264,18 +244,16 @@ Defines the injection options for services, entities, repositories. | :------ | :------ | | `override?` | [`Type`](../interfaces/types.Type.md)<`TOverride`\> | | `resolutionKey?` | `string` | -| `scope?` | `LifetimeType` | -| `type` | `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"repository"``\> | #### Defined in -[src/types.ts:28](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L28) +[src/types.ts:36](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L36) ___ -### RoutesInjectionOptions +### RouterInjectionOptions -Ƭ **RoutesInjectionOptions**: `Object` +Ƭ **RouterInjectionOptions**: `Object` Defines the injection options for routes. @@ -284,11 +262,10 @@ Defines the injection options for routes. | Name | Type | | :------ | :------ | | `router` | [`RoutesInjectionRouterConfiguration`](types.md#routesinjectionrouterconfiguration)[] | -| `type` | `Extract`<[`InjectableComponentTypes`](types.md#injectablecomponenttypes), ``"route"``\> | #### Defined in -[src/types.ts:55](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L55) +[src/types.ts:54](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L54) ___ @@ -296,7 +273,7 @@ ___ Ƭ **RoutesInjectionRouterConfiguration**: `Object` -Define a route and its configuration. +Define a router and its configuration. #### Type declaration @@ -309,4 +286,30 @@ Define a route and its configuration. #### Defined in -[src/types.ts:45](https://github.com/adrien2p/medusa-extender/blob/8756338/src/types.ts#L45) +[src/types.ts:44](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L44) + +___ + +### ServiceInjectableOptions + +Ƭ **ServiceInjectableOptions**<`TOverride`\>: `Object` + +Defines the injection options for service. + +#### Type parameters + +| Name | Type | +| :------ | :------ | +| `TOverride` | `unknown` | + +#### Type declaration + +| Name | Type | +| :------ | :------ | +| `override?` | [`Type`](../interfaces/types.Type.md)<`TOverride`\> | +| `resolutionKey?` | `string` | +| `scope?` | `LifetimeType` | + +#### Defined in + +[src/types.ts:27](https://github.com/adrien2p/medusa-extender/blob/1127b65/src/types.ts#L27) diff --git a/src/decorators/components.decorator.ts b/src/decorators/components.decorator.ts new file mode 100644 index 00000000..5079e4dc --- /dev/null +++ b/src/decorators/components.decorator.ts @@ -0,0 +1,61 @@ +import { + EntityInjectableOptions, + MiddlewareInjectionOptions, + RepositoryInjectableOptions, + RouterInjectionOptions, + ServiceInjectableOptions, +} from './'; +import { buildComponentDecorator } from './utils'; + +/** + * Mark a class as an entity to be used by the loader. + * @param options + * @constructor + */ +export function Entity(options: EntityInjectableOptions): ClassDecorator { + return buildComponentDecorator({ type: 'entity', ...options }); +} + +/** + * Mark a class as a service to be used by the loader. + * @param options + * @constructor + */ +export function Service(options: ServiceInjectableOptions): ClassDecorator { + return buildComponentDecorator({ type: 'service', ...options }); +} + +/** + * Mark a class as a repository to be used by the loader. + * @param options + * @constructor + */ +export function Repository(options: RepositoryInjectableOptions): ClassDecorator { + return buildComponentDecorator({ type: 'repository', ...options }); +} + +/** + * Mark a class as a middleware to be used by the loader. + * @param options + * @constructor + */ +export function Middleware(options: MiddlewareInjectionOptions): ClassDecorator { + return buildComponentDecorator({ type: 'middleware', ...options }); +} + +/** + * Mark a class as a migration to be used by the loader. + * @constructor + */ +export function Migration(): ClassDecorator { + return buildComponentDecorator({ type: 'migration' }); +} + +/** + * Mark a class as a router to be used by the loader. + * @param options + * @constructor + */ +export function Router(options: RouterInjectionOptions): ClassDecorator { + return buildComponentDecorator({ type: 'router', ...options }); +} diff --git a/src/decorators/index.ts b/src/decorators/index.ts index ede894bb..88d6670c 100644 --- a/src/decorators/index.ts +++ b/src/decorators/index.ts @@ -1,5 +1,5 @@ export * from './module.decorator'; -export * from './injectable.decorator'; +export * from './components.decorator'; export * from './onMedusaEntityEvent.decorator'; export * from '../contants'; diff --git a/src/decorators/injectable.decorator.ts b/src/decorators/injectable.decorator.ts deleted file mode 100644 index 9eef27ed..00000000 --- a/src/decorators/injectable.decorator.ts +++ /dev/null @@ -1,14 +0,0 @@ -import 'reflect-metadata'; - -import { INJECTABLE_OPTIONS_KEY, InjectableOptions } from './'; - -/** - * Mark a class as an injectable to be used by the loader. - * @param options - * @constructor - */ -export function Injectable(options: InjectableOptions): ClassDecorator { - return (target: object) => { - Reflect.defineMetadata(INJECTABLE_OPTIONS_KEY, options, target); - }; -} diff --git a/src/decorators/module.decorator.ts b/src/decorators/module.decorator.ts index 4c3a66e4..526dc759 100644 --- a/src/decorators/module.decorator.ts +++ b/src/decorators/module.decorator.ts @@ -1,5 +1,4 @@ import 'reflect-metadata'; - import { MODULE_KEY, Type } from './'; /** diff --git a/src/decorators/onMedusaEntityEvent.decorator.ts b/src/decorators/onMedusaEntityEvent.decorator.ts index c0fcd316..fd026479 100644 --- a/src/decorators/onMedusaEntityEvent.decorator.ts +++ b/src/decorators/onMedusaEntityEvent.decorator.ts @@ -1,3 +1,4 @@ +import 'reflect-metadata'; import { Type } from '../types'; import { customEventEmitter } from '../event-emmiter'; import { EntityManager, InsertEvent, RemoveEvent, UpdateEvent } from 'typeorm'; diff --git a/src/decorators/utils.ts b/src/decorators/utils.ts new file mode 100644 index 00000000..0b89a590 --- /dev/null +++ b/src/decorators/utils.ts @@ -0,0 +1,11 @@ +import 'reflect-metadata'; +import { InjectableComponentTypes, InjectableOptions } from './'; +import { INJECTABLE_OPTIONS_KEY } from '../contants'; + +export function buildComponentDecorator( + options: InjectableOptions & { type: InjectableComponentTypes } +): ClassDecorator { + return (target: object) => { + Reflect.defineMetadata(INJECTABLE_OPTIONS_KEY, options, target); + }; +} diff --git a/src/index.ts b/src/index.ts index 455f5698..17b50523 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,6 @@ export { Medusa } from './medusa'; export { customEventEmitter as eventEmitter } from './event-emmiter'; export { - Injectable, Module, OnMedusaEntityEvent, EntityEventActionOptions, @@ -14,6 +13,7 @@ export { MedusaEventEmittedParams, MedusaEventHandlerParams, } from './decorators'; +export * from './decorators/components.decorator'; import { Utils as _Utils } from './utils'; diff --git a/src/loader.ts b/src/loader.ts index d7df2688..e26cc325 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -41,14 +41,14 @@ export class Loader { await databaseLoader(moduleComponentsOptions.get('entity'), moduleComponentsOptions.get('repository')); await pluginsLoadersAndListeners(express); await servicesLoader(moduleComponentsOptions.get('service')); - unauthenticatedRoutesLoader(moduleComponentsOptions.get('route'), express); + unauthenticatedRoutesLoader(moduleComponentsOptions.get('router'), express); const { app, container, dbConnection } = await loaders({ directory: rootDir, expressApp: express, }); - authenticatedRoutesLoader(moduleComponentsOptions.get('route'), app); + authenticatedRoutesLoader(moduleComponentsOptions.get('router'), app); await migrationsLoader(moduleComponentsOptions.get('migration'), dbConnection); diff --git a/src/loaders/routes.loader.ts b/src/loaders/routes.loader.ts index 2da51ef8..041aaa47 100644 --- a/src/loaders/routes.loader.ts +++ b/src/loaders/routes.loader.ts @@ -1,7 +1,7 @@ import { GetInjectableOptions, MedusaAuthenticatedRequest, RoutesInjectionRouterConfiguration } from './'; import { Express, NextFunction, Request, Response } from 'express'; -export function authenticatedRoutesLoader(routesOptions: GetInjectableOptions<'route'>, app: Express): void { +export function authenticatedRoutesLoader(routesOptions: GetInjectableOptions<'router'>, app: Express): void { for (const routeOptions of routesOptions) { routeOptions.router.forEach((router) => { if (!!router.requiredAuth) { @@ -11,7 +11,7 @@ export function authenticatedRoutesLoader(routesOptions: GetInjectableOptions<'r } } -export function unauthenticatedRoutesLoader(routesOptions: GetInjectableOptions<'route'>, app: Express): void { +export function unauthenticatedRoutesLoader(routesOptions: GetInjectableOptions<'router'>, app: Express): void { for (const routeOptions of routesOptions) { routeOptions.router.forEach((router) => { if (!router.requiredAuth) { diff --git a/src/loaders/tests/entities.loader.spec.ts b/src/loaders/tests/entities.loader.spec.ts index 7f7601fe..76fb542f 100644 --- a/src/loaders/tests/entities.loader.spec.ts +++ b/src/loaders/tests/entities.loader.spec.ts @@ -6,11 +6,11 @@ import { User as MedusaUser } from '@medusajs/medusa/dist'; import { asArray } from './utils/asArray'; import { entitiesLoader, overrideEntitiesLoader } from '../entities.loader'; import { asValue, createContainer } from 'awilix'; -import { Injectable, Module } from '../../decorators'; +import { Entity as MedusaEntity, Module } from '../../decorators'; import { metadataReader } from '../../metadata-reader'; import { Entity } from 'typeorm'; -@Injectable({ type: 'entity', override: MedusaUser }) +@MedusaEntity({ override: MedusaUser }) @Entity() class User extends MedusaUser { testProperty = 'I am the property from User that extend MedusaUser'; @@ -19,7 +19,7 @@ class User extends MedusaUser { @Module({ imports: [User] }) class UserModule {} -@Injectable({ type: 'entity', resolutionKey: 'anotherEntity' }) +@MedusaEntity({ resolutionKey: 'anotherEntity' }) @Entity() class Another {} diff --git a/src/loaders/tests/middlewares.loader.spec.ts b/src/loaders/tests/middlewares.loader.spec.ts index 95b6a2cf..063306a3 100644 --- a/src/loaders/tests/middlewares.loader.spec.ts +++ b/src/loaders/tests/middlewares.loader.spec.ts @@ -3,7 +3,7 @@ import 'core-js/stable'; import 'regenerator-runtime/runtime'; import { asValue, createContainer } from 'awilix'; -import { Injectable } from '../../decorators/injectable.decorator'; +import { Middleware } from '../../decorators/components.decorator'; import { MedusaAuthenticatedRequest, MedusaMiddleware } from '../../types'; import { NextFunction, Request, Response } from 'express'; import { middlewaresLoader } from '../middlewares.loader'; @@ -17,7 +17,7 @@ const MiddlewareServiceMock = { addPostAuthentication: jest.fn(), }; -@Injectable({ type: 'middleware', requireAuth: false, routerOptions: [{ path: '/admin/test', method: 'get' }] }) +@Middleware({ requireAuth: false, routerOptions: [{ path: '/admin/test', method: 'get' }] }) class PreAuthUserMiddleware implements MedusaMiddleware { consume(): (req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction) => void | Promise { return function () { @@ -26,7 +26,7 @@ class PreAuthUserMiddleware implements MedusaMiddleware { } } -@Injectable({ type: 'middleware', requireAuth: true, routerOptions: [{ path: '/admin/test', method: 'get' }] }) +@Middleware({ requireAuth: true, routerOptions: [{ path: '/admin/test', method: 'get' }] }) class PostAuthUserMiddleware implements MedusaMiddleware { consume(): (req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction) => void | Promise { return function () { diff --git a/src/loaders/tests/repositories.loader.spec.ts b/src/loaders/tests/repositories.loader.spec.ts index bade6ba5..ef83582f 100644 --- a/src/loaders/tests/repositories.loader.spec.ts +++ b/src/loaders/tests/repositories.loader.spec.ts @@ -8,16 +8,16 @@ import { overrideRepositoriesLoader, repositoriesLoader } from '../repository.lo import { createContainer } from 'awilix'; import { Entity, Repository, EntityRepository } from 'typeorm'; import { Utils } from '../../utils'; -import { Injectable, Module } from '../../decorators'; +import { Repository as MedusaRepository, Entity as MedusaEntity, Module } from '../../decorators'; import { metadataReader } from '../../metadata-reader'; -@Injectable({ type: 'entity', override: MedusaOrder }) +@MedusaEntity({ override: MedusaOrder }) @Entity() class Order extends MedusaOrder { testPropertyOrder = 'toto'; } -@Injectable({ type: 'repository', override: MedusaOrderRepository }) +@MedusaRepository({ override: MedusaOrderRepository }) @EntityRepository() class OrderRepository extends Repository { testProperty = 'I am the property from UserRepository that extend MedusaOrderRepository'; @@ -28,14 +28,11 @@ Utils.repositoryMixin(OrderRepository, MedusaOrderRepository); @Module({ imports: [OrderRepository] }) class OrderModule {} -@Injectable({ type: 'entity', resolutionKey: 'another' }) +@MedusaEntity({ resolutionKey: 'anotherEntity' }) @Entity() -class Another { - static isHandledByMedusa = true; - static resolutionKey = 'anotherEntity'; -} +class Another {} -@Injectable({ type: 'repository', resolutionKey: 'anotherRepository' }) +@MedusaRepository({ resolutionKey: 'anotherRepository' }) @EntityRepository() class AnotherRepository extends Repository {} diff --git a/src/metadata-reader.ts b/src/metadata-reader.ts index cb4b1821..836927b2 100644 --- a/src/metadata-reader.ts +++ b/src/metadata-reader.ts @@ -22,7 +22,7 @@ export function metadataReader(modules: Type[]): CustomMap { const options = componentsMetadataReader(component); let metatype; - if (options.type === 'repository' && !!options.override) { + if (options.type === 'repository' && 'override' in options) { metatype = Utils.repositoryMixin(component, options.override); } else { metatype = component; @@ -40,6 +40,8 @@ export function metadataReader(modules: Type[]): CustomMap { * Return the options from components. * @param component */ -export function componentsMetadataReader(component: Type): InjectableOptions { +export function componentsMetadataReader( + component: Type +): InjectableOptions & { type: InjectableComponentTypes } { return Reflect.getMetadata(INJECTABLE_OPTIONS_KEY, component); } diff --git a/src/types.ts b/src/types.ts index a7ff44d2..3b830103 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,37 +10,36 @@ export type Constructor = new (...args: unknown[]) => T; /** * Components that does not required any other options that Type. */ -export type InjectableComponentTypes = 'entity' | 'repository' | 'service' | 'migration' | 'route' | 'middleware'; +export type InjectableComponentTypes = 'entity' | 'repository' | 'service' | 'migration' | 'router' | 'middleware'; /** - * Defines the injection options for services, entities. + * Defines the injection options for entities. */ -export type ComplexInjectableOptions = { - type: Extract; +export type EntityInjectableOptions = { scope?: LifetimeType; resolutionKey?: string; override?: Type; }; /** - * Defines the injection options for services, entities, repositories. + * Defines the injection options for service. */ -export type RepositoryInjectableOptions = { - type: Extract; +export type ServiceInjectableOptions = { scope?: LifetimeType; resolutionKey?: string; override?: Type; }; /** - * Defines the injection options for migrations. + * Defines the injection options for services, entities, repositories. */ -export type MigrationInjectionOptions = { - type: Extract; +export type RepositoryInjectableOptions = { + resolutionKey?: string; + override?: Type; }; /** - * Define a route and its configuration. + * Define a router and its configuration. */ export type RoutesInjectionRouterConfiguration = { requiredAuth: boolean; @@ -52,8 +51,7 @@ export type RoutesInjectionRouterConfiguration = { /** * Defines the injection options for routes. */ -export type RoutesInjectionOptions = { - type: Extract; +export type RouterInjectionOptions = { router: RoutesInjectionRouterConfiguration[]; }; @@ -61,7 +59,6 @@ export type RoutesInjectionOptions = { * Defines the injection options for middlewares. */ export type MiddlewareInjectionOptions = { - type: Extract; requireAuth: boolean; routerOptions: MedusaRouteOptions[]; }; @@ -70,27 +67,32 @@ export type MiddlewareInjectionOptions = { * Union of all options type possible for injectable. */ export type InjectableOptions = - | ComplexInjectableOptions + | EntityInjectableOptions + | ServiceInjectableOptions | RepositoryInjectableOptions - | MigrationInjectionOptions | MiddlewareInjectionOptions - | RoutesInjectionOptions; + | RouterInjectionOptions; /** * Determine which options type it actually is depending on the component type. */ export type GetInjectableOption = - (TComponentType extends Extract - ? ComplexInjectableOptions + (TComponentType extends Extract + ? EntityInjectableOptions + : TComponentType extends Extract + ? ServiceInjectableOptions : TComponentType extends Extract ? RepositoryInjectableOptions : TComponentType extends Extract - ? MigrationInjectionOptions - : TComponentType extends Extract - ? RoutesInjectionOptions + ? any + : TComponentType extends Extract + ? RouterInjectionOptions : TComponentType extends Extract ? MiddlewareInjectionOptions - : never) & { metatype: TComponentType extends 'middleware' ? Type : Type }; + : never) & { + type: InjectableComponentTypes; + metatype: TComponentType extends 'middleware' ? Type : Type; + }; /** * Determine which options type it actually is depending on the component type.