Chore(medusa,utils,types,inventory,stock-location): remove core dependency modules (#3531)

This commit is contained in:
Carlos R. L. Rodrigues
2023-03-23 08:07:32 -03:00
committed by GitHub
parent bfef22b33e
commit 4e9d257d3b
159 changed files with 1769 additions and 693 deletions

View File

@@ -0,0 +1,345 @@
import { ExtendedFindConfig, FindConfig } from "@medusajs/types"
import {
FindManyOptions,
FindOperator,
FindOptionsRelations,
FindOptionsSelect,
FindOptionsWhere,
In,
IsNull,
LessThan,
LessThanOrEqual,
MoreThan,
MoreThanOrEqual,
} from "typeorm"
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"
import { isObject } from "./is-object"
/**
* Used to build TypeORM queries.
* @param selector The selector
* @param config The config
* @return The QueryBuilderConfig
*/
export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
selector: TWhereKeys,
config: FindConfig<TEntity> = {}
) {
const query: ExtendedFindConfig<TEntity> = {
where: buildWhere<TWhereKeys, TEntity>(selector),
}
if ("deleted_at" in selector) {
query.withDeleted = true
}
if ("skip" in config) {
;(query as FindManyOptions<TEntity>).skip = config.skip
}
if ("take" in config) {
;(query as FindManyOptions<TEntity>).take = config.take
}
if (config.relations) {
query.relations = buildRelations<TEntity>(config.relations)
}
if (config.select) {
query.select = buildSelects<TEntity>(config.select as string[])
}
if (config.order) {
query.order = buildOrder<TEntity>(config.order)
}
return query
}
/**
* @param constraints
*
* @example
* const q = buildWhere(
* {
* id: "1234",
* test1: ["123", "12", "1"],
* test2: Not("this"),
* date: { gt: date },
* amount: { gt: 10 },
* },
*)
*
* // Output
* {
* id: "1234",
* test1: In(["123", "12", "1"]),
* test2: Not("this"),
* date: MoreThan(date),
* amount: MoreThan(10)
* }
*/
function buildWhere<TWhereKeys extends object, TEntity>(
constraints: TWhereKeys
): FindOptionsWhere<TEntity> {
const where: FindOptionsWhere<TEntity> = {}
for (const [key, value] of Object.entries(constraints)) {
if (value === undefined) {
continue
}
if (value === null) {
where[key] = IsNull()
continue
}
if (value instanceof FindOperator) {
where[key] = value
continue
}
if (Array.isArray(value)) {
where[key] = In(value)
continue
}
if (typeof value === "object") {
Object.entries(value).forEach(([objectKey, objectValue]) => {
switch (objectKey) {
case "lt":
where[key] = LessThan(objectValue)
break
case "gt":
where[key] = MoreThan(objectValue)
break
case "lte":
where[key] = LessThanOrEqual(objectValue)
break
case "gte":
where[key] = MoreThanOrEqual(objectValue)
break
default:
if (objectValue != undefined && typeof objectValue === "object") {
where[key] = buildWhere<any, TEntity>(objectValue)
return
}
where[key] = value
}
return
})
continue
}
where[key] = value
}
return where
}
/**
* Revert new object structure of find options to the legacy structure of previous version
* @example
* input: {
* test: {
* test1: true,
* test2: true,
* test3: {
* test4: true
* },
* },
* test2: true
* }
* output: ['test.test1', 'test.test2', 'test.test3.test4', 'test2']
* @param input
*/
export function buildLegacyFieldsListFrom<TEntity>(
input:
| FindOptionsWhere<TEntity>
| FindOptionsSelect<TEntity>
| FindOptionsOrder<TEntity>
| FindOptionsRelations<TEntity> = {}
): (keyof TEntity)[] {
if (!Object.keys(input).length) {
return []
}
const output: Set<string> = new Set(Object.keys(input))
for (const key of Object.keys(input)) {
if (input[key] != undefined && typeof input[key] === "object") {
const deepRes = buildLegacyFieldsListFrom(input[key])
const items = deepRes.reduce((acc, val) => {
acc.push(`${key}.${val}`)
return acc
}, [] as string[])
items.forEach((item) => output.add(item))
continue
}
output.add(key)
}
return Array.from(output) as (keyof TEntity)[]
}
export function buildSelects<TEntity>(
selectCollection: string[]
): FindOptionsSelect<TEntity> {
return buildRelationsOrSelect(selectCollection) as FindOptionsSelect<TEntity>
}
export function buildRelations<TEntity>(
relationCollection: string[]
): FindOptionsRelations<TEntity> {
return buildRelationsOrSelect(
relationCollection
) as FindOptionsRelations<TEntity>
}
export function addOrderToSelect<TEntity>(
order: FindOptionsOrder<TEntity>,
select: FindOptionsSelect<TEntity>
): void {
for (const orderBy of Object.keys(order)) {
if (isObject(order[orderBy])) {
select[orderBy] =
select[orderBy] && isObject(select[orderBy]) ? select[orderBy] : {}
addOrderToSelect(order[orderBy], select[orderBy])
continue
}
select[orderBy] = isObject(select[orderBy])
? { ...select[orderBy], id: true, [orderBy]: true }
: true
}
}
/**
* Convert an collection of dot string into a nested object
* @example
* input: [
* order,
* order.items,
* order.swaps,
* order.swaps.additional_items,
* order.discounts,
* order.discounts.rule,
* order.claims,
* order.claims.additional_items,
* additional_items,
* additional_items.variant,
* return_order,
* return_order.items,
* return_order.shipping_method,
* return_order.shipping_method.tax_lines
* ]
* output: {
* "order": {
* "items": true,
* "swaps": {
* "additional_items": true
* },
* "discounts": {
* "rule": true
* },
* "claims": {
* "additional_items": true
* }
* },
* "additional_items": {
* "variant": true
* },
* "return_order": {
* "items": true,
* "shipping_method": {
* "tax_lines": true
* }
* }
* }
* @param collection
*/
function buildRelationsOrSelect<TEntity>(
collection: string[]
): FindOptionsRelations<TEntity> | FindOptionsSelect<TEntity> {
const output: FindOptionsRelations<TEntity> | FindOptionsSelect<TEntity> = {}
for (const relation of collection) {
if (relation.indexOf(".") > -1) {
const nestedRelations = relation.split(".")
let parent = output
while (nestedRelations.length > 1) {
const nestedRelation = nestedRelations.shift() as string
parent = parent[nestedRelation] =
parent[nestedRelation] !== true &&
typeof parent[nestedRelation] === "object"
? parent[nestedRelation]
: {}
}
parent[nestedRelations[0]] = true
continue
}
output[relation] = output[relation] ?? true
}
return output
}
/**
* Convert an order of dot string into a nested object
* @example
* input: { id: "ASC", "items.title": "ASC", "items.variant.title": "ASC" }
* output: {
* "id": "ASC",
* "items": {
* "id": "ASC",
* "variant": {
* "title": "ASC"
* }
* },
* }
* @param orderBy
*/
function buildOrder<TEntity>(orderBy: {
[k: string]: "ASC" | "DESC"
}): FindOptionsOrder<TEntity> {
const output: FindOptionsOrder<TEntity> = {}
const orderKeys = Object.keys(orderBy)
for (const order of orderKeys) {
if (order.indexOf(".") > -1) {
const nestedOrder = order.split(".")
let parent = output
while (nestedOrder.length > 1) {
const nestedRelation = nestedOrder.shift() as string
parent = parent[nestedRelation] = parent[nestedRelation] ?? {}
}
parent[nestedOrder[0]] = orderBy[order]
continue
}
output[order] = orderBy[order]
}
return output
}
export function nullableValue(value: any): FindOperator<any> {
if (value === null) {
return IsNull()
} else {
return value
}
}

