fix(product): Serialize typings (#4602)

What:
Fix the serializer
This commit is contained in:
Adrien de Peretti
2023-07-26 08:08:36 +00:00
committed by GitHub
parent 173684371b
commit 585ebf2454
4 changed files with 22 additions and 40 deletions
+6
View File
@@ -0,0 +1,6 @@
---
"@medusajs/product": patch
"@medusajs/types": patch
---
fix(product): Serialize typings
+9 -19
View File
@@ -74,16 +74,13 @@ const updateDeletedAtRecursively = async <T extends object = any>(
} }
} }
const serializer = < const serializer = <TOutput extends object>(
T extends object | object[], data: any,
TResult extends object | object[]
>(
data: T,
options?: any options?: any
): Promise<TResult> => { ): Promise<TOutput> => {
options ??= {} options ??= {}
const result = serialize(data, options) const result = serialize(data, options)
return Array.isArray(data) ? result : result[0] return result as unknown as Promise<TOutput>
} }
export abstract class AbstractBaseRepository<T = any> export abstract class AbstractBaseRepository<T = any>
@@ -110,11 +107,11 @@ export abstract class AbstractBaseRepository<T = any>
return await transactionWrapper.apply(this, arguments) return await transactionWrapper.apply(this, arguments)
} }
serialize< async serialize<TOutput extends object | object[]>(
TData extends object | object[] = object[], data: any,
TResult extends object | object[] = object[] options?: any
>(data: TData, options?: any): Promise<TResult> { ): Promise<TOutput> {
return serializer<TData, TResult>(data, options) return await serializer<TOutput>(data, options)
} }
abstract find(options?: DAL.FindOptions<T>, context?: Context) abstract find(options?: DAL.FindOptions<T>, context?: Context)
@@ -203,13 +200,6 @@ export class BaseRepository extends AbstractBaseRepository {
super(...arguments) super(...arguments)
} }
serialize<
TData extends object | object[] = object[],
TResult extends object | object[] = object[]
>(data: TData, options?: any): Promise<TResult> {
return serializer<TData, TResult>(data, options)
}
create(data: unknown[], context?: Context): Promise<any[]> { create(data: unknown[], context?: Context): Promise<any[]> {
throw new Error("Method not implemented.") throw new Error("Method not implemented.")
} }
@@ -285,10 +285,7 @@ export default class ProductModuleService<
async create(data: ProductTypes.CreateProductDTO[], sharedContext?: Context) { async create(data: ProductTypes.CreateProductDTO[], sharedContext?: Context) {
const products = await this.create_(data, sharedContext) const products = await this.create_(data, sharedContext)
return this.baseRepository_.serialize< return this.baseRepository_.serialize<ProductTypes.ProductDTO[]>(products, {
TProduct[],
ProductTypes.ProductDTO[]
>(products, {
populate: true, populate: true,
}) })
} }
@@ -425,10 +422,7 @@ export default class ProductModuleService<
): Promise<ProductTypes.ProductDTO[]> { ): Promise<ProductTypes.ProductDTO[]> {
const products = await this.softDelete_(productIds, sharedContext) const products = await this.softDelete_(productIds, sharedContext)
return this.baseRepository_.serialize< return this.baseRepository_.serialize<ProductTypes.ProductDTO[]>(products, {
TProduct[],
ProductTypes.ProductDTO[]
>(products, {
populate: true, populate: true,
}) })
} }
@@ -447,10 +441,7 @@ export default class ProductModuleService<
): Promise<ProductTypes.ProductDTO[]> { ): Promise<ProductTypes.ProductDTO[]> {
const products = await this.restore_(productIds, sharedContext) const products = await this.restore_(productIds, sharedContext)
return this.baseRepository_.serialize< return this.baseRepository_.serialize<ProductTypes.ProductDTO[]>(products, {
TProduct[],
ProductTypes.ProductDTO[]
>(products, {
populate: true, populate: true,
}) })
} }
+4 -9
View File
@@ -17,15 +17,10 @@ export interface RepositoryService<T = any> {
} }
): Promise<any> ): Promise<any>
serialize<TData extends object, TResult extends object, TOptions = any>( serialize<TOutput extends object | object[]>(
data: TData, data: any,
options?: TOptions options?: any
): Promise<TResult> ): Promise<TOutput>
serialize<TData extends object[], TResult extends object[], TOptions = any>(
data: TData[],
options?: TOptions
): Promise<TResult>
find(options?: FindOptions<T>, context?: Context): Promise<T[]> find(options?: FindOptions<T>, context?: Context): Promise<T[]>