chore: Start cleaning up medusa-core-utils (#7450)

**What**
- remove medusa-core-utils
- dispatch the utils where they belongs
- update usage

**NOTE**
I have been wondering if the graceful class should go into the utils package or medusa package, I ve put it in the medusa package as it seems to be the best place I can see for now and is tight to the server as well. Also, I wanted to avoid the utils package to depends on http and net dependencies, happy to change that if you feel like it
This commit is contained in:
Adrien de Peretti
2024-05-27 10:00:15 +02:00
committed by GitHub
parent 28a3f9a3df
commit b8bc3ed16f
69 changed files with 61 additions and 1384 deletions

View File

@@ -1,8 +1,7 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IInventoryServiceNext } from "@medusajs/types"
import { promiseAll } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { MedusaError } from "medusa-core-utils"
import { MedusaError, promiseAll } from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
interface StepInput {
items: {

View File

@@ -1,5 +1,5 @@
import { BigNumberInput } from "@medusajs/types"
import { MedusaError } from "medusa-core-utils"
import { MedusaError } from "@medusajs/utils"
interface ConfirmInventoryPreparationInput {
product_variant_inventory_items: {

View File

@@ -1,6 +1,5 @@
import { RegionTypes } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
import { isDefined } from "medusa-core-utils"
import { isDefined, MedusaError } from "@medusajs/utils"
import { WorkflowArguments } from "@medusajs/workflows-sdk"

View File

@@ -1,5 +1,4 @@
import { MedusaError } from "@medusajs/utils"
import { isDefined } from "medusa-core-utils"
import { isDefined, MedusaError } from "@medusajs/utils"
import { WorkflowArguments } from "@medusajs/workflows-sdk"

View File

@@ -63,7 +63,6 @@
"@medusajs/utils": "^1.11.9",
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"medusa-core-utils": "^1.2.2",
"randomatic": "^3.1.1"
},
"gitHead": "81a7ff73d012fda722f6e9ef0bd9ba0232d37808"

View File

@@ -2,7 +2,10 @@ const path = require("path")
const express = require("express")
const getPort = require("get-port")
const { isObject, promiseAll } = require("@medusajs/utils")
const { GracefulShutdownServer } = require("medusa-core-utils")
// TODO: fix that once we find the appropriate place to put this util
const {
GracefulShutdownServer,
} = require("@medusajs/medusa/dist/utils/graceful-shutdown-server")
async function bootstrapApp({ cwd, env = {} } = {}) {
const app = express()
@@ -16,7 +19,6 @@ async function bootstrapApp({ cwd, env = {} } = {}) {
const { container, shutdown } = await loaders({
directory: path.resolve(cwd || process.cwd()),
expressApp: app,
isTest: false,
})
const PORT = await getPort()

View File

@@ -0,0 +1,12 @@
import { buildRegexpIfValid } from "../build-regexp-if-valid"
describe("buildRegexpIfValid", function () {
it("should return either a regexp or undefined", () => {
expect(buildRegexpIfValid("abc")).not.toBeDefined()
expect(buildRegexpIfValid("/abc/")).toBeTruthy()
expect(buildRegexpIfValid("/ab#/[c]/ig")).toBeTruthy()
expect(buildRegexpIfValid("@ab#/[c]@ig")).toBeTruthy()
expect(buildRegexpIfValid("/ab/[c/ig")).not.toBeDefined()
expect(buildRegexpIfValid("/abc/gig")).not.toBeDefined()
})
})

View File

@@ -0,0 +1,16 @@
import { parseCorsOrigins } from "../parse-cors-origins"
describe("parseCorsOrigins", function () {
it("should return an array containing both string and regexp", () => {
const cors = "abc,/abc/,/ab#/[c]/ig,@ab#/[c]@ig,/ab/[c/ig,/abc/gig"
const [origin1, origin2, origin3, origin4, origin5, origin6] =
parseCorsOrigins(cors)
expect(typeof origin1).toBe("string")
expect(origin2).toBeInstanceOf(RegExp)
expect(origin3).toBeInstanceOf(RegExp)
expect(origin4).toBeInstanceOf(RegExp)
expect(typeof origin5).toBe("string")
expect(typeof origin6).toBe("string")
})
})

View File

@@ -0,0 +1,10 @@
export function buildRegexpIfValid(str: string): RegExp | undefined {
try {
const m = str.match(/^([\/~@;%#'])(.*?)\1([gimsuy]*)$/)
if (m) {
return new RegExp(m[2], m[3])
}
} catch (e) {}
return
}

View File

@@ -6,7 +6,7 @@ import { join } from "path"
* @param {string} configName - the name of the config file.
* @return {object} an object containing the config module and its path as well as an error property if the config couldn't be loaded.
*/
function getConfigFile<TConfig = unknown>(
export function getConfigFile<TConfig = unknown>(
rootDir: string,
configName: string
): { configModule: TConfig; configFilePath: string; error?: any } {
@@ -28,5 +28,3 @@ function getConfigFile<TConfig = unknown>(
return { configModule, configFilePath, error: err }
}
export default getConfigFile

View File

@@ -59,3 +59,5 @@ export * from "./upper-case-first"
export * from "./wrap-handler"
export * from "./to-handle"
export * from "./validate-handle"
export * from "./parse-cors-origins"
export * from "./build-regexp-if-valid"

View File

@@ -0,0 +1,9 @@
import { buildRegexpIfValid } from "./build-regexp-if-valid"
export function parseCorsOrigins(str: string): (string | RegExp)[] {
return !str
? []
: str.split(",").map((subStr) => {
return buildRegexpIfValid(subStr) ?? subStr
})
}