fix(medusa): Plugin loader must also check for TransactionBaseService (#1601)

This commit is contained in:
Adrien de Peretti
2022-06-13 17:53:52 +02:00
committed by GitHub
parent 012513b6a1
commit 28ddf10446
2 changed files with 16 additions and 7 deletions

View File

@@ -13,6 +13,12 @@ const buildServiceTemplate = (name: string) => {
export default class ${name}Service extends BaseService {}
`
}
const buildTransactionBaseServiceServiceTemplate = (name: string) => {
return `
import { TransactionBaseService } from "${resolve(__dirname, "../../interfaces")}"
export default class ${name}Service extends TransactionBaseService {}
`
}
describe('plugins loader', () => {
const container = createContainer() as MedusaContainer
@@ -30,6 +36,7 @@ describe('plugins loader', () => {
mkdirSync(servicesTestTargetDirectoryPath, { mode: "777", recursive: true })
writeFileSync(resolve(servicesTestTargetDirectoryPath, "test.js"), buildServiceTemplate("test"))
writeFileSync(resolve(servicesTestTargetDirectoryPath, "test2.js"), buildServiceTemplate("test2"))
writeFileSync(resolve(servicesTestTargetDirectoryPath, "test3.js"), buildTransactionBaseServiceServiceTemplate("test3"))
writeFileSync(resolve(servicesTestTargetDirectoryPath, "test2.js.map"), "map:file")
writeFileSync(resolve(servicesTestTargetDirectoryPath, "test2.d.ts"), "export interface Test {}")
})
@@ -51,11 +58,14 @@ describe('plugins loader', () => {
const testService: (...args: unknown[]) => any = container.resolve("testService")
const test2Service: (...args: unknown[]) => any = container.resolve("test2Service")
const test3Service: (...args: unknown[]) => any = container.resolve("test3Service")
expect(testService).toBeTruthy()
expect(testService.constructor.name).toBe("testService")
expect(test2Service).toBeTruthy()
expect(test2Service.constructor.name).toBe("test2Service")
expect(test3Service).toBeTruthy()
expect(test3Service.constructor.name).toBe("test3Service")
})
})
})

View File

@@ -2,7 +2,7 @@ import glob from "glob"
import { Express } from "express"
import { EntitySchema } from "typeorm"
import {
BaseService,
BaseService as LegacyBaseService,
PaymentService,
FulfillmentService,
NotificationService,
@@ -10,18 +10,17 @@ import {
OauthService,
SearchService,
} from "medusa-interfaces"
import { getConfigFile, createRequireFromPath } from "medusa-core-utils"
import { createRequireFromPath } from "medusa-core-utils"
import _ from "lodash"
import path from "path"
import fs from "fs"
import { asValue, asClass, asFunction, aliasTo } from "awilix"
import { sync as existsSync } from "fs-exists-cached"
import {
AbstractFileService,
AbstractTaxService,
isFileService,
isTaxCalculationStrategy,
TransactionBaseService,
TransactionBaseService as BaseService,
} from "../interfaces"
import formatRegistrationName from "../utils/format-registration-name"
import {
@@ -297,11 +296,11 @@ export async function registerServices(
const name = formatRegistrationName(fn)
if (
!(loaded.prototype instanceof BaseService) &&
!(loaded.prototype instanceof TransactionBaseService)
!(loaded.prototype instanceof LegacyBaseService) &&
!(loaded.prototype instanceof BaseService)
) {
const logger = container.resolve<Logger>("logger")
const message = `File must be a valid service implementation, please check ${fn}`
const message = `The class must be a valid service implementation, please check ${fn}`
logger.error(message)
throw new Error(message)
}