vike-solid-query 0.0.0 → 0.0.1-commit-429fd54
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 +190 -0
- package/dist/integration/+config.d.ts +24 -0
- package/dist/integration/+config.js +23 -0
- package/dist/integration/Wrapper.d.ts +4 -0
- package/dist/integration/Wrapper.js +12 -0
- package/dist/src/QueryBoundary.d.ts +26 -0
- package/dist/src/QueryBoundary.jsx +37 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/package.json +47 -2
- package/readme.md +0 -3
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
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
<!-- WARNING: keep links absolute in this file so they work on NPM too -->
|
|
2
|
+
|
|
3
|
+
[<img src="https://vike.dev/vike-readme.svg" align="right" height="90">](https://vike.dev)
|
|
4
|
+
[](https://www.npmjs.com/package/vike-solid-query)
|
|
5
|
+
|
|
6
|
+
# `vike-solid-query`
|
|
7
|
+
|
|
8
|
+
Enables your Solid components to fetch data using [TanStack Query](https://tanstack.com/query/latest).
|
|
9
|
+
|
|
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/createQuery#usage-with-suspense) for triggering SolidJS Suspense and ErrorBoundary components when the query is in a pending or error state.
|
|
12
|
+
|
|
13
|
+
[Installation](#installation)
|
|
14
|
+
[Basic usage](#basic-usage)
|
|
15
|
+
[`QueryBoundary`](#queryboundary)
|
|
16
|
+
[`<head>` tags](#head-tags)
|
|
17
|
+
[See also](#see-also)
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
1. `npm install @tanstack/solid-query vike-solid-query`
|
|
22
|
+
2. Extend `+config.js`:
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
// pages/+config.js
|
|
26
|
+
|
|
27
|
+
import vikeSolid from 'vike-solid/config'
|
|
28
|
+
import vikeSolidQuery from 'vike-solid-query/config'
|
|
29
|
+
|
|
30
|
+
export default {
|
|
31
|
+
// ...
|
|
32
|
+
extends: [vikeSolid, vikeSolidQuery]
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
> [!NOTE]
|
|
36
|
+
> The `vike-solid-query` extension requires [`vike-solid`](https://vike.dev/vike-solid).
|
|
37
|
+
|
|
38
|
+
## Basic usage
|
|
39
|
+
|
|
40
|
+
```tsx
|
|
41
|
+
import { createQuery } from "@tanstack/solid-query";
|
|
42
|
+
import { Suspense } from "solid-js";
|
|
43
|
+
|
|
44
|
+
const Movie = (props: { id }) => {
|
|
45
|
+
const query = createQuery(() => ({
|
|
46
|
+
queryKey: ["movies", props.id],
|
|
47
|
+
queryFn: () =>
|
|
48
|
+
fetch(`https://brillout.github.io/star-wars/api/films/${props.id}.json`)
|
|
49
|
+
.then((res) => res.json()),
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<Suspense fallback={"Loading ..."}>
|
|
54
|
+
Title: <b>{query.data?.title}</b>
|
|
55
|
+
</Suspense>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## `QueryBoundary`
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
// Define the loading fallback
|
|
64
|
+
<QueryBoundary query={query} loadingFallback={Loading}>
|
|
65
|
+
{(data) => <div>{data.something}</div>}
|
|
66
|
+
</QueryBoundary>
|
|
67
|
+
// Define the loading and error fallback
|
|
68
|
+
<QueryBoundary query={query} loadingFallback={Loading} errorFallback={Error}>
|
|
69
|
+
{(data) => <div>{data.something}</div>}
|
|
70
|
+
</QueryBoundary>
|
|
71
|
+
// Define the loading, error and not found fallback
|
|
72
|
+
<QueryBoundary query={query} loadingFallback={Loading} errorFallback={Error} notFoundFallback={NotFound}>
|
|
73
|
+
{(data) => <div>{data.something}</div>}
|
|
74
|
+
</QueryBoundary>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Types :**
|
|
78
|
+
```js
|
|
79
|
+
query: CreateQueryResult<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
|
+
```
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
// Movie.tsx
|
|
88
|
+
|
|
89
|
+
import { createQuery } from "@tanstack/solid-query";
|
|
90
|
+
import { QueryBoundary } from "vike-solid-query";
|
|
91
|
+
|
|
92
|
+
function Movie(props: { id: string }) {
|
|
93
|
+
const query = createQuery(() => ({
|
|
94
|
+
queryKey: ["movies", props.id],
|
|
95
|
+
queryFn: () =>
|
|
96
|
+
fetch(`https://brillout.github.io/star-wars/api/films/${props.id}.json`)
|
|
97
|
+
.then((res) => res.json())
|
|
98
|
+
}));
|
|
99
|
+
|
|
100
|
+
return (
|
|
101
|
+
<QueryBoundary
|
|
102
|
+
query={query}
|
|
103
|
+
loadingFallback={<p>Loading movie {props.id}</p>}
|
|
104
|
+
errorFallback={(err, reset) => (
|
|
105
|
+
<>
|
|
106
|
+
<div>Failed to load movie {props.id}</div>
|
|
107
|
+
<button
|
|
108
|
+
onClick={async () => {
|
|
109
|
+
reset();
|
|
110
|
+
await query.refetch();
|
|
111
|
+
}}
|
|
112
|
+
>
|
|
113
|
+
Retry
|
|
114
|
+
</button>
|
|
115
|
+
</>
|
|
116
|
+
)}
|
|
117
|
+
>
|
|
118
|
+
{(movie) => (
|
|
119
|
+
<div>
|
|
120
|
+
Title: <b>{movie.title}</b>
|
|
121
|
+
</div>
|
|
122
|
+
)}
|
|
123
|
+
</QueryBoundary>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## `<head>` tags
|
|
129
|
+
|
|
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).
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
import { createQuery } from "@tanstack/solid-query";
|
|
134
|
+
import { Config } from 'vike-solid/Config'
|
|
135
|
+
import { Head } from 'vike-solid/Head'
|
|
136
|
+
import { QueryBoundary } from "vike-solid-query";
|
|
137
|
+
import { For } from "solid-js";
|
|
138
|
+
|
|
139
|
+
function Movies() {
|
|
140
|
+
const query = createQuery(() => ({
|
|
141
|
+
queryKey: ["movies"],
|
|
142
|
+
queryFn: () => fetch('https://star-wars.brillout.com/api/films.json')
|
|
143
|
+
}));
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<QueryBoundary query={query} loadingFallback={<p>Loading movies ...</p>}>
|
|
147
|
+
{(movies) => (
|
|
148
|
+
<>
|
|
149
|
+
<Config title={`${movies.length} Star Wars movies`} />
|
|
150
|
+
<Head>
|
|
151
|
+
<meta name="description" content={`All ${movies.length} movies from the Star Wars franchise.`} />
|
|
152
|
+
</Head>
|
|
153
|
+
<h1>Star Wars Movies</h1>
|
|
154
|
+
<ol>
|
|
155
|
+
<For each={movies}>
|
|
156
|
+
{(movie) => (
|
|
157
|
+
<li>{movie.title}</li>
|
|
158
|
+
)}
|
|
159
|
+
</For>
|
|
160
|
+
</ol>
|
|
161
|
+
</>
|
|
162
|
+
)}
|
|
163
|
+
</QueryBoundary>
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Settings
|
|
169
|
+
|
|
170
|
+
You can modify the defaults defined by [`QueryClient`](https://tanstack.com/query/latest/docs/reference/QueryClient).
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
// +config.js
|
|
174
|
+
|
|
175
|
+
export default {
|
|
176
|
+
queryClientConfig: {
|
|
177
|
+
defaultOptions: {
|
|
178
|
+
queries: {
|
|
179
|
+
staleTime: 60 * 1000
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## See also
|
|
187
|
+
|
|
188
|
+
- [Example](https://github.com/vikejs/vike-solid/tree/main/examples/solid-query)
|
|
189
|
+
- [TanStack Query > CreateQuery > Usage with Suspense](https://tanstack.com/query/latest/docs/framework/solid/reference/createQuery#usage-with-suspense)
|
|
190
|
+
- [Vike > Data Fetching](https://vike.dev/data-fetching)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import "vike-solid/config";
|
|
2
|
+
declare const _default: {
|
|
3
|
+
name: string;
|
|
4
|
+
require: {
|
|
5
|
+
"vike-solid": string;
|
|
6
|
+
};
|
|
7
|
+
Wrapper: "import:vike-solid-query/__internal/integration/Wrapper:default";
|
|
8
|
+
queryClientConfig: {
|
|
9
|
+
defaultOptions: {
|
|
10
|
+
queries: {
|
|
11
|
+
staleTime: number;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
meta: {
|
|
16
|
+
queryClientConfig: {
|
|
17
|
+
env: {
|
|
18
|
+
server: true;
|
|
19
|
+
client: true;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
export default _default;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import "vike-solid/config"; // Needed for declaration merging of Config
|
|
2
|
+
export default {
|
|
3
|
+
name: "vike-solid-query",
|
|
4
|
+
require: {
|
|
5
|
+
"vike-solid": ">=0.7.3",
|
|
6
|
+
},
|
|
7
|
+
Wrapper: "import:vike-solid-query/__internal/integration/Wrapper:default",
|
|
8
|
+
queryClientConfig: {
|
|
9
|
+
defaultOptions: {
|
|
10
|
+
queries: {
|
|
11
|
+
staleTime: 5000,
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
meta: {
|
|
16
|
+
queryClientConfig: {
|
|
17
|
+
env: {
|
|
18
|
+
server: true,
|
|
19
|
+
client: true,
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createComponent } from "solid-js";
|
|
2
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/solid-query";
|
|
3
|
+
import { usePageContext } from "vike-solid/usePageContext";
|
|
4
|
+
export default function Wrapper(props) {
|
|
5
|
+
const pageContext = usePageContext();
|
|
6
|
+
return createComponent(QueryClientProvider, {
|
|
7
|
+
client: new QueryClient(pageContext.config.queryClientConfig),
|
|
8
|
+
get children() {
|
|
9
|
+
return props.children;
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CreateQueryResult } from "@tanstack/solid-query";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
export interface QueryBoundaryProps<T = unknown> {
|
|
4
|
+
query: CreateQueryResult<T, Error>;
|
|
5
|
+
/**
|
|
6
|
+
* Triggered when the data is initially loading.
|
|
7
|
+
*/
|
|
8
|
+
loadingFallback?: JSX.Element;
|
|
9
|
+
/**
|
|
10
|
+
* Triggered when fetching is complete, but the returned data was falsey.
|
|
11
|
+
*/
|
|
12
|
+
notFoundFallback?: JSX.Element;
|
|
13
|
+
/**
|
|
14
|
+
* Triggered when the query results in an error.
|
|
15
|
+
*/
|
|
16
|
+
errorFallback?: JSX.Element | ((err: any, reset: () => void) => JSX.Element);
|
|
17
|
+
/**
|
|
18
|
+
* Triggered when fetching is complete, and the returned data is not falsey.
|
|
19
|
+
*/
|
|
20
|
+
children: (data: Exclude<T, null | false | undefined>) => JSX.Element;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Convenience wrapper that handles suspense and errors for queries. Makes the results of query.data available to
|
|
24
|
+
* children (as a render prop) in a type-safe way.
|
|
25
|
+
*/
|
|
26
|
+
export declare function QueryBoundary<T>(props: QueryBoundaryProps<T>): JSX.Element;
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { QueryBoundary } from "./QueryBoundary";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { QueryBoundary } from "./QueryBoundary";
|
package/package.json
CHANGED
|
@@ -1,4 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vike-solid-query",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
|
|
3
|
+
"version": "0.0.1-commit-429fd54",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/src/index.js",
|
|
6
|
+
"typings": "dist/src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./dist/src/index.js",
|
|
9
|
+
"./config": "./dist/integration/+config.js",
|
|
10
|
+
"./__internal/integration/Wrapper": "./dist/integration/Wrapper.js"
|
|
11
|
+
},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"@tanstack/solid-query": ">=5.0.0",
|
|
14
|
+
"solid-js": "^1.8.7",
|
|
15
|
+
"vike-solid": ">=0.7.4"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@brillout/release-me": "^0.4.1",
|
|
19
|
+
"@tanstack/solid-query": "^5.56.2",
|
|
20
|
+
"rimraf": "^6.0.1",
|
|
21
|
+
"solid-js": "^1.8.22",
|
|
22
|
+
"typescript": "^5.6.2",
|
|
23
|
+
"vike": "^0.4.195",
|
|
24
|
+
"vike-solid": "^0.7.5"
|
|
25
|
+
},
|
|
26
|
+
"typesVersions": {
|
|
27
|
+
"*": {
|
|
28
|
+
"config": [
|
|
29
|
+
"dist/integration/+config.d.ts"
|
|
30
|
+
],
|
|
31
|
+
"__internal/integration/Wrapper": [
|
|
32
|
+
"dist/integration/Wrapper.d.ts"
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"repository": "github:vikejs/vike-solid",
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"scripts": {
|
|
42
|
+
"dev": "tsc --watch",
|
|
43
|
+
"dev:typecheck": "tsc --noEmit --watch",
|
|
44
|
+
"build": "rimraf dist/ && tsc",
|
|
45
|
+
"release": "LANG=en_US release-me patch",
|
|
46
|
+
"release:minor": "LANG=en_US release-me minor",
|
|
47
|
+
"release:commit": "LANG=en_US release-me commit"
|
|
48
|
+
}
|
|
49
|
+
}
|
package/readme.md
DELETED