Skip to content

Commit

Permalink
feat(core): Enable hydration of translations relation
Browse files Browse the repository at this point in the history
This allows you to use the EntityHydrator to hydrate the
`translations` relation of translatable entities, which was
not possible before due to the definition of the types.
  • Loading branch information
michaelbromley committed Nov 25, 2024
1 parent 76666a2 commit 84710d5
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions packages/core/src/common/types/entity-relation-paths.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { VendureEntity } from '../../entity/base/base.entity';

import { Translation } from './locale-types';

/**
* @description
* This type allows type-safe access to entity relations using strings with dot notation.
Expand Down Expand Up @@ -27,11 +29,11 @@ export type EntityRelationPaths<T extends VendureEntity> =
| TripleDotPath;

export type EntityRelationKeys<T extends VendureEntity> = {
[K in Extract<keyof T, string>]: Required<T>[K] extends VendureEntity | null
? K
: Required<T>[K] extends VendureEntity[]
[K in Extract<keyof T, string>]: Required<T>[K] extends VendureEntity | Translation<VendureEntity> | null
? K
: never;
: Required<T>[K] extends VendureEntity[] | Array<Translation<VendureEntity>>
? K
: never;
}[Extract<keyof T, string>];

export type EntityRelations<T extends VendureEntity> = {
Expand All @@ -50,8 +52,8 @@ export type PathsToStringProps2<T extends VendureEntity> = T extends string
[K in EntityRelationKeys<T>]: T[K] extends VendureEntity[]
? [K, PathsToStringProps1<T[K][number]>]
: T[K] extends VendureEntity | undefined
? [K, PathsToStringProps1<NonNullable<T[K]>>]
: never;
? [K, PathsToStringProps1<NonNullable<T[K]>>]
: never;
}[Extract<EntityRelationKeys<T>, string>];

export type TripleDotPath = `${string}.${string}.${string}`;
Expand All @@ -60,10 +62,10 @@ export type TripleDotPath = `${string}.${string}.${string}`;
export type Join<T extends Array<string | any>, D extends string> = T extends []
? never
: T extends [infer F]
? F
: // eslint-disable-next-line no-shadow,@typescript-eslint/no-shadow
T extends [infer F, ...infer R]
? F extends string
? `${F}${D}${Join<Extract<R, string[]>, D>}`
: never
: string;
? F
: // eslint-disable-next-line no-shadow,@typescript-eslint/no-shadow
T extends [infer F, ...infer R]
? F extends string
? `${F}${D}${Join<Extract<R, string[]>, D>}`
: never
: string;

0 comments on commit 84710d5

Please sign in to comment.