add Medusa Cloud Email provider (#13781)

* add Medusa Cloud Email provider

* move cloud config to project level

* add tests

* Create breezy-flowers-fly.md

* rename medusa_cloud_config to cloud

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Pedro Guzman
2025-10-27 12:39:32 +01:00
committed by GitHub
parent a9dbd035a5
commit cc2614ded7
11 changed files with 721 additions and 18 deletions

View File

@@ -2010,4 +2010,371 @@ describe("defineConfig", function () {
}
`)
})
it("should add cloud options to the project config and relevant modules if the environment variables are set", function () {
const originalEnv = { ...process.env }
process.env.MEDUSA_CLOUD_ENVIRONMENT_HANDLE = "test-environment"
process.env.MEDUSA_CLOUD_API_KEY = "test-api-key"
process.env.MEDUSA_CLOUD_EMAILS_ENDPOINT = "test-emails-endpoint"
const config = defineConfig()
process.env = { ...originalEnv }
expect(config).toMatchInlineSnapshot(`
{
"admin": {
"backendUrl": "/",
"path": "/app",
},
"featureFlags": {},
"logger": undefined,
"modules": {
"api_key": {
"resolve": "@medusajs/medusa/api-key",
},
"auth": {
"options": {
"providers": [
{
"id": "emailpass",
"resolve": "@medusajs/medusa/auth-emailpass",
},
],
},
"resolve": "@medusajs/medusa/auth",
},
"cache": {
"resolve": "@medusajs/medusa/cache-inmemory",
},
"cart": {
"resolve": "@medusajs/medusa/cart",
},
"currency": {
"resolve": "@medusajs/medusa/currency",
},
"customer": {
"resolve": "@medusajs/medusa/customer",
},
"event_bus": {
"resolve": "@medusajs/medusa/event-bus-local",
},
"file": {
"options": {
"providers": [
{
"id": "local",
"resolve": "@medusajs/medusa/file-local",
},
],
},
"resolve": "@medusajs/medusa/file",
},
"fulfillment": {
"options": {
"providers": [
{
"id": "manual",
"resolve": "@medusajs/medusa/fulfillment-manual",
},
],
},
"resolve": "@medusajs/medusa/fulfillment",
},
"inventory": {
"resolve": "@medusajs/medusa/inventory",
},
"locking": {
"resolve": "@medusajs/medusa/locking",
},
"notification": {
"options": {
"cloud": {
"api_key": "test-api-key",
"endpoint": "test-emails-endpoint",
"environment_handle": "test-environment",
},
"providers": [
{
"id": "local",
"options": {
"channels": [
"feed",
],
"name": "Local Notification Provider",
},
"resolve": "@medusajs/medusa/notification-local",
},
],
},
"resolve": "@medusajs/medusa/notification",
},
"order": {
"resolve": "@medusajs/medusa/order",
},
"payment": {
"resolve": "@medusajs/medusa/payment",
},
"pricing": {
"resolve": "@medusajs/medusa/pricing",
},
"product": {
"resolve": "@medusajs/medusa/product",
},
"promotion": {
"resolve": "@medusajs/medusa/promotion",
},
"region": {
"resolve": "@medusajs/medusa/region",
},
"sales_channel": {
"resolve": "@medusajs/medusa/sales-channel",
},
"settings": {
"resolve": "@medusajs/medusa/settings",
},
"stock_location": {
"resolve": "@medusajs/medusa/stock-location",
},
"store": {
"resolve": "@medusajs/medusa/store",
},
"tax": {
"resolve": "@medusajs/medusa/tax",
},
"user": {
"options": {
"jwt_options": undefined,
"jwt_public_key": undefined,
"jwt_secret": "supersecret",
"jwt_verify_options": undefined,
},
"resolve": "@medusajs/medusa/user",
},
"workflows": {
"resolve": "@medusajs/medusa/workflow-engine-inmemory",
},
},
"plugins": [
{
"options": {},
"resolve": "@medusajs/draft-order",
},
],
"projectConfig": {
"databaseUrl": "postgres://localhost/medusa-starter-default",
"http": {
"adminCors": "http://localhost:7000,http://localhost:7001,http://localhost:5173",
"authCors": "http://localhost:7000,http://localhost:7001,http://localhost:5173",
"cookieSecret": "supersecret",
"jwtPublicKey": undefined,
"jwtSecret": "supersecret",
"restrictedFields": {
"store": [
${DEFAULT_STORE_RESTRICTED_FIELDS.map((v) => `"${v}"`).join(
",\n "
)},
],
},
"storeCors": "http://localhost:8000",
},
"medusaCloudOptions": {
"apiKey": "test-api-key",
"emailsEndpoint": "test-emails-endpoint",
"environmentHandle": "test-environment",
},
"redisOptions": {
"retryStrategy": [Function],
},
"sessionOptions": {},
},
}
`)
})
it("should merge custom projectConfig.medusaCloudOptions", function () {
const originalEnv = { ...process.env }
process.env.MEDUSA_CLOUD_ENVIRONMENT_HANDLE = "test-environment"
process.env.MEDUSA_CLOUD_API_KEY = "test-api-key"
process.env.MEDUSA_CLOUD_EMAILS_ENDPOINT = "test-emails-endpoint"
const config = defineConfig({
projectConfig: {
http: {} as any,
medusaCloudOptions: {
environmentHandle: "overriden-environment",
apiKey: "overriden-api-key",
emailsEndpoint: "overriden-emails-endpoint",
},
},
})
process.env = { ...originalEnv }
expect(config).toMatchInlineSnapshot(`
{
"admin": {
"backendUrl": "/",
"path": "/app",
},
"featureFlags": {},
"logger": undefined,
"modules": {
"api_key": {
"resolve": "@medusajs/medusa/api-key",
},
"auth": {
"options": {
"providers": [
{
"id": "emailpass",
"resolve": "@medusajs/medusa/auth-emailpass",
},
],
},
"resolve": "@medusajs/medusa/auth",
},
"cache": {
"resolve": "@medusajs/medusa/cache-inmemory",
},
"cart": {
"resolve": "@medusajs/medusa/cart",
},
"currency": {
"resolve": "@medusajs/medusa/currency",
},
"customer": {
"resolve": "@medusajs/medusa/customer",
},
"event_bus": {
"resolve": "@medusajs/medusa/event-bus-local",
},
"file": {
"options": {
"providers": [
{
"id": "local",
"resolve": "@medusajs/medusa/file-local",
},
],
},
"resolve": "@medusajs/medusa/file",
},
"fulfillment": {
"options": {
"providers": [
{
"id": "manual",
"resolve": "@medusajs/medusa/fulfillment-manual",
},
],
},
"resolve": "@medusajs/medusa/fulfillment",
},
"inventory": {
"resolve": "@medusajs/medusa/inventory",
},
"locking": {
"resolve": "@medusajs/medusa/locking",
},
"notification": {
"options": {
"cloud": {
"api_key": "overriden-api-key",
"endpoint": "overriden-emails-endpoint",
"environment_handle": "overriden-environment",
},
"providers": [
{
"id": "local",
"options": {
"channels": [
"feed",
],
"name": "Local Notification Provider",
},
"resolve": "@medusajs/medusa/notification-local",
},
],
},
"resolve": "@medusajs/medusa/notification",
},
"order": {
"resolve": "@medusajs/medusa/order",
},
"payment": {
"resolve": "@medusajs/medusa/payment",
},
"pricing": {
"resolve": "@medusajs/medusa/pricing",
},
"product": {
"resolve": "@medusajs/medusa/product",
},
"promotion": {
"resolve": "@medusajs/medusa/promotion",
},
"region": {
"resolve": "@medusajs/medusa/region",
},
"sales_channel": {
"resolve": "@medusajs/medusa/sales-channel",
},
"settings": {
"resolve": "@medusajs/medusa/settings",
},
"stock_location": {
"resolve": "@medusajs/medusa/stock-location",
},
"store": {
"resolve": "@medusajs/medusa/store",
},
"tax": {
"resolve": "@medusajs/medusa/tax",
},
"user": {
"options": {
"jwt_options": undefined,
"jwt_public_key": undefined,
"jwt_secret": "supersecret",
"jwt_verify_options": undefined,
},
"resolve": "@medusajs/medusa/user",
},
"workflows": {
"resolve": "@medusajs/medusa/workflow-engine-inmemory",
},
},
"plugins": [
{
"options": {},
"resolve": "@medusajs/draft-order",
},
],
"projectConfig": {
"databaseUrl": "postgres://localhost/medusa-starter-default",
"http": {
"adminCors": "http://localhost:7000,http://localhost:7001,http://localhost:5173",
"authCors": "http://localhost:7000,http://localhost:7001,http://localhost:5173",
"cookieSecret": "supersecret",
"jwtPublicKey": undefined,
"jwtSecret": "supersecret",
"restrictedFields": {
"store": [
${DEFAULT_STORE_RESTRICTED_FIELDS.map((v) => `"${v}"`).join(
",\n "
)},
],
},
"storeCors": "http://localhost:8000",
},
"medusaCloudOptions": {
"apiKey": "overriden-api-key",
"emailsEndpoint": "overriden-emails-endpoint",
"environmentHandle": "overriden-environment",
},
"redisOptions": {
"retryStrategy": [Function],
},
"sessionOptions": {},
},
}
`)
})
})

View File

@@ -3,6 +3,7 @@ import {
InputConfig,
InputConfigModules,
InternalModuleDeclaration,
MedusaCloudOptions,
} from "@medusajs/types"
import {
MODULE_PACKAGE_NAMES,
@@ -49,6 +50,7 @@ export function defineConfig(config: InputConfig = {}): ConfigModule {
const projectConfig = normalizeProjectConfig(config.projectConfig, options)
const adminConfig = normalizeAdminConfig(config.admin)
const modules = resolveModules(config.modules, options, config.projectConfig)
applyCloudOptionsToModules(modules, projectConfig?.medusaCloudOptions)
const plugins = resolvePlugins(config.plugins, options)
return {
@@ -363,8 +365,23 @@ function normalizeProjectConfig(
projectConfig: InputConfig["projectConfig"],
{ isCloud }: { isCloud: boolean }
): ConfigModule["projectConfig"] {
const { http, redisOptions, sessionOptions, ...restOfProjectConfig } =
projectConfig || {}
const {
http,
redisOptions,
sessionOptions,
medusaCloudOptions,
...restOfProjectConfig
} = projectConfig || {}
const mergedCloudOptions: MedusaCloudOptions = {
environmentHandle: process.env.MEDUSA_CLOUD_ENVIRONMENT_HANDLE,
apiKey: process.env.MEDUSA_CLOUD_API_KEY,
emailsEndpoint: process.env.MEDUSA_CLOUD_EMAILS_ENDPOINT,
...medusaCloudOptions,
}
const hasCloudOptions = Object.values(mergedCloudOptions).some(
(value) => value !== undefined
)
/**
* The defaults to use for the project config. They are shallow merged
@@ -426,6 +443,8 @@ function normalizeProjectConfig(
: {}),
...sessionOptions,
},
// If there are no cloud options, we better don't pollute the project config for people not using the cloud
...(hasCloudOptions ? { medusaCloudOptions: mergedCloudOptions } : {}),
...restOfProjectConfig,
} satisfies ConfigModule["projectConfig"]
@@ -445,3 +464,35 @@ function normalizeAdminConfig(
...adminConfig,
}
}
function applyCloudOptionsToModules(
modules: Exclude<ConfigModule["modules"], undefined>,
config?: MedusaCloudOptions
) {
if (!config) {
return
}
for (const name in modules) {
const module = modules[name]
if (typeof module !== "object") {
continue
}
switch (name) {
case Modules.NOTIFICATION:
module.options = {
cloud: {
api_key: config.apiKey,
endpoint: config.emailsEndpoint,
environment_handle: config.environmentHandle,
},
...(module.options ?? {}),
}
break
// Will add payment module soon
default:
break
}
}
}