Refactor query pattern.
This commit is contained in:
1 parent
c59248f7a5
commit
24281cd491
5 files changed
+50
-66
No files matched your search
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Copyright 2026 James Pace
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
//
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
//
|
||||
|
||||
import { atomWithQuery } from "jotai-tanstack-query";
|
||||
|
||||
export async function fetchJson(url: string): Promise<any> {
|
||||
const resp = await fetch(url);
|
||||
if (!resp.ok) {
|
||||
throw new Error(`Network response was not ok: ${resp.statusText}`);
|
||||
}
|
||||
return resp.json();
|
||||
}
|
||||
|
||||
interface CreateQueryAtomOptions {
|
||||
refetchInterval?: number;
|
||||
retry?: boolean;
|
||||
}
|
||||
|
||||
export function createQueryAtom(url: string, options?: CreateQueryAtomOptions) {
|
||||
return atomWithQuery(() => ({
|
||||
queryKey: [url],
|
||||
queryFn: () => fetchJson(url),
|
||||
...options,
|
||||
}));
|
||||
}
|
||||
Reference in new issue
Block a user