Browse Source

feat(core): Enable hydration of translations relation

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.
Michael Bromley 1 year ago
parent
commit
84710d51dc
1 changed files with 15 additions and 13 deletions
  1. 15 13
      packages/core/src/common/types/entity-relation-paths.ts

+ 15 - 13
packages/core/src/common/types/entity-relation-paths.ts

@@ -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.
@@ -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> = {
@@ -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}`;
@@ -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;