View File

@@ -0,0 +1,66 @@
import path from "path"
import { Column, ColumnOptions, ColumnType } from "typeorm"
import getConfigFile from "./get-config-file"
const pgSqliteTypeMapping: { [key: string]: ColumnType } = {
increment: "rowid",
timestamptz: "datetime",
jsonb: "simple-json",
enum: "text",
}
const pgSqliteGenerationMapping: {
[key: string]: "increment" | "uuid" | "rowid"
} = {
increment: "rowid",
}
let dbType: string
export function resolveDbType(pgSqlType: ColumnType): ColumnType {
if (!dbType) {
const { configModule } = getConfigFile(
path.resolve("."),
`medusa-config`
) as any
dbType = configModule?.projectConfig?.database_type || "postgres"
}
if (dbType === "sqlite" && (pgSqlType as string) in pgSqliteTypeMapping) {
return pgSqliteTypeMapping[pgSqlType.toString()]
}
return pgSqlType
}
export function resolveDbGenerationStrategy(
pgSqlType: "increment" | "uuid" | "rowid"
): "increment" | "uuid" | "rowid" {
if (!dbType) {
const { configModule } = getConfigFile(
path.resolve("."),
`medusa-config`
) as any
dbType = configModule?.projectConfig?.database_type || "postgres"
}
if (dbType === "sqlite" && pgSqlType in pgSqliteTypeMapping) {
return pgSqliteGenerationMapping[pgSqlType]
}
return pgSqlType
}
export function DbAwareColumn(columnOptions: ColumnOptions): PropertyDecorator {
const pre = columnOptions.type
if (columnOptions.type) {
columnOptions.type = resolveDbType(columnOptions.type)
}
if (pre === "jsonb" && pre !== columnOptions.type) {
if ("default" in columnOptions) {
columnOptions.default = JSON.stringify(columnOptions.default)
}
}
return Column(columnOptions)
}

