Skip to content

Commit

Permalink
Feat() Update readme and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Jan 23, 2022
1 parent badcc5e commit 31c396f
Show file tree
Hide file tree
Showing 30 changed files with 172 additions and 88 deletions.
48 changes: 45 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ This packages exports the necessary bits and pieces to extend [medusajs](https:/

> `Medusa` is the entry point from `medusa-extender` and does not correspond to `medusa` itself
This is the main entry point that will allow to start your medusa server, including your custom extensions.
This is the main entry point that will allow to start your medusa server,
including your custom extensions.

Here is an example of how it works

Expand Down Expand Up @@ -121,7 +122,7 @@ The main purpuse of it is to be able to intercept entity event and add custom lo

### Usage

For that purpose an utility is provided that allow multiple to attach a new subscriver on the active connection
For that purpose an utility is provided that allow multiple to attach a new subscriber on the active connection
handled by the medusa container.

Here is an example
Expand Down Expand Up @@ -162,9 +163,50 @@ export default class StoreSubscriber
}
}
```
Those events will be attached attached/removed for each request to be sure that if any

Those events will be subscribed/unsubscribed for each request to be sure that if any
services are request scoped that you can access the actual cradle.

Here is an example of extended services that listen to an event

```typescript
interface ConstructorParams<TSearchService extends DefaultSearchService = DefaultSearchService> {
loggedInUser: User;
manager: EntityManager;
productRepository: ObjectType<typeof ProductRepository>;
productVariantRepository: ObjectType<typeof ProductVariantRepository>;
productOptionRepository: ObjectType<typeof ProductOptionRepository>;
eventBusService: EventBusService;
productVariantService: ProductVariantService;
productCollectionService: ProductCollectionService;
productTypeRepository: ObjectType<typeof ProductTypeRepository>;
productTagRepository: ObjectType<typeof ProductTagRepository>;
imageRepository: ObjectType<typeof ImageRepository>;
searchService: TSearchService;
}

export default class ProductService extends MedusaProductService implements MedusaService<typeof ProductService> {
public static overriddenType = MedusaProductService;
public static isHandledByMedusa = true;
public static scope = Lifetime.SCOPED;

readonly #manager: EntityManager;

constructor(private readonly container: ConstructorParams) {
super(container);
this.#manager = container.manager;
}

@OnMedusaEvent.Before.Insert(Product, { async: true })
public async attachStoreToProduct(
params: MedusaEventHandlerParams<Product, 'Insert'>
): Promise<EntityEventType<Product, 'Insert'>> {
// Your custom implementation
return event;
}
}
```

## Types

[Read more about it](./docs/modules/types.md)
Expand Down
48 changes: 45 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ This packages exports the necessary bits and pieces to extend [medusajs](https:/

> `Medusa` is the entry point from `medusa-extender` and does not correspond to `medusa` itself
This is the main entry point that will allow to start your medusa server, including your custom extensions.
This is the main entry point that will allow to start your medusa server,
including your custom extensions.

Here is an example of how it works

Expand Down Expand Up @@ -121,7 +122,7 @@ The main purpuse of it is to be able to intercept entity event and add custom lo

### Usage

For that purpose an utility is provided that allow multiple to attach a new subscriver on the active connection
For that purpose an utility is provided that allow multiple to attach a new subscriber on the active connection
handled by the medusa container.

Here is an example
Expand Down Expand Up @@ -162,9 +163,50 @@ export default class StoreSubscriber
}
}
```
Those events will be attached attached/removed for each request to be sure that if any

Those events will be subscribed/unsubscribed for each request to be sure that if any
services are request scoped that you can access the actual cradle.

Here is an example of extended services that listen to an event

