From 295e40b3d686f3763f0c007f62ae1447e6b59856 Mon Sep 17 00:00:00 2001 From: bysxx <0415white@gmail.com> Date: Fri, 2 Feb 2024 14:14:59 +0900 Subject: [PATCH] feat: change api to server action --- app/api/example/route.ts | 12 ------------ app/example/fetching/fetching-component.tsx | 14 ++++++++------ app/example/fetching/page.tsx | 5 +---- app/example/request.ts | 8 -------- app/{api => server}/db-connect.ts | 0 app/server/example/action.ts | 13 +++++++++++++ app/{ => server}/example/interfaces.ts | 0 app/{api => server}/example/model.ts | 2 +- app/{api => server}/example/service.ts | 4 ++-- 9 files changed, 25 insertions(+), 33 deletions(-) delete mode 100644 app/api/example/route.ts delete mode 100644 app/example/request.ts rename app/{api => server}/db-connect.ts (100%) create mode 100644 app/server/example/action.ts rename app/{ => server}/example/interfaces.ts (100%) rename app/{api => server}/example/model.ts (88%) rename app/{api => server}/example/service.ts (87%) diff --git a/app/api/example/route.ts b/app/api/example/route.ts deleted file mode 100644 index 7a27301..0000000 --- a/app/api/example/route.ts +++ /dev/null @@ -1,12 +0,0 @@ -import type { IExample } from 'app/example/interfaces'; -import { NextResponse } from 'next/server'; - -export function GET() { - // const data = await getExample() - const data: IExample = { - title: 'hello world!', - description: 'This is an example route.', - }; - - return NextResponse.json(data); -} diff --git a/app/example/fetching/fetching-component.tsx b/app/example/fetching/fetching-component.tsx index ab9e2cc..3dd4a2b 100644 --- a/app/example/fetching/fetching-component.tsx +++ b/app/example/fetching/fetching-component.tsx @@ -1,10 +1,12 @@ -export async function getData() { - // eslint-disable-next-line - return await new Promise((resolve) => setTimeout(resolve, 1000)).then((_) => 'Success!'); -} +import { getExample } from 'app/server/example/action'; export default async function FetchingComponent() { - const data = await getData(); + const data = await getExample(); - return
{data}
; + return ( +
+

{data.title}

+ {data.description} +
+ ); } diff --git a/app/example/fetching/page.tsx b/app/example/fetching/page.tsx index 089c9da..734340d 100644 --- a/app/example/fetching/page.tsx +++ b/app/example/fetching/page.tsx @@ -1,10 +1,9 @@ import Loading from 'app/common/components/ui/loading'; import { Suspense } from 'react'; -import ClientComponent from './client-component'; import FetchingComponent from './fetching-component'; -export const runtime = 'edge'; +export const revalidate = 0; export default function ExamplePage() { return ( @@ -12,8 +11,6 @@ export default function ExamplePage() { }> - - ); } diff --git a/app/example/request.ts b/app/example/request.ts deleted file mode 100644 index 085a55d..0000000 --- a/app/example/request.ts +++ /dev/null @@ -1,8 +0,0 @@ -import type { IExample } from 'app/example/interfaces'; - -export async function getExample() { - const res = await fetch('/api/example'); - const data: IExample = await res.json(); - - return data; -} diff --git a/app/api/db-connect.ts b/app/server/db-connect.ts similarity index 100% rename from app/api/db-connect.ts rename to app/server/db-connect.ts diff --git a/app/server/example/action.ts b/app/server/example/action.ts new file mode 100644 index 0000000..946c72c --- /dev/null +++ b/app/server/example/action.ts @@ -0,0 +1,13 @@ +'use server'; + +import type { IExample } from 'app/server/example/interfaces'; + +export async function getExample() { + // const data = await getExample() <- this is where you would call your service + const data: IExample = { + title: 'hello world!', + description: 'This is an example route.', + }; + + return data; +} diff --git a/app/example/interfaces.ts b/app/server/example/interfaces.ts similarity index 100% rename from app/example/interfaces.ts rename to app/server/example/interfaces.ts diff --git a/app/api/example/model.ts b/app/server/example/model.ts similarity index 88% rename from app/api/example/model.ts rename to app/server/example/model.ts index a4d177d..6159888 100644 --- a/app/api/example/model.ts +++ b/app/server/example/model.ts @@ -1,4 +1,4 @@ -import type { IExample } from 'app/example/interfaces'; +import type { IExample } from 'app/server/example/interfaces'; import type { Model } from 'mongoose'; import mongoose, { model } from 'mongoose'; diff --git a/app/api/example/service.ts b/app/server/example/service.ts similarity index 87% rename from app/api/example/service.ts rename to app/server/example/service.ts index 3b753ff..f286c17 100644 --- a/app/api/example/service.ts +++ b/app/server/example/service.ts @@ -1,5 +1,5 @@ -import dbConnect from '@api/db-connect'; -import type { IExample } from 'app/example/interfaces'; +import dbConnect from 'app/server/db-connect'; +import type { IExample } from 'app/server/example/interfaces'; import Example from './model';