@croct/plug-react 0.4.2 → 0.5.0-next.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.
Files changed (68) hide show
  1. package/CroctProvider.d.ts +7 -7
  2. package/CroctProvider.js +37 -0
  3. package/CroctProvider.js.map +1 -0
  4. package/README.md +245 -107
  5. package/api/evaluate.d.ts +7 -0
  6. package/api/evaluate.js +15 -0
  7. package/api/evaluate.js.map +1 -0
  8. package/api/fetchContent.d.ts +13 -0
  9. package/api/fetchContent.js +17 -0
  10. package/api/fetchContent.js.map +1 -0
  11. package/api/index.d.ts +2 -0
  12. package/api/index.js +19 -0
  13. package/api/index.js.map +1 -0
  14. package/components/Personalization/index.d.ts +10 -10
  15. package/components/Personalization/index.js +13 -0
  16. package/components/Personalization/index.js.map +1 -0
  17. package/components/Slot/index.d.ts +19 -19
  18. package/components/Slot/index.js +13 -0
  19. package/components/Slot/index.js.map +1 -0
  20. package/components/index.d.ts +2 -2
  21. package/components/index.js +19 -0
  22. package/components/index.js.map +1 -0
  23. package/hooks/Cache.d.ts +22 -22
  24. package/hooks/Cache.js +62 -0
  25. package/hooks/Cache.js.map +1 -0
  26. package/hooks/index.d.ts +3 -3
  27. package/hooks/index.js +20 -0
  28. package/hooks/index.js.map +1 -0
  29. package/hooks/useContent.d.ts +18 -17
  30. package/hooks/useContent.js +25 -0
  31. package/hooks/useContent.js.map +1 -0
  32. package/hooks/useCroct.d.ts +2 -2
  33. package/hooks/useCroct.js +14 -0
  34. package/hooks/useCroct.js.map +1 -0
  35. package/hooks/useEvaluation.d.ts +11 -11
  36. package/hooks/useEvaluation.js +35 -0
  37. package/hooks/useEvaluation.js.map +1 -0
  38. package/hooks/useLoader.d.ts +5 -5
  39. package/hooks/useLoader.js +41 -0
  40. package/hooks/useLoader.js.map +1 -0
  41. package/index.d.ts +5 -3
  42. package/index.js +20 -337
  43. package/index.js.map +1 -1
  44. package/package.json +31 -44
  45. package/ssr-polyfills.d.ts +3 -3
  46. package/ssr-polyfills.js +64 -0
  47. package/ssr-polyfills.js.map +1 -0
  48. package/CroctProvider.test.d.ts +0 -1
  49. package/components/Personalization/index.d.test.d.ts +0 -1
  50. package/components/Personalization/index.stories.d.ts +0 -7
  51. package/components/Personalization/index.test.d.ts +0 -1
  52. package/components/Slot/index.d.test.d.ts +0 -1
  53. package/components/Slot/index.stories.d.ts +0 -17
  54. package/components/Slot/index.test.d.ts +0 -1
  55. package/hooks/Cache.test.d.ts +0 -1
  56. package/hooks/useContent.d.test.d.ts +0 -1
  57. package/hooks/useContent.ssr.test.d.ts +0 -1
  58. package/hooks/useContent.stories.d.ts +0 -19
  59. package/hooks/useContent.test.d.ts +0 -1
  60. package/hooks/useCroct.ssr.test.d.ts +0 -1
  61. package/hooks/useCroct.test.d.ts +0 -1
  62. package/hooks/useEvaluation.d.test.d.ts +0 -1
  63. package/hooks/useEvaluation.ssr.test.d.ts +0 -1
  64. package/hooks/useEvaluation.stories.d.ts +0 -8
  65. package/hooks/useEvaluation.test.d.ts +0 -1
  66. package/hooks/useLoader.test.d.ts +0 -1
  67. package/ssr-polyfills.ssr.test.d.ts +0 -1
  68. package/ssr-polyfills.test.d.ts +0 -1
