@katlux/providers 0.1.1-alpha.95 → 0.1.1-alpha.97

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.
@@ -0,0 +1,241 @@
1
+ 'use strict';
2
+
3
+ class DefaultHydrationAdapter {
4
+ getSSRData(key, context) {
5
+ const contextData = context?.payload?.data?.[key] ?? context?.ssrContext?.payload?.data?.[key];
6
+ if (contextData !== void 0 && contextData !== null) {
7
+ return contextData;
8
+ }
9
+ if (typeof window !== "undefined") {
10
+ const win = window;
11
+ const clientData = win.__KATLUX_DATA__?.[key] ?? win.__INITIAL_DATA__?.[key] ?? win.__NEXT_DATA__?.props?.pageProps?.[key] ?? win.__NUXT__?.payload?.data?.[key];
12
+ if (clientData !== void 0 && clientData !== null) {
13
+ return clientData;
14
+ }
15
+ }
16
+ return null;
17
+ }
18
+ async fetchWithSSR(key, fetcher, context) {
19
+ const cached = this.getSSRData(key, context);
20
+ if (cached !== null) {
21
+ return cached;
22
+ }
23
+ try {
24
+ const result = await fetcher();
25
+ if (context?.payload?.data) {
26
+ context.payload.data[key] = result;
27
+ } else if (context?.ssrContext?.payload?.data) {
28
+ context.ssrContext.payload.data[key] = result;
29
+ }
30
+ return result;
31
+ } catch {
32
+ return null;
33
+ }
34
+ }
35
+ clearSSRData(key, context) {
36
+ if (context?.payload?.data) {
37
+ delete context.payload.data[key];
38
+ }
39
+ if (context?.ssrContext?.payload?.data) {
40
+ delete context.ssrContext.payload.data[key];
41
+ }
42
+ if (typeof window !== "undefined") {
43
+ const win = window;
44
+ if (win.__KATLUX_DATA__?.payload?.data) delete win.__KATLUX_DATA__.payload.data[key];
45
+ if (win.__INITIAL_DATA__?.payload?.data) delete win.__INITIAL_DATA__.payload.data[key];
46
+ if (win.__NUXT__?.payload?.data) delete win.__NUXT__.payload.data[key];
47
+ }
48
+ }
49
+ getSSRPayload(context) {
50
+ if (context?.payload?.data) return context.payload.data;
51
+ if (context?.ssrContext?.payload?.data) return context.ssrContext.payload.data;
52
+ if (typeof window !== "undefined") {
53
+ const win = window;
54
+ return win.__KATLUX_DATA__?.payload?.data || win.__KATLUX_DATA__ || win.__INITIAL_DATA__?.payload?.data || win.__INITIAL_DATA__ || win.__NUXT__?.payload?.data || null;
55
+ }
56
+ return null;
57
+ }
58
+ }
59
+
60
+ function getGlobalNuxt() {
61
+ if (typeof globalThis.useNuxtApp === "function") {
62
+ try {
63
+ return globalThis.useNuxtApp();
64
+ } catch {
65
+ return null;
66
+ }
67
+ }
68
+ return null;
69
+ }
70
+ class NuxtHydrationAdapter {
71
+ fallback = new DefaultHydrationAdapter();
72
+ getSSRData(key, context) {
73
+ const app = context || getGlobalNuxt();
74
+ if (app?.runWithContext && typeof globalThis.useNuxtData === "function") {
75
+ try {
76
+ const res = app.runWithContext(() => globalThis.useNuxtData(key));
77
+ const val = res?.data?.value ?? res?.data ?? res;
78
+ if (val !== void 0 && val !== null) return val;
79
+ } catch {
80
+ }
81
+ } else if (typeof globalThis.useNuxtData === "function") {
82
+ try {
83
+ const res = globalThis.useNuxtData(key);
84
+ const val = res?.data?.value ?? res?.data ?? res;
85
+ if (val !== void 0 && val !== null) return val;
86
+ } catch {
87
+ }
88
+ }
89
+ return this.fallback.getSSRData(key, context);
90
+ }
91
+ async fetchWithSSR(key, fetcher, context) {
92
+ const app = context || getGlobalNuxt();
93
+ if (app?.runWithContext && typeof globalThis.useAsyncData === "function") {
94
+ try {
95
+ const res = await app.runWithContext(() => globalThis.useAsyncData(key, fetcher));
96
+ const val = res?.data?.value ?? res?.data ?? res;
97
+ if (val !== void 0 && val !== null) return val;
98
+ } catch {
99
+ }
100
+ } else if (typeof globalThis.useAsyncData === "function") {
101
+ try {
102
+ const res = await globalThis.useAsyncData(key, fetcher);
103
+ const val = res?.data?.value ?? res?.data ?? res;
104
+ if (val !== void 0 && val !== null) return val;
105
+ } catch {
106
+ }
107
+ }
108
+ return this.fallback.fetchWithSSR(key, fetcher, context);
109
+ }
110
+ clearSSRData(key, context) {
111
+ const app = context || getGlobalNuxt();
112
+ if (app?.runWithContext && typeof globalThis.clearNuxtData === "function") {
113
+ try {
114
+ app.runWithContext(() => globalThis.clearNuxtData(key));
115
+ } catch {
116
+ }
117
+ } else if (typeof globalThis.clearNuxtData === "function") {
118
+ try {
119
+ globalThis.clearNuxtData(key);
120
+ } catch {
121
+ }
122
+ }
123
+ this.fallback.clearSSRData(key, context);
124
+ }
125
+ getSSRPayload(context) {
126
+ const app = context || getGlobalNuxt();
127
+ const payload = app?.payload?.data || app?.ssrContext?.payload?.data;
128
+ if (payload) return payload;
129
+ return this.fallback.getSSRPayload(context);
130
+ }
131
+ }
132
+
133
+ let currentAdapter = null;
134
+ function getHydrationAdapter() {
135
+ if (!currentAdapter) {
136
+ if (typeof globalThis.useNuxtApp === "function") {
137
+ currentAdapter = new NuxtHydrationAdapter();
138
+ } else {
139
+ currentAdapter = new DefaultHydrationAdapter();
140
+ }
141
+ }
142
+ return currentAdapter;
143
+ }
144
+ function setHydrationAdapter(adapter) {
145
+ currentAdapter = adapter;
146
+ }
147
+ function getSSRData(key, context) {
148
+ return getHydrationAdapter().getSSRData(key, context);
149
+ }
150
+ async function fetchWithSSR(key, fetcher, context) {
151
+ return await getHydrationAdapter().fetchWithSSR(key, fetcher, context);
152
+ }
153
+ function clearSSRData(key, context) {
154
+ getHydrationAdapter().clearSSRData?.(key, context);
155
+ }
156
+ function getSSRPayload(context) {
157
+ return getHydrationAdapter().getSSRPayload?.(context) ?? null;
158
+ }
159
+ function getPlatformApp() {
160
+ if (typeof globalThis.useNuxtApp === "function") {
161
+ try {
162
+ return globalThis.useNuxtApp();
163
+ } catch {
164
+ return null;
165
+ }
166
+ }
167
+ return null;
168
+ }
169
+ function getAppRoute() {
170
+ if (typeof globalThis.useRoute === "function") {
171
+ try {
172
+ return globalThis.useRoute();
173
+ } catch {
174
+ }
175
+ }
176
+ if (typeof window !== "undefined") {
177
+ const searchParams = new URLSearchParams(window.location.search);
178
+ const query = {};
179
+ searchParams.forEach((val, key) => {
180
+ query[key] = val;
181
+ });
182
+ return { query, path: window.location.pathname };
183
+ }
184
+ return null;
185
+ }
186
+ function getAppRouter() {
187
+ if (typeof globalThis.useRouter === "function") {
188
+ try {
189
+ return globalThis.useRouter();
190
+ } catch {
191
+ }
192
+ }
193
+ if (typeof window !== "undefined") {
194
+ return {
195
+ replace: (options) => {
196
+ const url = new URL(window.location.href);
197
+ url.search = "";
198
+ if (options.query) {
199
+ for (const [k, v] of Object.entries(options.query)) {
200
+ if (v !== void 0 && v !== null) {
201
+ url.searchParams.set(k, String(v));
202
+ }
203
+ }
204
+ }
205
+ window.history.replaceState({}, "", url.toString());
206
+ }
207
+ };
208
+ }
209
+ return null;
210
+ }
211
+ function onPlatformReady(cb) {
212
+ if (typeof globalThis.onNuxtReady === "function") {
213
+ try {
214
+ globalThis.onNuxtReady(cb);
215
+ return;
216
+ } catch {
217
+ }
218
+ }
219
+ if (typeof window !== "undefined") {
220
+ if (document.readyState === "complete") {
221
+ setTimeout(cb, 0);
222
+ } else {
223
+ window.addEventListener("DOMContentLoaded", cb, { once: true });
224
+ }
225
+ } else {
226
+ setTimeout(cb, 0);
227
+ }
228
+ }
229
+
230
+ exports.DefaultHydrationAdapter = DefaultHydrationAdapter;
231
+ exports.NuxtHydrationAdapter = NuxtHydrationAdapter;
232
+ exports.clearSSRData = clearSSRData;
233
+ exports.fetchWithSSR = fetchWithSSR;
234
+ exports.getAppRoute = getAppRoute;
235
+ exports.getAppRouter = getAppRouter;
236
+ exports.getHydrationAdapter = getHydrationAdapter;
237
+ exports.getPlatformApp = getPlatformApp;
238
+ exports.getSSRData = getSSRData;
239
+ exports.getSSRPayload = getSSRPayload;
240
+ exports.onPlatformReady = onPlatformReady;
241
+ exports.setHydrationAdapter = setHydrationAdapter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@katlux/providers",
3
- "version": "0.1.1-alpha.95",
3
+ "version": "0.1.1-alpha.97",
4
4
  "description": "Core data calculation and caching providers for the Katlux ecosystem",
5
5
  "author": "Katlux",
6
6
  "license": "Apache-2.0",
@@ -31,8 +31,8 @@
31
31
  "require": "./dist/nuxtAppCompat.server.cjs"
32
32
  },
33
33
  "node": {
34
- "import": "./dist/nuxtAppCompat.server.mjs",
35
- "require": "./dist/nuxtAppCompat.server.cjs"
34
+ "import": "./dist/nuxtAppCompat.mjs",
35
+ "require": "./dist/nuxtAppCompat.cjs"
36
36
  },
37
37
  "default": {
38
38
  "import": "./dist/nuxtAppCompat.mjs",
@@ -40,7 +40,10 @@
40
40
  }
41
41
  }
42
42
  },
43
- "dependencies": {},
43
+ "dependencies": {
44
+ "h3": "^1.15.0",
45
+ "ofetch": "^1.4.0"
46
+ },
44
47
  "files": [
45
48
  "dist"
46
49
  ]