Browse Source

refactor(core): Extract EntityRelationPaths helper to common location

Michael Bromley 4 years ago
parent
commit
f84e88583f

+ 1 - 0
packages/core/src/common/index.ts

@@ -9,6 +9,7 @@ export * from './permission-definition';
 export * from './ttl-cache';
 export * from './ttl-cache';
 export * from './self-refreshing-cache';
 export * from './self-refreshing-cache';
 export * from './types/common-types';
 export * from './types/common-types';
+export * from './types/entity-relation-paths';
 export * from './types/injectable-strategy';
 export * from './types/injectable-strategy';
 export * from './types/locale-types';
 export * from './types/locale-types';
 export * from './utils';
 export * from './utils';

+ 66 - 0
packages/core/src/common/types/entity-relation-paths.ts

@@ -0,0 +1,66 @@
+import { VendureEntity } from '../../entity/base/base.entity';
+
+/**
+ * @description
+ * This type allows type-safe access to entity relations using strings with dot notation.
+ * It works to 2 levels deep.
+ *
+ * @example
+ * ```TypeScript
+ * type T1 = EntityRelationPaths<Product>;
+ * ```
+ * In the above example, the type `T1` will be a string union of all relations of the
+ * `Product` entity:
+ *
+ *  * `'featuredAsset'`
+ *  * `'variants'`
+ *  * `'variants.options'`
+ *  * `'variants.featuredAsset'`
+ *  * etc.
+ *
+ * @docsCategory Common
+ */
+export type EntityRelationPaths<T extends VendureEntity> =
+    | PathsToStringProps1<T>
+    | Join<PathsToStringProps2<T>, '.'>
+    | TripleDotPath;
+
+export type EntityRelationKeys<T extends VendureEntity> = {
+    [K in Extract<keyof T, string>]: T[K] extends VendureEntity
+        ? K
+        : T[K] extends VendureEntity[]
+        ? K
+        : never;
+}[Extract<keyof T, string>];
+
+export type EntityRelations<T extends VendureEntity> = {
+    [K in EntityRelationKeys<T>]: T[K];
+};
+
+export type PathsToStringProps1<T extends VendureEntity> = T extends string
+    ? []
+    : {
+          [K in EntityRelationKeys<T>]: K;
+      }[Extract<EntityRelationKeys<T>, string>];
+
+export type PathsToStringProps2<T extends VendureEntity> = T extends string
+    ? never
+    : {
+          [K in EntityRelationKeys<T>]: T[K] extends VendureEntity[]
+              ? [K, PathsToStringProps1<T[K][number]>]
+              : [K, PathsToStringProps1<T[K]>];
+      }[Extract<EntityRelationKeys<T>, string>];
+
+export type TripleDotPath = `${string}.${string}.${string}`;
+
+// Based on https://stackoverflow.com/a/47058976/772859
+export type Join<T extends Array<string | any>, D extends string> = T extends []
+    ? never
+    : T extends [infer F]
+    ? F
+    : // tslint:disable-next-line:no-shadowed-variable
+    T extends [infer F, ...infer R]
+    ? F extends string
+        ? `${F}${D}${Join<Extract<R, string[]>, D>}`
+        : never
+    : string;

+ 1 - 46
packages/core/src/service/helpers/entity-hydrator/entity-hydrator-types.ts

@@ -1,3 +1,4 @@
+import { EntityRelationPaths } from '../../../common/types/entity-relation-paths';
 import { VendureEntity } from '../../../entity/base/base.entity';
 import { VendureEntity } from '../../../entity/base/base.entity';
 
 
 /**
 /**
@@ -26,49 +27,3 @@ export interface HydrateOptions<Entity extends VendureEntity> {
      */
      */
     applyProductVariantPrices?: boolean;
     applyProductVariantPrices?: boolean;
 }
 }
-
-// The following types are all related to allowing dot-notation access to relation properties
-export type EntityRelationKeys<T extends VendureEntity> = {
-    [K in Extract<keyof T, string>]: T[K] extends VendureEntity
-        ? K
-        : T[K] extends VendureEntity[]
-        ? K
-        : never;
-}[Extract<keyof T, string>];
-
-export type EntityRelations<T extends VendureEntity> = {
-    [K in EntityRelationKeys<T>]: T[K];
-};
-
-export type PathsToStringProps1<T extends VendureEntity> = T extends string
-    ? []
-    : {
-          [K in EntityRelationKeys<T>]: K;
-      }[Extract<EntityRelationKeys<T>, string>];
-
-export type PathsToStringProps2<T extends VendureEntity> = T extends string
-    ? never
-    : {
-          [K in EntityRelationKeys<T>]: T[K] extends VendureEntity[]
-              ? [K, PathsToStringProps1<T[K][number]>]
-              : [K, PathsToStringProps1<T[K]>];
-      }[Extract<EntityRelationKeys<T>, string>];
-
-export type TripleDotPath = `${string}.${string}.${string}`;
-
-export type EntityRelationPaths<T extends VendureEntity> =
-    | PathsToStringProps1<T>
-    | Join<PathsToStringProps2<T>, '.'>
-    | TripleDotPath;
-
-// Based on https://stackoverflow.com/a/47058976/772859
-export type Join<T extends Array<string | any>, D extends string> = T extends []
-    ? never
-    : T extends [infer F]
-    ? F
-    : // tslint:disable-next-line:no-shadowed-variable
-    T extends [infer F, ...infer R]
-    ? F extends string
-        ? `${F}${D}${Join<Extract<R, string[]>, D>}`
-        : never
-    : string;