-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat() Move decorator and types to this package
- Loading branch information
Showing
14 changed files
with
226 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { Constructor, MedusaServiceStatic } from '../types'; | ||
import { EntityActions, EntityEventActionOptions, MedusaEventEmittedParams } from './types'; | ||
import { medusaEventEmitter } from '../medusa-event-emitter'; | ||
|
||
export class OnMedusaEvent { | ||
readonly #when: string; | ||
#targetEntity: any; | ||
|
||
constructor(when?: string) { | ||
this.#when = when; | ||
} | ||
|
||
static get Before(): OnMedusaEvent { | ||
return this.build('Before'); | ||
} | ||
|
||
static get After(): OnMedusaEvent { | ||
return this.build('After'); | ||
} | ||
|
||
private static build(when: string): OnMedusaEvent { | ||
return new OnMedusaEvent(when); | ||
} | ||
|
||
public InsertEvent<Entity>(entity: Constructor<Entity>): string { | ||
return `${this.#when}Insert${entity.name}`; | ||
} | ||
|
||
public UpdateEvent<Entity>(entity: Constructor<Entity>): string { | ||
return `${this.#when}Update${entity.name}`; | ||
} | ||
|
||
public RemoveEvent<Entity>(entity: Constructor<Entity>): string { | ||
return `${this.#when}Remove${entity.name}`; | ||
} | ||
|
||
public Insert<Entity>( | ||
entity: Constructor<Entity>, | ||
options: EntityEventActionOptions<Entity> = { async: false } | ||
): MethodDecorator { | ||
return this.buildDecorator('Insert', entity, options); | ||
} | ||
|
||
public Update<Entity>( | ||
entity: Constructor<Entity>, | ||
options: EntityEventActionOptions<Entity> = { async: false } | ||
): MethodDecorator { | ||
return this.buildDecorator('Update', entity, options); | ||
} | ||
|
||
public Remove<Entity>( | ||
entity: Constructor<Entity>, | ||
options: EntityEventActionOptions<Entity> = { async: false } | ||
): MethodDecorator { | ||
return this.buildDecorator('Remove', entity, options); | ||
} | ||
|
||
private buildDecorator<Entity>( | ||
action: EntityActions, | ||
entity: Constructor<Entity>, | ||
options: EntityEventActionOptions<Entity> = { async: false } | ||
) { | ||
this.#targetEntity = entity; | ||
return OnMedusaEntityEventDecorator(`${this.#when}${action}${entity.name}`, entity, options); | ||
} | ||
} | ||
|
||
/** | ||
* Allow to decorate a class method to register it as an event handler for an entity event. | ||
* @param eventName The event that we are listening to | ||
* @param targetEntity The entity for which the event is triggered | ||
* @param async Should the event be awaiting the result | ||
* @param customMetatype The key that represent the class in the container it belongs to (Used to resolve the real instance) | ||
*/ | ||
function OnMedusaEntityEventDecorator<TMetatype, Entity>( | ||
eventName: string, | ||
targetEntity: Constructor<Entity>, | ||
{ async, customMetatype }: { async: boolean; customMetatype?: MedusaServiceStatic<TMetatype> } = { | ||
async: false, | ||
} | ||
): MethodDecorator { | ||
return (target: MedusaServiceStatic, propertyKey: string, descriptor: PropertyDescriptor): void => { | ||
const original = descriptor.value; | ||
descriptor.value = async function <Entity>({ | ||
values, | ||
resolveOrReject, | ||
}: MedusaEventEmittedParams<Entity, any>): Promise<void> { | ||
if (!(values.event.entity instanceof targetEntity)) { | ||
return; | ||
} | ||
|
||
const promise = original.apply(this, [values]); | ||
if (async) { | ||
return promise | ||
.then((res: unknown) => { | ||
return resolveOrReject(null, res); | ||
}) | ||
.catch((err: Error) => { | ||
return resolveOrReject(err); | ||
}); | ||
} else { | ||
return resolveOrReject(); | ||
} | ||
}; | ||
|
||
medusaEventEmitter.register(eventName, propertyKey, customMetatype ?? target); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { EntityManager, InsertEvent, RemoveEvent, UpdateEvent } from 'typeorm'; | ||
import { MedusaServiceStatic } from '../types'; | ||
|
||
export type EntityEventActionOptions<T> = { | ||
async: boolean; | ||
customMetatype?: MedusaServiceStatic<T>; | ||
}; | ||
|
||
export type EntityActions = 'Insert' | 'Update' | 'Remove'; | ||
|
||
/** | ||
* Event types that can be emitted. | ||
*/ | ||
export type EntityEventType<Entity, TEntityActions extends EntityActions> = TEntityActions extends 'Insert' | ||
? InsertEvent<Entity> | ||
: TEntityActions extends 'Update' | ||
? UpdateEvent<Entity> | ||
: RemoveEvent<Entity>; | ||
|
||
/** | ||
* The arguments expected by the {@link OnMedusaEvent} decorator. | ||
*/ | ||
export type MedusaEventEmittedParams<Entity, TEntityActions extends EntityActions> = { | ||
values: MedusaEventHandlerParams<Entity, TEntityActions>; | ||
resolveOrReject: (err?: Error, res?: unknown) => void; | ||
}; | ||
|
||
/** | ||
* The arguments expected by the event handler. | ||
*/ | ||
export type MedusaEventHandlerParams<Entity, TEntityActions extends EntityActions> = { | ||
event: EntityEventType<Entity, TEntityActions>; | ||
transactionalEntityManager?: EntityManager; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters