chore(): start moving some packages to the core directory (#7215)
This commit is contained in:
committed by
GitHub
parent
fdee748eed
commit
bbccd6481d
9
packages/core/utils/src/feature-flags/analytics.ts
Normal file
9
packages/core/utils/src/feature-flags/analytics.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { FeatureFlagTypes } from "@medusajs/types"
|
||||
|
||||
export const AnalyticsFeatureFlag: FeatureFlagTypes.FlagSettings = {
|
||||
key: "analytics",
|
||||
default_val: true,
|
||||
env_key: "MEDUSA_FF_ANALYTICS",
|
||||
description:
|
||||
"Enable Medusa to collect data on usage, errors and performance for the purpose of improving the product",
|
||||
}
|
||||
10
packages/core/utils/src/feature-flags/index.ts
Normal file
10
packages/core/utils/src/feature-flags/index.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export * from "./analytics"
|
||||
export * from "./many-to-many-inventory"
|
||||
export * from "./medusa-v2"
|
||||
export * from "./order-editing"
|
||||
export * from "./product-categories"
|
||||
export * from "./publishable-api-keys"
|
||||
export * from "./sales-channels"
|
||||
export * from "./tax-inclusive-pricing"
|
||||
export * from "./utils"
|
||||
export * from "./workflows"
|
||||
@@ -0,0 +1,9 @@
|
||||
import { FeatureFlagTypes } from "@medusajs/types"
|
||||
|
||||
export const ManyToManyInventoryFeatureFlag: FeatureFlagTypes.FlagSettings = {
|
||||
key: "many_to_many_inventory",
|
||||
default_val: false,
|
||||
env_key: "MEDUSA_FF_MANY_TO_MANY_INVENTORY",
|
||||
description:
|
||||
"Enable capability to have many to many relationship between inventory items and variants",
|
||||
}
|
||||
8
packages/core/utils/src/feature-flags/medusa-v2.ts
Normal file
8
packages/core/utils/src/feature-flags/medusa-v2.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { FeatureFlagTypes } from "@medusajs/types"
|
||||
|
||||
export const MedusaV2Flag: FeatureFlagTypes.FlagSettings = {
|
||||
key: "medusa_v2",
|
||||
default_val: false,
|
||||
env_key: "MEDUSA_FF_MEDUSA_V2",
|
||||
description: "[WIP] Enable Medusa V2",
|
||||
}
|
||||
8
packages/core/utils/src/feature-flags/order-editing.ts
Normal file
8
packages/core/utils/src/feature-flags/order-editing.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { FeatureFlagTypes } from "@medusajs/types"
|
||||
|
||||
export const OrderEditingFeatureFlag: FeatureFlagTypes.FlagSettings = {
|
||||
key: "order_editing",
|
||||
default_val: true,
|
||||
env_key: "MEDUSA_FF_ORDER_EDITING",
|
||||
description: "[WIP] Enable the order editing feature",
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { FeatureFlagTypes } from "@medusajs/types"
|
||||
|
||||
export const ProductCategoryFeatureFlag: FeatureFlagTypes.FlagSettings = {
|
||||
key: "product_categories",
|
||||
default_val: false,
|
||||
env_key: "MEDUSA_FF_PRODUCT_CATEGORIES",
|
||||
description: "[WIP] Enable the product categories feature",
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { FeatureFlagTypes } from "@medusajs/types"
|
||||
|
||||
export const PublishableAPIKeysFeatureFlag: FeatureFlagTypes.FlagSettings = {
|
||||
key: "publishable_api_keys",
|
||||
default_val: true,
|
||||
env_key: "MEDUSA_FF_PUBLISHABLE_API_KEYS",
|
||||
description: "[WIP] Enable the publishable API keys feature",
|
||||
}
|
||||
8
packages/core/utils/src/feature-flags/sales-channels.ts
Normal file
8
packages/core/utils/src/feature-flags/sales-channels.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { FeatureFlagTypes } from "@medusajs/types"
|
||||
|
||||
export const SalesChannelFeatureFlag: FeatureFlagTypes.FlagSettings = {
|
||||
key: "sales_channels",
|
||||
default_val: true,
|
||||
env_key: "MEDUSA_FF_SALES_CHANNELS",
|
||||
description: "[WIP] Enable the sales channels feature",
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { FeatureFlagTypes } from "@medusajs/types"
|
||||
|
||||
export const TaxInclusivePricingFeatureFlag: FeatureFlagTypes.FlagSettings = {
|
||||
key: "tax_inclusive_pricing",
|
||||
default_val: false,
|
||||
env_key: "MEDUSA_FF_TAX_INCLUSIVE_PRICING",
|
||||
description: "[WIP] Enable tax inclusive pricing",
|
||||
}
|
||||
77
packages/core/utils/src/feature-flags/utils/flag-router.ts
Normal file
77
packages/core/utils/src/feature-flags/utils/flag-router.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { FeatureFlagTypes } from "@medusajs/types"
|
||||
import { isObject, isString } from "../../common"
|
||||
|
||||
export class FlagRouter implements FeatureFlagTypes.IFlagRouter {
|
||||
private readonly flags: Record<string, boolean | Record<string, boolean>> = {}
|
||||
|
||||
constructor(flags: Record<string, boolean | Record<string, boolean>>) {
|
||||
this.flags = flags
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a feature flag is enabled.
|
||||
* There are two ways of using this method:
|
||||
* 1. `isFeatureEnabled("myFeatureFlag")`
|
||||
* 2. `isFeatureEnabled({ myNestedFeatureFlag: "someNestedFlag" })`
|
||||
* We use 1. for top-level feature flags and 2. for nested feature flags. Almost all flags are top-level.
|
||||
* An example of a nested flag is workflows. To use it, you would do:
|
||||
* `isFeatureEnabled({ workflows: Workflows.CreateCart })`
|
||||
* @param flag - The flag to check
|
||||
* @return {boolean} - Whether the flag is enabled or not
|
||||
*/
|
||||
public isFeatureEnabled(
|
||||
flag: string | string[] | Record<string, string>
|
||||
): boolean {
|
||||
if (isObject(flag)) {
|
||||
const [nestedFlag, value] = Object.entries(flag)[0]
|
||||
if (typeof this.flags[nestedFlag] === "boolean") {
|
||||
return this.flags[nestedFlag] as boolean
|
||||
}
|
||||
return !!this.flags[nestedFlag]?.[value]
|
||||
}
|
||||
|
||||
const flags = (Array.isArray(flag) ? flag : [flag]) as string[]
|
||||
return flags.every((flag_) => {
|
||||
if (!isString(flag_)) {
|
||||
throw Error("Flag must be a string an array of string or an object")
|
||||
}
|
||||
return !!this.flags[flag_]
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a feature flag.
|
||||
* Flags take two shapes:
|
||||
* `setFlag("myFeatureFlag", true)`
|
||||
* `setFlag("myFeatureFlag", { nestedFlag: true })`
|
||||
* These shapes are used for top-level and nested flags respectively, as explained in isFeatureEnabled.
|
||||
* @param key - The key of the flag to set.
|
||||
* @param value - The value of the flag to set.
|
||||
* @return {void} - void
|
||||
*/
|
||||
public setFlag(
|
||||
key: string,
|
||||
value: boolean | { [key: string]: boolean }
|
||||
): void {
|
||||
if (isObject(value)) {
|
||||
const existing = this.flags[key]
|
||||
|
||||
if (!existing) {
|
||||
this.flags[key] = value
|
||||
return
|
||||
}
|
||||
|
||||
this.flags[key] = { ...(this.flags[key] as object), ...value }
|
||||
return
|
||||
}
|
||||
|
||||
this.flags[key] = value
|
||||
}
|
||||
|
||||
public listFlags(): FeatureFlagTypes.FeatureFlagsResponse {
|
||||
return Object.entries(this.flags || {}).map(([key, value]) => ({
|
||||
key,
|
||||
value,
|
||||
}))
|
||||
}
|
||||
}
|
||||
1
packages/core/utils/src/feature-flags/utils/index.ts
Normal file
1
packages/core/utils/src/feature-flags/utils/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./flag-router"
|
||||
8
packages/core/utils/src/feature-flags/workflows.ts
Normal file
8
packages/core/utils/src/feature-flags/workflows.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { FeatureFlagTypes } from "@medusajs/types"
|
||||
|
||||
export const WorkflowsFeatureFlag: FeatureFlagTypes.FlagSettings = {
|
||||
key: "workflows",
|
||||
default_val: false,
|
||||
env_key: "MEDUSA_FF_WORKFLOWS",
|
||||
description: "[WIP] Enable workflows",
|
||||
}
|
||||
Reference in New Issue
Block a user