@mittwald/react-ghostmaker 1.2.0 → 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/README.md CHANGED
@@ -841,21 +841,19 @@ Creates a ghost proxy from any object or class instance.
841
841
  - **`.invalidate(queryClient)`** - Invalidate this specific ghost's cached data
842
842
  - **`await ghost`** - Directly await the ghost to get the resolved value
843
843
 
844
+
844
845
  ### Utility Functions
845
846
 
846
847
  - **`asGhostProps(props)`** - Converts props to ghost-compatible format
847
- - **`registerModelIdentifier(fn)`** - Register ID extraction function for
848
- caching
848
+ - **`GhostMakerModel(options)`** - Decorator to define model name and ID for automatic query key generation
849
849
  - **`invalidateGhostsById(id)`** - Globally invalidate cached data by ID
850
850
  - **`getGhostId(ghost)`** - Get the unique ID of a ghost
851
851
 
852
852
  ### Types
853
853
 
854
- - **`MaybeReactGhost<T>`** - Type for props that accept both ghosts and regular
855
- objects
854
+ - **`MaybeReactGhost<T>`** - Type for props that accept both ghosts and regular objects
856
855
  - **`ReactGhost<T>`** - Type for ghost proxy objects
857
- - **`UseGhostReturn<T>`** - Return type of `useGhost()` with
858
- `{ value, invalidate }`
856
+ - **`UseGhostReturn<T>`** - Return type of `useGhost()` with `{ value, invalidate }`
859
857
 
860
858
  ## 🤝 Requirements
861
859
 
package/dist/esm/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export { makeGhost } from "./makeGhost";
2
+ export { registerModelIdentifier } from "./modelIdentifier";
2
3
  export { getQueryContext, forwardQueryContext } from "./context";
3
4
  export { invalidateGhosts } from "./invalidate";
4
5
  export * from "./maybeGhost";
5
- export { GhostMakerModel } from "./metaData";
6
+ export { GhostMakerModel, ghostMakerModel } from "./metaData";
6
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);
@@ -1,7 +1,8 @@
1
1
  export { makeGhost } from "./makeGhost";
2
+ export { registerModelIdentifier } from "./modelIdentifier";
2
3
  export { type ReactGhost, UseGhostReturn } from "./types";
3
4
  export { getQueryContext, forwardQueryContext } from "./context";
4
5
  export { invalidateGhosts } from "./invalidate";
5
6
  export * from "./maybeGhost";
6
- export { GhostMakerModel } from "./metaData";
7
+ export { GhostMakerModel, ghostMakerModel } from "./metaData";
7
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.0",
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>"