```typescript
interface ConstructorParams<TSearchService extends DefaultSearchService = DefaultSearchService> {
loggedInUser: User;
manager: EntityManager;
productRepository: ObjectType<typeof ProductRepository>;
productVariantRepository: ObjectType<typeof ProductVariantRepository>;
productOptionRepository: ObjectType<typeof ProductOptionRepository>;
eventBusService: EventBusService;
productVariantService: ProductVariantService;
productCollectionService: ProductCollectionService;
productTypeRepository: ObjectType<typeof ProductTypeRepository>;
productTagRepository: ObjectType<typeof ProductTagRepository>;
imageRepository: ObjectType<typeof ImageRepository>;
searchService: TSearchService;
}

export default class ProductService extends MedusaProductService implements MedusaService<typeof ProductService> {
public static overriddenType = MedusaProductService;
public static isHandledByMedusa = true;
public static scope = Lifetime.SCOPED;

readonly #manager: EntityManager;

constructor(private readonly container: ConstructorParams) {
super(container);
this.#manager = container.manager;
}

@OnMedusaEvent.Before.Insert(Product, { async: true })
public async attachStoreToProduct(
params: MedusaEventHandlerParams<Product, 'Insert'>
): Promise<EntityEventType<Product, 'Insert'>> {
// Your custom implementation
return event;
}
}
```

## Types

[Read more about it](./docs/modules/types.md)
Expand Down
22 changes: 11 additions & 11 deletions docs/classes/medusa.Medusa.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Register steps that must be executed before/after medusa is load.

#### Defined in