View File

@@ -0,0 +1,55 @@
/**
* @typedef MedusaErrorType
*
*/
export const MedusaErrorTypes = {
/** Errors stemming from the database */
DB_ERROR: "database_error",
DUPLICATE_ERROR: "duplicate_error",
INVALID_ARGUMENT: "invalid_argument",
INVALID_DATA: "invalid_data",
UNAUTHORIZED: "unauthorized",
NOT_FOUND: "not_found",
NOT_ALLOWED: "not_allowed",
UNEXPECTED_STATE: "unexpected_state",
CONFLICT: "conflict",
PAYMENT_AUTHORIZATION_ERROR: "payment_authorization_error",
}
export const MedusaErrorCodes = {
INSUFFICIENT_INVENTORY: "insufficient_inventory",
CART_INCOMPATIBLE_STATE: "cart_incompatible_state",
}
/**
* Standardized error to be used across Medusa project.
* @extends Error
*/
export class MedusaError extends Error {
public type: string
public message: string
public code?: string
public date: Date
public static Types = MedusaErrorTypes
public static Codes = MedusaErrorCodes
/**
* Creates a standardized error to be used across Medusa project.
* @param {string} type - type of error
* @param {string} message - message to go along with error
* @param {string} code - code of error
* @param {Array} params - params
*/
constructor(type: string, message: string, code?: string, ...params: any) {
super(...params)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, MedusaError)
}
this.type = type
this.code = code
this.message = message
this.date = new Date()
}
}

View File

@@ -0,0 +1,16 @@
import { ulid } from "ulid"
/**
* Generate a composed id based on the input parameters and return either the is if it exists or the generated one.
* @param idProperty
* @param prefix
*/
export function generateEntityId(idProperty: string, prefix?: string): string {
if (idProperty) {
return idProperty
}
const id = ulid()
prefix = prefix ? `${prefix}_` : ""
return `${prefix}${id}`
}

View File