@@ -0,0 +1,7 @@
1
+ import { EvaluationOptions as BaseOptions } from '@croct/sdk/evaluator';
2
+ import { JsonValue } from '@croct/plug/sdk/json';
3
+ export type EvaluationOptions<T extends JsonValue = JsonValue> = BaseOptions & {
4
+ apiKey: string;
5
+ fallback?: T;
6
+ };
7
+ export declare function evaluate<T extends JsonValue>(query: string, options: EvaluationOptions<T>): Promise<T>;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.evaluate = void 0;
4
+ const evaluator_1 = require("@croct/sdk/evaluator");
5
+ function evaluate(query, options) {
6
+ const { apiKey, fallback, ...rest } = options;
7
+ const promise = (new evaluator_1.Evaluator({ apiKey: apiKey }))
8
+ .evaluate(query, rest);
9
+ if (fallback !== undefined) {
10
+ return promise.catch(() => fallback);
11
+ }
12
+ return promise;
13
+ }
14
+ exports.evaluate = evaluate;
15
+ //# sourceMappingURL=evaluate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evaluate.js","sourceRoot":"","sources":["../../src/api/evaluate.ts"],"names":[],"mappings":";;;AAAA,oDAAiF;AAQjF,SAAgB,QAAQ,CAAsB,KAAa,EAAE,OAA6B;IACtF,MAAM,EAAC,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAC,GAAG,OAAO,CAAC;IAC5C,MAAM,OAAO,GAAG,CAAC,IAAI,qBAAS,CAAC,EAAC,MAAM,EAAE,MAAM,EAAC,CAAC,CAAC;SAC5C,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAe,CAAC;IAEzC,IAAI,QAAQ,KAAK,SAAS,EAAE;QACxB,OAAO,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,CAAC;KACxC;IAED,OAAO,OAAO,CAAC;AACnB,CAAC;AAVD,4BAUC"}
@@ -0,0 +1,13 @@
1
+ import { DynamicContentOptions as BaseDynamicOptions, StaticContentOptions as BaseStaticOptions } from '@croct/sdk/contentFetcher';
2
+ import { JsonObject } from '@croct/plug/sdk/json';
3
+ import { FetchResponse } from '@croct/plug/plug';
4
+ import { SlotContent, VersionedSlotId } from '@croct/plug/slot';
5
+ type ServerSideOptions<T extends JsonObject> = {
6
+ apiKey: string;
7
+ fallback?: T;
8
+ };
9
+ export type DynamicContentOptions<T extends JsonObject = JsonObject> = Omit<BaseDynamicOptions, 'version'> & ServerSideOptions<T>;
10
+ export type StaticContentOptions<T extends JsonObject = JsonObject> = Omit<BaseStaticOptions, 'version'> & ServerSideOptions<T>;
11
+ export type FetchOptions<T extends JsonObject = JsonObject> = DynamicContentOptions<T> | StaticContentOptions<T>;
12
+ export declare function fetchContent<I extends VersionedSlotId, C extends JsonObject>(slotId: I, options?: FetchOptions<SlotContent<I, C>>): Promise<Omit<FetchResponse<I, C>, 'payload'>>;
13
+ export {};
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchContent = void 0;
4
+ const contentFetcher_1 = require("@croct/sdk/contentFetcher");
5
+ function fetchContent(slotId, options) {
6
+ const { apiKey, fallback, ...fetchOptions } = options !== null && options !== void 0 ? options : {};
7
+ const promise = (new contentFetcher_1.ContentFetcher({ apiKey: apiKey }))
8
+ .fetch(slotId, fetchOptions);
9
+ if (fallback !== undefined) {
10
+ return promise.catch(() => ({
11
+ content: fallback,
12
+ }));
13
+ }
14
+ return promise;
15
+ }
16
+ exports.fetchContent = fetchContent;
17
+ //# sourceMappingURL=fetchContent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetchContent.js","sourceRoot":"","sources":["../../src/api/fetchContent.ts"],"names":[],"mappings":";;;AAAA,8DAImC;AAkBnC,SAAgB,YAAY,CACxB,MAAS,EACT,OAAyC;IAEzC,MAAM,EAAC,MAAM,EAAE,QAAQ,EAAE,GAAG,YAAY,EAAC,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAC;IAC1D,MAAM,OAAO,GAAG,CAAC,IAAI,+BAAc,CAAC,EAAC,MAAM,EAAE,MAAM,EAAC,CAAC,CAAC;SACjD,KAAK,CAAoB,MAAM,EAAE,YAAY,CAAC,CAAC;IAEpD,IAAI,QAAQ,KAAK,SAAS,EAAE;QACxB,OAAO,OAAO,CAAC,KAAK,CAChB,GAAG,EAAE,CAAC,CAAC;YACH,OAAO,EAAE,QAAQ;SACpB,CAAC,CACL,CAAC;KACL;IAED,OAAO,OAAO,CAAC;AACnB,CAAC;AAjBD,oCAiBC"}
package/api/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './evaluate';
2
+ export * from './fetchContent';
package/api/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./evaluate"), exports);
18
+ __exportStar(require("./fetchContent"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,iDAA+B"}
@@ -1,10 +1,10 @@
1
- import { ReactChild, ReactElement } from 'react';
2
- import { JsonValue } from '@croct/plug/sdk/json';
3
- import { UseEvaluationOptions } from '../../hooks';
4
- declare type Renderer<T> = (result: T) => ReactChild;
5
- export declare type PersonalizationProps<T extends JsonValue = JsonValue, I = T, F = T> = UseEvaluationOptions<I, F> & {
6
- expression: string;
7
- children: Renderer<T | I | F>;
8
- };
9
- export declare function Personalization<T extends JsonValue, I, F>(props: Extract<T | I | F, JsonValue> extends never ? PersonalizationProps : PersonalizationProps<T, I, F>): ReactElement;
10
- export {};
1
+ import { ReactElement } from 'react';
2
+ import { JsonValue } from '@croct/plug/sdk/json';
3
+ import { UseEvaluationOptions } from '../../hooks';
4
+ type Renderer<T> = (result: T) => ReactElement | string | number;
5
+ export type PersonalizationProps<T extends JsonValue = JsonValue, I = T, F = T> = UseEvaluationOptions<I, F> & {
6
+ query: string;
7
+ children: Renderer<T | I | F>;
8
+ };
9
+ export declare function Personalization<T extends JsonValue, I, F>(props: Extract<T | I | F, JsonValue> extends never ? PersonalizationProps : PersonalizationProps<T, I, F>): ReactElement;
10
+ export {};
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Personalization = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("react");
6
+ const hooks_1 = require("../../hooks");
7
+ function Personalization(props) {
8
+ const { query, children, ...options } = props;
9
+ const result = (0, hooks_1.useEvaluation)(query, options);
10
+ return ((0, jsx_runtime_1.jsx)(react_1.Fragment, { children: children(result) }));
11
+ }
12
+ exports.Personalization = Personalization;
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/components/Personalization/index.tsx"],"names":[],"mappings":";;;;AAAA,iCAA6C;AAE7C,uCAAgE;AAgBhE,SAAgB,eAAe,CAAO,KAA4C;IAC9E,MAAM,EAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,OAAO,EAAC,GAAG,KAAK,CAAC;IAC5C,MAAM,MAAM,GAAG,IAAA,qBAAa,EAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAE7C,OAAO,CAAC,uBAAC,gBAAQ,cAAE,QAAQ,CAAC,MAAM,CAAC,GAAY,CAAC,CAAC;AACrD,CAAC;AALD,0CAKC"}
@@ -1,19 +1,19 @@
1
- import { ReactChild, ReactElement } from 'react';
2
- import { SlotContent, SlotId, SlotMap } from '@croct/plug/fetch';
3
- import { NullableJsonObject } from '@croct/plug/sdk/json';
4
- import { UseContentOptions } from '../../hooks';
5
- declare type Renderer<P> = (props: P) => ReactChild;
6
- export declare type SlotProps<P, I = P, F = P, S extends SlotId = SlotId> = UseContentOptions<I, F> & {
7
- id: S;
8
- children: Renderer<P | I | F>;
9
- };
10
- declare type SlotComponent = {
11
- <P, I, F>(props: Extract<P | I | F, NullableJsonObject> extends never ? SlotProps<NullableJsonObject, never, never, keyof SlotMap extends never ? string : never> : SlotProps<P, I, F, keyof SlotMap extends never ? string : never>): ReactElement;
12
- <S extends keyof SlotMap>(props: SlotProps<SlotContent<S>, never, never, S>): ReactElement;
13
- <I, S extends keyof SlotMap>(props: SlotProps<SlotContent<S>, I, never, S>): ReactElement;
14
- <F, S extends keyof SlotMap>(props: SlotProps<SlotContent<S>, never, F, S>): ReactElement;
15
- <I, F, S extends keyof SlotMap>(props: SlotProps<SlotContent<S>, I, F, S>): ReactElement;
16
- (props: SlotProps<void, void, void>): ReactElement;
17
- };
18
- export declare const Slot: SlotComponent;
19
- export {};
1
+ import { ReactElement, ReactNode } from 'react';
2
+ import { SlotContent, VersionedSlotId, VersionedSlotMap } from '@croct/plug/slot';
3
+ import { JsonObject } from '@croct/plug/sdk/json';
4
+ import { UseContentOptions } from '../../hooks';
5
+ type Renderer<P> = (props: P) => ReactNode;
6
+ export type SlotProps<P, I = P, F = P, S extends VersionedSlotId = VersionedSlotId> = UseContentOptions<I, F> & {
7
+ id: S;
8
+ children: Renderer<P | I | F>;
9
+ };
10
+ type SlotComponent = {
11
+ <P, I, F>(props: Extract<P | I | F, JsonObject> extends never ? SlotProps<JsonObject, never, never, keyof VersionedSlotMap extends never ? string : never> : SlotProps<P, I, F, keyof VersionedSlotMap extends never ? string : never>): ReactElement;
12
+ <S extends VersionedSlotId>(props: SlotProps<SlotContent<S>, never, never, S>): ReactElement;
13
+ <I, S extends VersionedSlotId>(props: SlotProps<SlotContent<S>, I, never, S>): ReactElement;
14
+ <F, S extends VersionedSlotId>(props: SlotProps<SlotContent<S>, never, F, S>): ReactElement;
15
+ <I, F, S extends VersionedSlotId>(props: SlotProps<SlotContent<S>, I, F, S>): ReactElement;
16
+ (props: SlotProps<void, void, void>): ReactElement;
17
+ };
18
+ export declare const Slot: SlotComponent;
19
+ export {};
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Slot = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("react");
6
+ const hooks_1 = require("../../hooks");
7
+ const Slot = (props) => {
8
+ const { id, children, ...options } = props;
9
+ const data = (0, hooks_1.useContent)(id, options);
10
+ return (0, jsx_runtime_1.jsx)(react_1.Fragment, { children: children(data) });
11
+ };
12
+ exports.Slot = Slot;
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/components/Slot/index.tsx"],"names":[],"mappings":";;;;AAAA,iCAAwD;AAGxD,uCAA0D;AA4BnD,MAAM,IAAI,GAAkB,CAAO,KAAkC,EAAgB,EAAE;IAC1F,MAAM,EAAC,EAAE,EAAE,QAAQ,EAAE,GAAG,OAAO,EAAC,GAAG,KAAK,CAAC;IACzC,MAAM,IAAI,GAAgC,IAAA,kBAAU,EAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IAElE,OAAO,uBAAC,gBAAQ,cAAE,QAAQ,CAAC,IAAI,CAAC,GAAY,CAAC;AACjD,CAAC,CAAC;AALW,QAAA,IAAI,QAKf"}
@@ -1,2 +1,2 @@
1
- export * from './Personalization';
2
- export * from './Slot';
1
+ export * from './Personalization';
2
+ export * from './Slot';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Personalization"), exports);
18
+ __exportStar(require("./Slot"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,yCAAuB"}
package/hooks/Cache.d.ts CHANGED
@@ -1,22 +1,22 @@
1
- export declare type EntryLoader<R> = (...args: any) => Promise<R>;
2
- export declare type EntryOptions<R> = {
3
- cacheKey: string;
4
- loader: EntryLoader<R>;
5
- fallback?: R;
6
- expiration?: number;
7
- };
8
- declare type Entry<R = any> = {
9
- promise: Promise<any>;
10
- result?: R;
11
- dispose: () => void;
12
- timeout?: number;
13
- error?: any;
14
- };
15
- export declare class Cache {
16
- private readonly cache;
17
- private readonly defaultExpiration;
18
- constructor(defaultExpiration: number);
19
- load<R>(configuration: EntryOptions<R>): R;
20
- get<R>(cacheKey: string): Entry<R> | undefined;
21
- }
22
- export {};
1
+ export type EntryLoader<R> = (...args: any) => Promise<R>;
2
+ export type EntryOptions<R> = {
3
+ cacheKey: string;
4
+ loader: EntryLoader<R>;
5
+ fallback?: R;
6
+ expiration?: number;
7
+ };
8
+ type Entry<R = any> = {
9
+ promise: Promise<any>;
10
+ result?: R;
11
+ dispose: () => void;
12
+ timeout?: number;
13
+ error?: any;
14
+ };
15
+ export declare class Cache {
16
+ private readonly cache;
17
+ private readonly defaultExpiration;
18
+ constructor(defaultExpiration: number);
19
+ load<R>(configuration: EntryOptions<R>): R;
20
+ get<R>(cacheKey: string): Entry<R> | undefined;
21
+ }
22
+ export {};
package/hooks/Cache.js ADDED
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Cache = void 0;
4
+ class Cache {
5
+ constructor(defaultExpiration) {
6
+ this.cache = {};
7
+ this.defaultExpiration = defaultExpiration;
8
+ }
9
+ load(configuration) {
10
+ const { cacheKey, loader, fallback, expiration = this.defaultExpiration } = configuration;
11
+ const cachedEntry = this.get(cacheKey);
12
+ if (cachedEntry !== undefined) {
13
+ if (cachedEntry.error !== undefined) {
14
+ if (fallback !== undefined) {
15
+ return fallback;
16
+ }
17
+ throw cachedEntry.error;
18
+ }
19
+ if (cachedEntry.result !== undefined) {
20
+ return cachedEntry.result;
21
+ }
22
+ throw cachedEntry.promise;
23
+ }
24
+ const entry = {
25
+ dispose: () => {
26
+ if (entry.timeout !== undefined || expiration < 0) {
27
+ return;
28
+ }
29
+ entry.timeout = window.setTimeout(() => {
30
+ delete this.cache[cacheKey];
31
+ }, expiration);
32
+ },
33
+ promise: loader()
34
+ .then((result) => {
35
+ entry.result = result;
36
+ return result;
37
+ })
38
+ .catch(error => {
39
+ entry.error = error;
40
+ })
41
+ .finally(() => {
42
+ entry.dispose();
43
+ }),
44
+ };
45
+ this.cache[cacheKey] = entry;
46
+ throw entry.promise;
47
+ }
48
+ get(cacheKey) {
49
+ const entry = this.cache[cacheKey];
50
+ if (entry === undefined) {
51
+ return undefined;
52
+ }
53
+ if (entry.timeout !== undefined) {
54
+ clearTimeout(entry.timeout);
55
+ delete entry.timeout;
56
+ entry.dispose();
57
+ }
58
+ return entry;
59
+ }
60
+ }
61
+ exports.Cache = Cache;
62
+ //# sourceMappingURL=Cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Cache.js","sourceRoot":"","sources":["../../src/hooks/Cache.ts"],"names":[],"mappings":";;;AAiBA,MAAa,KAAK;IAKd,YAAmB,iBAAyB;QAJ3B,UAAK,GAA0B,EAAE,CAAC;QAK/C,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;IAC/C,CAAC;IAEM,IAAI,CAAI,aAA8B;QACzC,MAAM,EAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,GAAG,IAAI,CAAC,iBAAiB,EAAC,GAAG,aAAa,CAAC;QAExF,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAI,QAAQ,CAAC,CAAC;QAE1C,IAAI,WAAW,KAAK,SAAS,EAAE;YAC3B,IAAI,WAAW,CAAC,KAAK,KAAK,SAAS,EAAE;gBACjC,IAAI,QAAQ,KAAK,SAAS,EAAE;oBACxB,OAAO,QAAQ,CAAC;iBACnB;gBAED,MAAM,WAAW,CAAC,KAAK,CAAC;aAC3B;YAED,IAAI,WAAW,CAAC,MAAM,KAAK,SAAS,EAAE;gBAClC,OAAO,WAAW,CAAC,MAAM,CAAC;aAC7B;YAED,MAAM,WAAW,CAAC,OAAO,CAAC;SAC7B;QAED,MAAM,KAAK,GAAa;YACpB,OAAO,EAAE,GAAG,EAAE;gBACV,IAAI,KAAK,CAAC,OAAO,KAAK,SAAS,IAAI,UAAU,GAAG,CAAC,EAAE;oBAC/C,OAAO;iBACV;gBAED,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,UAAU,CAC7B,GAAS,EAAE;oBACP,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;gBAChC,CAAC,EACD,UAAU,CACb,CAAC;YACN,CAAC;YACD,OAAO,EAAE,MAAM,EAAE;iBACZ,IAAI,CAAC,CAAC,MAAM,EAAK,EAAE;gBAChB,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC;gBAEtB,OAAO,MAAM,CAAC;YAClB,CAAC,CAAC;iBACD,KAAK,CAAC,KAAK,CAAC,EAAE;gBACX,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC;YACxB,CAAC,CAAC;iBACD,OAAO,CAAC,GAAG,EAAE;gBACV,KAAK,CAAC,OAAO,EAAE,CAAC;YACpB,CAAC,CAAC;SACT,CAAC;QAEF,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC;QAE7B,MAAM,KAAK,CAAC,OAAO,CAAC;IACxB,CAAC;IAEM,GAAG,CAAI,QAAgB;QAC1B,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAEnC,IAAI,KAAK,KAAK,SAAS,EAAE;YACrB,OAAO,SAAS,CAAC;SACpB;QAED,IAAI,KAAK,CAAC,OAAO,KAAK,SAAS,EAAE;YAC7B,YAAY,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAE5B,OAAO,KAAK,CAAC,OAAO,CAAC;YAErB,KAAK,CAAC,OAAO,EAAE,CAAC;SACnB;QAED,OAAO,KAAK,CAAC;IACjB,CAAC;CACJ;AA/ED,sBA+EC"}
package/hooks/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from './useEvaluation';
2
- export * from './useContent';
3
- export * from './useCroct';
1
+ export * from './useEvaluation';
2
+ export * from './useContent';
3
+ export * from './useCroct';
package/hooks/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./useEvaluation"), exports);
18
+ __exportStar(require("./useContent"), exports);
19
+ __exportStar(require("./useCroct"), exports);
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,+CAA6B;AAC7B,6CAA2B"}
@@ -1,17 +1,18 @@
1
- import { SlotContent, SlotMap } from '@croct/plug/fetch';
2
- import { NullableJsonObject } from '@croct/plug/sdk/json';
3
- export declare type UseContentOptions<I, F> = {
4
- fallback?: F;
5
- initial?: I;
6
- cacheKey?: string;
7
- expiration?: number;
8
- };
9
- declare type UseContentHook = {
10
- <P extends NullableJsonObject, I = P, F = P>(id: keyof SlotMap extends never ? string : never, options?: UseContentOptions<I, F>): P | I | F;
11
- <S extends keyof SlotMap>(id: S, options?: UseContentOptions<never, never>): SlotContent<S>;
12
- <I, S extends keyof SlotMap>(id: S, options?: UseContentOptions<I, never>): SlotContent<S> | I;
13
- <F, S extends keyof SlotMap>(id: S, options?: UseContentOptions<never, F>): SlotContent<S> | F;
14
- <I, F, S extends keyof SlotMap>(id: S, options?: UseContentOptions<I, F>): SlotContent<S> | I | F;
15
- };
16
- export declare const useContent: UseContentHook;
17
- export {};
1
+ import { SlotContent, VersionedSlotId, VersionedSlotMap } from '@croct/plug/slot';
2
+ import { JsonObject } from '@croct/plug/sdk/json';
3
+ import { FetchOptions } from '@croct/plug/plug';
4
+ export type UseContentOptions<I, F> = FetchOptions & {
5
+ fallback?: F;
6
+ initial?: I;
7
+ cacheKey?: string;
8
+ expiration?: number;
9
+ };
10
+ type UseContentHook = {
11
+ <P extends JsonObject, I = P, F = P>(id: keyof VersionedSlotMap extends never ? string : never, options?: UseContentOptions<I, F>): P | I | F;
12
+ <S extends VersionedSlotId>(id: S, options?: UseContentOptions<never, never>): SlotContent<S>;
13
+ <I, S extends VersionedSlotId>(id: S, options?: UseContentOptions<I, never>): SlotContent<S> | I;
14
+ <F, S extends VersionedSlotId>(id: S, options?: UseContentOptions<never, F>): SlotContent<S> | F;
15
+ <I, F, S extends VersionedSlotId>(id: S, options?: UseContentOptions<I, F>): SlotContent<S> | I | F;
16
+ };
17
+ export declare const useContent: UseContentHook;
18
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useContent = void 0;
4
+ const useLoader_1 = require("./useLoader");
5
+ const useCroct_1 = require("./useCroct");
6
+ const ssr_polyfills_1 = require("../ssr-polyfills");
7
+ function useCsrContent(id, options = {}) {
8
+ const { fallback, initial, cacheKey, expiration, ...fetchOptions } = options;
9
+ const croct = (0, useCroct_1.useCroct)();
10
+ return (0, useLoader_1.useLoader)({
11
+ cacheKey: `useContent:${cacheKey !== null && cacheKey !== void 0 ? cacheKey : ''}:${id}`,
12
+ loader: () => croct.fetch(id, fetchOptions).then(({ content }) => content),
13
+ initial: initial,
14
+ fallback: fallback,
15
+ expiration: expiration,
16
+ });
17
+ }
18
+ function useSsrContent(_, { initial } = {}) {
19
+ if (initial === undefined) {
20
+ throw new Error('The initial value is required for server-side rendering (SSR).');
21
+ }
22
+ return initial;
23
+ }
24
+ exports.useContent = (0, ssr_polyfills_1.isSsr)() ? useSsrContent : useCsrContent;
25
+ //# sourceMappingURL=useContent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useContent.js","sourceRoot":"","sources":["../../src/hooks/useContent.ts"],"names":[],"mappings":";;;AAGA,2CAAsC;AACtC,yCAAoC;AACpC,oDAAuC;AASvC,SAAS,aAAa,CAClB,EAAmB,EACnB,UAAmC,EAAE;IAErC,MAAM,EAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,GAAG,YAAY,EAAC,GAAG,OAAO,CAAC;IAC3E,MAAM,KAAK,GAAG,IAAA,mBAAQ,GAAE,CAAC;IAEzB,OAAO,IAAA,qBAAS,EAAC;QACb,QAAQ,EAAE,cAAc,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,EAAE,IAAI,EAAE,EAAE;QAC9C,MAAM,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,EAAE,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,EAAC,OAAO,EAAC,EAAE,EAAE,CAAC,OAAO,CAAC;QACxE,OAAO,EAAE,OAAO;QAChB,QAAQ,EAAE,QAAQ;QAClB,UAAU,EAAE,UAAU;KACzB,CAAC,CAAC;AACP,CAAC;AAED,SAAS,aAAa,CAClB,CAAkB,EAClB,EAAC,OAAO,KAA6B,EAAE;IAEvC,IAAI,OAAO,KAAK,SAAS,EAAE;QACvB,MAAM,IAAI,KAAK,CAAC,gEAAgE,CAAC,CAAC;KACrF;IAED,OAAO,OAAO,CAAC;AACnB,CAAC;AA6BY,QAAA,UAAU,GAAmB,IAAA,qBAAK,GAAE,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,aAAa,CAAC"}
@@ -1,2 +1,2 @@
1
- import { Plug } from '@croct/plug';
2
- export declare function useCroct(): Plug;
1
+ import { Plug } from '@croct/plug';
2
+ export declare function useCroct(): Plug;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useCroct = void 0;
4
+ const react_1 = require("react");
5
+ const CroctProvider_1 = require("../CroctProvider");
6
+ function useCroct() {
7
+ const context = (0, react_1.useContext)(CroctProvider_1.CroctContext);
8
+ if (context === null) {
9
+ throw new Error('useCroct() can only be used in the context of a <CroctProvider> component.');
10
+ }
11
+ return context.plug;
12
+ }
13
+ exports.useCroct = useCroct;
14
+ //# sourceMappingURL=useCroct.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useCroct.js","sourceRoot":"","sources":["../../src/hooks/useCroct.ts"],"names":[],"mappings":";;;AACA,iCAAiC;AACjC,oDAA8C;AAE9C,SAAgB,QAAQ;IACpB,MAAM,OAAO,GAAG,IAAA,kBAAU,EAAC,4BAAY,CAAC,CAAC;IAEzC,IAAI,OAAO,KAAK,IAAI,EAAE;QAClB,MAAM,IAAI,KAAK,CAAC,4EAA4E,CAAC,CAAC;KACjG;IAED,OAAO,OAAO,CAAC,IAAI,CAAC;AACxB,CAAC;AARD,4BAQC"}
@@ -1,11 +1,11 @@
1
- import { JsonValue } from '@croct/plug/sdk/json';
2
- import { EvaluationOptions } from '@croct/sdk/facade/evaluatorFacade';
3
- export declare type UseEvaluationOptions<I, F> = EvaluationOptions & {
4
- initial?: I;
5
- fallback?: F;
6
- cacheKey?: string;
7
- expiration?: number;
8
- };
9
- declare type UseEvaluationHook = <T extends JsonValue, I = T, F = T>(expression: string, options?: UseEvaluationOptions<I, F>) => T | I | F;
10
- export declare const useEvaluation: UseEvaluationHook;
11
- export {};
1
+ import { JsonValue } from '@croct/plug/sdk/json';
2
+ import { EvaluationOptions } from '@croct/sdk/facade/evaluatorFacade';
3
+ export type UseEvaluationOptions<I, F> = EvaluationOptions & {
4
+ initial?: I;
5
+ fallback?: F;
6
+ cacheKey?: string;
7
+ expiration?: number;
8
+ };
9
+ type UseEvaluationHook = <T extends JsonValue, I = T, F = T>(query: string, options?: UseEvaluationOptions<I, F>) => T | I | F;
10
+ export declare const useEvaluation: UseEvaluationHook;
11
+ export {};
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useEvaluation = void 0;
4
+ const useLoader_1 = require("./useLoader");
5
+ const useCroct_1 = require("./useCroct");
6
+ const ssr_polyfills_1 = require("../ssr-polyfills");
7
+ function cleanEvaluationOptions(options) {
8
+ const result = {};
9
+ for (const [key, value] of Object.entries(options)) {
10
+ if (value !== undefined) {
11
+ result[key] = value;
12
+ }
13
+ }
14
+ return result;
15
+ }
16
+ function useCsrEvaluation(query, options = {}) {
17
+ var _a;
18
+ const { cacheKey, fallback, initial, expiration, ...evaluationOptions } = options;
19
+ const croct = (0, useCroct_1.useCroct)();
20
+ return (0, useLoader_1.useLoader)({
21
+ cacheKey: `useEvaluation:${cacheKey !== null && cacheKey !== void 0 ? cacheKey : ''}:${query}:${JSON.stringify((_a = options.attributes) !== null && _a !== void 0 ? _a : '')}`,
22
+ loader: () => croct.evaluate(query, cleanEvaluationOptions(evaluationOptions)),
23
+ initial: initial,
24
+ fallback: fallback,
25
+ expiration: expiration,
26
+ });
27
+ }
28
+ function useSsrEvaluation(_, { initial } = {}) {
29
+ if (initial === undefined) {
30
+ throw new Error('The initial value is required for server-side rendering (SSR).');
31
+ }
32
+ return initial;
33
+ }
34
+ exports.useEvaluation = (0, ssr_polyfills_1.isSsr)() ? useSsrEvaluation : useCsrEvaluation;
35
+ //# sourceMappingURL=useEvaluation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useEvaluation.js","sourceRoot":"","sources":["../../src/hooks/useEvaluation.ts"],"names":[],"mappings":";;;AAEA,2CAAsC;AACtC,yCAAoC;AACpC,oDAAuC;AAEvC,SAAS,sBAAsB,CAAC,OAA0B;IACtD,MAAM,MAAM,GAAsB,EAAE,CAAC;IAErC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QAChD,IAAI,KAAK,KAAK,SAAS,EAAE;YACrB,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;SACvB;KACJ;IAED,OAAO,MAAM,CAAC;AAClB,CAAC;AAcD,SAAS,gBAAgB,CACrB,KAAa,EACb,UAAsC,EAAE;;IAExC,MAAM,EAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,iBAAiB,EAAC,GAAG,OAAO,CAAC;IAChF,MAAM,KAAK,GAAG,IAAA,mBAAQ,GAAE,CAAC;IAEzB,OAAO,IAAA,qBAAS,EAAY;QACxB,QAAQ,EAAE,iBAAiB,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,EAAE,IAAI,KAAK,IAAI,IAAI,CAAC,SAAS,CAAC,MAAA,OAAO,CAAC,UAAU,mCAAI,EAAE,CAAC,EAAE;QAChG,MAAM,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAgB,KAAK,EAAE,sBAAsB,CAAC,iBAAiB,CAAC,CAAC;QAC7F,OAAO,EAAE,OAAO;QAChB,QAAQ,EAAE,QAAQ;QAClB,UAAU,EAAE,UAAU;KACzB,CAAC,CAAC;AACP,CAAC;AAED,SAAS,gBAAgB,CACrB,CAAS,EACT,EAAC,OAAO,KAAgC,EAAE;IAE1C,IAAI,OAAO,KAAK,SAAS,EAAE;QACvB,MAAM,IAAI,KAAK,CAAC,gEAAgE,CAAC,CAAC;KACrF;IAED,OAAO,OAAO,CAAC;AACnB,CAAC;AAEY,QAAA,aAAa,GAAsB,IAAA,qBAAK,GAAE,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,gBAAgB,CAAC"}
@@ -1,5 +1,5 @@
1
- import { EntryOptions } from './Cache';
2
- export declare type CacheOptions<R> = EntryOptions<R> & {
3
- initial?: R;
4
- };
5
- export declare function useLoader<R>({ initial, ...options }: CacheOptions<R>): R;
1
+ import { EntryOptions } from './Cache';
2
+ export type CacheOptions<R> = EntryOptions<R> & {
3
+ initial?: R;
4
+ };
5
+ export declare function useLoader<R>({ initial, ...options }: CacheOptions<R>): R;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useLoader = void 0;
4
+ const react_1 = require("react");
5
+ const Cache_1 = require("./Cache");
6
+ const cache = new Cache_1.Cache(60 * 1000);
7
+ function useLoader({ initial, ...options }) {
8
+ var _a;
9
+ const loadedValue = (_a = cache.get(options.cacheKey)) === null || _a === void 0 ? void 0 : _a.result;
10
+ const [value, setValue] = (0, react_1.useState)(loadedValue !== undefined ? loadedValue : initial);
11
+ const mountedRef = (0, react_1.useRef)(true);
12
+ const optionsRef = (0, react_1.useRef)(initial !== undefined ? options : undefined);
13
+ (0, react_1.useEffect)(() => {
14
+ if (optionsRef.current !== undefined) {
15
+ try {
16
+ setValue(cache.load(optionsRef.current));
17
+ }
18
+ catch (result) {
19
+ if (result instanceof Promise) {
20
+ result.then((resolvedValue) => {
21
+ if (mountedRef.current) {
22
+ setValue(resolvedValue);
23
+ }
24
+ });
25
+ return;
26
+ }
27
+ setValue(undefined);
28
+ return;
29
+ }
30
+ }
31
+ return () => {
32
+ mountedRef.current = false;
33
+ };
34
+ }, []);
35
+ if (value === undefined) {
36
+ return cache.load(options);
37
+ }
38
+ return value;
39
+ }
40
+ exports.useLoader = useLoader;
41
+ //# sourceMappingURL=useLoader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useLoader.js","sourceRoot":"","sources":["../../src/hooks/useLoader.ts"],"names":[],"mappings":";;;AAAA,iCAAkD;AAClD,mCAA4C;AAE5C,MAAM,KAAK,GAAG,IAAI,aAAK,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC;AAMnC,SAAgB,SAAS,CAAI,EAAC,OAAO,EAAE,GAAG,OAAO,EAAkB;;IAC/D,MAAM,WAAW,GAAgB,MAAA,KAAK,CAAC,GAAG,CAAI,OAAO,CAAC,QAAQ,CAAC,0CAAE,MAAM,CAAC;IACxE,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,IAAA,gBAAQ,EAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;IACtF,MAAM,UAAU,GAAG,IAAA,cAAM,EAAC,IAAI,CAAC,CAAC;IAChC,MAAM,UAAU,GAAG,IAAA,cAAM,EAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IAEvE,IAAA,iBAAS,EACL,GAAG,EAAE;QACD,IAAI,UAAU,CAAC,OAAO,KAAK,SAAS,EAAE;YAClC,IAAI;gBACA,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC;aAC5C;YAAC,OAAO,MAAe,EAAE;gBACtB,IAAI,MAAM,YAAY,OAAO,EAAE;oBAC3B,MAAM,CAAC,IAAI,CAAC,CAAC,aAAgB,EAAE,EAAE;wBAC7B,IAAI,UAAU,CAAC,OAAO,EAAE;4BACpB,QAAQ,CAAC,aAAa,CAAC,CAAC;yBAC3B;oBACL,CAAC,CAAC,CAAC;oBAEH,OAAO;iBACV;gBAED,QAAQ,CAAC,SAAS,CAAC,CAAC;gBAEpB,OAAO;aACV;SACJ;QAED,OAAO,GAAG,EAAE;YACR,UAAU,CAAC,OAAO,GAAG,KAAK,CAAC;QAC/B,CAAC,CAAC;IACN,CAAC,EACD,EAAE,CACL,CAAC;IAEF,IAAI,KAAK,KAAK,SAAS,EAAE;QACrB,OAAO,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;KAC9B;IAED,OAAO,KAAK,CAAC;AACjB,CAAC;AAxCD,8BAwCC"}
package/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
- export * from './CroctProvider';
2
- export * from './hooks';
3
- export * from './components';
1
+ export * from '@croct/plug/sdk/json';
2
+ export * from '@croct/plug/slot';
3
+ export * from './CroctProvider';
4
+ export * from './hooks';
5
+ export * from './components';