chore: More cleanup of medusa package (#7332)

This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-05-15 09:42:06 +02:00
1 parent 70fd355e46
commit 5c14d283d0
28 files changed
+12 -463

No files matched your search

-11
View File
@@ -1,11 +0,0 @@
import { isDate } from "lodash"
export const isPast = (date: Date | null) => {
const now = new Date()
return isDate(date) && now > date
}
export const isFuture = (date: Date | null) => {
const now = new Date()
return isDate(date) && date > now
}
-4
View File
@@ -1,10 +1,6 @@
export * from "./clean-response-data"
export * from "./exception-formatter"
export * from "./is-date"
export * from "./is-object"
export * from "./is-string"
export * from "./omit-deep"
export * from "./remote-query-fetch-data"
export * from "./remove-undefined-properties"
export * from "./set-metadata"
export * from "./middlewares"
-4
View File
@@ -1,4 +0,0 @@
export function isDate(value: any): value is Date {
const date = new Date(value)
return !isNaN(date.valueOf())
}
-3
View File
@@ -1,3 +0,0 @@
export function isObject(obj: any): obj is object {
return obj != null && obj?.constructor?.name === "Object"
}
-3
View File
@@ -1,3 +0,0 @@
export function isString(val: any): val is string {
return val != null && typeof val === "string"
}
@@ -1,29 +0,0 @@
import { ConfigModule } from "@medusajs/types"
/**
* Merge the modules config from the medusa-config file with the modules config from medusa package
* @param modules
* @param medusaInternalModulesConfig
*/
export function mergeModulesConfig(
modules: ConfigModule["modules"] = {},
medusaInternalModulesConfig = {}
) {
const modules_ = ({ ...modules } as ConfigModule["modules"])!
const userModulesConfigKeys = Object.keys(modules)
const internalModulesConfigKeys = Object.keys(medusaInternalModulesConfig)
const allModulesKeys = new Set([
...userModulesConfigKeys,
...internalModulesConfigKeys,
])
for (const moduleName of allModulesKeys) {
const internalModuleConfig = medusaInternalModulesConfig[moduleName]
modules_[moduleName] ??= internalModuleConfig
}
return modules_
}
@@ -1,39 +0,0 @@
import { NextFunction, Request, RequestHandler, Response } from "express"
type handler = (req: Request, res: Response) => Promise<void>
/**
* @deprecated use `import { wrapHandler } from "@medusajs/utils"`
*/
export default (fn: handler): RequestHandler => {
return async (req: Request, res: Response, next: NextFunction) => {
if (req?.errors?.length) {
return res.status(400).json({
errors: req.errors,
message:
"Provided request body contains errors. Please check the data and retry the request",
})
}
try {
return await fn(req, res)
} catch (err) {
next(err)
}
}
}
/**
* @schema MultipleErrors
* title: "Multiple Errors"
* type: object
* properties:
* errors:
* type: array
* description: Array of errors
* items:
* $ref: "#/components/schemas/Error"
* message:
* type: string
* default: "Provided request body contains errors. Please check the data and retry the request"
*/
@@ -1,15 +0,0 @@
import { NextFunction, Request, Response } from "express"
export function checkRegisteredModules(services: {
[serviceName: string]: string
}): (req: Request, res: Response, next: NextFunction) => Promise<void> {
return async (req: Request, res: Response, next: NextFunction) => {
for (const service of Object.keys(services)) {
if (!req.scope.resolve(service, { allowUnregistered: true })) {
return next(new Error(services[service]))
}
}
next()
}
}
@@ -1,18 +0,0 @@
import { FlagRouter } from "@medusajs/utils"
import { NextFunction, Request, Response } from "express"
export function isFeatureFlagEnabled(
flagKey: string
): (req: Request, res: Response, next: NextFunction) => Promise<void> {
return async (req: Request, res: Response, next: NextFunction) => {
const featureFlagRouter = req.scope.resolve(
"featureFlagRouter"
) as FlagRouter
if (!featureFlagRouter.isFeatureEnabled(flagKey)) {
res.sendStatus(404)
} else {
next()
}
}
}
@@ -1,7 +1,5 @@
export { default as authenticateLegacy } from "./authenticate"
export { authenticate } from "./authenticate-middleware"
export { default as authenticateCustomer } from "./authenticate-customer"
export { default as wrapHandler } from "./await-middleware"
export { default as errorHandler } from "./error-handler"
export { isFeatureFlagEnabled } from "./feature-flag-enabled"
export { default as requireCustomerAuthentication } from "./require-customer-authentication"
+1 -1
View File
@@ -1,4 +1,4 @@
import { isObject } from "./is-object"
import { isObject } from "@medusajs/utils"
export function omitDeep<T extends object = object>(
input: object,
-45
View File
@@ -1,45 +0,0 @@
import { MedusaError } from "medusa-core-utils/dist"
/**
* Dedicated method to set metadata.
* @param metadata - the metadata to set
* @param obj - the entity to apply metadata to.
* @return resolves to the updated result.
*/
export function setMetadata(
obj: { metadata: Record<string, unknown> | null } | null | undefined,
metadata: Record<string, unknown>
): Record<string, unknown> {
const existing = obj?.metadata || {}
const newData = {}
for (const [key, value] of Object.entries(metadata)) {
if (typeof key !== "string") {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Key type is invalid. Metadata keys must be strings"
)
}
/**
* We reserve the empty string as a way to delete a key.
* If the value is an empty string, we don't
* set it, and if it exists in the existing metadata, we
* unset the field.
*/
if (value === "") {
if (key in existing) {
delete existing[key]
}
continue
}
newData[key] = value
}
return {
...existing,
...newData,
}
}
-3
View File
@@ -1,3 +0,0 @@
import { promisify } from "util"
export const sleep = promisify(setTimeout)