From 5c11ec462f4f1d9e052649ad3fd8de974a3d05b6 Mon Sep 17 00:00:00 2001 From: adrien2p Date: Thu, 21 Apr 2022 14:19:01 +0200 Subject: [PATCH] feat: Improve starters --- starters/plugin-module/package.json | 4 +-- .../user/attachUserSubscribers.middleware.ts | 31 +++---------------- .../src/modules/user/user.service.ts | 19 +++++------- starters/server/package.json | 2 +- .../user/attachUserSubscribers.middleware.ts | 31 +++---------------- .../server/src/modules/user/user.service.ts | 19 +++++------- 6 files changed, 27 insertions(+), 79 deletions(-) diff --git a/starters/plugin-module/package.json b/starters/plugin-module/package.json index b2e7bf89..3611ab03 100644 --- a/starters/plugin-module/package.json +++ b/starters/plugin-module/package.json @@ -17,7 +17,7 @@ "test:cov": "NODE_ENV=test jest --coverage --detectOpenHandles" }, "peerDependencies": { - "medusa-extender": "^1.5.2", + "medusa-extender": "^1.6.5", "awilix": "^4.2.3", "@medusajs/medusa": "^1.2.1", "medusa-core-utils": "^1.1.31", @@ -37,7 +37,7 @@ "jest": "^27.3.1", "medusa-core-utils": "^1.1.31", "medusa-interfaces": "^1.2.1", - "medusa-extender": "^1.6.1", + "medusa-extender": "^1.6.5", "prettier": "^2.5.0", "reflect-metadata": "^0.1.13", "supertest": "^6.1.6", diff --git a/starters/plugin-module/src/modules/user/attachUserSubscribers.middleware.ts b/starters/plugin-module/src/modules/user/attachUserSubscribers.middleware.ts index 814d9d3a..9201ab3d 100644 --- a/starters/plugin-module/src/modules/user/attachUserSubscribers.middleware.ts +++ b/starters/plugin-module/src/modules/user/attachUserSubscribers.middleware.ts @@ -1,38 +1,17 @@ -import { Express, NextFunction, Request, Response } from 'express'; +import { NextFunction, Request, Response } from 'express'; import { MEDUSA_RESOLVER_KEYS, MedusaAuthenticatedRequest, - MedusaRouteOptions, Middleware, - Utils as MedusaUtils, } from 'medusa-extender'; import { Connection } from 'typeorm'; import UserSubscriber from './user.subscriber'; @Middleware({ requireAuth: true, routes: [{ method: 'post', path: '/admin/users/' }] }) export default class AttachUserSubscribersMiddleware { - public static get routesOptions(): MedusaRouteOptions { - return { - path: '/admin/users/', - method: 'post', - }; - } - - public consume(options: { - app: Express; - }): (req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction) => void | Promise { - const routeOptions = AttachUserSubscribersMiddleware.routesOptions; - options.app.use((req: MedusaAuthenticatedRequest, res: Response, next: NextFunction): void => { - if ( - req.method.toLowerCase() === routeOptions.method && - req.originalUrl.toLowerCase() === routeOptions.path.toLowerCase() - ) { - const { connection } = req.scope.resolve(MEDUSA_RESOLVER_KEYS.manager) as { connection: Connection }; - MedusaUtils.attachOrReplaceEntitySubscriber(connection, UserSubscriber); - } - return next(); - }); - - return (req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction) => next(); + public consume(req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction): void { + const { connection } = req.scope.resolve(MEDUSA_RESOLVER_KEYS.manager) as { connection: Connection }; + UserSubscriber.attachTo(connection); + return next(); } } diff --git a/starters/plugin-module/src/modules/user/user.service.ts b/starters/plugin-module/src/modules/user/user.service.ts index a965d7ab..474686a4 100644 --- a/starters/plugin-module/src/modules/user/user.service.ts +++ b/starters/plugin-module/src/modules/user/user.service.ts @@ -1,27 +1,22 @@ import { Service } from 'medusa-extender'; import { EntityManager } from 'typeorm'; -import { UserService as MedusaUserService } from '@medusajs/medusa/dist/services'; -import EventBusService from '@medusajs/medusa/dist/services/event-bus'; +import { EventBusService, UserService as MedusaUserService } from '@medusajs/medusa/dist/services'; import UserRepository from './user.repository'; -import { User } from './user.entity'; -type ConstructorParams = { - loggedInUser: User; +type InjectedDependencies = { manager: EntityManager; userRepository: typeof UserRepository; eventBusService: EventBusService; }; -@Service({ override: MedusaUserService, scope: 'SCOPED' }) +@Service({ override: MedusaUserService }) export default class UserService extends MedusaUserService { private readonly manager: EntityManager; private readonly userRepository: typeof UserRepository; - private readonly eventBus: EventBusService; - constructor(private readonly container: ConstructorParams) { - super(container); - this.manager = container.manager; - this.userRepository = container.userRepository; - this.eventBus = container.eventBusService; + constructor({ manager, userRepository, eventBusService }: InjectedDependencies) { + super({ manager, userRepository, eventBusService }); + this.manager = manager; + this.userRepository = userRepository; } } diff --git a/starters/server/package.json b/starters/server/package.json index d285d934..3c059c81 100644 --- a/starters/server/package.json +++ b/starters/server/package.json @@ -33,7 +33,7 @@ "jsonwebtoken": "^8.5.1", "lodash": "^4.17.21", "medusa-core-utils": "^1.1.31", - "medusa-extender": "^1.6.1", + "medusa-extender": "^1.6.5", "medusa-fulfillment-manual": "^1.1.30", "medusa-interfaces": "^1.1.34", "medusa-payment-manual": "^1.0.12", diff --git a/starters/server/src/modules/user/attachUserSubscribers.middleware.ts b/starters/server/src/modules/user/attachUserSubscribers.middleware.ts index 814d9d3a..9201ab3d 100644 --- a/starters/server/src/modules/user/attachUserSubscribers.middleware.ts +++ b/starters/server/src/modules/user/attachUserSubscribers.middleware.ts @@ -1,38 +1,17 @@ -import { Express, NextFunction, Request, Response } from 'express'; +import { NextFunction, Request, Response } from 'express'; import { MEDUSA_RESOLVER_KEYS, MedusaAuthenticatedRequest, - MedusaRouteOptions, Middleware, - Utils as MedusaUtils, } from 'medusa-extender'; import { Connection } from 'typeorm'; import UserSubscriber from './user.subscriber'; @Middleware({ requireAuth: true, routes: [{ method: 'post', path: '/admin/users/' }] }) export default class AttachUserSubscribersMiddleware { - public static get routesOptions(): MedusaRouteOptions { - return { - path: '/admin/users/', - method: 'post', - }; - } - - public consume(options: { - app: Express; - }): (req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction) => void | Promise { - const routeOptions = AttachUserSubscribersMiddleware.routesOptions; - options.app.use((req: MedusaAuthenticatedRequest, res: Response, next: NextFunction): void => { - if ( - req.method.toLowerCase() === routeOptions.method && - req.originalUrl.toLowerCase() === routeOptions.path.toLowerCase() - ) { - const { connection } = req.scope.resolve(MEDUSA_RESOLVER_KEYS.manager) as { connection: Connection }; - MedusaUtils.attachOrReplaceEntitySubscriber(connection, UserSubscriber); - } - return next(); - }); - - return (req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction) => next(); + public consume(req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction): void { + const { connection } = req.scope.resolve(MEDUSA_RESOLVER_KEYS.manager) as { connection: Connection }; + UserSubscriber.attachTo(connection); + return next(); } } diff --git a/starters/server/src/modules/user/user.service.ts b/starters/server/src/modules/user/user.service.ts index a965d7ab..474686a4 100644 --- a/starters/server/src/modules/user/user.service.ts +++ b/starters/server/src/modules/user/user.service.ts @@ -1,27 +1,22 @@ import { Service } from 'medusa-extender'; import { EntityManager } from 'typeorm'; -import { UserService as MedusaUserService } from '@medusajs/medusa/dist/services'; -import EventBusService from '@medusajs/medusa/dist/services/event-bus'; +import { EventBusService, UserService as MedusaUserService } from '@medusajs/medusa/dist/services'; import UserRepository from './user.repository'; -import { User } from './user.entity'; -type ConstructorParams = { - loggedInUser: User; +type InjectedDependencies = { manager: EntityManager; userRepository: typeof UserRepository; eventBusService: EventBusService; }; -@Service({ override: MedusaUserService, scope: 'SCOPED' }) +@Service({ override: MedusaUserService }) export default class UserService extends MedusaUserService { private readonly manager: EntityManager; private readonly userRepository: typeof UserRepository; - private readonly eventBus: EventBusService; - constructor(private readonly container: ConstructorParams) { - super(container); - this.manager = container.manager; - this.userRepository = container.userRepository; - this.eventBus = container.eventBusService; + constructor({ manager, userRepository, eventBusService }: InjectedDependencies) { + super({ manager, userRepository, eventBusService }); + this.manager = manager; + this.userRepository = userRepository; } }