@@ -0,0 +1,32 @@
import { join } from "path"
/**
* Attempts to resolve the config file in a given root directory.
* @param {string} rootDir - the directory to find the config file in.
* @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>(
rootDir: string,
configName: string
): { configModule: TConfig; configFilePath: string; error?: any } {
const configPath = join(rootDir, configName)
let configFilePath = ``
let configModule
let err
try {
configFilePath = require.resolve(configPath)
configModule = require(configFilePath)
} catch (e) {
err = e
}
if (configModule && typeof configModule.default === "object") {
configModule = configModule.default
}
return { configModule, configFilePath, error: err }
}
export default getConfigFile

View File

@@ -0,0 +1,15 @@
export * from "./build-query"
export * from "./db-aware-column"
export * from "./errors"
export * from "./generate-entity-id"
export * from "./get-config-file"
export * from "./is-date"
export * from "./is-defined"
export * from "./is-email"
export * from "./is-object"
export * from "./is-string"
export * from "./medusa-container"
export * from "./models"
export * from "./set-metadata"
export * from "./transaction-base-service"
export * from "./wrap-handler"

View File

@@ -0,0 +1,4 @@
export function isDate(value: any): value is Date {
const date = new Date(value)
return !isNaN(date.valueOf())
}

View File

@@ -0,0 +1,5 @@
export function isDefined<T = undefined | unknown>(
val: T
): val is T extends undefined ? never : T {
return typeof val !== "undefined"
}

View File

@@ -0,0 +1,20 @@
import { isEmail } from "class-validator"
import { MedusaError } from "./errors"
/**
* Used to validate user email.
* @param {string} email - email to validate
* @return {string} the validated email
*/
export function validateEmail(email: string): string {
const validatedEmail = isEmail(email)
if (!validatedEmail) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"The email is not valid"
)
}
return email.toLowerCase()
}

View File

@@ -0,0 +1,3 @@
export function isObject(obj: unknown): obj is object {
return typeof obj === "object" && !!obj
}

View File

@@ -0,0 +1,3 @@
export function isString(val: any): val is string {
return val != null && typeof val === "string"
}

View File

@@ -0,0 +1,57 @@
import { MedusaContainer } from "@medusajs/types"
import {
asFunction,
asValue,
AwilixContainer,
ClassOrFunctionReturning,
createContainer,
Resolver,
} from "awilix"
function asArray(
resolvers: (ClassOrFunctionReturning<unknown> | Resolver<unknown>)[]
): { resolve: (container: AwilixContainer) => unknown[] } {
return {
resolve: (container: AwilixContainer) =>
resolvers.map((resolver) => container.build(resolver)),
}
}
function registerAdd(
this: MedusaContainer,
name: string,
registration: typeof asFunction | typeof asValue
) {
const storeKey = name + "_STORE"
if (this.registrations[storeKey] === undefined) {
this.register(storeKey, asValue([] as Resolver<unknown>[]))
}
const store = this.resolve(storeKey) as (
| ClassOrFunctionReturning<unknown>
| Resolver<unknown>
)[]
if (this.registrations[name] === undefined) {
this.register(name, asArray(store))
}
store.unshift(registration)
return this
}
export function createMedusaContainer(...args): MedusaContainer {
const container = createContainer.apply(null, args) as MedusaContainer
container.registerAdd = registerAdd.bind(container)
const originalScope = container.createScope
container.createScope = () => {
const scoped = originalScope() as MedusaContainer
scoped.registerAdd = registerAdd.bind(scoped)
return scoped
}
return container
}

View File

@@ -0,0 +1,16 @@
import { CreateDateColumn, PrimaryColumn, UpdateDateColumn } from "typeorm"
import { resolveDbType } from "../db-aware-column"
/**
* Base abstract entity for all entities
*/
export abstract class BaseEntity {
@PrimaryColumn()
id: string
@CreateDateColumn({ type: resolveDbType("timestamptz") })
created_at: Date
@UpdateDateColumn({ type: resolveDbType("timestamptz") })
updated_at: Date
}

View File

@@ -0,0 +1,2 @@
export * from "./base-entity"
export * from "./soft-deletable-entity"

View File

@@ -0,0 +1,8 @@
import { DeleteDateColumn } from "typeorm"
import { resolveDbType } from "../db-aware-column"
import { BaseEntity } from "./base-entity"
export abstract class SoftDeletableEntity extends BaseEntity {
@DeleteDateColumn({ type: resolveDbType("timestamptz") })
deleted_at: Date | null
}

View File

@@ -0,0 +1,45 @@
import { MedusaError } from "./errors"
/**
* Dedicated method to set metadata.
* @param obj - the entity to apply metadata to.
* @param metadata - the metadata to set
* @return resolves to the updated result.
*/
export function setMetadata(
obj: { metadata: Record<string, unknown> | null },
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,
}
}

View File

