chore(utils): clean util package deps (#4146)

This commit is contained in:
Frane Polić
2023-05-26 13:20:12 +02:00
committed by GitHub
parent f47b11293e
commit db41995307
36 changed files with 593 additions and 996 deletions

View File

@@ -1,318 +0,0 @@
import {
And,
FindOptionsOrder,
FindOptionsSelect,
In,
LessThanOrEqual,
MoreThan,
MoreThanOrEqual,
Not,
} from "typeorm"
import {
addOrderToSelect,
buildQuery,
objectToStringPath,
} from "../build-query"
describe("buildQuery", () => {
it("successfully creates query", () => {
const date = new Date()
const q = buildQuery(
{
id: "1234",
test1: ["123", "12", "1"],
test2: Not("this"),
date: { gt: date },
amount: { gt: 10 },
rule: {
type: "fixed",
},
updated_at: {
gte: "value",
lte: "value",
},
},
{
select: [
"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",
],
relations: [
"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",
"items.variants",
"items.variants.product",
"items",
"items.tax_lines",
"items.adjustments",
],
order: {
id: "ASC",
"items.id": "ASC",
"items.variant.id": "ASC",
},
}
)
expect(q).toEqual({
where: {
id: "1234",
test1: In(["123", "12", "1"]),
test2: Not("this"),
date: MoreThan(date),
amount: MoreThan(10),
rule: {
type: "fixed",
},
updated_at: And(MoreThanOrEqual("value"), LessThanOrEqual("value")),
},
select: {
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,
},
},
},
relations: {
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,
},
},
items: {
variants: {
product: true,
},
tax_lines: true,
adjustments: true,
},
},
order: {
id: "ASC",
items: {
id: "ASC",
variant: {
id: "ASC",
},
},
},
})
})
})
describe("objectToStringPath", () => {
it("successfully build back select object shape to list", () => {
const q = objectToStringPath({
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,
},
},
})
expect(q.length).toBe(14)
expect(q).toEqual(
expect.arrayContaining([
"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",
])
)
})
it("successfully build back relation object shape to list", () => {
const q = objectToStringPath({
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,
},
},
items: {
variants: {
product: true,
},
tax_lines: true,
adjustments: true,
},
})
expect(q.length).toBe(19)
expect(q).toEqual(
expect.arrayContaining([
"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",
"items.variants",
"items.variants.product",
"items",
"items.tax_lines",
"items.adjustments",
])
)
})
it("successfully build back order object shape to list", () => {
const q = objectToStringPath({
id: "ASC",
items: {
id: "ASC",
variant: {
id: "ASC",
},
},
})
expect(q.length).toBe(5)
expect(q).toEqual(
expect.arrayContaining([
"id",
"items",
"items.id",
"items.variant",
"items.variant.id",
])
)
})
describe("addOrderToSelect", function () {
it("successfully add the order fields to the select object", () => {
const select: FindOptionsSelect<any> = {
item: {
variant: {
id: true,
},
},
}
const order: FindOptionsOrder<any> = {
item: {
variant: {
rank: "ASC",
},
},
}
addOrderToSelect(order, select)
expect(select).toEqual({
item: {
variant: {
id: true,
rank: true,
},
},
})
})
})
})

View File

@@ -0,0 +1,21 @@
import { validateEmail } from "../is-email"
describe("validateEmail", () => {
it("successfully validates an email", () => {
expect(validateEmail("test@email.com")).toBe("test@email.com")
expect(validateEmail("test.test@email.com")).toBe("test.test@email.com")
expect(validateEmail("test.test123@email.com")).toBe(
"test.test123@email.com"
)
})
it("throws on an invalidates email", () => {
expect.assertions(1)
try {
validateEmail("not-an-email")
} catch (e) {
expect(e.message).toBe("The email is not valid")
}
})
})

View File

