vike-solid-query 0.0.1 → 0.1.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.md +9 -0
- package/README.md +25 -20
- package/dist/{src/QueryBoundary.d.ts → index.d.ts} +9 -5
- package/dist/index.js +66 -0
- package/dist/integration/Wrapper.d.ts +1 -1
- package/dist/integration/{+config.js → config.js} +3 -3
- package/dist/server.js +47 -0
- package/package.json +46 -30
- package/dist/src/QueryBoundary.jsx +0 -37
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.js +0 -1
- /package/dist/integration/{+config.d.ts → config.d.ts} +0 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present Joël Charles
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
Enables your Solid components to fetch data using [TanStack Query](https://tanstack.com/query/latest).
|
|
9
9
|
|
|
10
10
|
> [!NOTE]
|
|
11
|
-
> You'll also get [progressive rendering](https://vike.dev/streaming#progressive-rendering) and [supports](https://tanstack.com/query/latest/docs/framework/solid/reference/
|
|
11
|
+
> You'll also get [progressive rendering](https://vike.dev/streaming#progressive-rendering) and [supports](https://tanstack.com/query/latest/docs/framework/solid/reference/useQuery#usage-with-suspense) for triggering SolidJS Suspense and ErrorBoundary components when the query is in a pending or error state.
|
|
12
12
|
|
|
13
13
|
[Installation](#installation)
|
|
14
14
|
[Basic usage](#basic-usage)
|
|
@@ -20,6 +20,7 @@ Enables your Solid components to fetch data using [TanStack Query](https://tanst
|
|
|
20
20
|
|
|
21
21
|
1. `npm install @tanstack/solid-query vike-solid-query`
|
|
22
22
|
2. Extend `+config.js`:
|
|
23
|
+
|
|
23
24
|
```js
|
|
24
25
|
// pages/+config.js
|
|
25
26
|
|
|
@@ -36,12 +37,12 @@ Enables your Solid components to fetch data using [TanStack Query](https://tanst
|
|
|
36
37
|
|
|
37
38
|
## Basic usage
|
|
38
39
|
|
|
39
|
-
```
|
|
40
|
-
import {
|
|
40
|
+
```tsx
|
|
41
|
+
import { useQuery } from "@tanstack/solid-query";
|
|
41
42
|
import { Suspense } from "solid-js";
|
|
42
43
|
|
|
43
44
|
const Movie = (props: { id }) => {
|
|
44
|
-
const query =
|
|
45
|
+
const query = useQuery(() => ({
|
|
45
46
|
queryKey: ["movies", props.id],
|
|
46
47
|
queryFn: () =>
|
|
47
48
|
fetch(`https://brillout.github.io/star-wars/api/films/${props.id}.json`)
|
|
@@ -58,34 +59,38 @@ const Movie = (props: { id }) => {
|
|
|
58
59
|
|
|
59
60
|
## `QueryBoundary`
|
|
60
61
|
|
|
61
|
-
```
|
|
62
|
-
// Define loading fallback
|
|
62
|
+
```tsx
|
|
63
|
+
// Define the loading fallback
|
|
63
64
|
<QueryBoundary query={query} loadingFallback={Loading}>
|
|
64
65
|
{(data) => <div>{data.something}</div>}
|
|
65
66
|
</QueryBoundary>
|
|
66
|
-
// Define loading and error fallback
|
|
67
|
+
// Define the loading and error fallback
|
|
67
68
|
<QueryBoundary query={query} loadingFallback={Loading} errorFallback={Error}>
|
|
68
69
|
{(data) => <div>{data.something}</div>}
|
|
69
70
|
</QueryBoundary>
|
|
70
|
-
// Define loading, error and not found fallback
|
|
71
|
+
// Define the loading, error and not found fallback
|
|
71
72
|
<QueryBoundary query={query} loadingFallback={Loading} errorFallback={Error} notFoundFallback={NotFound}>
|
|
72
73
|
{(data) => <div>{data.something}</div>}
|
|
73
74
|
</QueryBoundary>
|
|
74
75
|
```
|
|
75
76
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
**Types :**
|
|
78
|
+
```js
|
|
79
|
+
query: UseQueryResult<T, Error>;
|
|
80
|
+
loadingFallback?: JSX.Element;
|
|
81
|
+
notFoundFallback?: JSX.Element;
|
|
82
|
+
errorFallback?: JSX.Element | ((err: any, reset: () => void) => JSX.Element);
|
|
83
|
+
children: (data: Exclude<T, null | false | undefined>) => JSX.Element;
|
|
84
|
+
```
|
|
82
85
|
|
|
83
86
|
```tsx
|
|
84
|
-
|
|
87
|
+
// Movie.tsx
|
|
88
|
+
|
|
89
|
+
import { useQuery } from "@tanstack/solid-query";
|
|
85
90
|
import { QueryBoundary } from "vike-solid-query";
|
|
86
91
|
|
|
87
92
|
function Movie(props: { id: string }) {
|
|
88
|
-
const query =
|
|
93
|
+
const query = useQuery(() => ({
|
|
89
94
|
queryKey: ["movies", props.id],
|
|
90
95
|
queryFn: () =>
|
|
91
96
|
fetch(`https://brillout.github.io/star-wars/api/films/${props.id}.json`)
|
|
@@ -124,15 +129,15 @@ function Movie(props: { id: string }) {
|
|
|
124
129
|
|
|
125
130
|
To set tags such as `<title>` and `<meta name="description">` based on fetched data, you can use [`<Config>`, `<Head>`, and `useConfig()`](https://vike.dev/useConfig).
|
|
126
131
|
|
|
127
|
-
```
|
|
128
|
-
import {
|
|
132
|
+
```tsx
|
|
133
|
+
import { useQuery } from "@tanstack/solid-query";
|
|
129
134
|
import { Config } from 'vike-solid/Config'
|
|
130
135
|
import { Head } from 'vike-solid/Head'
|
|
131
136
|
import { QueryBoundary } from "vike-solid-query";
|
|
132
137
|
import { For } from "solid-js";
|
|
133
138
|
|
|
134
139
|
function Movies() {
|
|
135
|
-
const query =
|
|
140
|
+
const query = useQuery(() => ({
|
|
136
141
|
queryKey: ["movies"],
|
|
137
142
|
queryFn: () => fetch('https://star-wars.brillout.com/api/films.json')
|
|
138
143
|
}));
|
|
@@ -181,5 +186,5 @@ export default {
|
|
|
181
186
|
## See also
|
|
182
187
|
|
|
183
188
|
- [Example](https://github.com/vikejs/vike-solid/tree/main/examples/solid-query)
|
|
184
|
-
- [TanStack Query >
|
|
189
|
+
- [TanStack Query > useQuery > Usage with Suspense](https://tanstack.com/query/latest/docs/framework/solid/reference/useQuery#usage-with-suspense)
|
|
185
190
|
- [Vike > Data Fetching](https://vike.dev/data-fetching)
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { UseQueryResult } from '@tanstack/solid-query';
|
|
2
|
+
import { JSX } from 'solid-js';
|
|
3
|
+
|
|
4
|
+
interface QueryBoundaryProps<T = unknown> {
|
|
5
|
+
query: UseQueryResult<T, Error>;
|
|
5
6
|
/**
|
|
6
7
|
* Triggered when the data is initially loading.
|
|
7
8
|
*/
|
|
@@ -23,4 +24,7 @@ export interface QueryBoundaryProps<T = unknown> {
|
|
|
23
24
|
* Convenience wrapper that handles suspense and errors for queries. Makes the results of query.data available to
|
|
24
25
|
* children (as a render prop) in a type-safe way.
|
|
25
26
|
*/
|
|
26
|
-
|
|
27
|
+
declare function QueryBoundary<T>(props: QueryBoundaryProps<T>): JSX.Element;
|
|
28
|
+
|
|
29
|
+
export { QueryBoundary };
|
|
30
|
+
export type { QueryBoundaryProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { delegateEvents, createComponent, getNextElement, memo, template, runHydrationEvents, insert } from 'solid-js/web';
|
|
2
|
+
import { ErrorBoundary, Suspense, Switch, Match } from 'solid-js';
|
|
3
|
+
|
|
4
|
+
var _tmpl$ = /*#__PURE__*/template(`<div><div class=query-boundary-error></div><button>Retry`),
|
|
5
|
+
_tmpl$2 = /*#__PURE__*/template(`<div>Not Found,`),
|
|
6
|
+
_tmpl$3 = /*#__PURE__*/template(`<button>Refetch`);
|
|
7
|
+
/**
|
|
8
|
+
* Convenience wrapper that handles suspense and errors for queries. Makes the results of query.data available to
|
|
9
|
+
* children (as a render prop) in a type-safe way.
|
|
10
|
+
*/
|
|
11
|
+
function QueryBoundary(props) {
|
|
12
|
+
return createComponent(ErrorBoundary, {
|
|
13
|
+
get fallback() {
|
|
14
|
+
return memo(() => !!props.errorFallback)() ? props.errorFallback : (err, reset) => (() => {
|
|
15
|
+
var _el$ = getNextElement(_tmpl$),
|
|
16
|
+
_el$2 = _el$.firstChild,
|
|
17
|
+
_el$3 = _el$2.nextSibling;
|
|
18
|
+
insert(_el$2, () => err.toString());
|
|
19
|
+
_el$3.$$click = async () => {
|
|
20
|
+
reset();
|
|
21
|
+
await props.query.refetch();
|
|
22
|
+
};
|
|
23
|
+
runHydrationEvents();
|
|
24
|
+
return _el$;
|
|
25
|
+
})();
|
|
26
|
+
},
|
|
27
|
+
get children() {
|
|
28
|
+
return createComponent(Suspense, {
|
|
29
|
+
get fallback() {
|
|
30
|
+
return props.loadingFallback;
|
|
31
|
+
},
|
|
32
|
+
get children() {
|
|
33
|
+
return createComponent(Switch, {
|
|
34
|
+
get children() {
|
|
35
|
+
return [createComponent(Match, {
|
|
36
|
+
get when() {
|
|
37
|
+
return memo(() => !!!props.query.isFetching)() && !props.query.data;
|
|
38
|
+
},
|
|
39
|
+
get children() {
|
|
40
|
+
return memo(() => !!props.notFoundFallback)() ? props.notFoundFallback : [getNextElement(_tmpl$2), (() => {
|
|
41
|
+
var _el$5 = getNextElement(_tmpl$3);
|
|
42
|
+
_el$5.$$click = async () => {
|
|
43
|
+
await props.query.refetch();
|
|
44
|
+
};
|
|
45
|
+
runHydrationEvents();
|
|
46
|
+
return _el$5;
|
|
47
|
+
})()];
|
|
48
|
+
}
|
|
49
|
+
}), createComponent(Match, {
|
|
50
|
+
get when() {
|
|
51
|
+
return props.query.data;
|
|
52
|
+
},
|
|
53
|
+
get children() {
|
|
54
|
+
return props.children(props.query.data);
|
|
55
|
+
}
|
|
56
|
+
})];
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
delegateEvents(["click"]);
|
|
65
|
+
|
|
66
|
+
export { QueryBoundary };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
var
|
|
1
|
+
var config = {
|
|
2
2
|
name: "vike-solid-query",
|
|
3
3
|
require: {
|
|
4
|
-
"vike-solid": ">=0.7.
|
|
4
|
+
"vike-solid": ">=0.7.4"
|
|
5
5
|
},
|
|
6
6
|
Wrapper: "import:vike-solid-query/__internal/integration/Wrapper:default",
|
|
7
7
|
queryClientConfig: {
|
|
@@ -21,4 +21,4 @@ var _config = {
|
|
|
21
21
|
}
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
export {
|
|
24
|
+
export { config as default };
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createComponent, ssr, ssrHydrationKey, escape } from 'solid-js/web';
|
|
2
|
+
import { ErrorBoundary, Suspense, Switch, Match } from 'solid-js';
|
|
3
|
+
|
|
4
|
+
var _tmpl$ = ["<div", "><div class=\"query-boundary-error\">", "</div><button>Retry</button></div>"],
|
|
5
|
+
_tmpl$2 = ["<div", ">Not Found,</div>"],
|
|
6
|
+
_tmpl$3 = ["<button", ">Refetch</button>"];
|
|
7
|
+
/**
|
|
8
|
+
* Convenience wrapper that handles suspense and errors for queries. Makes the results of query.data available to
|
|
9
|
+
* children (as a render prop) in a type-safe way.
|
|
10
|
+
*/
|
|
11
|
+
function QueryBoundary(props) {
|
|
12
|
+
return createComponent(ErrorBoundary, {
|
|
13
|
+
get fallback() {
|
|
14
|
+
return props.errorFallback ? props.errorFallback : (err, reset) => ssr(_tmpl$, ssrHydrationKey(), escape(err.toString()));
|
|
15
|
+
},
|
|
16
|
+
get children() {
|
|
17
|
+
return createComponent(Suspense, {
|
|
18
|
+
get fallback() {
|
|
19
|
+
return props.loadingFallback;
|
|
20
|
+
},
|
|
21
|
+
get children() {
|
|
22
|
+
return createComponent(Switch, {
|
|
23
|
+
get children() {
|
|
24
|
+
return [createComponent(Match, {
|
|
25
|
+
get when() {
|
|
26
|
+
return !props.query.isFetching && !props.query.data;
|
|
27
|
+
},
|
|
28
|
+
get children() {
|
|
29
|
+
return props.notFoundFallback ? props.notFoundFallback : [ssr(_tmpl$2, ssrHydrationKey()), ssr(_tmpl$3, ssrHydrationKey())];
|
|
30
|
+
}
|
|
31
|
+
}), createComponent(Match, {
|
|
32
|
+
get when() {
|
|
33
|
+
return props.query.data;
|
|
34
|
+
},
|
|
35
|
+
get children() {
|
|
36
|
+
return props.children(props.query.data);
|
|
37
|
+
}
|
|
38
|
+
})];
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export { QueryBoundary };
|
package/package.json
CHANGED
|
@@ -1,44 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vike-solid-query",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
|
|
5
|
+
"main": "./dist/server.js",
|
|
6
|
+
"typings": "dist/index.d.ts",
|
|
7
|
+
"module": "./dist/server.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"browser": {
|
|
10
|
+
"./dist/server.js": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"browser": "./dist/index.js",
|
|
15
|
+
"node": "./dist/server.js",
|
|
16
|
+
"default": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts"
|
|
18
|
+
},
|
|
19
|
+
"./config": "./dist/integration/config.js",
|
|
20
|
+
"./__internal/integration/Wrapper": {
|
|
21
|
+
"solid": "./dist/integration/Wrapper.js",
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./dist/integration/Wrapper.d.ts",
|
|
24
|
+
"default": "./dist/integration/Wrapper.js"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
12
27
|
},
|
|
13
28
|
"peerDependencies": {
|
|
14
29
|
"@tanstack/solid-query": ">=5.0.0",
|
|
15
30
|
"solid-js": "^1.8.7",
|
|
16
|
-
"vike-solid": "
|
|
31
|
+
"vike-solid": "^0.7.12"
|
|
17
32
|
},
|
|
18
33
|
"devDependencies": {
|
|
19
|
-
"@brillout/release-me": "^0.4.
|
|
20
|
-
"@rollup/plugin-babel": "6.0.4",
|
|
21
|
-
"@rollup/plugin-node-resolve": "
|
|
22
|
-
"@tanstack/solid-query": "^5.
|
|
23
|
-
"
|
|
24
|
-
"rollup": "^
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"vike": "^0.4.193",
|
|
30
|
-
"vike-solid": "^0.7.3",
|
|
31
|
-
"vite": "5.4.2"
|
|
32
|
-
},
|
|
33
|
-
"exports": {
|
|
34
|
-
".": "./dist/src/index.js",
|
|
35
|
-
"./config": "./dist/integration/+config.js",
|
|
36
|
-
"./__internal/integration/Wrapper": "./dist/integration/Wrapper.js"
|
|
34
|
+
"@brillout/release-me": "^0.4.8",
|
|
35
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
36
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
37
|
+
"@tanstack/solid-query": "^5.85.3",
|
|
38
|
+
"rollup": "^4.46.3",
|
|
39
|
+
"rollup-plugin-dts": "^6.2.3",
|
|
40
|
+
"solid-js": "^1.9.9",
|
|
41
|
+
"typescript": "^5.9.2",
|
|
42
|
+
"vike": "^0.4.237",
|
|
43
|
+
"vike-solid": "0.7.12"
|
|
37
44
|
},
|
|
38
45
|
"typesVersions": {
|
|
39
46
|
"*": {
|
|
40
47
|
"config": [
|
|
41
|
-
"dist/integration
|
|
48
|
+
"dist/integration/config.d.ts"
|
|
42
49
|
],
|
|
43
50
|
"__internal/integration/Wrapper": [
|
|
44
51
|
"dist/integration/Wrapper.d.ts"
|
|
@@ -49,5 +56,14 @@
|
|
|
49
56
|
"dist/"
|
|
50
57
|
],
|
|
51
58
|
"repository": "github:vikejs/vike-solid",
|
|
52
|
-
"license": "MIT"
|
|
53
|
-
|
|
59
|
+
"license": "MIT",
|
|
60
|
+
"scripts": {
|
|
61
|
+
"dev": "rollup -c rollup.config.js --watch",
|
|
62
|
+
"dev:typecheck": "tsc --noEmit --watch",
|
|
63
|
+
"build": "tsc --noEmit && rollup -c rollup.config.js",
|
|
64
|
+
"release": "LANG=en_US release-me patch",
|
|
65
|
+
"release:minor": "LANG=en_US release-me minor",
|
|
66
|
+
"release:commit": "LANG=en_US release-me commit",
|
|
67
|
+
"test:types": "tsc --noEmit"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { ErrorBoundary, Match, Suspense, Switch } from "solid-js";
|
|
2
|
-
/**
|
|
3
|
-
* Convenience wrapper that handles suspense and errors for queries. Makes the results of query.data available to
|
|
4
|
-
* children (as a render prop) in a type-safe way.
|
|
5
|
-
*/
|
|
6
|
-
export function QueryBoundary(props) {
|
|
7
|
-
return (<ErrorBoundary fallback={props.errorFallback
|
|
8
|
-
? props.errorFallback
|
|
9
|
-
: (err, reset) => (<div>
|
|
10
|
-
<div class="query-boundary-error">{err.toString()}</div>
|
|
11
|
-
<button onClick={async () => {
|
|
12
|
-
reset();
|
|
13
|
-
await props.query.refetch();
|
|
14
|
-
}}>
|
|
15
|
-
Retry
|
|
16
|
-
</button>
|
|
17
|
-
</div>)}>
|
|
18
|
-
<Suspense fallback={props.loadingFallback}>
|
|
19
|
-
<Switch>
|
|
20
|
-
<Match when={!props.query.isFetching && !props.query.data}>
|
|
21
|
-
{props.notFoundFallback ? (props.notFoundFallback) : (<>
|
|
22
|
-
<div>Not Found,</div>
|
|
23
|
-
<button onClick={async () => {
|
|
24
|
-
await props.query.refetch();
|
|
25
|
-
}}>
|
|
26
|
-
Refetch
|
|
27
|
-
</button>
|
|
28
|
-
</>)}
|
|
29
|
-
</Match>
|
|
30
|
-
|
|
31
|
-
<Match when={props.query.data}>
|
|
32
|
-
{props.children(props.query.data)}
|
|
33
|
-
</Match>
|
|
34
|
-
</Switch>
|
|
35
|
-
</Suspense>
|
|
36
|
-
</ErrorBoundary>);
|
|
37
|
-
}
|
package/dist/src/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { QueryBoundary } from "./QueryBoundary";
|
package/dist/src/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { QueryBoundary } from "./QueryBoundary";
|
|
File without changes
|