[medusa.ts:30](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa.ts#L30)
[medusa.ts:30](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa.ts#L30)

## Properties

Expand All @@ -53,7 +53,7 @@ Register steps that must be executed before/after medusa is load.

#### Defined in

[medusa.ts:24](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa.ts#L24)
[medusa.ts:24](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa.ts#L24)

___

Expand All @@ -63,7 +63,7 @@ ___

#### Defined in

[medusa.ts:23](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa.ts#L23)
[medusa.ts:23](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa.ts#L23)

___

Expand All @@ -73,7 +73,7 @@ ___

#### Defined in

[medusa.ts:20](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa.ts#L20)
[medusa.ts:20](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa.ts#L20)

___

Expand All @@ -83,7 +83,7 @@ ___

#### Defined in

[medusa.ts:21](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa.ts#L21)
[medusa.ts:21](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa.ts#L21)

___

Expand All @@ -93,7 +93,7 @@ ___

#### Defined in

[medusa.ts:22](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa.ts#L22)
[medusa.ts:22](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa.ts#L22)

## Methods

Expand All @@ -115,7 +115,7 @@ Register middlewares to be apply before/after auth middleware.

#### Defined in

[medusa.ts:39](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa.ts#L39)
[medusa.ts:39](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa.ts#L39)

___

Expand All @@ -131,7 +131,7 @@ Launch all the steps before/after medusa according to the `stepOptions`.

#### Defined in

[medusa.ts:76](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa.ts#L76)
[medusa.ts:76](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa.ts#L76)

___

Expand All @@ -153,7 +153,7 @@ Register a step that must be executed after medusa is loaded.

#### Defined in

[medusa.ts:67](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa.ts#L67)
[medusa.ts:67](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa.ts#L67)

___

Expand All @@ -175,7 +175,7 @@ Register a step that must be executed before medusa is loaded.

#### Defined in

[medusa.ts:57](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa.ts#L57)
[medusa.ts:57](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa.ts#L57)

___

Expand All @@ -197,4 +197,4 @@ Promise<void>

#### Defined in

[medusa.ts:99](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa.ts#L99)
[medusa.ts:99](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa.ts#L99)
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ EventEmitter.constructor

#### Defined in

[medusa-event-emitter.ts:20](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-event-emitter.ts#L20)
[medusa-event-emitter.ts:20](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-event-emitter.ts#L20)

## Properties

Expand All @@ -51,7 +51,7 @@ EventEmitter.constructor

#### Defined in

[medusa-event-emitter.ts:18](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-event-emitter.ts#L18)
[medusa-event-emitter.ts:18](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-event-emitter.ts#L18)

## Methods

Expand All @@ -74,7 +74,7 @@ Emit an asynchrone event entity based and wait for the result.

#### Defined in

[medusa-event-emitter.ts:69](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-event-emitter.ts#L69)
[medusa-event-emitter.ts:69](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-event-emitter.ts#L69)

___

Expand Down Expand Up @@ -104,7 +104,7 @@ Register a new event handler.

#### Defined in

[medusa-event-emitter.ts:30](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-event-emitter.ts#L30)
[medusa-event-emitter.ts:30](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-event-emitter.ts#L30)

___

Expand All @@ -126,7 +126,7 @@ Apply all event handlers hold by the `listenerDescriptor`.

#### Defined in

[medusa-event-emitter.ts:42](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-event-emitter.ts#L42)
[medusa-event-emitter.ts:42](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-event-emitter.ts#L42)

___

Expand All @@ -140,4 +140,4 @@ ___

#### Defined in

[medusa-event-emitter.ts:57](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-event-emitter.ts#L57)
[medusa-event-emitter.ts:57](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-event-emitter.ts#L57)
8 changes: 4 additions & 4 deletions docs/classes/medusa_loader.MedusaLoader.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal container and database connection.

#### Defined in

[medusa-loader.ts:68](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-loader.ts#L68)
[medusa-loader.ts:68](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-loader.ts#L68)

___

Expand All @@ -68,7 +68,7 @@ Register all listeners before the plugins are loaded to be sure that the scope m

#### Defined in

[medusa-loader.ts:104](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-loader.ts#L104)
[medusa-loader.ts:104](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-loader.ts#L104)

___

Expand All @@ -89,7 +89,7 @@ ___

#### Defined in

[medusa-loader.ts:149](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-loader.ts#L149)
[medusa-loader.ts:149](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-loader.ts#L149)

___

Expand All @@ -110,4 +110,4 @@ ___

#### Defined in

[medusa-loader.ts:134](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-loader.ts#L134)
[medusa-loader.ts:134](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-loader.ts#L134)
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Map&lt;

#### Defined in

[medusa-middleware.repository.ts:14](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-middleware.repository.ts#L14)
[medusa-middleware.repository.ts:14](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-middleware.repository.ts#L14)

## Methods

Expand All @@ -73,7 +73,7 @@ Register a new middleware that must applied after auth.

#### Defined in

[medusa-middleware.repository.ts:36](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-middleware.repository.ts#L36)
[medusa-middleware.repository.ts:36](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-middleware.repository.ts#L36)

___

Expand All @@ -95,4 +95,4 @@ Register a new middleware that must applied before auth.

#### Defined in

[medusa-middleware.repository.ts:24](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-middleware.repository.ts#L24)
[medusa-middleware.repository.ts:24](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-middleware.repository.ts#L24)
12 changes: 6 additions & 6 deletions docs/classes/medusa_utils.Utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Attach a new subscriber to a specific entities.

#### Defined in

[medusa-utils.ts:38](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-utils.ts#L38)
[medusa-utils.ts:38](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-utils.ts#L38)

___

Expand All @@ -78,7 +78,7 @@ Log all the routes from the express instance.

#### Defined in

[medusa-utils.ts:59](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-utils.ts#L59)
[medusa-utils.ts:59](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-utils.ts#L59)

___

Expand All @@ -101,7 +101,7 @@ Prepare the log to be shown to be consistent everywhere.

#### Defined in

[medusa-utils.ts:68](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-utils.ts#L68)
[medusa-utils.ts:68](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-utils.ts#L68)

___

Expand All @@ -122,7 +122,7 @@ ___

#### Defined in

[medusa-utils.ts:81](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-utils.ts#L81)
[medusa-utils.ts:81](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-utils.ts#L81)

___

Expand Down Expand Up @@ -153,7 +153,7 @@ Since it is not possible to use multiple extend, you can use that utilities to a

#### Defined in

[medusa-utils.ts:17](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-utils.ts#L17)
[medusa-utils.ts:17](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-utils.ts#L17)

___

Expand All @@ -173,4 +173,4 @@ ___

#### Defined in

[medusa-utils.ts:101](https://github.com/adrien2p/medusa-extender/blob/7afa3be/src/medusa-utils.ts#L101)
[medusa-utils.ts:101](https://github.com/adrien2p/medusa-extender/blob/badcc5e/src/medusa-utils.ts#L101)
Loading

0 comments on commit 31c396f

Please sign in to comment.