@@ -1,248 +1,27 @@
import {
And,
FindManyOptions,
FindOperator,
FindOptionsRelations,
FindOptionsSelect,
FindOptionsWhere,
ILike,
In,
IsNull,
LessThan,
LessThanOrEqual,
MoreThan,
MoreThanOrEqual,
} from "typeorm"
import { ExtendedFindConfig, FindConfig } from "@medusajs/types"
// Those utils are used in a typeorm context and we can't be sure that they can be used elsewhere
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"
import { isObject } from "./is-object"
type Order = {
[key: string]: "ASC" | "DESC" | Order
}
/**
* 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),
}
type Selects = {
[key: string]: boolean | Selects
}
if ("deleted_at" in selector) {
query.withDeleted = true
}
type Relations = {
[key: string]: boolean | Relations
}
if ("skip" in config) {
;(query as FindManyOptions<TEntity>).skip = config.skip
}
export function buildSelects(selectCollection: string[]): Selects {
return buildRelationsOrSelect(selectCollection)
}
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
export function buildRelations(relationCollection: string[]): Relations {
return buildRelationsOrSelect(relationCollection)
}
/**
* @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]) => {
where[key] = where[key] || []
switch (objectKey) {
case "lt":
where[key].push(LessThan(objectValue))
break
case "gt":
where[key].push(MoreThan(objectValue))
break
case "lte":
where[key].push(LessThanOrEqual(objectValue))
break
case "gte":
where[key].push(MoreThanOrEqual(objectValue))
break
case "contains":
where[key].push(ILike(`%${objectValue}%`))
break
case "starts_with":
where[key].push(ILike(`${objectValue}%`))
break
case "ends_with":
where[key].push(ILike(`%${objectValue}`))
break
default:
if (objectValue != undefined && typeof objectValue === "object") {
where[key] = buildWhere<any, TEntity>(objectValue)
return
}
where[key] = value
}
return
})
if (!Array.isArray(where[key])) {
continue
}
if (where[key].length === 1) {
where[key] = where[key][0]
} else {
where[key] = And(...where[key])
}
continue
}
where[key] = value
}
return where
}
/**
* Converts a typeorms structure of find options to an
* array of string paths
* @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 objectToStringPath<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 = objectToStringPath(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
* Convert a collection of dot string into a nested object
* @example
* input: [
* order,
@@ -285,10 +64,8 @@ export function addOrderToSelect<TEntity>(
* }
* @param collection
*/
function buildRelationsOrSelect<TEntity>(
collection: string[]
): FindOptionsRelations<TEntity> | FindOptionsSelect<TEntity> {
const output: FindOptionsRelations<TEntity> | FindOptionsSelect<TEntity> = {}
function buildRelationsOrSelect(collection: string[]): Selects | Relations {
const output: Selects | Relations = {}
for (const relation of collection) {
if (relation.indexOf(".") > -1) {
@@ -298,11 +75,12 @@ function buildRelationsOrSelect<TEntity>(
while (nestedRelations.length > 1) {
const nestedRelation = nestedRelations.shift() as string
parent = parent[nestedRelation] =
parent = parent[nestedRelation] = (
parent[nestedRelation] !== true &&
typeof parent[nestedRelation] === "object"
? parent[nestedRelation]
: {}
) as Selects | Relations
}
parent[nestedRelations[0]] = true
@@ -331,10 +109,8 @@ function buildRelationsOrSelect<TEntity>(
* }
* @param orderBy
*/
function buildOrder<TEntity>(orderBy: {
[k: string]: "ASC" | "DESC"
}): FindOptionsOrder<TEntity> {
const output: FindOptionsOrder<TEntity> = {}
export function buildOrder<T>(orderBy: { [k: string]: "ASC" | "DESC" }): Order {
const output: Order = {}
const orderKeys = Object.keys(orderBy)
@@ -346,7 +122,7 @@ function buildOrder<TEntity>(orderBy: {
while (nestedOrder.length > 1) {
const nestedRelation = nestedOrder.shift() as string
parent = parent[nestedRelation] = parent[nestedRelation] ?? {}
parent = (parent[nestedRelation] as Order) ??= {}
}
parent[nestedOrder[0]] = orderBy[order]
@@ -359,11 +135,3 @@ function buildOrder<TEntity>(orderBy: {
return output
}
export function nullableValue(value: any): FindOperator<any> {
if (value === null) {
return IsNull()
} else {
return value
}
}

View File

@@ -1,26 +0,0 @@
import { Column, ColumnOptions, ColumnType } from "typeorm"
export function resolveDbType(pgSqlType: ColumnType): ColumnType {
return pgSqlType
}
export function resolveDbGenerationStrategy(
pgSqlType: "increment" | "uuid" | "rowid"
): "increment" | "uuid" | "rowid" {
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

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

View File

@@ -1,6 +1,16 @@
import { isEmail } from "class-validator"
import { MedusaError } from "./errors"
const EMAIL_REGEX =
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
/**
* Check whether provided string is an email.
* @param email - string to check
*/
function isEmail(email: string) {
return email.toLowerCase().match(EMAIL_REGEX)
}
/**
* Used to validate user email.
* @param {string} email - email to validate

View File

@@ -1,16 +0,0 @@
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

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

View File

@@ -1,8 +0,0 @@
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,44 @@
import { isObject } from "./is-object"
/**
* Converts a structure of find options to an
* array of string paths
* @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 objectToStringPath(input: object = {}): string[] {
if (!isObject(input) || !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 = objectToStringPath(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)
}

View File

@@ -1,152 +0,0 @@
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
}
}
}
}