@spoosh/plugin-initial-data 0.2.5 → 0.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 +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +17 -15
- package/dist/index.mjs +17 -15
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ npm install @spoosh/plugin-initial-data
|
|
|
16
16
|
import { Spoosh } from "@spoosh/core";
|
|
17
17
|
import { initialDataPlugin } from "@spoosh/plugin-initial-data";
|
|
18
18
|
|
|
19
|
-
const
|
|
19
|
+
const spoosh = new Spoosh<ApiSchema, Error>("/api").use([initialDataPlugin()]);
|
|
20
20
|
|
|
21
21
|
const { data, isInitialData } = useRead((api) => api("posts").GET(), {
|
|
22
22
|
initialData: prefetchedPosts,
|
package/dist/index.d.mts
CHANGED
|
@@ -33,7 +33,7 @@ declare module "@spoosh/core" {
|
|
|
33
33
|
* ```ts
|
|
34
34
|
* import { Spoosh } from "@spoosh/core";
|
|
35
35
|
*
|
|
36
|
-
* const
|
|
36
|
+
* const spoosh = new Spoosh<ApiSchema, Error>("/api")
|
|
37
37
|
* .use([
|
|
38
38
|
* initialDataPlugin(),
|
|
39
39
|
* cachePlugin({ staleTime: 5000 }),
|
package/dist/index.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ declare module "@spoosh/core" {
|
|
|
33
33
|
* ```ts
|
|
34
34
|
* import { Spoosh } from "@spoosh/core";
|
|
35
35
|
*
|
|
36
|
-
* const
|
|
36
|
+
* const spoosh = new Spoosh<ApiSchema, Error>("/api")
|
|
37
37
|
* .use([
|
|
38
38
|
* initialDataPlugin(),
|
|
39
39
|
* cachePlugin({ staleTime: 5000 }),
|
package/dist/index.js
CHANGED
|
@@ -25,30 +25,21 @@ __export(src_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(src_exports);
|
|
26
26
|
|
|
27
27
|
// src/plugin.ts
|
|
28
|
+
var PLUGIN_NAME = "spoosh:initialData";
|
|
28
29
|
function initialDataPlugin() {
|
|
29
30
|
const initialDataAppliedFor = /* @__PURE__ */ new Set();
|
|
30
31
|
return {
|
|
31
|
-
name:
|
|
32
|
+
name: PLUGIN_NAME,
|
|
32
33
|
operations: ["read", "infiniteRead"],
|
|
33
34
|
middleware: async (context, next) => {
|
|
35
|
+
const t = context.tracer?.(PLUGIN_NAME);
|
|
34
36
|
const pluginOptions = context.pluginOptions;
|
|
35
37
|
if (pluginOptions?.initialData === void 0) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
context.stateManager.setMeta(context.queryKey, {
|
|
39
|
-
isInitialData: false
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
return response2;
|
|
38
|
+
t?.skip("No initial data", { color: "muted" });
|
|
39
|
+
return next();
|
|
43
40
|
}
|
|
44
41
|
if (!context.instanceId) {
|
|
45
|
-
|
|
46
|
-
if (!response2.error) {
|
|
47
|
-
context.stateManager.setMeta(context.queryKey, {
|
|
48
|
-
isInitialData: false
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
return response2;
|
|
42
|
+
return next();
|
|
52
43
|
}
|
|
53
44
|
if (initialDataAppliedFor.has(context.instanceId)) {
|
|
54
45
|
const response2 = await next();
|
|
@@ -61,6 +52,7 @@ function initialDataPlugin() {
|
|
|
61
52
|
}
|
|
62
53
|
const cached = context.stateManager.getCache(context.queryKey);
|
|
63
54
|
if (cached?.state?.data !== void 0) {
|
|
55
|
+
t?.skip("Cache exists", { color: "muted" });
|
|
64
56
|
initialDataAppliedFor.add(context.instanceId);
|
|
65
57
|
const response2 = await next();
|
|
66
58
|
if (!response2.error) {
|
|
@@ -71,6 +63,14 @@ function initialDataPlugin() {
|
|
|
71
63
|
return response2;
|
|
72
64
|
}
|
|
73
65
|
initialDataAppliedFor.add(context.instanceId);
|
|
66
|
+
t?.log("Applied initial data", {
|
|
67
|
+
color: "success",
|
|
68
|
+
diff: {
|
|
69
|
+
before: void 0,
|
|
70
|
+
after: pluginOptions.initialData,
|
|
71
|
+
label: "Set initial data to cache"
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
74
|
context.stateManager.setCache(context.queryKey, {
|
|
75
75
|
state: {
|
|
76
76
|
data: pluginOptions.initialData,
|
|
@@ -83,8 +83,10 @@ function initialDataPlugin() {
|
|
|
83
83
|
isInitialData: true
|
|
84
84
|
});
|
|
85
85
|
if (pluginOptions.refetchOnInitialData === false) {
|
|
86
|
+
t?.return("Skip refetch", { color: "muted" });
|
|
86
87
|
return { data: pluginOptions.initialData, status: 200 };
|
|
87
88
|
}
|
|
89
|
+
t?.log("Background refetch", { color: "info" });
|
|
88
90
|
const response = await next();
|
|
89
91
|
if (!response.error) {
|
|
90
92
|
context.stateManager.setMeta(context.queryKey, {
|
package/dist/index.mjs
CHANGED
|
@@ -1,28 +1,19 @@
|
|
|
1
1
|
// src/plugin.ts
|
|
2
|
+
var PLUGIN_NAME = "spoosh:initialData";
|
|
2
3
|
function initialDataPlugin() {
|
|
3
4
|
const initialDataAppliedFor = /* @__PURE__ */ new Set();
|
|
4
5
|
return {
|
|
5
|
-
name:
|
|
6
|
+
name: PLUGIN_NAME,
|
|
6
7
|
operations: ["read", "infiniteRead"],
|
|
7
8
|
middleware: async (context, next) => {
|
|
9
|
+
const t = context.tracer?.(PLUGIN_NAME);
|
|
8
10
|
const pluginOptions = context.pluginOptions;
|
|
9
11
|
if (pluginOptions?.initialData === void 0) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
context.stateManager.setMeta(context.queryKey, {
|
|
13
|
-
isInitialData: false
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
return response2;
|
|
12
|
+
t?.skip("No initial data", { color: "muted" });
|
|
13
|
+
return next();
|
|
17
14
|
}
|
|
18
15
|
if (!context.instanceId) {
|
|
19
|
-
|
|
20
|
-
if (!response2.error) {
|
|
21
|
-
context.stateManager.setMeta(context.queryKey, {
|
|
22
|
-
isInitialData: false
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
return response2;
|
|
16
|
+
return next();
|
|
26
17
|
}
|
|
27
18
|
if (initialDataAppliedFor.has(context.instanceId)) {
|
|
28
19
|
const response2 = await next();
|
|
@@ -35,6 +26,7 @@ function initialDataPlugin() {
|
|
|
35
26
|
}
|
|
36
27
|
const cached = context.stateManager.getCache(context.queryKey);
|
|
37
28
|
if (cached?.state?.data !== void 0) {
|
|
29
|
+
t?.skip("Cache exists", { color: "muted" });
|
|
38
30
|
initialDataAppliedFor.add(context.instanceId);
|
|
39
31
|
const response2 = await next();
|
|
40
32
|
if (!response2.error) {
|
|
@@ -45,6 +37,14 @@ function initialDataPlugin() {
|
|
|
45
37
|
return response2;
|
|
46
38
|
}
|
|
47
39
|
initialDataAppliedFor.add(context.instanceId);
|
|
40
|
+
t?.log("Applied initial data", {
|
|
41
|
+
color: "success",
|
|
42
|
+
diff: {
|
|
43
|
+
before: void 0,
|
|
44
|
+
after: pluginOptions.initialData,
|
|
45
|
+
label: "Set initial data to cache"
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
48
|
context.stateManager.setCache(context.queryKey, {
|
|
49
49
|
state: {
|
|
50
50
|
data: pluginOptions.initialData,
|
|
@@ -57,8 +57,10 @@ function initialDataPlugin() {
|
|
|
57
57
|
isInitialData: true
|
|
58
58
|
});
|
|
59
59
|
if (pluginOptions.refetchOnInitialData === false) {
|
|
60
|
+
t?.return("Skip refetch", { color: "muted" });
|
|
60
61
|
return { data: pluginOptions.initialData, status: 200 };
|
|
61
62
|
}
|
|
63
|
+
t?.log("Background refetch", { color: "info" });
|
|
62
64
|
const response = await next();
|
|
63
65
|
if (!response.error) {
|
|
64
66
|
context.stateManager.setMeta(context.queryKey, {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spoosh/plugin-initial-data",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Initial data plugin for Spoosh - show data immediately before fetch completes",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@spoosh/core": ">=0.
|
|
36
|
+
"@spoosh/core": ">=0.13.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@spoosh/core": "0.
|
|
40
|
-
"@spoosh/test-utils": "0.
|
|
39
|
+
"@spoosh/core": "0.13.0",
|
|
40
|
+
"@spoosh/test-utils": "0.2.0"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"dev": "tsup --watch",
|