@spoosh/plugin-initial-data 0.1.0-beta.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/LICENSE +21 -0
- package/README.md +45 -0
- package/dist/index.d.mts +60 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +104 -0
- package/dist/index.mjs +81 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Spoosh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# @spoosh/plugin-initial-data
|
|
2
|
+
|
|
3
|
+
Initial data plugin for Spoosh - show data immediately before fetch completes.
|
|
4
|
+
|
|
5
|
+
**[Documentation](https://spoosh.dev/docs/plugins/initial-data)** · **Requirements:** TypeScript >= 5.0 · **Peer Dependencies:** `@spoosh/core`
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @spoosh/plugin-initial-data
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { initialDataPlugin } from "@spoosh/plugin-initial-data";
|
|
17
|
+
|
|
18
|
+
const plugins = [initialDataPlugin()];
|
|
19
|
+
|
|
20
|
+
// Show prefetched data immediately, then refetch in background
|
|
21
|
+
const { data, isInitialData } = useRead((api) => api.posts.$get(), {
|
|
22
|
+
initialData: prefetchedPosts,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Show initial data without background refetch
|
|
26
|
+
const { data } = useRead((api) => api.posts.$get(), {
|
|
27
|
+
initialData: prefetchedPosts,
|
|
28
|
+
refetchOnInitialData: false,
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Options
|
|
33
|
+
|
|
34
|
+
### Per-Request Options
|
|
35
|
+
|
|
36
|
+
| Option | Type | Default | Description |
|
|
37
|
+
| ---------------------- | --------- | ------- | --------------------------------------------- |
|
|
38
|
+
| `initialData` | `TData` | - | Data to show immediately on first mount |
|
|
39
|
+
| `refetchOnInitialData` | `boolean` | `true` | Whether to refetch after showing initial data |
|
|
40
|
+
|
|
41
|
+
### Result
|
|
42
|
+
|
|
43
|
+
| Property | Type | Description |
|
|
44
|
+
| --------------- | --------- | ---------------------------------------------------------- |
|
|
45
|
+
| `isInitialData` | `boolean` | `true` if currently showing initial data (not yet fetched) |
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { SpooshPlugin } from '@spoosh/core';
|
|
2
|
+
|
|
3
|
+
interface InitialDataReadOptions<TData = unknown> {
|
|
4
|
+
/** Data to use immediately on first mount (before fetch completes) */
|
|
5
|
+
initialData?: TData;
|
|
6
|
+
/** Refetch fresh data after showing initial data. Default: true */
|
|
7
|
+
refetchOnInitialData?: boolean;
|
|
8
|
+
}
|
|
9
|
+
type InitialDataInfiniteReadOptions<TData = unknown> = InitialDataReadOptions<TData>;
|
|
10
|
+
interface InitialDataReadResult {
|
|
11
|
+
/** True if currently showing initial data (not yet fetched from server) */
|
|
12
|
+
isInitialData: boolean;
|
|
13
|
+
}
|
|
14
|
+
type InitialDataWriteOptions = object;
|
|
15
|
+
type InitialDataWriteResult = object;
|
|
16
|
+
declare module "@spoosh/core" {
|
|
17
|
+
interface PluginResolvers<TContext> {
|
|
18
|
+
initialData: TContext["data"] | undefined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Enables providing initial data for queries.
|
|
24
|
+
*
|
|
25
|
+
* Initial data is shown immediately on first mount, before any fetch completes.
|
|
26
|
+
* By default, a background refetch is triggered to get fresh data.
|
|
27
|
+
*
|
|
28
|
+
* @see {@link https://spoosh.dev/docs/plugins/initial-data | Initial Data Plugin Documentation}
|
|
29
|
+
*
|
|
30
|
+
* @returns Initial data plugin instance
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const plugins = [
|
|
35
|
+
* initialDataPlugin(),
|
|
36
|
+
* cachePlugin({ staleTime: 5000 }),
|
|
37
|
+
* ];
|
|
38
|
+
*
|
|
39
|
+
* // Basic usage - shows initialData, refetches in background
|
|
40
|
+
* const { data, isInitialData } = useRead(
|
|
41
|
+
* (api) => api.posts.$get(),
|
|
42
|
+
* { initialData: prefetchedPosts }
|
|
43
|
+
* );
|
|
44
|
+
*
|
|
45
|
+
* // Disable background refetch
|
|
46
|
+
* const { data } = useRead(
|
|
47
|
+
* (api) => api.posts.$get(),
|
|
48
|
+
* { initialData: prefetchedPosts, refetchOnInitialData: false }
|
|
49
|
+
* );
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function initialDataPlugin(): SpooshPlugin<{
|
|
53
|
+
readOptions: InitialDataReadOptions;
|
|
54
|
+
writeOptions: InitialDataWriteOptions;
|
|
55
|
+
infiniteReadOptions: InitialDataInfiniteReadOptions;
|
|
56
|
+
readResult: InitialDataReadResult;
|
|
57
|
+
writeResult: InitialDataWriteResult;
|
|
58
|
+
}>;
|
|
59
|
+
|
|
60
|
+
export { type InitialDataInfiniteReadOptions, type InitialDataReadOptions, type InitialDataReadResult, type InitialDataWriteOptions, type InitialDataWriteResult, initialDataPlugin };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { SpooshPlugin } from '@spoosh/core';
|
|
2
|
+
|
|
3
|
+
interface InitialDataReadOptions<TData = unknown> {
|
|
4
|
+
/** Data to use immediately on first mount (before fetch completes) */
|
|
5
|
+
initialData?: TData;
|
|
6
|
+
/** Refetch fresh data after showing initial data. Default: true */
|
|
7
|
+
refetchOnInitialData?: boolean;
|
|
8
|
+
}
|
|
9
|
+
type InitialDataInfiniteReadOptions<TData = unknown> = InitialDataReadOptions<TData>;
|
|
10
|
+
interface InitialDataReadResult {
|
|
11
|
+
/** True if currently showing initial data (not yet fetched from server) */
|
|
12
|
+
isInitialData: boolean;
|
|
13
|
+
}
|
|
14
|
+
type InitialDataWriteOptions = object;
|
|
15
|
+
type InitialDataWriteResult = object;
|
|
16
|
+
declare module "@spoosh/core" {
|
|
17
|
+
interface PluginResolvers<TContext> {
|
|
18
|
+
initialData: TContext["data"] | undefined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Enables providing initial data for queries.
|
|
24
|
+
*
|
|
25
|
+
* Initial data is shown immediately on first mount, before any fetch completes.
|
|
26
|
+
* By default, a background refetch is triggered to get fresh data.
|
|
27
|
+
*
|
|
28
|
+
* @see {@link https://spoosh.dev/docs/plugins/initial-data | Initial Data Plugin Documentation}
|
|
29
|
+
*
|
|
30
|
+
* @returns Initial data plugin instance
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const plugins = [
|
|
35
|
+
* initialDataPlugin(),
|
|
36
|
+
* cachePlugin({ staleTime: 5000 }),
|
|
37
|
+
* ];
|
|
38
|
+
*
|
|
39
|
+
* // Basic usage - shows initialData, refetches in background
|
|
40
|
+
* const { data, isInitialData } = useRead(
|
|
41
|
+
* (api) => api.posts.$get(),
|
|
42
|
+
* { initialData: prefetchedPosts }
|
|
43
|
+
* );
|
|
44
|
+
*
|
|
45
|
+
* // Disable background refetch
|
|
46
|
+
* const { data } = useRead(
|
|
47
|
+
* (api) => api.posts.$get(),
|
|
48
|
+
* { initialData: prefetchedPosts, refetchOnInitialData: false }
|
|
49
|
+
* );
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function initialDataPlugin(): SpooshPlugin<{
|
|
53
|
+
readOptions: InitialDataReadOptions;
|
|
54
|
+
writeOptions: InitialDataWriteOptions;
|
|
55
|
+
infiniteReadOptions: InitialDataInfiniteReadOptions;
|
|
56
|
+
readResult: InitialDataReadResult;
|
|
57
|
+
writeResult: InitialDataWriteResult;
|
|
58
|
+
}>;
|
|
59
|
+
|
|
60
|
+
export { type InitialDataInfiniteReadOptions, type InitialDataReadOptions, type InitialDataReadResult, type InitialDataWriteOptions, type InitialDataWriteResult, initialDataPlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
initialDataPlugin: () => initialDataPlugin
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// src/plugin.ts
|
|
28
|
+
function initialDataPlugin() {
|
|
29
|
+
const initialDataAppliedFor = /* @__PURE__ */ new Set();
|
|
30
|
+
return {
|
|
31
|
+
name: "spoosh:initialData",
|
|
32
|
+
operations: ["read", "infiniteRead"],
|
|
33
|
+
middleware: async (context, next) => {
|
|
34
|
+
const pluginOptions = context.pluginOptions;
|
|
35
|
+
if (pluginOptions?.initialData === void 0) {
|
|
36
|
+
const response2 = await next();
|
|
37
|
+
if (!response2.error) {
|
|
38
|
+
context.stateManager.setPluginResult(context.queryKey, {
|
|
39
|
+
isInitialData: false
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return response2;
|
|
43
|
+
}
|
|
44
|
+
if (!context.hookId) {
|
|
45
|
+
const response2 = await next();
|
|
46
|
+
if (!response2.error) {
|
|
47
|
+
context.stateManager.setPluginResult(context.queryKey, {
|
|
48
|
+
isInitialData: false
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return response2;
|
|
52
|
+
}
|
|
53
|
+
if (initialDataAppliedFor.has(context.hookId)) {
|
|
54
|
+
const response2 = await next();
|
|
55
|
+
if (!response2.error) {
|
|
56
|
+
context.stateManager.setPluginResult(context.queryKey, {
|
|
57
|
+
isInitialData: false
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return response2;
|
|
61
|
+
}
|
|
62
|
+
const cached = context.stateManager.getCache(context.queryKey);
|
|
63
|
+
if (cached?.state?.data !== void 0) {
|
|
64
|
+
initialDataAppliedFor.add(context.hookId);
|
|
65
|
+
const response2 = await next();
|
|
66
|
+
if (!response2.error) {
|
|
67
|
+
context.stateManager.setPluginResult(context.queryKey, {
|
|
68
|
+
isInitialData: false
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return response2;
|
|
72
|
+
}
|
|
73
|
+
initialDataAppliedFor.add(context.hookId);
|
|
74
|
+
context.stateManager.setCache(context.queryKey, {
|
|
75
|
+
state: {
|
|
76
|
+
data: pluginOptions.initialData,
|
|
77
|
+
error: void 0,
|
|
78
|
+
timestamp: Date.now()
|
|
79
|
+
},
|
|
80
|
+
tags: context.tags
|
|
81
|
+
});
|
|
82
|
+
context.stateManager.setPluginResult(context.queryKey, {
|
|
83
|
+
isInitialData: true
|
|
84
|
+
});
|
|
85
|
+
if (pluginOptions.refetchOnInitialData === false) {
|
|
86
|
+
return { data: pluginOptions.initialData, status: 200 };
|
|
87
|
+
}
|
|
88
|
+
const response = await next();
|
|
89
|
+
if (!response.error) {
|
|
90
|
+
context.stateManager.setPluginResult(context.queryKey, {
|
|
91
|
+
isInitialData: false
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return response;
|
|
95
|
+
},
|
|
96
|
+
lifecycle: {
|
|
97
|
+
onUnmount(context) {
|
|
98
|
+
if (context.hookId) {
|
|
99
|
+
initialDataAppliedFor.delete(context.hookId);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
function initialDataPlugin() {
|
|
3
|
+
const initialDataAppliedFor = /* @__PURE__ */ new Set();
|
|
4
|
+
return {
|
|
5
|
+
name: "spoosh:initialData",
|
|
6
|
+
operations: ["read", "infiniteRead"],
|
|
7
|
+
middleware: async (context, next) => {
|
|
8
|
+
const pluginOptions = context.pluginOptions;
|
|
9
|
+
if (pluginOptions?.initialData === void 0) {
|
|
10
|
+
const response2 = await next();
|
|
11
|
+
if (!response2.error) {
|
|
12
|
+
context.stateManager.setPluginResult(context.queryKey, {
|
|
13
|
+
isInitialData: false
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return response2;
|
|
17
|
+
}
|
|
18
|
+
if (!context.hookId) {
|
|
19
|
+
const response2 = await next();
|
|
20
|
+
if (!response2.error) {
|
|
21
|
+
context.stateManager.setPluginResult(context.queryKey, {
|
|
22
|
+
isInitialData: false
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return response2;
|
|
26
|
+
}
|
|
27
|
+
if (initialDataAppliedFor.has(context.hookId)) {
|
|
28
|
+
const response2 = await next();
|
|
29
|
+
if (!response2.error) {
|
|
30
|
+
context.stateManager.setPluginResult(context.queryKey, {
|
|
31
|
+
isInitialData: false
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return response2;
|
|
35
|
+
}
|
|
36
|
+
const cached = context.stateManager.getCache(context.queryKey);
|
|
37
|
+
if (cached?.state?.data !== void 0) {
|
|
38
|
+
initialDataAppliedFor.add(context.hookId);
|
|
39
|
+
const response2 = await next();
|
|
40
|
+
if (!response2.error) {
|
|
41
|
+
context.stateManager.setPluginResult(context.queryKey, {
|
|
42
|
+
isInitialData: false
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return response2;
|
|
46
|
+
}
|
|
47
|
+
initialDataAppliedFor.add(context.hookId);
|
|
48
|
+
context.stateManager.setCache(context.queryKey, {
|
|
49
|
+
state: {
|
|
50
|
+
data: pluginOptions.initialData,
|
|
51
|
+
error: void 0,
|
|
52
|
+
timestamp: Date.now()
|
|
53
|
+
},
|
|
54
|
+
tags: context.tags
|
|
55
|
+
});
|
|
56
|
+
context.stateManager.setPluginResult(context.queryKey, {
|
|
57
|
+
isInitialData: true
|
|
58
|
+
});
|
|
59
|
+
if (pluginOptions.refetchOnInitialData === false) {
|
|
60
|
+
return { data: pluginOptions.initialData, status: 200 };
|
|
61
|
+
}
|
|
62
|
+
const response = await next();
|
|
63
|
+
if (!response.error) {
|
|
64
|
+
context.stateManager.setPluginResult(context.queryKey, {
|
|
65
|
+
isInitialData: false
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return response;
|
|
69
|
+
},
|
|
70
|
+
lifecycle: {
|
|
71
|
+
onUnmount(context) {
|
|
72
|
+
if (context.hookId) {
|
|
73
|
+
initialDataAppliedFor.delete(context.hookId);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
initialDataPlugin
|
|
81
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spoosh/plugin-initial-data",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "Initial data plugin for Spoosh - show data immediately before fetch completes",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/nxnom/spoosh.git",
|
|
9
|
+
"directory": "packages/plugin-initial-data"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/nxnom/spoosh/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://spoosh.dev/docs/plugins/initial-data",
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"spoosh",
|
|
20
|
+
"plugin",
|
|
21
|
+
"initial-data",
|
|
22
|
+
"api-client",
|
|
23
|
+
"prefetch"
|
|
24
|
+
],
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.mjs",
|
|
32
|
+
"require": "./dist/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@spoosh/core": ">=0.1.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@spoosh/core": "0.1.0-beta.0",
|
|
40
|
+
"@spoosh/test-utils": "0.1.0-beta.0"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"dev": "tsup --watch",
|
|
44
|
+
"build": "tsup",
|
|
45
|
+
"typecheck": "tsc --noEmit",
|
|
46
|
+
"lint": "eslint src --max-warnings 0",
|
|
47
|
+
"format": "prettier --write 'src/**/*.ts'"
|
|
48
|
+
}
|
|
49
|
+
}
|