|
|
|
@@ -1,5 +1,5 @@
|
|
|
|
|
import glob from "glob"
|
|
|
|
|
import { Express } from 'express'
|
|
|
|
|
import { Express } from "express"
|
|
|
|
|
import { EntitySchema } from "typeorm"
|
|
|
|
|
import {
|
|
|
|
|
BaseService,
|
|
|
|
@@ -16,9 +16,20 @@ import path from "path"
|
|
|
|
|
import fs from "fs"
|
|
|
|
|
import { asValue, asClass, asFunction, aliasTo } from "awilix"
|
|
|
|
|
import { sync as existsSync } from "fs-exists-cached"
|
|
|
|
|
import { AbstractTaxService, isTaxCalculationStrategy } from "../interfaces"
|
|
|
|
|
import {
|
|
|
|
|
AbstractFileService,
|
|
|
|
|
AbstractTaxService,
|
|
|
|
|
isFileService,
|
|
|
|
|
isTaxCalculationStrategy,
|
|
|
|
|
TransactionBaseService,
|
|
|
|
|
} from "../interfaces"
|
|
|
|
|
import formatRegistrationName from "../utils/format-registration-name"
|
|
|
|
|
import { ClassConstructor, ConfigModule, Logger, MedusaContainer } from "../types/global"
|
|
|
|
|
import {
|
|
|
|
|
ClassConstructor,
|
|
|
|
|
ConfigModule,
|
|
|
|
|
Logger,
|
|
|
|
|
MedusaContainer,
|
|
|
|
|
} from "../types/global"
|
|
|
|
|
import { MiddlewareService } from "../services"
|
|
|
|
|
|
|
|
|
|
type Options = {
|
|
|
|
@@ -40,7 +51,13 @@ type PluginDetails = {
|
|
|
|
|
/**
|
|
|
|
|
* Registers all services in the services directory
|
|
|
|
|
*/
|
|
|
|
|
export default async ({ rootDirectory, container, app, configModule, activityId }: Options): Promise<void> => {
|
|
|
|
|
export default async ({
|
|
|
|
|
rootDirectory,
|
|
|
|
|
container,
|
|
|
|
|
app,
|
|
|
|
|
configModule,
|
|
|
|
|
activityId,
|
|
|
|
|
}: Options): Promise<void> => {
|
|
|
|
|
const resolved = getResolvedPlugins(rootDirectory, configModule) || []
|
|
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
@@ -59,7 +76,10 @@ export default async ({ rootDirectory, container, app, configModule, activityId
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getResolvedPlugins(rootDirectory: string, configModule: ConfigModule): undefined | PluginDetails[] {
|
|
|
|
|
function getResolvedPlugins(
|
|
|
|
|
rootDirectory: string,
|
|
|
|
|
configModule: ConfigModule
|
|
|
|
|
): undefined | PluginDetails[] {
|
|
|
|
|
const { plugins } = configModule
|
|
|
|
|
|
|
|
|
|
const resolved = plugins.map((plugin) => {
|
|
|
|
@@ -85,10 +105,14 @@ function getResolvedPlugins(rootDirectory: string, configModule: ConfigModule):
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function registerPluginModels({
|
|
|
|
|
rootDirectory,
|
|
|
|
|
container,
|
|
|
|
|
configModule
|
|
|
|
|
}: { rootDirectory: string; container: MedusaContainer; configModule: ConfigModule; }): Promise<void> {
|
|
|
|
|
rootDirectory,
|
|
|
|
|
container,
|
|
|
|
|
configModule,
|
|
|
|
|
}: {
|
|
|
|
|
rootDirectory: string
|
|
|
|
|
container: MedusaContainer
|
|
|
|
|
configModule: ConfigModule
|
|
|
|
|
}): Promise<void> {
|
|
|
|
|
const resolved = getResolvedPlugins(rootDirectory, configModule) || []
|
|
|
|
|
await Promise.all(
|
|
|
|
|
resolved.map(async (pluginDetails) => {
|
|
|
|
@@ -97,7 +121,10 @@ export async function registerPluginModels({
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function runLoaders(pluginDetails: PluginDetails, container: MedusaContainer): Promise<void> {
|
|
|
|
|
async function runLoaders(
|
|
|
|
|
pluginDetails: PluginDetails,
|
|
|
|
|
container: MedusaContainer
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const loaderFiles = glob.sync(
|
|
|
|
|
`${pluginDetails.resolve}/loaders/[!__]*.js`,
|
|
|
|
|
{}
|
|
|
|
@@ -118,12 +145,18 @@ async function runLoaders(pluginDetails: PluginDetails, container: MedusaContain
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function registerMedusaApi(pluginDetails: PluginDetails, container: MedusaContainer): void {
|
|
|
|
|
function registerMedusaApi(
|
|
|
|
|
pluginDetails: PluginDetails,
|
|
|
|
|
container: MedusaContainer
|
|
|
|
|
): void {
|
|
|
|
|
registerMedusaMiddleware(pluginDetails, container)
|
|
|
|
|
registerStrategies(pluginDetails, container)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function registerStrategies(pluginDetails: PluginDetails, container: MedusaContainer): void {
|
|
|
|
|
function registerStrategies(
|
|
|
|
|
pluginDetails: PluginDetails,
|
|
|
|
|
container: MedusaContainer
|
|
|
|
|
): void {
|
|
|
|
|
let module
|
|
|
|
|
try {
|
|
|
|
|
const path = `${pluginDetails.resolve}/strategies/tax-calculation`
|
|
|
|
@@ -150,7 +183,10 @@ function registerStrategies(pluginDetails: PluginDetails, container: MedusaConta
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function registerMedusaMiddleware(pluginDetails: PluginDetails, container: MedusaContainer): void {
|
|
|
|
|
function registerMedusaMiddleware(
|
|
|
|
|
pluginDetails: PluginDetails,
|
|
|
|
|
container: MedusaContainer
|
|
|
|
|
): void {
|
|
|
|
|
let module
|
|
|
|
|
try {
|
|
|
|
|
module = require(`${pluginDetails.resolve}/api/medusa-middleware`).default
|
|
|
|
@@ -158,7 +194,8 @@ function registerMedusaMiddleware(pluginDetails: PluginDetails, container: Medus
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const middlewareService = container.resolve<MiddlewareService>("middlewareService")
|
|
|
|
|
const middlewareService =
|
|
|
|
|
container.resolve<MiddlewareService>("middlewareService")
|
|
|
|
|
if (module.postAuthentication) {
|
|
|
|
|
middlewareService.addPostAuthentication(
|
|
|
|
|
module.postAuthentication,
|
|
|
|
@@ -178,8 +215,12 @@ function registerMedusaMiddleware(pluginDetails: PluginDetails, container: Medus
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function registerCoreRouters(pluginDetails: PluginDetails, container: MedusaContainer): void {
|
|
|
|
|
const middlewareService = container.resolve<MiddlewareService>("middlewareService")
|
|
|
|
|
function registerCoreRouters(
|
|
|
|
|
pluginDetails: PluginDetails,
|
|
|
|
|
container: MedusaContainer
|
|
|
|
|
): void {
|
|
|
|
|
const middlewareService =
|
|
|
|
|
container.resolve<MiddlewareService>("middlewareService")
|
|
|
|
|
const { resolve } = pluginDetails
|
|
|
|
|
const adminFiles = glob.sync(`${resolve}/api/admin/[!__]*.js`, {})
|
|
|
|
|
const storeFiles = glob.sync(`${resolve}/api/store/[!__]*.js`, {})
|
|
|
|
@@ -245,16 +286,22 @@ function registerApi(
|
|
|
|
|
* registered
|
|
|
|
|
* @return {void}
|
|
|
|
|
*/
|
|
|
|
|
export async function registerServices(pluginDetails: PluginDetails, container: MedusaContainer): Promise<void> {
|
|
|
|
|
export async function registerServices(
|
|
|
|
|
pluginDetails: PluginDetails,
|
|
|
|
|
container: MedusaContainer
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const files = glob.sync(`${pluginDetails.resolve}/services/[!__]*.js`, {})
|
|
|
|
|
await Promise.all(
|
|
|
|
|
files.map(async (fn) => {
|
|
|
|
|
const loaded = require(fn).default
|
|
|
|
|
const name = formatRegistrationName(fn)
|
|
|
|
|
|
|
|
|
|
if (!(loaded.prototype instanceof BaseService)) {
|
|
|
|
|
if (
|
|
|
|
|
!(loaded.prototype instanceof BaseService) &&
|
|
|
|
|
!(loaded.prototype instanceof TransactionBaseService)
|
|
|
|
|
) {
|
|
|
|
|
const logger = container.resolve<Logger>("logger")
|
|
|
|
|
const message = `Services must inherit from BaseService, please check ${fn}`
|
|
|
|
|
const message = `File must be a valid service implementation, please check ${fn}`
|
|
|
|
|
logger.error(message)
|
|
|
|
|
throw new Error(message)
|
|
|
|
|
}
|
|
|
|
@@ -277,7 +324,8 @@ export async function registerServices(pluginDetails: PluginDetails, container:
|
|
|
|
|
} else if (loaded.prototype instanceof OauthService) {
|
|
|
|
|
const appDetails = loaded.getAppDetails(pluginDetails.options)
|
|
|
|
|
|
|
|
|
|
const oauthService = container.resolve<typeof OauthService>("oauthService")
|
|
|
|
|
const oauthService =
|
|
|
|
|
container.resolve<typeof OauthService>("oauthService")
|
|
|
|
|
await oauthService.registerOauthApp(appDetails)
|
|
|
|
|
|
|
|
|
|
const name = appDetails.application_name
|
|
|
|
@@ -324,6 +372,15 @@ export async function registerServices(pluginDetails: PluginDetails, container:
|
|
|
|
|
),
|
|
|
|
|
[`fileService`]: aliasTo(name),
|
|
|
|
|
})
|
|
|
|
|
} else if (isFileService(loaded.prototype)) {
|
|
|
|
|
// Add the service directly to the container in order to make simple
|
|
|
|
|
// resolution if we already know which file storage provider we need to use
|
|
|
|
|
container.register({
|
|
|
|
|
[name]: asFunction(
|
|
|
|
|
(cradle) => new loaded(cradle, pluginDetails.options)
|
|
|
|
|
),
|
|
|
|
|
[`fileService`]: aliasTo(name),
|
|
|
|
|
})
|
|
|
|
|
} else if (loaded.prototype instanceof SearchService) {
|
|
|
|
|
// Add the service directly to the container in order to make simple
|
|
|
|
|
// resolution if we already know which search provider we need to use
|
|
|
|
@@ -365,7 +422,10 @@ export async function registerServices(pluginDetails: PluginDetails, container:
|
|
|
|
|
* registered
|
|
|
|
|
* @return {void}
|
|
|
|
|
*/
|
|
|
|
|
function registerSubscribers(pluginDetails: PluginDetails, container: MedusaContainer): void {
|
|
|
|
|
function registerSubscribers(
|
|
|
|
|
pluginDetails: PluginDetails,
|
|
|
|
|
container: MedusaContainer
|
|
|
|
|
): void {
|
|
|
|
|
const files = glob.sync(`${pluginDetails.resolve}/subscribers/*.js`, {})
|
|
|
|
|
files.forEach((fn) => {
|
|
|
|
|
const loaded = require(fn).default
|
|
|
|
@@ -387,19 +447,24 @@ function registerSubscribers(pluginDetails: PluginDetails, container: MedusaCont
|
|
|
|
|
* registered
|
|
|
|
|
* @return {void}
|
|
|
|
|
*/
|
|
|
|
|
function registerRepositories(pluginDetails: PluginDetails, container: MedusaContainer): void {
|
|
|
|
|
function registerRepositories(
|
|
|
|
|
pluginDetails: PluginDetails,
|
|
|
|
|
container: MedusaContainer
|
|
|
|
|
): void {
|
|
|
|
|
const files = glob.sync(`${pluginDetails.resolve}/repositories/*.js`, {})
|
|
|
|
|
files.forEach((fn) => {
|
|
|
|
|
const loaded = require(fn) as ClassConstructor<unknown>
|
|
|
|
|
|
|
|
|
|
Object.entries(loaded).map(([, val]: [string, ClassConstructor<unknown>]) => {
|
|
|
|
|
if (typeof val === "function") {
|
|
|
|
|
const name = formatRegistrationName(fn)
|
|
|
|
|
container.register({
|
|
|
|
|
[name]: asClass(val),
|
|
|
|
|
})
|
|
|
|
|
Object.entries(loaded).map(
|
|
|
|
|
([, val]: [string, ClassConstructor<unknown>]) => {
|
|
|
|
|
if (typeof val === "function") {
|
|
|
|
|
const name = formatRegistrationName(fn)
|
|
|
|
|
container.register({
|
|
|
|
|
[name]: asClass(val),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -414,21 +479,26 @@ function registerRepositories(pluginDetails: PluginDetails, container: MedusaCon
|
|
|
|
|
* registered
|
|
|
|
|
* @return {void}
|
|
|
|
|
*/
|
|
|
|
|
function registerModels(pluginDetails: PluginDetails, container: MedusaContainer): void {
|
|
|
|
|
function registerModels(
|
|
|
|
|
pluginDetails: PluginDetails,
|
|
|
|
|
container: MedusaContainer
|
|
|
|
|
): void {
|
|
|
|
|
const files = glob.sync(`${pluginDetails.resolve}/models/*.js`, {})
|
|
|
|
|
files.forEach((fn) => {
|
|
|
|
|
const loaded = require(fn) as ClassConstructor<unknown> | EntitySchema
|
|
|
|
|
|
|
|
|
|
Object.entries(loaded).map(([, val]: [string, ClassConstructor<unknown> | EntitySchema]) => {
|
|
|
|
|
if (typeof val === "function" || val instanceof EntitySchema) {
|
|
|
|
|
const name = formatRegistrationName(fn)
|
|
|
|
|
container.register({
|
|
|
|
|
[name]: asValue(val),
|
|
|
|
|
})
|
|
|
|
|
Object.entries(loaded).map(
|
|
|
|
|
([, val]: [string, ClassConstructor<unknown> | EntitySchema]) => {
|
|
|
|
|
if (typeof val === "function" || val instanceof EntitySchema) {
|
|
|
|
|
const name = formatRegistrationName(fn)
|
|
|
|
|
container.register({
|
|
|
|
|
[name]: asValue(val),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
container.registerAdd("db_entities", asValue(val))
|
|
|
|
|
container.registerAdd("db_entities", asValue(val))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -446,11 +516,11 @@ function createPluginId(name: string): string {
|
|
|
|
|
* @return {object} the plugin details
|
|
|
|
|
*/
|
|
|
|
|
function resolvePlugin(pluginName: string): {
|
|
|
|
|
resolve: string;
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
resolve: string
|
|
|
|
|
id: string
|
|
|
|
|
name: string
|
|
|
|
|
options: Record<string, unknown>
|
|
|
|
|
version: string;
|
|
|
|
|
version: string
|
|
|
|
|
} {
|
|
|
|
|
// Only find plugins when we're not given an absolute path
|
|
|
|
|
if (!existsSync(pluginName)) {
|
|
|
|
|