Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(product): Serialize typings #4602

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/dirty-pumas-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/product": patch
"@medusajs/types": patch
---

fix(product): Serialize typings
28 changes: 9 additions & 19 deletions packages/product/src/repositories/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,13 @@ const updateDeletedAtRecursively = async <T extends object = any>(
}
}

const serializer = <
T extends object | object[],
TResult extends object | object[]
>(
data: T,
const serializer = <TOutput extends object>(
data: any,
options?: any
): Promise<TResult> => {
): Promise<TOutput> => {
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>
Expand All @@ -110,11 +107,11 @@ export abstract class AbstractBaseRepository<T = any>
return await transactionWrapper.apply(this, arguments)
}

serialize<
TData extends object | object[] = object[],
TResult extends object | object[] = object[]
>(data: TData, options?: any): Promise<TResult> {
return serializer<TData, TResult>(data, options)
async serialize<TOutput extends object | object[]>(
data: any,
options?: any
): Promise<TOutput> {
return await serializer<TOutput>(data, options)
}

abstract find(options?: DAL.FindOptions<T>, context?: Context)
Expand Down Expand Up @@ -203,13 +200,6 @@ export class BaseRepository extends AbstractBaseRepository {
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[]> {
throw new Error("Method not implemented.")
}
Expand Down
15 changes: 3 additions & 12 deletions packages/product/src/services/product-module-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,7 @@ export default class ProductModuleService<
async create(data: ProductTypes.CreateProductDTO[], sharedContext?: Context) {
const products = await this.create_(data, sharedContext)

return this.baseRepository_.serialize<
TProduct[],
ProductTypes.ProductDTO[]
>(products, {
return this.baseRepository_.serialize<ProductTypes.ProductDTO[]>(products, {
populate: true,
})
}
Expand Down Expand Up @@ -425,10 +422,7 @@ export default class ProductModuleService<
): Promise<ProductTypes.ProductDTO[]> {
const products = await this.softDelete_(productIds, sharedContext)

return this.baseRepository_.serialize<
TProduct[],
ProductTypes.ProductDTO[]
>(products, {
return this.baseRepository_.serialize<ProductTypes.ProductDTO[]>(products, {
populate: true,
})
}
Expand All @@ -447,10 +441,7 @@ export default class ProductModuleService<
): Promise<ProductTypes.ProductDTO[]> {
const products = await this.restore_(productIds, sharedContext)

return this.baseRepository_.serialize<
TProduct[],
ProductTypes.ProductDTO[]
>(products, {
return this.baseRepository_.serialize<ProductTypes.ProductDTO[]>(products, {
populate: true,
})
}
Expand Down
13 changes: 4 additions & 9 deletions packages/types/src/dal/repository-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ export interface RepositoryService<T = any> {
}
): Promise<any>

serialize<TData extends object, TResult extends object, TOptions = any>(
data: TData,
options?: TOptions
): Promise<TResult>

serialize<TData extends object[], TResult extends object[], TOptions = any>(
data: TData[],
options?: TOptions
): Promise<TResult>
serialize<TOutput extends object | object[]>(
data: any,
options?: any
): Promise<TOutput>

find(options?: FindOptions<T>, context?: Context): Promise<T[]>

Expand Down