Skip to content

Commit

Permalink
feat: Improve starters
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Apr 21, 2022
1 parent 1c16ce8 commit 5c11ec4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 79 deletions.
4 changes: 2 additions & 2 deletions starters/plugin-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> {
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();
}
}
19 changes: 7 additions & 12 deletions starters/plugin-module/src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion starters/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> {
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();
}
}
19 changes: 7 additions & 12 deletions starters/server/src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 5c11ec4

Please sign in to comment.