|
1
|
import { |
|
2
|
AdminOptions, |
|
3
|
ConfigModule, |
|
4
|
InputConfig, |
|
5
|
InputConfigModules, |
|
6
|
InputConfigWithArrayModules, |
|
7
|
InputConfigWithObjectModules, |
|
8
|
InternalModuleDeclaration, |
|
9
|
MedusaCloudOptions, |
|
10
|
} from "@medusajs/types" |
|
11
|
import { FeatureFlag } from "../feature-flags/flag-router" |
|
12
|
import { |
|
13
|
MODULE_PACKAGE_NAMES, |
|
14
|
Modules, |
|
15
|
REVERSED_MODULE_PACKAGE_NAMES, |
|
16
|
TEMPORARY_REDIS_MODULE_PACKAGE_NAMES, |
|
17
|
} from "../modules-sdk" |
|
18
|
import { isObject } from "./is-object" |
|
19
|
import { isProduction } from "./is-production" |
|
20
|
import { isString } from "./is-string" |
|
21
|
import { normalizeImportPathWithSource } from "./normalize-import-path-with-source" |
|
22
|
import { resolveExports } from "./resolve-exports" |
|
23
|
import { resolveFromProject } from "./resolve-from-project" |
|
24
|
import { tryConvertToNumber } from "./try-convert-to-number" |
|
25
|
|
|
26
|
const MEDUSA_CLOUD_EXECUTION_CONTEXT = "medusa-cloud" |
|
27
|
const DEFAULT_SECRET = "supersecret" |
|
28
|
|
|
29
|
/** |
|
30
|
* Returns the secret to use for signing tokens/cookies. In production |
|
31
|
* environments we never fall back to the hardcoded `DEFAULT_SECRET` since |
|
32
|
* it is publicly known and would allow forging tokens. The caller is |
|
33
|
* expected to surface a proper error downstream when the returned value |
|
34
|
* is `undefined` in production. |
|
35
|
*/ |
|
36
|
function resolveEnvSecret(envValue: string | undefined): string | undefined { |
|
37
|
if (envValue) { |
|
38
|
return envValue |
|
39
|
} |
|
40
|
|
|
41
|
if (isProduction()) { |
|
42
|
return undefined |
|
43
|
} |
|
44
|
|
|
45
|
return DEFAULT_SECRET |
|
46
|
} |
|
47
|
const DEFAULT_ADMIN_URL = "/" |
|
48
|
const DEFAULT_USER_AUTH_METHODS = ["emailpass"] |
|
49
|
const DEFAULT_STORE_CORS = "http://localhost:8000" |
|
50
|
const DEFAULT_DATABASE_URL = "postgres://localhost/medusa-starter-default" |
|
51
|
const DEFAULT_ADMIN_CORS = |
|
52
|
"http://localhost:7000,http://localhost:7001,http://localhost:5173" |
|
53
|
|
|
54
|
export const DEFAULT_STORE_RESTRICTED_FIELDS = [ |
|
55
|
"order", |
|
56
|
"orders", |
|
57
|
/* "customer", |
|
58
|
"customers", |
|
59
|
"payment_collection", |
|
60
|
"payment_collections"*/ |
|
61
|
] |
|
62
|
|
|
63
|
/** |
|
64
|
* The "defineConfig" helper can be used to define the configuration |
|
65
|
* of a medusa application. |
|
66
|
* |
|
67
|
* The helper under the hood merges your config with a set of defaults to |
|
68
|
* make an application work seamlessly, but still provide you the ability |
|
69
|
* to override configuration as needed. |
|
70
|
*/ |
|
71
|
export function defineConfig(config?: InputConfigWithArrayModules): ConfigModule |
|
72
|
/** |
|
73
|
* @deprecated Use array-based modules configuration instead |
|
74
|
*/ |
|
75
|
export function defineConfig( |
|
76
|
config?: InputConfigWithObjectModules |
|
77
|
): ConfigModule |
|
78
|
export function defineConfig(config: InputConfig = {}): ConfigModule { |
|
79
|
const options = { |
|
80
|
isCloud: process.env.EXECUTION_CONTEXT === MEDUSA_CLOUD_EXECUTION_CONTEXT, |
|
81
|
} |
|
82
|
|
|
83
|
const projectConfig = normalizeProjectConfig(config.projectConfig, options) |
|
84
|
const adminConfig = normalizeAdminConfig(config.admin) |
|
85
|
const projectDir = process.cwd() |
|
86
|
const modules = resolveModules( |
|
87
|
config.modules, |
|
88
|
options, |
|
89
|
config.projectConfig, |
|
90
|
projectDir |
|
91
|
) |
|
92
|
applyCloudOptionsToModules(modules, projectConfig?.cloud, adminConfig) |
|
93
|
const plugins = resolvePlugins(config.plugins, options) |
|
94
|
|
|
95
|
return { |
|
96
|
projectConfig, |
|
97
|
featureFlags: (config.featureFlags ?? {}) as ConfigModule["featureFlags"], |
|
98
|
admin: adminConfig, |
|
99
|
modules: modules, |
|
100
|
logger: config.logger, |
|
101
|
plugins, |
|
102
|
} |
|
103
|
} |
|
104
|
|
|
105
|
/** |
|
106
|
* Transforms an array of modules into an object. The last module will |
|
107
|
* take precedence in case of duplicate modules |
|
108
|
*/ |
|
109
|
export function transformModules( |
|
110
|
modules: InputConfigModules, |
|
111
|
projectDir: string = process.cwd() |
|
112
|
): Exclude<ConfigModule["modules"], undefined> { |
|
113
|
const remappedModules = modules.reduce((acc, moduleConfig) => { |
|
114
|
if (moduleConfig.scope === "external" && !moduleConfig.key) { |
|
115
|
throw new Error( |
|
116
|
"External modules configuration must have a 'key'. Please provide a key for the module." |
|
117
|
) |
|
118
|
} |
|
119
|
|
|
120
|
if ("disable" in moduleConfig && "key" in moduleConfig) { |
|
121
|
acc[moduleConfig.key!] = moduleConfig |
|
122
|
} |
|
123
|
|
|
124
|
// TODO: handle external modules later |
|
125
|
let serviceName: string = getKnownModuleName(moduleConfig) ?? "" |
|
126
|
delete moduleConfig.key |
|
127
|
|
|
128
|
if (!serviceName && "resolve" in moduleConfig) { |
|
129
|
if (!moduleConfig.resolve) { |
|
130
|
throw new Error( |
|
131
|
`Module must have a 'resolve' property in medusa-config.ts and it can't be empty. Please provide a module name or path for the module.` |
|
132
|
) |
|
133
|
} |
|
134
|
|
|
135
|
const resolution = isString(moduleConfig.resolve!) |
|
136
|
? normalizeImportPathWithSource( |
|
137
|
moduleConfig.resolve as string, |
|
138
|
projectDir |
|
139
|
) |
|
140
|
: moduleConfig.resolve |
|
141
|
|
|
142
|
/** |
|
143
|
* Plugin modules are referenced by a bare specifier |
|
144
|
* ("<plugin>/.medusa/server/src/modules/<name>"), so they must be |
|
145
|
* resolved from the project directory. Otherwise the lookup starts in |
|
146
|
* whichever "node_modules" directory `@medusajs/utils` was hoisted into, |
|
147
|
* which in a workspace monorepo does not contain the plugin. |
|
148
|
*/ |
|
149
|
const moduleExport = isString(resolution) |
|
150
|
? require(resolveFromProject(resolution, projectDir)) |
|
151
|
: resolution |
|
152
|
|
|
153
|
const defaultExport = resolveExports(moduleExport).default |
|
154
|
|
|
155
|
const joinerConfig = |
|
156
|
typeof defaultExport.service.prototype.__joinerConfig === "function" |
|
157
|
? defaultExport.service.prototype.__joinerConfig() ?? {} |
|
158
|
: defaultExport.service.prototype.__joinerConfig ?? {} |
|
159
|
|
|
160
|
serviceName = joinerConfig.serviceName |
|
161
|
|
|
162
|
if (!serviceName) { |
|
163
|
throw new Error( |
|
164
|
`Module ${moduleConfig.resolve} doesn't have a serviceName. Please provide a 'key' for the module or check the service joiner config.` |
|
165
|
) |
|
166
|
} |
|
167
|
} |
|
168
|
|
|
169
|
acc[serviceName] = moduleConfig |
|
170
|
|
|
171
|
return acc |
|
172
|
}, {}) |
|
173
|
|
|
174
|
return remappedModules as Exclude<ConfigModule["modules"], undefined> |
|
175
|
} |
|
176
|
|
|
177
|
function getKnownModuleName( |
|
178
|
moduleConfig: InputConfigModules[number] |
|
179
|
): string | undefined { |
|
180
|
if ("key" in moduleConfig && moduleConfig.key) { |
|
181
|
return moduleConfig.key |
|
182
|
} |
|
183
|
|
|
184
|
if ( |
|
185
|
"resolve" in moduleConfig && |
|
186
|
isString(moduleConfig.resolve!) && |
|
187
|
REVERSED_MODULE_PACKAGE_NAMES[moduleConfig.resolve!] |
|
188
|
) { |
|
189
|
return REVERSED_MODULE_PACKAGE_NAMES[moduleConfig.resolve!] |
|
190
|
} |
|
191
|
|
|
192
|
return undefined |
|
193
|
} |
|
194
|
|
|
195
|
function asRecord(value: unknown): Record<string, unknown> { |
|
196
|
return isObject(value) ? (value as Record<string, unknown>) : {} |
|
197
|
} |
|
198
|
|
|
199
|
function applyDefaultAuthMfaOptions( |
|
200
|
modules: InputConfigModules, |
|
201
|
defaultAuthModuleOptions: Record<string, unknown> |
|
202
|
) { |
|
203
|
modules.forEach((moduleConfig) => { |
|
204
|
const moduleName = getKnownModuleName(moduleConfig) |
|
205
|
|
|
206
|
if ( |
|
207
|
moduleName !== Modules.AUTH || |
|
208
|
("disable" in moduleConfig && moduleConfig.disable) |
|
209
|
) { |
|
210
|
return |
|
211
|
} |
|
212
|
|
|
213
|
const options = asRecord(moduleConfig.options) |
|
214
|
const defaultMfaOptions = asRecord(defaultAuthModuleOptions.mfa) |
|
215
|
const mfaOptions = asRecord(options.mfa) |
|
216
|
|
|
217
|
moduleConfig.options = { |
|
218
|
...options, |
|
219
|
mfa: { |
|
220
|
...defaultMfaOptions, |
|
221
|
...mfaOptions, |
|
222
|
encryption_key: |
|
223
|
mfaOptions.encryption_key ?? defaultMfaOptions.encryption_key, |
|
224
|
}, |
|
225
|
} |
|
226
|
}) |
|
227
|
} |
|
228
|
|
|
229
|
function resolvePlugins( |
|
230
|
configPlugins: InputConfig["plugins"], |
|
231
|
{ isCloud }: { isCloud: boolean } |
|
232
|
): ConfigModule["plugins"] { |
|
233
|
const defaultPlugins: Map<string, ConfigModule["plugins"][number]> = new Map([ |
|
234
|
[ |
|
235
|
"@medusajs/draft-order", |
|
236
|
{ resolve: "@medusajs/draft-order", options: {} }, |
|
237
|
], |
|
238
|
]) |
|
239
|
|
|
240
|
if (configPlugins?.length) { |
|
241
|
configPlugins.forEach((plugin) => { |
|
242
|
if (typeof plugin === "string") { |
|
243
|
defaultPlugins.set(plugin, { resolve: plugin, options: {} }) |
|
244
|
} else { |
|
245
|
defaultPlugins.set(plugin.resolve, plugin) |
|
246
|
} |
|
247
|
}) |
|
248
|
} |
|
249
|
|
|
250
|
// We don't have any cloud plugins yet, but we might in the future |
|
251
|
const cloudPlugins = [...Array.from(defaultPlugins.values())] |
|
252
|
|
|
253
|
return isCloud ? cloudPlugins : Array.from(defaultPlugins.values()) |
|
254
|
} |
|
255
|
|
|
256
|
/** |
|
257
|
* The user API allow to use array of modules configuration. This method manage the loading of the |
|
258
|
* user modules along side the default modules and re map them to an object. |
|
259
|
* |
|
260
|
* @param configModules |
|
261
|
*/ |
|
262
|
function resolveModules( |
|
263
|
configModules: InputConfig["modules"], |
|
264
|
{ isCloud }: { isCloud: boolean }, |
|
265
|
projectConfig: InputConfig["projectConfig"], |
|
266
|
projectDir: string = process.cwd() |
|
267
|
): Exclude<ConfigModule["modules"], undefined> { |
|
268
|
const authMfaEncryptionKey = process.env.AUTH_MFA_ENCRYPTION_KEY |
|
269
|
const authModuleOptions = { |
|
270
|
mfa: { |
|
271
|
encryption_key: authMfaEncryptionKey, |
|
272
|
}, |
|
273
|
providers: [ |
|
274
|
{ |
|
275
|
resolve: "@medusajs/medusa/auth-emailpass", |
|
276
|
id: "emailpass", |
|
277
|
}, |
|
278
|
], |
|
279
|
} |
|
280
|
|
|
281
|
const sharedModules = [ |
|
282
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.STOCK_LOCATION] }, |
|
283
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.INVENTORY] }, |
|
284
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.PRODUCT] }, |
|
285
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.PRICING] }, |
|
286
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.PROMOTION] }, |
|
287
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.CUSTOMER] }, |
|
288
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.SALES_CHANNEL] }, |
|
289
|
|
|
290
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.CART] }, |
|
291
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.REGION] }, |
|
292
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.API_KEY] }, |
|
293
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.STORE] }, |
|
294
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.TAX] }, |
|
295
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.CURRENCY] }, |
|
296
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.PAYMENT] }, |
|
297
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.ORDER] }, |
|
298
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.SETTINGS] }, |
|
299
|
|
|
300
|
{ |
|
301
|
resolve: MODULE_PACKAGE_NAMES[Modules.TRANSLATION], |
|
302
|
disable: !FeatureFlag.isFeatureEnabled("translation"), |
|
303
|
}, |
|
304
|
{ |
|
305
|
resolve: MODULE_PACKAGE_NAMES[Modules.RBAC], |
|
306
|
disable: !FeatureFlag.isFeatureEnabled("rbac"), |
|
307
|
}, |
|
308
|
|
|
309
|
{ |
|
310
|
resolve: MODULE_PACKAGE_NAMES[Modules.AUTH], |
|
311
|
options: authModuleOptions, |
|
312
|
}, |
|
313
|
{ |
|
314
|
resolve: MODULE_PACKAGE_NAMES[Modules.USER], |
|
315
|
options: { |
|
316
|
jwt_secret: |
|
317
|
projectConfig?.http?.jwtSecret ?? |
|
318
|
resolveEnvSecret(process.env.JWT_SECRET), |
|
319
|
jwt_options: projectConfig?.http?.jwtOptions, |
|
320
|
jwt_verify_options: projectConfig?.http?.jwtVerifyOptions, |
|
321
|
jwt_public_key: projectConfig?.http?.jwtPublicKey, |
|
322
|
}, |
|
323
|
}, |
|
324
|
{ |
|
325
|
resolve: MODULE_PACKAGE_NAMES[Modules.FULFILLMENT], |
|
326
|
options: { |
|
327
|
providers: [ |
|
328
|
{ |
|
329
|
resolve: "@medusajs/medusa/fulfillment-manual", |
|
330
|
id: "manual", |
|
331
|
}, |
|
332
|
], |
|
333
|
}, |
|
334
|
}, |
|
335
|
{ |
|
336
|
resolve: MODULE_PACKAGE_NAMES[Modules.NOTIFICATION], |
|
337
|
options: { |
|
338
|
providers: [ |
|
339
|
{ |
|
340
|
resolve: "@medusajs/medusa/notification-local", |
|
341
|
id: "local", |
|
342
|
options: { |
|
343
|
name: "Local Notification Provider", |
|
344
|
channels: ["feed"], |
|
345
|
}, |
|
346
|
}, |
|
347
|
], |
|
348
|
}, |
|
349
|
}, |
|
350
|
// Self-hosted: native Postgres FTS. Cloud without a search endpoint: Lakebase. |
|
351
|
// Cloud with MEDUSA_CLOUD_SEARCH_ENDPOINT: search-medusa is the default |
|
352
|
// (registered from options.cloud, same pattern as notification cloud email |
|
353
|
// and Medusa Payments). Postgres stays registered so a provider switch can |
|
354
|
// drop the previous engine's indexes during `db:migrate`. |
|
355
|
{ |
|
356
|
resolve: MODULE_PACKAGE_NAMES[Modules.SEARCH], |
|
357
|
options: { |
|
358
|
providers: [ |
|
359
|
{ |
|
360
|
resolve: "@medusajs/medusa/search-postgres", |
|
361
|
id: "postgres", |
|
362
|
options: { engine: isCloud ? "lakebase" : "native" }, |
|
363
|
}, |
|
364
|
], |
|
365
|
...(process.env.MEDUSA_CLOUD_SEARCH_ENDPOINT |
|
366
|
? { default_provider: "search-medusa" } |
|
367
|
: {}), |
|
368
|
}, |
|
369
|
}, |
|
370
|
] |
|
371
|
|
|
372
|
const defaultModules = [ |
|
373
|
...sharedModules, |
|
374
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.CACHE] }, |
|
375
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.EVENT_BUS] }, |
|
376
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.WORKFLOW_ENGINE] }, |
|
377
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.LOCKING] }, |
|
378
|
|
|
379
|
{ |
|
380
|
resolve: MODULE_PACKAGE_NAMES[Modules.FILE], |
|
381
|
options: { |
|
382
|
providers: [ |
|
383
|
{ |
|
384
|
resolve: "@medusajs/medusa/file-local", |
|
385
|
id: "local", |
|
386
|
}, |
|
387
|
], |
|
388
|
}, |
|
389
|
}, |
|
390
|
] |
|
391
|
|
|
392
|
const cloudModules: InputConfig["modules"] = [ |
|
393
|
...sharedModules, |
|
394
|
{ |
|
395
|
resolve: MODULE_PACKAGE_NAMES[Modules.FILE], |
|
396
|
options: { |
|
397
|
providers: [ |
|
398
|
{ |
|
399
|
id: "s3", |
|
400
|
resolve: "@medusajs/medusa/file-s3", |
|
401
|
options: { |
|
402
|
authentication_method: "s3-iam-role", |
|
403
|
file_url: process.env.S3_FILE_URL, |
|
404
|
prefix: process.env.S3_PREFIX, |
|
405
|
region: process.env.S3_REGION, |
|
406
|
bucket: process.env.S3_BUCKET, |
|
407
|
endpoint: process.env.S3_ENDPOINT, |
|
408
|
}, |
|
409
|
}, |
|
410
|
], |
|
411
|
}, |
|
412
|
}, |
|
413
|
] |
|
414
|
|
|
415
|
if (process.env.REDIS_URL) { |
|
416
|
cloudModules.push( |
|
417
|
...[ |
|
418
|
{ |
|
419
|
resolve: |
|
420
|
TEMPORARY_REDIS_MODULE_PACKAGE_NAMES[Modules.WORKFLOW_ENGINE], |
|
421
|
options: { |
|
422
|
redis: { url: process.env.REDIS_URL }, |
|
423
|
}, |
|
424
|
}, |
|
425
|
{ |
|
426
|
resolve: TEMPORARY_REDIS_MODULE_PACKAGE_NAMES[Modules.CACHE], |
|
427
|
options: { redisUrl: process.env.REDIS_URL }, |
|
428
|
}, |
|
429
|
{ |
|
430
|
resolve: TEMPORARY_REDIS_MODULE_PACKAGE_NAMES[Modules.EVENT_BUS], |
|
431
|
options: { |
|
432
|
redisUrl: process.env.REDIS_URL, |
|
433
|
workerOptions: { concurrency: 1 }, |
|
434
|
}, |
|
435
|
}, |
|
436
|
{ |
|
437
|
resolve: MODULE_PACKAGE_NAMES[Modules.LOCKING], |
|
438
|
options: { |
|
439
|
providers: [ |
|
440
|
{ |
|
441
|
id: "locking-redis", |
|
442
|
resolve: TEMPORARY_REDIS_MODULE_PACKAGE_NAMES[Modules.LOCKING], |
|
443
|
is_default: true, |
|
444
|
options: { |
|
445
|
redisUrl: process.env.REDIS_URL, |
|
446
|
}, |
|
447
|
}, |
|
448
|
], |
|
449
|
}, |
|
450
|
}, |
|
451
|
] |
|
452
|
) |
|
453
|
} else { |
|
454
|
cloudModules.push( |
|
455
|
...[ |
|
456
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.CACHE] }, |
|
457
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.EVENT_BUS] }, |
|
458
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.WORKFLOW_ENGINE] }, |
|
459
|
{ resolve: MODULE_PACKAGE_NAMES[Modules.LOCKING] }, |
|
460
|
] |
|
461
|
) |
|
462
|
} |
|
463
|
|
|
464
|
if (process.env.CACHE_REDIS_URL) { |
|
465
|
cloudModules.push({ |
|
466
|
resolve: MODULE_PACKAGE_NAMES[Modules.CACHING], |
|
467
|
options: { |
|
468
|
providers: [ |
|
469
|
{ |
|
470
|
id: "caching-redis", |
|
471
|
resolve: "@medusajs/medusa/caching-redis", |
|
472
|
is_default: true, |
|
473
|
options: { |
|
474
|
redisUrl: process.env.CACHE_REDIS_URL, |
|
475
|
}, |
|
476
|
}, |
|
477
|
], |
|
478
|
}, |
|
479
|
}) |
|
480
|
} |
|
481
|
|
|
482
|
/** |
|
483
|
* The default set of modules to always use. The end user can swap |
|
484
|
* the modules by providing an alternate implementation via their |
|
485
|
* config. But they can never remove a module from this list. |
|
486
|
*/ |
|
487
|
const modules: InputConfig["modules"] = isCloud |
|
488
|
? cloudModules |
|
489
|
: defaultModules |
|
490
|
|
|
491
|
/** |
|
492
|
* Backward compatibility for the old way of defining modules (object vs array) |
|
493
|
*/ |
|
494
|
if (configModules) { |
|
495
|
if (isObject(configModules)) { |
|
496
|
const modules_ = (configModules ?? |
|
497
|
{}) as unknown as Required<ConfigModule>["modules"] |
|
498
|
|
|
499
|
Object.entries(modules_).forEach(([key, moduleConfig]) => { |
|
500
|
modules.push({ |
|
501
|
key, |
|
502
|
...(isObject(moduleConfig) |
|
503
|
? moduleConfig |
|
504
|
: { disable: !moduleConfig }), |
|
505
|
} as InputConfigModules[number]) |
|
506
|
}) |
|
507
|
} else if (Array.isArray(configModules)) { |
|
508
|
const modules_ = (configModules ?? []) as InternalModuleDeclaration[] |
|
509
|
modules.push(...(modules_ as InputConfigModules)) |
|
510
|
} else { |
|
511
|
throw new Error( |
|
512
|
"Invalid modules configuration. Should be an array or object." |
|
513
|
) |
|
514
|
} |
|
515
|
} |
|
516
|
|
|
517
|
applyDefaultAuthMfaOptions(modules, authModuleOptions) |
|
518
|
|
|
519
|
return transformModules(modules, projectDir) |
|
520
|
} |
|
521
|
|
|
522
|
function normalizeProjectConfig( |
|
523
|
projectConfig: InputConfig["projectConfig"], |
|
524
|
{ isCloud }: { isCloud: boolean } |
|
525
|
): ConfigModule["projectConfig"] { |
|
526
|
const { http, redisOptions, sessionOptions, cloud, ...restOfProjectConfig } = |
|
527
|
projectConfig || {} |
|
528
|
|
|
529
|
const mergedCloudOptions: MedusaCloudOptions = { |
|
530
|
environmentHandle: process.env.MEDUSA_CLOUD_ENVIRONMENT_HANDLE, |
|
531
|
sandboxHandle: process.env.MEDUSA_CLOUD_SANDBOX_HANDLE, |
|
532
|
apiKey: process.env.MEDUSA_CLOUD_API_KEY, |
|
533
|
webhookSecret: process.env.MEDUSA_CLOUD_WEBHOOK_SECRET, |
|
534
|
emailsEndpoint: process.env.MEDUSA_CLOUD_EMAILS_ENDPOINT, |
|
535
|
paymentsEndpoint: process.env.MEDUSA_CLOUD_PAYMENTS_ENDPOINT, |
|
536
|
paymentAccounts: process.env.MEDUSA_CLOUD_PAYMENT_ACCOUNTS?.split(",") |
|
537
|
.map((handle) => handle.trim()) |
|
538
|
.filter(Boolean), |
|
539
|
searchEndpoint: process.env.MEDUSA_CLOUD_SEARCH_ENDPOINT, |
|
540
|
oauthAuthorizeEndpoint: process.env.MEDUSA_CLOUD_OAUTH_AUTHORIZE_ENDPOINT, |
|
541
|
oauthTokenEndpoint: process.env.MEDUSA_CLOUD_OAUTH_TOKEN_ENDPOINT, |
|
542
|
oauthCallbackUrl: process.env.MEDUSA_CLOUD_OAUTH_CALLBACK_URL, |
|
543
|
oauthDisabled: |
|
544
|
process.env.MEDUSA_CLOUD_OAUTH_DISABLED === "true" ? true : undefined, |
|
545
|
oauthJwksUri: process.env.MEDUSA_CLOUD_OAUTH_JWKS_ENDPOINT, |
|
546
|
oauthAudience: process.env.MEDUSA_CLOUD_OAUTH_AUDIENCE, |
|
547
|
...cloud, |
|
548
|
} |
|
549
|
const hasCloudOptions = Object.values(mergedCloudOptions).some( |
|
550
|
(value) => value !== undefined |
|
551
|
) |
|
552
|
|
|
553
|
/** |
|
554
|
* The defaults to use for the project config. They are shallow merged |
|
555
|
* with the user defined config. |
|
556
|
*/ |
|
557
|
const config = { |
|
558
|
...(isCloud ? { redisUrl: process.env.REDIS_URL } : {}), |
|
559
|
databaseUrl: process.env.DATABASE_URL || DEFAULT_DATABASE_URL, |
|
560
|
http: { |
|
561
|
storeCors: process.env.STORE_CORS || DEFAULT_STORE_CORS, |
|
562
|
adminCors: process.env.ADMIN_CORS || DEFAULT_ADMIN_CORS, |
|
563
|
authCors: process.env.AUTH_CORS || DEFAULT_ADMIN_CORS, |
|
564
|
jwtSecret: resolveEnvSecret(process.env.JWT_SECRET), |
|
565
|
jwtPublicKey: process.env.JWT_PUBLIC_KEY, |
|
566
|
cookieSecret: resolveEnvSecret(process.env.COOKIE_SECRET), |
|
567
|
restrictedFields: { |
|
568
|
store: DEFAULT_STORE_RESTRICTED_FIELDS, |
|
569
|
}, |
|
570
|
...http, |
|
571
|
}, |
|
572
|
redisOptions: { |
|
573
|
retryStrategy(retries) { |
|
574
|
/** |
|
575
|
* Exponentially increase delay with every retry |
|
576
|
* attempt. Max to 4s |
|
577
|
*/ |
|
578
|
const delay = Math.min(Math.pow(2, retries) * 50, 4000) |
|
579
|
|
|
580
|
/** |
|
581
|
* Add a random jitter to not choke the server when multiple |
|
582
|
* clients are retrying at the same time |
|
583
|
*/ |
|
584
|
const jitter = Math.floor(Math.random() * 200) |
|
585
|
return delay + jitter |
|
586
|
}, |
|
587
|
...redisOptions, |
|
588
|
}, |
|
589
|
sessionOptions: { |
|
590
|
...(isCloud && process.env.SESSION_STORE === "dynamodb" |
|
591
|
? { |
|
592
|
dynamodbOptions: { |
|
593
|
prefix: process.env.DYNAMO_DB_SESSIONS_PREFIX ?? "sess:", |
|
594
|
hashKey: process.env.DYNAMO_DB_SESSIONS_HASH_KEY ?? "id", |
|
595
|
initialized: process.env.DYNAMO_DB_SESSIONS_CREATE_TABLE |
|
596
|
? false |
|
597
|
: true, |
|
598
|
table: process.env.DYNAMO_DB_SESSIONS_TABLE ?? "medusa-sessions", |
|
599
|
readCapacityUnits: tryConvertToNumber( |
|
600
|
process.env.DYNAMO_DB_SESSIONS_READ_UNITS, |
|
601
|
5 |
|
602
|
), |
|
603
|
writeCapacityUnits: tryConvertToNumber( |
|
604
|
process.env.DYNAMO_DB_SESSIONS_WRITE_UNITS, |
|
605
|
5 |
|
606
|
), |
|
607
|
skipThrowMissingSpecialKeys: true, |
|
608
|
}, |
|
609
|
} |
|
610
|
: {}), |
|
611
|
...sessionOptions, |
|
612
|
}, |
|
613
|
// If there are no cloud options, we better don't pollute the project config for people not using the cloud |
|
614
|
...(hasCloudOptions ? { cloud: mergedCloudOptions } : {}), |
|
615
|
...restOfProjectConfig, |
|
616
|
} satisfies ConfigModule["projectConfig"] |
|
617
|
|
|
618
|
config.http.authMethodsPerActor = { |
|
619
|
...config.http.authMethodsPerActor, |
|
620
|
user: config.http.authMethodsPerActor?.user ?? DEFAULT_USER_AUTH_METHODS, |
|
621
|
} |
|
622
|
|
|
623
|
if ( |
|
624
|
isCloud && |
|
625
|
!mergedCloudOptions.oauthDisabled && |
|
626
|
mergedCloudOptions.oauthAuthorizeEndpoint && |
|
627
|
mergedCloudOptions.oauthTokenEndpoint |
|
628
|
) { |
|
629
|
const userAuthMethods = config.http.authMethodsPerActor?.user ?? [ |
|
630
|
"emailpass", |
|
631
|
] |
|
632
|
config.http.authMethodsPerActor = { |
|
633
|
...config.http.authMethodsPerActor, |
|
634
|
user: userAuthMethods.concat("cloud"), |
|
635
|
} |
|
636
|
} |
|
637
|
|
|
638
|
return config |
|
639
|
} |
|
640
|
|
|
641
|
function normalizeAdminConfig( |
|
642
|
adminConfig: InputConfig["admin"] |
|
643
|
): ConfigModule["admin"] { |
|
644
|
/** |
|
645
|
* The defaults to use for the admin config. They are shallow merged |
|
646
|
* with the user defined config |
|
647
|
*/ |
|
648
|
return { |
|
649
|
backendUrl: process.env.MEDUSA_BACKEND_URL || DEFAULT_ADMIN_URL, |
|
650
|
path: "/app", |
|
651
|
maxUploadFileSize: 1024 * 1024, // 1MB default |
|
652
|
...adminConfig, |
|
653
|
} |
|
654
|
} |
|
655
|
|
|
656
|
function applyCloudOptionsToModules( |
|
657
|
modules: Exclude<ConfigModule["modules"], undefined>, |
|
658
|
config?: MedusaCloudOptions, |
|
659
|
adminConfig?: AdminOptions |
|
660
|
) { |
|
661
|
if (!config) { |
|
662
|
return |
|
663
|
} |
|
664
|
|
|
665
|
for (const name in modules) { |
|
666
|
const module = modules[name] |
|
667
|
if (typeof module !== "object") { |
|
668
|
continue |
|
669
|
} |
|
670
|
|
|
671
|
switch (name) { |
|
672
|
case Modules.NOTIFICATION: |
|
673
|
module.options = { |
|
674
|
cloud: { |
|
675
|
api_key: config.apiKey, |
|
676
|
endpoint: config.emailsEndpoint, |
|
677
|
environment_handle: config.environmentHandle, |
|
678
|
sandbox_handle: config.sandboxHandle, |
|
679
|
}, |
|
680
|
...(module.options ?? {}), |
|
681
|
} |
|
682
|
break |
|
683
|
case Modules.PAYMENT: |
|
684
|
module.options = { |
|
685
|
cloud: { |
|
686
|
api_key: config.apiKey, |
|
687
|
webhook_secret: config.webhookSecret, |
|
688
|
endpoint: config.paymentsEndpoint, |
|
689
|
payment_accounts: config.paymentAccounts, |
|
690
|
environment_handle: config.environmentHandle, |
|
691
|
sandbox_handle: config.sandboxHandle, |
|
692
|
}, |
|
693
|
...(module.options ?? {}), |
|
694
|
} |
|
695
|
break |
|
696
|
case Modules.SEARCH: |
|
697
|
module.options = { |
|
698
|
cloud: { |
|
699
|
api_key: config.apiKey, |
|
700
|
endpoint: config.searchEndpoint, |
|
701
|
environment_handle: config.environmentHandle, |
|
702
|
}, |
|
703
|
...(module.options ?? {}), |
|
704
|
} |
|
705
|
break |
|
706
|
case Modules.AUTH: |
|
707
|
let callbackUrl = config.oauthCallbackUrl |
|
708
|
if (!callbackUrl && adminConfig?.backendUrl) { |
|
709
|
callbackUrl = `${adminConfig?.backendUrl}${adminConfig?.path}/login?auth_provider=cloud` |
|
710
|
} |
|
711
|
module.options = { |
|
712
|
cloud: { |
|
713
|
oauth_authorize_endpoint: config.oauthAuthorizeEndpoint, |
|
714
|
oauth_token_endpoint: config.oauthTokenEndpoint, |
|
715
|
oauth_jwks_uri: config.oauthJwksUri, |
|
716
|
oauth_audience: config.oauthAudience, |
|
717
|
environment_handle: config.environmentHandle, |
|
718
|
sandbox_handle: config.sandboxHandle, |
|
719
|
api_key: config.apiKey, |
|
720
|
callback_url: callbackUrl, |
|
721
|
disabled: config.oauthDisabled, |
|
722
|
}, |
|
723
|
...(module.options ?? {}), |
|
724
|
} |
|
725
|
break |
|
726
|
default: |
|
727
|
break |
|
728
|
} |
|
729
|
} |
|
730
|
} |