feat(utils): define file config (#13283)
** What
- Allow auto-loaded Medusa files to export a config object.
- Currently supports isDisabled to control loading.
- new instance `FeatureFlag` exported by `@medusajs/framework/utils`
- `feature-flags` is now a supported folder for medusa projects, modules, providers and plugins. They will be loaded and added to `FeatureFlag`
** Why
- Enables conditional loading of routes, migrations, jobs, subscribers, workflows, and other files based on feature flags.
```ts
// /src/feature-flags
import { FlagSettings } from "@medusajs/framework/feature-flags"
const CustomFeatureFlag: FlagSettings = {
key: "custom_feature",
default_val: false,
env_key: "FF_MY_CUSTOM_FEATURE",
description: "Enable xyz",
}
export default CustomFeatureFlag
```
```ts
// /src/modules/my-custom-module/migration/Migration20250822135845.ts
import { FeatureFlag } from "@medusajs/framework/utils"
export class Migration20250822135845 extends Migration {
override async up(){ }
override async down(){ }
}
defineFileConfig({
isDisabled: () => !FeatureFlag.isFeatureEnabled("custom_feature")
})
```
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import { generateResetPasswordTokenWorkflow } from "@medusajs/core-flows"
|
||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
import jwt from "jsonwebtoken"
|
||||
import path from "path"
|
||||
import {
|
||||
adminHeaders,
|
||||
createAdminUser,
|
||||
} from "../../../../helpers/create-admin-user"
|
||||
import path from "path"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ const promotionData = {
|
||||
],
|
||||
}
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const env = {}
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import { medusaTshirtProduct } from "../../../__fixtures__/product"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const env = {}
|
||||
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
|
||||
|
||||
const shippingAddressData = {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { MedusaWorkflow } from "@medusajs/framework/workflows-sdk"
|
||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import path from "path"
|
||||
import { setTimeout as setTimeoutPromise } from "timers/promises"
|
||||
import { testJobHandler } from "../../__fixtures__/feature-flag/src/jobs/test-job"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
cwd: path.join(__dirname, "../../__fixtures__/feature-flag"),
|
||||
env: {
|
||||
CUSTOM_FF: true,
|
||||
},
|
||||
testSuite: ({ api, dbConnection }) => {
|
||||
describe("Resources loaded with feature flags", () => {
|
||||
it("should load migration when feature flag is enabled and run job", async () => {
|
||||
const migrationNotExecuted = await dbConnection.raw(
|
||||
`SELECT name FROM "mikro_orm_migrations" WHERE name = 'Noop'`
|
||||
)
|
||||
expect(migrationNotExecuted.rows).toHaveLength(0)
|
||||
|
||||
const migrationExecuted = await dbConnection.raw(
|
||||
`SELECT name FROM "mikro_orm_migrations" WHERE name = 'MigrationTest'`
|
||||
)
|
||||
|
||||
expect(migrationExecuted.rows).toHaveLength(1)
|
||||
expect(migrationExecuted.rows[0].name).toBe("MigrationTest")
|
||||
|
||||
await setTimeoutPromise(1000)
|
||||
|
||||
expect(testJobHandler).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("should load workflow when feature flag is enabled", async () => {
|
||||
expect(MedusaWorkflow.getWorkflow("test-workflow")).toBeDefined()
|
||||
})
|
||||
|
||||
it("should load scheduled job when feature flag is enabled", async () => {
|
||||
expect(
|
||||
MedusaWorkflow.getWorkflow("job-greeting-every-second")
|
||||
).toBeDefined()
|
||||
})
|
||||
|
||||
it("should load endpoint when feature flag is enabled", async () => {
|
||||
expect((await api.get("/custom")).status).toBe(200)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,46 @@
|
||||
import { MedusaWorkflow } from "@medusajs/framework/workflows-sdk"
|
||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import path from "path"
|
||||
import { setTimeout as setTimeoutPromise } from "timers/promises"
|
||||
import { testJobHandler } from "../../__fixtures__/feature-flag/src/jobs/test-job"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
cwd: path.join(__dirname, "../../__fixtures__/feature-flag"),
|
||||
testSuite: ({ api, dbConnection }) => {
|
||||
describe("Resources loaded without feature flags", () => {
|
||||
it("should not load migration when feature flag is disabled and not run job", async () => {
|
||||
const migrationNotExecuted = await dbConnection.raw(
|
||||
`SELECT name FROM "mikro_orm_migrations" WHERE name = 'MigrationTest'`
|
||||
)
|
||||
expect(migrationNotExecuted.rows).toHaveLength(0)
|
||||
|
||||
const migrationExecuted = await dbConnection.raw(
|
||||
`SELECT name FROM "mikro_orm_migrations" WHERE name = 'Noop'`
|
||||
)
|
||||
|
||||
expect(migrationExecuted.rows).toHaveLength(1)
|
||||
expect(migrationExecuted.rows[0].name).toBe("Noop")
|
||||
|
||||
await setTimeoutPromise(1000)
|
||||
|
||||
expect(testJobHandler).toHaveBeenCalledTimes(0)
|
||||
})
|
||||
|
||||
it("should not load workflow when feature flag is disabled", async () => {
|
||||
expect(MedusaWorkflow.getWorkflow("test-workflow")).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should not load scheduled job when feature flag is disabled", async () => {
|
||||
expect(
|
||||
MedusaWorkflow.getWorkflow("job-greeting-every-second")
|
||||
).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should not load endpoint when feature flag is disabled", async () => {
|
||||
expect(api.get("/custom")).rejects.toThrow()
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
+1
-1
@@ -7,7 +7,7 @@ import {
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const env = {}
|
||||
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const env = {}
|
||||
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
|
||||
@@ -3,7 +3,7 @@ import { createAdminUser } from "../../../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const env = {}
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import { adminHeaders, createAdminUser } from "../../helpers/create-admin-user"
|
||||
import { Modules, ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true, MEDUSA_FF_VIEW_CONFIGURATIONS: true }
|
||||
const env = { MEDUSA_FF_VIEW_CONFIGURATIONS: true }
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
env,
|
||||
|
||||
Reference in New Issue
Block a user