feat(region): Add admin region get + list endpoints (#6322)
**What** Add `GET /admin/regions` Add `GET /admin/regions/:id` Blocked by #6320 Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
@@ -13,13 +13,20 @@ export const moduleLoader = async ({
|
||||
container,
|
||||
moduleResolutions,
|
||||
logger,
|
||||
migrationOnly,
|
||||
}: {
|
||||
container: MedusaContainer
|
||||
moduleResolutions: Record<string, ModuleResolution>
|
||||
logger: Logger
|
||||
migrationOnly?: boolean
|
||||
}): Promise<void> => {
|
||||
for (const resolution of Object.values(moduleResolutions ?? {})) {
|
||||
const registrationResult = await loadModule(container, resolution, logger!)
|
||||
const registrationResult = await loadModule(
|
||||
container,
|
||||
resolution,
|
||||
logger!,
|
||||
migrationOnly
|
||||
)
|
||||
|
||||
if (registrationResult?.error) {
|
||||
const { error } = registrationResult
|
||||
@@ -40,7 +47,8 @@ export const moduleLoader = async ({
|
||||
async function loadModule(
|
||||
container: MedusaContainer,
|
||||
resolution: ModuleResolution,
|
||||
logger: Logger
|
||||
logger: Logger,
|
||||
migrationOnly?: boolean
|
||||
): Promise<{ error?: Error } | void> {
|
||||
const modDefinition = resolution.definition
|
||||
const registrationName = modDefinition.registrationName
|
||||
@@ -77,5 +85,5 @@ async function loadModule(
|
||||
return
|
||||
}
|
||||
|
||||
return await loadInternalModule(container, resolution, logger)
|
||||
return await loadInternalModule(container, resolution, logger, migrationOnly)
|
||||
}
|
||||
|
||||
@@ -16,7 +16,8 @@ import { asFunction, asValue } from "awilix"
|
||||
export async function loadInternalModule(
|
||||
container: MedusaContainer,
|
||||
resolution: ModuleResolution,
|
||||
logger: Logger
|
||||
logger: Logger,
|
||||
migrationOnly?: boolean
|
||||
): Promise<{ error?: Error } | void> {
|
||||
const registrationName = resolution.definition.registrationName
|
||||
|
||||
@@ -64,6 +65,17 @@ export async function loadInternalModule(
|
||||
}
|
||||
}
|
||||
|
||||
if (migrationOnly) {
|
||||
// Partially loaded module, only register the service __joinerConfig function to be able to resolve it later
|
||||
const moduleService = {
|
||||
__joinerConfig: loadedModule.service.prototype.__joinerConfig,
|
||||
}
|
||||
container.register({
|
||||
[registrationName]: asValue(moduleService),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const localContainer = createMedusaContainer()
|
||||
|
||||
const dependencies = resolution?.dependencies ?? []
|
||||
|
||||
@@ -67,7 +67,11 @@ export type SharedResources = {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadModules(modulesConfig, sharedContainer) {
|
||||
async function loadModules(
|
||||
modulesConfig,
|
||||
sharedContainer,
|
||||
migrationOnly = false
|
||||
) {
|
||||
const allModules = {}
|
||||
|
||||
await Promise.all(
|
||||
@@ -106,6 +110,7 @@ async function loadModules(modulesConfig, sharedContainer) {
|
||||
sharedContainer,
|
||||
moduleDefinition: definition as ModuleDefinition,
|
||||
moduleExports,
|
||||
migrationOnly,
|
||||
})) as LoadedModule
|
||||
|
||||
const service = loaded[moduleName]
|
||||
@@ -190,18 +195,7 @@ export type MedusaAppOutput = {
|
||||
runMigrations: RunMigrationFn
|
||||
}
|
||||
|
||||
export async function MedusaApp({
|
||||
sharedContainer,
|
||||
sharedResourcesConfig,
|
||||
servicesConfig,
|
||||
modulesConfigPath,
|
||||
modulesConfigFileName,
|
||||
modulesConfig,
|
||||
linkModules,
|
||||
remoteFetchData,
|
||||
injectedDependencies,
|
||||
onApplicationStartCb,
|
||||
}: {
|
||||
export type MedusaAppOptions = {
|
||||
sharedContainer?: MedusaContainer
|
||||
sharedResourcesConfig?: SharedResources
|
||||
loadedModules?: LoadedModule[]
|
||||
@@ -213,7 +207,21 @@ export async function MedusaApp({
|
||||
remoteFetchData?: RemoteFetchDataCallback
|
||||
injectedDependencies?: any
|
||||
onApplicationStartCb?: () => void
|
||||
} = {}): Promise<{
|
||||
}
|
||||
|
||||
async function MedusaApp_({
|
||||
sharedContainer,
|
||||
sharedResourcesConfig,
|
||||
servicesConfig,
|
||||
modulesConfigPath,
|
||||
modulesConfigFileName,
|
||||
modulesConfig,
|
||||
linkModules,
|
||||
remoteFetchData,
|
||||
injectedDependencies = {},
|
||||
onApplicationStartCb,
|
||||
migrationOnly = false,
|
||||
}: MedusaAppOptions & { migrationOnly?: boolean } = {}): Promise<{
|
||||
modules: Record<string, LoadedModule | LoadedModule[]>
|
||||
link: RemoteLink | undefined
|
||||
query: (
|
||||
@@ -224,8 +232,6 @@ export async function MedusaApp({
|
||||
notFound?: Record<string, Record<string, string>>
|
||||
runMigrations: RunMigrationFn
|
||||
}> {
|
||||
injectedDependencies ??= {}
|
||||
|
||||
const sharedContainer_ = createMedusaContainer({}, sharedContainer)
|
||||
|
||||
const modules: MedusaModuleConfig =
|
||||
@@ -279,7 +285,7 @@ export async function MedusaApp({
|
||||
})
|
||||
}
|
||||
|
||||
const allModules = await loadModules(modules, sharedContainer_)
|
||||
const allModules = await loadModules(modules, sharedContainer_, migrationOnly)
|
||||
|
||||
// Share Event bus with link modules
|
||||
injectedDependencies[ModuleRegistrationName.EVENT_BUS] =
|
||||
@@ -346,16 +352,35 @@ export async function MedusaApp({
|
||||
}))
|
||||
}
|
||||
|
||||
try {
|
||||
return {
|
||||
modules: allModules,
|
||||
link: remoteLink,
|
||||
query,
|
||||
entitiesMap: schema.getTypeMap(),
|
||||
notFound,
|
||||
runMigrations,
|
||||
}
|
||||
} finally {
|
||||
MedusaModule.onApplicationStart(onApplicationStartCb)
|
||||
return {
|
||||
modules: allModules,
|
||||
link: remoteLink,
|
||||
query,
|
||||
entitiesMap: schema.getTypeMap(),
|
||||
notFound,
|
||||
runMigrations,
|
||||
}
|
||||
}
|
||||
|
||||
export async function MedusaApp(
|
||||
options: MedusaAppOptions = {}
|
||||
): Promise<MedusaAppOutput> {
|
||||
try {
|
||||
return await MedusaApp_(options)
|
||||
} finally {
|
||||
MedusaModule.onApplicationStart(options.onApplicationStartCb)
|
||||
}
|
||||
}
|
||||
|
||||
export async function MedusaAppMigrateUp(
|
||||
options: MedusaAppOptions = {}
|
||||
): Promise<void> {
|
||||
const migrationOnly = true
|
||||
|
||||
const { runMigrations } = await MedusaApp_({
|
||||
...options,
|
||||
migrationOnly,
|
||||
})
|
||||
|
||||
await runMigrations().finally(MedusaModule.clearInstances)
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ import {
|
||||
InternalModuleDeclaration,
|
||||
LinkModuleDefinition,
|
||||
LoadedModule,
|
||||
MedusaContainer,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
MedusaContainer,
|
||||
ModuleBootstrapDeclaration,
|
||||
ModuleDefinition,
|
||||
ModuleExports,
|
||||
@@ -59,6 +59,11 @@ export type ModuleBootstrapOptions = {
|
||||
sharedContainer?: MedusaContainer
|
||||
moduleDefinition?: ModuleDefinition
|
||||
injectedDependencies?: Record<string, any>
|
||||
/**
|
||||
* In this mode, all instances are partially loaded, meaning that the module will not be fully loaded and the services will not be available.
|
||||
* Don't forget to clear the instances (MedusaModule.clearInstances()) after the migration are done.
|
||||
*/
|
||||
migrationOnly?: boolean
|
||||
}
|
||||
|
||||
export type LinkModuleBootstrapOptions = {
|
||||
@@ -213,6 +218,7 @@ export class MedusaModule {
|
||||
sharedContainer,
|
||||
moduleDefinition,
|
||||
injectedDependencies,
|
||||
migrationOnly,
|
||||
}: ModuleBootstrapOptions): Promise<{
|
||||
[key: string]: T
|
||||
}> {
|
||||
@@ -283,6 +289,7 @@ export class MedusaModule {
|
||||
container,
|
||||
moduleResolutions,
|
||||
logger,
|
||||
migrationOnly,
|
||||
})
|
||||
} catch (err) {
|
||||
errorLoading(err)
|
||||
|
||||
Reference in New Issue
Block a user