floppy-disk 2.3.0-beta.1 → 2.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 +33 -22
- package/esm/preact/create-query.d.ts +8 -0
- package/esm/preact/create-query.js +10 -0
- package/esm/react/create-query.d.ts +7 -0
- package/esm/react/create-query.js +10 -1
- package/lib/preact/create-query.d.ts +8 -0
- package/lib/preact/create-query.js +10 -0
- package/lib/react/create-query.d.ts +7 -0
- package/lib/react/create-query.js +9 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,28 +28,39 @@ import { createQuery, createMutation } from 'floppy-disk'; // 8.4 kB (gzipped: 2
|
|
|
28
28
|
|
|
29
29
|
## Key Features
|
|
30
30
|
|
|
31
|
-
- Create
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
|
|
39
|
-
-
|
|
40
|
-
|
|
41
|
-
-
|
|
42
|
-
-
|
|
43
|
-
-
|
|
44
|
-
-
|
|
45
|
-
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
-
|
|
52
|
-
-
|
|
31
|
+
- **Create Store**
|
|
32
|
+
- Get/set store inside/outside component
|
|
33
|
+
- Custom reactivity (like `useEffect`'s dependency array)
|
|
34
|
+
- Support middleware
|
|
35
|
+
- Set state interception
|
|
36
|
+
- Store event (`onSubscribe`, `onUnsubscribe`, etc.)
|
|
37
|
+
- Use store as local state manager
|
|
38
|
+
- **Create Stores**
|
|
39
|
+
- Same as store, but controlled with a store key
|
|
40
|
+
- **Create Query & Mutation**
|
|
41
|
+
- Backend agnostic (support GraphQL & any async function)
|
|
42
|
+
- TypeScript ready
|
|
43
|
+
- SSR/SSG support
|
|
44
|
+
- Custom reactivity (we choose when to re-render)
|
|
45
|
+
- **Create query**
|
|
46
|
+
- Dedupe multiple request
|
|
47
|
+
- Auto-fetch on mount or manual (lazy query)
|
|
48
|
+
- Enable/disable query
|
|
49
|
+
- Serve stale data while revalidating
|
|
50
|
+
- Retry on error (customizable)
|
|
51
|
+
- Optimistic update
|
|
52
|
+
- Invalidate query
|
|
53
|
+
- Reset query
|
|
54
|
+
- Query with param (query key)
|
|
55
|
+
- Paginated/infinite query
|
|
56
|
+
- Prefetch query
|
|
57
|
+
- Fetch from inside/outside component
|
|
58
|
+
- Get query state inside/outside component
|
|
59
|
+
- Suspense mode
|
|
60
|
+
- **Create mutation**
|
|
61
|
+
- Mutate from inside/outside component
|
|
62
|
+
- Get mutation state inside/outside component
|
|
63
|
+
- ... and [a lot more](https://floppy-disk.vercel.app/)
|
|
53
64
|
|
|
54
65
|
<br>
|
|
55
66
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { FunctionComponent } from 'preact';
|
|
1
3
|
import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
|
|
2
4
|
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
|
|
3
5
|
/**
|
|
@@ -227,5 +229,11 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
227
229
|
* Use query with suspense mode.
|
|
228
230
|
*/
|
|
229
231
|
suspend: (key?: TKey | null) => QueryState<TKey, TResponse, TData, TError>;
|
|
232
|
+
Render: (props: {
|
|
233
|
+
queryKey?: TKey | null;
|
|
234
|
+
loading?: FunctionComponent<TKey>;
|
|
235
|
+
success?: FunctionComponent<TKey>;
|
|
236
|
+
error?: FunctionComponent<TKey>;
|
|
237
|
+
}) => JSX.Element;
|
|
230
238
|
};
|
|
231
239
|
export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError>) => UseQuery<TKey, TResponse, TData, TError>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { h as createElement } from 'preact';
|
|
1
2
|
import { useState } from 'preact/hooks';
|
|
2
3
|
import { hasValue, identityFn, noop } from '../utils';
|
|
3
4
|
import { createStores } from './create-stores';
|
|
@@ -352,5 +353,14 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
352
353
|
throw state.error;
|
|
353
354
|
return state;
|
|
354
355
|
};
|
|
356
|
+
const defaultElement = () => null;
|
|
357
|
+
useQuery.Render = (props) => {
|
|
358
|
+
const { queryKey, loading = defaultElement, success = defaultElement, error = defaultElement, } = props;
|
|
359
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
360
|
+
const state = useQuery(queryKey);
|
|
361
|
+
if (state.data)
|
|
362
|
+
return createElement(success, state.key);
|
|
363
|
+
return createElement(state.isLoading ? loading : error, state.key);
|
|
364
|
+
};
|
|
355
365
|
return useQuery;
|
|
356
366
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { FunctionComponent } from 'react';
|
|
1
2
|
import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
|
|
2
3
|
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
|
|
3
4
|
/**
|
|
@@ -227,5 +228,11 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
227
228
|
* Use query with suspense mode.
|
|
228
229
|
*/
|
|
229
230
|
suspend: (key?: TKey | null) => QueryState<TKey, TResponse, TData, TError>;
|
|
231
|
+
Render: (props: {
|
|
232
|
+
queryKey?: TKey | null;
|
|
233
|
+
loading?: FunctionComponent<TKey>;
|
|
234
|
+
success?: FunctionComponent<TKey>;
|
|
235
|
+
error?: FunctionComponent<TKey>;
|
|
236
|
+
}) => JSX.Element;
|
|
230
237
|
};
|
|
231
238
|
export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError>) => UseQuery<TKey, TResponse, TData, TError>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState } from 'react';
|
|
1
|
+
import { createElement, useState } from 'react';
|
|
2
2
|
import { hasValue, identityFn, noop } from '../utils';
|
|
3
3
|
import { createStores } from './create-stores';
|
|
4
4
|
const getDecision = (value, param, { ifTrue, ifAlways }) => {
|
|
@@ -352,5 +352,14 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
352
352
|
throw state.error;
|
|
353
353
|
return state;
|
|
354
354
|
};
|
|
355
|
+
const defaultElement = () => null;
|
|
356
|
+
useQuery.Render = (props) => {
|
|
357
|
+
const { queryKey, loading = defaultElement, success = defaultElement, error = defaultElement, } = props;
|
|
358
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
359
|
+
const state = useQuery(queryKey);
|
|
360
|
+
if (state.data)
|
|
361
|
+
return createElement(success, state.key);
|
|
362
|
+
return createElement(state.isLoading ? loading : error, state.key);
|
|
363
|
+
};
|
|
355
364
|
return useQuery;
|
|
356
365
|
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { FunctionComponent } from 'preact';
|
|
1
3
|
import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
|
|
2
4
|
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
|
|
3
5
|
/**
|
|
@@ -227,5 +229,11 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
227
229
|
* Use query with suspense mode.
|
|
228
230
|
*/
|
|
229
231
|
suspend: (key?: TKey | null) => QueryState<TKey, TResponse, TData, TError>;
|
|
232
|
+
Render: (props: {
|
|
233
|
+
queryKey?: TKey | null;
|
|
234
|
+
loading?: FunctionComponent<TKey>;
|
|
235
|
+
success?: FunctionComponent<TKey>;
|
|
236
|
+
error?: FunctionComponent<TKey>;
|
|
237
|
+
}) => JSX.Element;
|
|
230
238
|
};
|
|
231
239
|
export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError>) => UseQuery<TKey, TResponse, TData, TError>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createQuery = void 0;
|
|
4
|
+
const preact_1 = require("preact");
|
|
4
5
|
const hooks_1 = require("preact/hooks");
|
|
5
6
|
const utils_1 = require("../utils");
|
|
6
7
|
const create_stores_1 = require("./create-stores");
|
|
@@ -355,6 +356,15 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
355
356
|
throw state.error;
|
|
356
357
|
return state;
|
|
357
358
|
};
|
|
359
|
+
const defaultElement = () => null;
|
|
360
|
+
useQuery.Render = (props) => {
|
|
361
|
+
const { queryKey, loading = defaultElement, success = defaultElement, error = defaultElement, } = props;
|
|
362
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
363
|
+
const state = useQuery(queryKey);
|
|
364
|
+
if (state.data)
|
|
365
|
+
return (0, preact_1.h)(success, state.key);
|
|
366
|
+
return (0, preact_1.h)(state.isLoading ? loading : error, state.key);
|
|
367
|
+
};
|
|
358
368
|
return useQuery;
|
|
359
369
|
};
|
|
360
370
|
exports.createQuery = createQuery;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { FunctionComponent } from 'react';
|
|
1
2
|
import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
|
|
2
3
|
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
|
|
3
4
|
/**
|
|
@@ -227,5 +228,11 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
227
228
|
* Use query with suspense mode.
|
|
228
229
|
*/
|
|
229
230
|
suspend: (key?: TKey | null) => QueryState<TKey, TResponse, TData, TError>;
|
|
231
|
+
Render: (props: {
|
|
232
|
+
queryKey?: TKey | null;
|
|
233
|
+
loading?: FunctionComponent<TKey>;
|
|
234
|
+
success?: FunctionComponent<TKey>;
|
|
235
|
+
error?: FunctionComponent<TKey>;
|
|
236
|
+
}) => JSX.Element;
|
|
230
237
|
};
|
|
231
238
|
export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError>) => UseQuery<TKey, TResponse, TData, TError>;
|
|
@@ -355,6 +355,15 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
355
355
|
throw state.error;
|
|
356
356
|
return state;
|
|
357
357
|
};
|
|
358
|
+
const defaultElement = () => null;
|
|
359
|
+
useQuery.Render = (props) => {
|
|
360
|
+
const { queryKey, loading = defaultElement, success = defaultElement, error = defaultElement, } = props;
|
|
361
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
362
|
+
const state = useQuery(queryKey);
|
|
363
|
+
if (state.data)
|
|
364
|
+
return (0, react_1.createElement)(success, state.key);
|
|
365
|
+
return (0, react_1.createElement)(state.isLoading ? loading : error, state.key);
|
|
366
|
+
};
|
|
358
367
|
return useQuery;
|
|
359
368
|
};
|
|
360
369
|
exports.createQuery = createQuery;
|