@@ -0,0 +1,152 @@
import { EntityManager } from "typeorm"
import { IsolationLevel } from "typeorm/driver/types/IsolationLevel"
export abstract class TransactionBaseService {
protected manager_: EntityManager
protected transactionManager_: EntityManager | undefined
protected get activeManager_(): EntityManager {
return this.transactionManager_ ?? this.manager_
}
protected constructor(
protected readonly __container__: any,
protected readonly __configModule__?: Record<string, unknown>,
protected readonly __moduleDeclaration__?: Record<string, unknown>
) {
this.manager_ = __container__.manager
}
withTransaction(transactionManager?: EntityManager): this {
if (!transactionManager) {
return this
}
const cloned = new (this.constructor as any)(
this.__container__,
this.__configModule__,
this.__moduleDeclaration__
)
cloned.manager_ = transactionManager
cloned.transactionManager_ = transactionManager
return cloned
}
protected shouldRetryTransaction_(
err: { code: string } | Record<string, unknown>
): boolean {
if (!(err as { code: string })?.code) {
return false
}
const code = (err as { code: string })?.code
return code === "40001" || code === "40P01"
}
/**
* Wraps some work within a transactional block. If the service already has
* a transaction manager attached this will be reused, otherwise a new
* transaction manager is created.
* @param work - the transactional work to be done
* @param isolationOrErrorHandler - the isolation level to be used for the work.
* @param maybeErrorHandlerOrDontFail Potential error handler
* @return the result of the transactional work
*/
protected async atomicPhase_<TResult, TError>(
work: (transactionManager: EntityManager) => Promise<TResult | never>,
isolationOrErrorHandler?:
| IsolationLevel
| ((error: TError) => Promise<never | TResult | void>),
maybeErrorHandlerOrDontFail?: (
error: TError
) => Promise<never | TResult | void>
): Promise<never | TResult> {
let errorHandler = maybeErrorHandlerOrDontFail
let isolation:
| IsolationLevel
| ((error: TError) => Promise<never | TResult | void>)
| undefined
| null = isolationOrErrorHandler
let dontFail = false
if (typeof isolationOrErrorHandler === "function") {
isolation = null
errorHandler = isolationOrErrorHandler
dontFail = !!maybeErrorHandlerOrDontFail
}
if (this.transactionManager_) {
const doWork = async (m: EntityManager): Promise<never | TResult> => {
this.manager_ = m
this.transactionManager_ = m
try {
return await work(m)
} catch (error) {
if (errorHandler) {
const queryRunner = this.transactionManager_.queryRunner
if (queryRunner && queryRunner.isTransactionActive) {
await queryRunner.rollbackTransaction()
}
await errorHandler(error)
}
throw error
}
}
return await doWork(this.transactionManager_)
} else {
const temp = this.manager_
const doWork = async (m: EntityManager): Promise<never | TResult> => {
this.manager_ = m
this.transactionManager_ = m
try {
const result = await work(m)
this.manager_ = temp
this.transactionManager_ = undefined
return result
} catch (error) {
this.manager_ = temp
this.transactionManager_ = undefined
throw error
}
}
if (isolation && this.manager_) {
let result
try {
result = await this.manager_.transaction(
isolation as IsolationLevel,
async (m) => doWork(m)
)
return result
} catch (error) {
if (this.shouldRetryTransaction_(error)) {
return this.manager_.transaction(
isolation as IsolationLevel,
async (m): Promise<never | TResult> => doWork(m)
)
} else {
if (errorHandler) {
await errorHandler(error)
}
throw error
}
}
}
try {
return await this.manager_.transaction(async (m) => doWork(m))
} catch (error) {
if (errorHandler) {
const result = await errorHandler(error)
if (dontFail) {
return result as TResult
}
}
throw error
}
}
}
}

View File

@@ -0,0 +1,36 @@
import { NextFunction, Request, RequestHandler, Response } from "express"
type handler = (req: Request, res: Response) => Promise<void>
export const wrapHandler = (fn: handler): RequestHandler => {
return (
req: Request & { errors?: Error[] },
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",
})
}
return fn(req, res).catch(next)
}
}
/**
* @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"
*/

View File

@@ -1,5 +1,5 @@
export * from "./bundles"
export * from "./common"
export * from "./decorators"
export * from "./event-bus"
export * from "./search"