@mittwald/react-ghostmaker 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -3,5 +3,5 @@ export { registerModelIdentifier } from "./modelIdentifier";
3
3
  export { getQueryContext, forwardQueryContext } from "./context";
4
4
  export { invalidateGhosts } from "./invalidate";
5
5
  export * from "./maybeGhost";
6
- export { GhostMakerModel } from "./metaData";
6
+ export { GhostMakerModel, ghostMakerModel } from "./metaData";
7
7
  export { getModelId, getModelName } from "./queries";
@@ -1,9 +1,36 @@
1
+ import is from "@sindresorhus/is";
1
2
  const store = new Map();
2
- export const getMetaData = (something) => {
3
- return store.get(something);
4
- };
5
3
  export function GhostMakerModel(options) {
6
4
  return function (target) {
7
5
  store.set(target, options);
8
6
  };
9
7
  }
8
+ const modelIdentifiers = [];
9
+ export function ghostMakerModel(modelIdentifier) {
10
+ modelIdentifiers.push(modelIdentifier);
11
+ }
12
+ const getMetaDataFromIdentifiers = (something) => {
13
+ for (const identifyModel of modelIdentifiers) {
14
+ const meta = identifyModel(something);
15
+ if (meta) {
16
+ return meta;
17
+ }
18
+ }
19
+ };
20
+ export const getMetaData = (something) => {
21
+ return (getMetaDataRecursive(something, undefined, is.object(something) ? something.constructor : undefined) ?? getMetaDataFromIdentifiers(something));
22
+ };
23
+ const getMetaDataRecursive = (something, collectedOptions,
24
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
25
+ klass) => {
26
+ const currentClass = is.object(something) ? something.constructor : undefined;
27
+ const meta = store.get(klass ?? currentClass);
28
+ const mergedMeta = meta || collectedOptions ? { ...meta, ...collectedOptions } : undefined;
29
+ if (mergedMeta && "getId" in mergedMeta && "name" in mergedMeta) {
30
+ return mergedMeta;
31
+ }
32
+ if (!klass) {
33
+ return mergedMeta;
34
+ }
35
+ return getMetaDataRecursive(something, mergedMeta, Object.getPrototypeOf(klass));
36
+ };
@@ -3,35 +3,15 @@ import { hashObject } from "./hash";
3
3
  import { getMetaData } from "./metaData";
4
4
  import { modelIdentifiers } from "./modelIdentifier";
5
5
  export const getModelName = (something) => {
6
- const isObject = is.object(something);
7
- const getModelNameRecursive = (
8
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
9
- klass) => {
10
- if (!klass) {
11
- return;
12
- }
13
- return (getMetaData(klass)?.name ??
14
- getModelNameRecursive(Object.getPrototypeOf(klass)));
15
- };
16
- const klass = isObject ? something.constructor : undefined;
17
- return getModelNameRecursive(klass);
6
+ return getMetaData(something)?.name;
18
7
  };
19
8
  export const getModelId = (something) => {
20
- const isObject = is.object(something);
21
- const getIdRecursive = (
22
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
23
- klass) => {
24
- if (!klass) {
25
- return;
26
- }
27
- const getId = getMetaData(klass)?.getId;
9
+ if (is.object(something)) {
10
+ const getId = getMetaData(something)?.getId;
28
11
  if (getId) {
29
12
  return getId(something);
30
13
  }
31
- return getIdRecursive(Object.getPrototypeOf(klass));
32
- };
33
- const klass = isObject ? something.constructor : undefined;
34
- return getIdRecursive(klass);
14
+ }
35
15
  };
36
16
  const getObjectName = (something) => {
37
17
  const modelName = getModelName(something);
@@ -4,5 +4,5 @@ export { type ReactGhost, UseGhostReturn } from "./types";
4
4
  export { getQueryContext, forwardQueryContext } from "./context";
5
5
  export { invalidateGhosts } from "./invalidate";
6
6
  export * from "./maybeGhost";
7
- export { GhostMakerModel } from "./metaData";
7
+ export { GhostMakerModel, ghostMakerModel } from "./metaData";
8
8
  export { getModelId, getModelName } from "./queries";
@@ -1,8 +1,11 @@
1
- import type { Class } from "type-fest";
2
- interface GhostMakerModelOptions<T extends Class<unknown>> {
1
+ import type { AbstractClass, Class } from "type-fest";
2
+ type DecoratorTarget = Class<unknown> | AbstractClass<unknown>;
3
+ interface GhostMakerModelMeta<T extends DecoratorTarget> {
3
4
  getId?: (instance: InstanceType<T>) => string;
4
5
  name?: string;
5
6
  }
6
- export declare const getMetaData: (something: unknown) => GhostMakerModelOptions<Class<unknown>> | undefined;
7
- export declare function GhostMakerModel<T extends Class<unknown>>(options: GhostMakerModelOptions<T>): (target: T) => void;
7
+ export declare function GhostMakerModel<T extends DecoratorTarget>(options: GhostMakerModelMeta<T>): (target: T) => void;
8
+ type IdentifyModel = (something: unknown) => GhostMakerModelMeta<DecoratorTarget> | undefined;
9
+ export declare function ghostMakerModel(modelIdentifier: IdentifyModel): void;
10
+ export declare const getMetaData: (something: unknown) => GhostMakerModelMeta<DecoratorTarget> | undefined;
8
11
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mittwald/react-ghostmaker",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "author": "Mittwald CM Service GmbH & Co. KG <opensource@mittwald.de>",
5
5
  "contributors": [
6
6
  "Marco Falkenberg <m.falkenberg@mittwald.de>"