chore: move v2 api behind ff (#6213)

This commit is contained in:
Sebastian Rindom
2024-01-25 14:39:22 +00:00
committed by GitHub
parent 4c4c0f655b
commit 4ad761788b
31 changed files with 78 additions and 41 deletions
+30 -2
View File
@@ -1,7 +1,10 @@
import path from "path"
import { FeatureFlagUtils, FlagRouter } from "@medusajs/utils"
import { AwilixContainer } from "awilix"
import bodyParser from "body-parser"
import { Express } from "express"
import qs from "qs"
import { RoutesLoader } from "./helpers/routing"
import routes from "../api"
import { ConfigModule } from "../types/global"
@@ -9,9 +12,15 @@ type Options = {
app: Express
container: AwilixContainer
configModule: ConfigModule
featureFlagRouter?: FlagRouter
}
export default async ({ app, container, configModule }: Options) => {
export default async ({
app,
container,
configModule,
featureFlagRouter,
}: Options) => {
// This is a workaround for the issue described here: https://github.com/expressjs/express/issues/3454
// We parse the url and get the qs to be parsed and override the query prop from the request
app.use(function (req, res, next) {
@@ -25,7 +34,26 @@ export default async ({ app, container, configModule }: Options) => {
})
app.use(bodyParser.json())
app.use("/", routes(container, configModule.projectConfig))
if (featureFlagRouter?.isFeatureEnabled(FeatureFlagUtils.MedusaV2Flag.key)) {
// TODO: Figure out why this is causing issues with test when placed inside ./api.ts
// Adding this here temporarily
// Test: (packages/medusa/src/api/routes/admin/currencies/update-currency.ts)
try {
/**
* Register the Medusa CORE API routes using the file based routing.
*/
await new RoutesLoader({
app: app,
rootDir: path.join(__dirname, "../api-v2"),
configModule,
}).load()
} catch (err) {
throw Error("An error occurred while registering Medusa Core API Routes")
}
} else {
app.use("/", routes(container, configModule.projectConfig))
}
return app
}
@@ -108,7 +108,12 @@ export const createServer = async (rootDir) => {
}).load()
// the apiLoader needs to be called after plugins otherwise the core middleware bleads into the plugins
await apiLoader({ container, app: app, configModule: config })
await apiLoader({
container,
app: app,
configModule: config,
featureFlagRouter,
})
const superRequest = supertest(app)
+6 -18
View File
@@ -22,7 +22,6 @@ import loadConfig from "./config"
import defaultsLoader from "./defaults"
import expressLoader from "./express"
import featureFlagsLoader from "./feature-flags"
import { RoutesLoader } from "./helpers/routing"
import Logger from "./logger"
import loadMedusaApp, { mergeDefaultModules } from "./medusa-app"
import modelsLoader from "./models"
@@ -197,22 +196,6 @@ export default async ({
next()
})
// TODO: Figure out why this is causing issues with test when placed inside ./api.ts
// Adding this here temporarily
// Test: (packages/medusa/src/api/routes/admin/currencies/update-currency.ts)
try {
/**
* Register the Medusa CORE API routes using the file based routing.
*/
await new RoutesLoader({
app: expressApp,
rootDir: path.join(__dirname, "../api-v2"),
configModule,
}).load()
} catch (err) {
throw Error("An error occurred while registering Medusa Core API Routes")
}
const pluginsActivity = Logger.activity(`Initializing plugins${EOL}`)
track("PLUGINS_INIT_STARTED")
await pluginsLoader({
@@ -233,7 +216,12 @@ export default async ({
const apiActivity = Logger.activity(`Initializing API${EOL}`)
track("API_INIT_STARTED")
await apiLoader({ container, app: expressApp, configModule })
await apiLoader({
container,
app: expressApp,
configModule,
featureFlagRouter,
})
const apiAct = Logger.success(apiActivity, "API initialized") || {}
track("API_INIT_COMPLETED", { duration: apiAct.duration })