Feat(modules-sdk,inventory,stock-location): modules isolated connection (#3329)
* feat: scoped container for modules
This commit is contained in:
@@ -9,7 +9,7 @@ import { join } from "path"
|
||||
function getConfigFile<TConfig = unknown>(
|
||||
rootDir: string,
|
||||
configName: string
|
||||
): { configModule: TConfig; configFilePath: string, error?: any } {
|
||||
): { configModule: TConfig; configFilePath: string; error?: any } {
|
||||
const configPath = join(rootDir, configName)
|
||||
let configFilePath = ``
|
||||
let configModule
|
||||
@@ -22,6 +22,10 @@ function getConfigFile<TConfig = unknown>(
|
||||
err = e
|
||||
}
|
||||
|
||||
if (configModule && typeof configModule.default === "object") {
|
||||
configModule = configModule.default
|
||||
}
|
||||
|
||||
return { configModule, configFilePath, error: err }
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ export * from "./graceful-shutdown-server"
|
||||
export { default as humanizeAmount } from "./humanize-amount"
|
||||
export { indexTypes } from "./index-types"
|
||||
export * from "./is-defined"
|
||||
export * from "./medusa-container"
|
||||
export { parseCorsOrigins } from "./parse-cors-origins"
|
||||
export { transformIdableFields } from "./transform-idable-fields"
|
||||
export { default as Validator } from "./validator"
|
||||
export { default as zeroDecimalCurrencies } from "./zero-decimal-currencies"
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
asFunction,
|
||||
asValue,
|
||||
AwilixContainer,
|
||||
ClassOrFunctionReturning,
|
||||
createContainer,
|
||||
Resolver,
|
||||
} from "awilix"
|
||||
|
||||
export type MedusaContainer = AwilixContainer & {
|
||||
registerAdd: <T>(name: string, registration: T) => MedusaContainer
|
||||
createScope: () => MedusaContainer
|
||||
}
|
||||
|
||||
function asArray(
|
||||
resolvers: (ClassOrFunctionReturning<unknown> | Resolver<unknown>)[]
|
||||
): { resolve: (container: AwilixContainer) => unknown[] } {
|
||||
return {
|
||||
resolve: (container: AwilixContainer) =>
|
||||
resolvers.map((resolver) => container.build(resolver)),
|
||||
}
|
||||
}
|
||||
|
||||
function registerAdd(
|
||||
this: MedusaContainer,
|
||||
name: string,
|
||||
registration: typeof asFunction | typeof asValue
|
||||
) {
|
||||
const storeKey = name + "_STORE"
|
||||
|
||||
if (this.registrations[storeKey] === undefined) {
|
||||
this.register(storeKey, asValue([] as Resolver<unknown>[]))
|
||||
}
|
||||
const store = this.resolve(storeKey) as (
|
||||
| ClassOrFunctionReturning<unknown>
|
||||
| Resolver<unknown>
|
||||
)[]
|
||||
|
||||
if (this.registrations[name] === undefined) {
|
||||
this.register(name, asArray(store))
|
||||
}
|
||||
store.unshift(registration)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
export function createMedusaContainer(...args): MedusaContainer {
|
||||
const container = createContainer.apply(null, args) as MedusaContainer
|
||||
|
||||
container.registerAdd = registerAdd.bind(container)
|
||||
|
||||
const originalScope = container.createScope
|
||||
container.createScope = () => {
|
||||
const scoped = originalScope() as MedusaContainer
|
||||
scoped.registerAdd = registerAdd.bind(scoped)
|
||||
|
||||
return scoped
|
||||
}
|
||||
|
||||
return container
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
import { default as Joi } from "joi"
|
||||
|
||||
const dateFilter = () => {
|
||||
return Joi.object({
|
||||
lt: Joi.alternatives(Joi.date().timestamp("unix"), Joi.date()),
|
||||
gt: Joi.alternatives(Joi.date().timestamp("unix"), Joi.date()),
|
||||
gte: Joi.alternatives(Joi.date().timestamp("unix"), Joi.date()),
|
||||
lte: Joi.alternatives(Joi.date().timestamp("unix"), Joi.date()),
|
||||
})
|
||||
}
|
||||
|
||||
Object.assign(Joi, {
|
||||
objectId: require("joi-objectid")(Joi),
|
||||
address: () => {
|
||||
return Joi.alternatives().try(
|
||||
Joi.string(),
|
||||
Joi.object().keys({
|
||||
first_name: Joi.string().required(),
|
||||
last_name: Joi.string().required(),
|
||||
company: Joi.string().optional(),
|
||||
address_1: Joi.string().required(),
|
||||
address_2: Joi.string().allow(null, "").optional(),
|
||||
city: Joi.string().required(),
|
||||
country_code: Joi.string().required(),
|
||||
province: Joi.string().allow(null, "").optional(),
|
||||
postal_code: Joi.string().required(),
|
||||
phone: Joi.string().optional(),
|
||||
metadata: Joi.object().allow(null, {}).optional(),
|
||||
})
|
||||
)
|
||||
},
|
||||
dateFilter,
|
||||
orderFilter: () => {
|
||||
return Joi.object().keys({
|
||||
id: Joi.string(),
|
||||
q: Joi.string(),
|
||||
status: Joi.array()
|
||||
.items(
|
||||
Joi.string().valid(
|
||||
"pending",
|
||||
"completed",
|
||||
"archived",
|
||||
"canceled",
|
||||
"requires_action"
|
||||
)
|
||||
)
|
||||
.single(),
|
||||
fulfillment_status: Joi.array()
|
||||
.items(
|
||||
Joi.string().valid(
|
||||
"not_fulfilled",
|
||||
"fulfilled",
|
||||
"partially_fulfilled",
|
||||
"shipped",
|
||||
"partially_shipped",
|
||||
"canceled",
|
||||
"returned",
|
||||
"partially_returned",
|
||||
"requires_action"
|
||||
)
|
||||
)
|
||||
.single(),
|
||||
payment_status: Joi.array()
|
||||
.items(
|
||||
Joi.string().valid(
|
||||
"captured",
|
||||
"awaiting",
|
||||
"not_paid",
|
||||
"refunded",
|
||||
"partially_refunded",
|
||||
"canceled",
|
||||
"requires_action"
|
||||
)
|
||||
)
|
||||
.single(),
|
||||
display_id: Joi.string(),
|
||||
cart_id: Joi.string(),
|
||||
offset: Joi.string(),
|
||||
limit: Joi.string(),
|
||||
expand: Joi.string(),
|
||||
fields: Joi.string(),
|
||||
customer_id: Joi.string(),
|
||||
email: Joi.string(),
|
||||
region_id: Joi.string(),
|
||||
currency_code: Joi.string(),
|
||||
tax_rate: Joi.string(),
|
||||
canceled_at: dateFilter(),
|
||||
created_at: dateFilter(),
|
||||
updated_at: dateFilter(),
|
||||
})
|
||||
},
|
||||
productFilter: () => {
|
||||
return Joi.object().keys({
|
||||
id: Joi.string(),
|
||||
q: Joi.string().allow(null, ""),
|
||||
status: Joi.array()
|
||||
.items(Joi.string().valid("proposed", "draft", "published", "rejected"))
|
||||
.single(),
|
||||
collection_id: Joi.array().items(Joi.string()).single(),
|
||||
tags: Joi.array().items(Joi.string()).single(),
|
||||
title: Joi.string(),
|
||||
description: Joi.string(),
|
||||
handle: Joi.string(),
|
||||
is_giftcard: Joi.string(),
|
||||
type: Joi.string(),
|
||||
offset: Joi.string(),
|
||||
limit: Joi.string(),
|
||||
expand: Joi.string(),
|
||||
fields: Joi.string(),
|
||||
order: Joi.string().optional(),
|
||||
created_at: dateFilter(),
|
||||
updated_at: dateFilter(),
|
||||
deleted_at: dateFilter(),
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
declare module "joi" {
|
||||
interface Root {
|
||||
objectId: Joi.StringSchema
|
||||
address: <T>() => Joi.AlternativesSchema
|
||||
dateFilter: <T>() => Joi.ObjectSchema<T>
|
||||
orderFilter: <T>() => Joi.ObjectSchema<T>
|
||||
productFilter: <T>() => Joi.ObjectSchema<T>
|
||||
}
|
||||
}
|
||||
|
||||
export default Joi
|
||||
Reference in New Issue
Block a user