@mittwald/react-ghostmaker 1.2.1 → 1.3.1

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,47 @@
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
+ function isClass(value) {
21
+ return typeof value === "function" && /class[\s{]/.test(value.toString());
22
+ }
23
+ const getClass = (something) => {
24
+ return isClass(something)
25
+ ? something
26
+ : is.object(something)
27
+ ? something.constructor
28
+ : undefined;
29
+ };
30
+ export const getMetaData = (something) => {
31
+ return (getMetaDataRecursive(something, undefined, getClass(something)) ??
32
+ getMetaDataFromIdentifiers(something));
33
+ };
34
+ const getMetaDataRecursive = (something, collectedMeta,
35
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
36
+ klass) => {
37
+ const currentClass = getClass(something);
38
+ const meta = store.get(klass ?? currentClass);
39
+ const mergedMeta = meta || collectedMeta ? { ...meta, ...collectedMeta } : undefined;
40
+ if (mergedMeta && "getId" in mergedMeta && "name" in mergedMeta) {
41
+ return mergedMeta;
42
+ }
43
+ if (!klass) {
44
+ return mergedMeta;
45
+ }
46
+ return getMetaDataRecursive(something, mergedMeta, Object.getPrototypeOf(klass));
47
+ };
@@ -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);
@@ -0,0 +1,106 @@
1
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
2
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
3
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
4
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
5
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
6
+ var _, done = false;
7
+ for (var i = decorators.length - 1; i >= 0; i--) {
8
+ var context = {};
9
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
10
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
11
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
12
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
13
+ if (kind === "accessor") {
14
+ if (result === void 0) continue;
15
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
16
+ if (_ = accept(result.get)) descriptor.get = _;
17
+ if (_ = accept(result.set)) descriptor.set = _;
18
+ if (_ = accept(result.init)) initializers.unshift(_);
19
+ }
20
+ else if (_ = accept(result)) {
21
+ if (kind === "field") initializers.unshift(_);
22
+ else descriptor[key] = _;
23
+ }
24
+ }
25
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
26
+ done = true;
27
+ };
28
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
29
+ var useValue = arguments.length > 2;
30
+ for (var i = 0; i < initializers.length; i++) {
31
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
32
+ }
33
+ return useValue ? value : void 0;
34
+ };
35
+ import { getMetaData, GhostMakerModel } from "./metaData";
36
+ import { expect, test } from "vitest";
37
+ let GrandParent = (() => {
38
+ let _classDecorators = [GhostMakerModel({
39
+ name: "TheGrandParent",
40
+ getId: (grandParent) => grandParent.id,
41
+ })];
42
+ let _classDescriptor;
43
+ let _classExtraInitializers = [];
44
+ let _classThis;
45
+ var GrandParent = class {
46
+ static { _classThis = this; }
47
+ static {
48
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
49
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
50
+ GrandParent = _classThis = _classDescriptor.value;
51
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
52
+ __runInitializers(_classThis, _classExtraInitializers);
53
+ }
54
+ id;
55
+ constructor(id) {
56
+ this.id = id;
57
+ }
58
+ };
59
+ return GrandParent = _classThis;
60
+ })();
61
+ let Parent = (() => {
62
+ let _classDecorators = [GhostMakerModel({
63
+ name: "TheParent",
64
+ })];
65
+ let _classDescriptor;
66
+ let _classExtraInitializers = [];
67
+ let _classThis;
68
+ let _classSuper = GrandParent;
69
+ var Parent = class extends _classSuper {
70
+ static { _classThis = this; }
71
+ static {
72
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
73
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
74
+ Parent = _classThis = _classDescriptor.value;
75
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
76
+ __runInitializers(_classThis, _classExtraInitializers);
77
+ }
78
+ };
79
+ return Parent = _classThis;
80
+ })();
81
+ let Child = (() => {
82
+ let _classDecorators = [GhostMakerModel({
83
+ name: "TheChild",
84
+ })];
85
+ let _classDescriptor;
86
+ let _classExtraInitializers = [];
87
+ let _classThis;
88
+ let _classSuper = Parent;
89
+ var Child = class extends _classSuper {
90
+ static { _classThis = this; }
91
+ static {
92
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
93
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
94
+ Child = _classThis = _classDescriptor.value;
95
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
96
+ __runInitializers(_classThis, _classExtraInitializers);
97
+ }
98
+ };
99
+ return Child = _classThis;
100
+ })();
101
+ test("getMetaData returns correct names", () => {
102
+ expect(getMetaData(new Child("id"))?.name).toBe("TheChild");
103
+ expect(getMetaData(new Parent("id"))?.name).toBe("TheParent");
104
+ expect(getMetaData(Parent)?.name).toBe("TheParent");
105
+ expect(getMetaData(Child)?.name).toBe("TheChild");
106
+ });
@@ -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 {};
@@ -0,0 +1 @@
1
+ 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.1",
4
4
  "author": "Mittwald CM Service GmbH & Co. KG <opensource@mittwald.de>",
5
5
  "contributors": [
6
6
  "Marco Falkenberg <m.falkenberg@mittwald.de>"