@crafter8/sdk 0.1.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.
- package/LICENSE +21 -0
- package/README.md +175 -0
- package/build.js +553 -0
- package/index.d.ts +473 -0
- package/index.js +1831 -0
- package/mock.d.ts +128 -0
- package/mock.js +471 -0
- package/package.json +61 -0
- package/react.d.ts +30 -0
- package/react.js +124 -0
package/react.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
function isPlainObject(value) {
|
|
2
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function isContextValue(value) {
|
|
6
|
+
return (
|
|
7
|
+
isPlainObject(value) &&
|
|
8
|
+
isPlainObject(value.appContext) &&
|
|
9
|
+
isPlainObject(value.client) &&
|
|
10
|
+
isPlainObject(value.artifact)
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function isAppContextLike(value) {
|
|
15
|
+
return isPlainObject(value) && isPlainObject(value.client) && isPlainObject(value.artifact);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function normalizeContextValue(input) {
|
|
19
|
+
const value = isContextValue(input)
|
|
20
|
+
? input
|
|
21
|
+
: isAppContextLike(input)
|
|
22
|
+
? {
|
|
23
|
+
appContext: input,
|
|
24
|
+
client: input.client,
|
|
25
|
+
host: input.host,
|
|
26
|
+
runtime: input.runtime,
|
|
27
|
+
artifact: input.artifact,
|
|
28
|
+
}
|
|
29
|
+
: null;
|
|
30
|
+
|
|
31
|
+
if (!value) {
|
|
32
|
+
throw new TypeError("Crafter8Provider requires an appContext or Crafter8 context value.");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (typeof value.client.session?.get !== "function") {
|
|
36
|
+
throw new TypeError("Crafter8Provider requires a valid Crafter8Client.");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!value.artifact || typeof value.artifact.id !== "string") {
|
|
40
|
+
throw new TypeError("Crafter8Provider requires artifact metadata.");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return Object.freeze({
|
|
44
|
+
appContext: value.appContext,
|
|
45
|
+
client: value.client,
|
|
46
|
+
...(value.host ? { host: value.host } : {}),
|
|
47
|
+
...(value.runtime ? { runtime: value.runtime } : {}),
|
|
48
|
+
artifact: value.artifact,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function assertReactLike(reactLike) {
|
|
53
|
+
if (
|
|
54
|
+
!reactLike ||
|
|
55
|
+
typeof reactLike.createContext !== "function" ||
|
|
56
|
+
typeof reactLike.createElement !== "function" ||
|
|
57
|
+
typeof reactLike.useContext !== "function" ||
|
|
58
|
+
typeof reactLike.useMemo !== "function"
|
|
59
|
+
) {
|
|
60
|
+
throw new TypeError("createCrafter8ReactBindings requires a React-like object.");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const REACT_BINDINGS_CACHE = new WeakMap();
|
|
65
|
+
|
|
66
|
+
export function createCrafter8ReactBindings(React) {
|
|
67
|
+
assertReactLike(React);
|
|
68
|
+
|
|
69
|
+
const cached = REACT_BINDINGS_CACHE.get(React);
|
|
70
|
+
if (cached) {
|
|
71
|
+
return cached;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const Crafter8ReactContext = React.createContext(null);
|
|
75
|
+
|
|
76
|
+
function useCrafter8() {
|
|
77
|
+
const context = React.useContext(Crafter8ReactContext);
|
|
78
|
+
if (!context) {
|
|
79
|
+
throw new Error("Crafter8 React context is missing. Wrap the app with <Crafter8Provider>.");
|
|
80
|
+
}
|
|
81
|
+
return context;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function Crafter8Provider({ appContext, value, children }) {
|
|
85
|
+
const normalizedValue = React.useMemo(
|
|
86
|
+
() => normalizeContextValue(appContext ?? value),
|
|
87
|
+
[appContext, value],
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
return React.createElement(Crafter8ReactContext.Provider, { value: normalizedValue }, children);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function useCrafter8AppContext() {
|
|
94
|
+
return useCrafter8().appContext;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function useCrafter8Client() {
|
|
98
|
+
return useCrafter8().client;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function useCrafter8Host() {
|
|
102
|
+
return useCrafter8().host;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function useCrafter8Artifact() {
|
|
106
|
+
return useCrafter8().artifact;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function useCrafter8Runtime() {
|
|
110
|
+
return useCrafter8().runtime;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const bindings = Object.freeze({
|
|
114
|
+
Crafter8Provider,
|
|
115
|
+
useCrafter8,
|
|
116
|
+
useCrafter8AppContext,
|
|
117
|
+
useCrafter8Client,
|
|
118
|
+
useCrafter8Host,
|
|
119
|
+
useCrafter8Artifact,
|
|
120
|
+
useCrafter8Runtime,
|
|
121
|
+
});
|
|
122
|
+
REACT_BINDINGS_CACHE.set(React, bindings);
|
|
123
|
+
return bindings;
|
|
124
|
+
}
|