diff --git a/example/app/layout.tsx b/example/app/layout.tsx
new file mode 100644
index 0000000..a14e64f
--- /dev/null
+++ b/example/app/layout.tsx
@@ -0,0 +1,16 @@
+export const metadata = {
+ title: 'Next.js',
+ description: 'Generated by Next.js',
+}
+
+export default function RootLayout({
+ children,
+}: {
+ children: React.ReactNode
+}) {
+ return (
+
+
{children}
+
+ )
+}
diff --git a/example/pages/index.tsx b/example/app/page.tsx
similarity index 69%
rename from example/pages/index.tsx
rename to example/app/page.tsx
index 4caf25c..b024519 100644
--- a/example/pages/index.tsx
+++ b/example/app/page.tsx
@@ -1,7 +1,3 @@
-import { Suspense } from 'react';
-
-import dynamic from 'next/dynamic';
-
import Posts from '../components/Posts';
export default function Docs() {
@@ -9,7 +5,6 @@ export default function Docs() {
);
diff --git a/example/components/Posts.tsx b/example/components/Posts.tsx
index cc2b5bd..76008d0 100644
--- a/example/components/Posts.tsx
+++ b/example/components/Posts.tsx
@@ -1,4 +1,6 @@
-import { Fetchtastic, suspender } from '../../lib/src';
+import { cache } from 'react';
+
+import { Fetchtastic } from '../../lib/src';
type Post = {
title: string;
@@ -17,7 +19,7 @@ function isPost(data: unknown): data is Post {
);
}
-export function assertPosts(data: unknown) {
+function assertPosts(data: unknown) {
if (data && Array.isArray(data) && data.every(isPost)) {
return data;
}
@@ -28,23 +30,20 @@ const api = new Fetchtastic('https://jsonplaceholder.typicode.com')
.appendHeader('Accept', 'application/json')
.appendHeader('Content-Type', 'application/json');
-export default function Posts() {
- const posts = suspender(() =>
- api
- .get('/postss')
- .notFound(() => console.log('not found!'))
- .json(assertPosts)
- .catch(() => []),
- );
+const fetPosts = cache(() =>
+ api
+ .get('/posts')
+ .notFound(() => console.log('not found!'))
+ .json(assertPosts)
+ .catch(() => [] as Post[]),
+);
- function addPost() {
- api.post('/posts', { title: 'test', body: 'test' }).resolve();
- }
+export default async function Posts() {
+ const posts = await fetPosts();
return (
Posts
-
{posts.map(post => (
{post.title}
diff --git a/example/tsconfig.json b/example/tsconfig.json
index f4800bc..88e1078 100644
--- a/example/tsconfig.json
+++ b/example/tsconfig.json
@@ -1,5 +1,13 @@
{
"extends": "tsconfig/nextjs.json",
- "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "../lib/src/index.ts"],
- "exclude": ["node_modules"]
+ "include": [
+ "next-env.d.ts",
+ "**/*.ts",
+ "**/*.tsx",
+ "../lib/src/index.ts",
+ ".next/types/**/*.ts"
+ ],
+ "exclude": [
+ "node_modules"
+ ]
}
diff --git a/lib/src/react/index.ts b/lib/src/react/index.ts
deleted file mode 100644
index 5a6342e..0000000
--- a/lib/src/react/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from './suspender';
diff --git a/lib/src/react/suspender.ts b/lib/src/react/suspender.ts
deleted file mode 100644
index f6e68b7..0000000
--- a/lib/src/react/suspender.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-let status = 'pending';
-let cache: unknown | null = null;
-let error: Error;
-
-export function suspender(getPromise: () => Promise) {
- let suspender: Promise = Promise.resolve();
-
- if (status === 'pending') {
- suspender = getPromise()
- .then(r => {
- status = 'fulfilled';
- cache = r;
- })
- .catch(e => {
- status = 'rejected';
- error = e;
- });
- }
-
- if (status === 'pending') {
- throw suspender;
- } else if (status === 'rejected') {
- throw error;
- } else {
- return cache as T;
- }
-}