Skip to content

Commit

Permalink
feat: change api to server action
Browse files Browse the repository at this point in the history
  • Loading branch information
bysxx committed Feb 2, 2024
1 parent ace0ff8 commit 295e40b
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 33 deletions.
12 changes: 0 additions & 12 deletions app/api/example/route.ts

This file was deleted.

14 changes: 8 additions & 6 deletions app/example/fetching/fetching-component.tsx
Original file line number Diff line number Diff line change
@@ -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 <div className="">{data}</div>;
return (
<div className="">
<h1>{data.title}</h1>
<span>{data.description}</span>
</div>
);
}
5 changes: 1 addition & 4 deletions app/example/fetching/page.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
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 (
<main className="flex min-h-screen flex-col items-center p-8">
<Suspense fallback={<Loading />}>
<FetchingComponent />
</Suspense>

<ClientComponent />
</main>
);
}
8 changes: 0 additions & 8 deletions app/example/request.ts

This file was deleted.

File renamed without changes.
13 changes: 13 additions & 0 deletions app/server/example/action.ts
Original file line number Diff line number Diff line change
@@ -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;
}
File renamed without changes.
2 changes: 1 addition & 1 deletion app/api/example/model.ts → app/server/example/model.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
4 changes: 2 additions & 2 deletions app/api/example/service.ts → app/server/example/service.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down

0 comments on commit 295e40b

Please sign in to comment.