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:
committed by
GitHub
parent
28a3f9a3df
commit
b8bc3ed16f
@@ -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()
|
||||
})
|
||||
})
|
||||
@@ -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")
|
||||
})
|
||||
})
|
||||
10
packages/core/utils/src/common/build-regexp-if-valid.ts
Normal file
10
packages/core/utils/src/common/build-regexp-if-valid.ts
Normal 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
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
9
packages/core/utils/src/common/parse-cors-origins.ts
Normal file
9
packages/core/utils/src/common/parse-cors-origins.ts
Normal 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
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user