Skip to content

Commit

Permalink
refactor: api 구조 리팩터링 (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
saseungmin authored Jan 25, 2024
1 parent b40190e commit 35b4542
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions @types/environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ namespace NodeJS {
interface ProcessEnv extends NodeJS.ProcessEnv {
NEXT_PUBLIC_API_HOST: string;
NEXT_PUBLIC_ORIGIN: string;
NEXT_PUBLIC_BLOG_HOST: string;
}
}
22 changes: 12 additions & 10 deletions src/app/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import dayjs from 'dayjs';

import { paramsSerializer } from '@/utils';

import { FetchRequest } from './model';
import { FetchRequest, UrlPrefixType } from './model';

const CACHE_MINUTE = 5;

Expand All @@ -24,29 +24,31 @@ export const getCacheDate = (cacheTime = CACHE_MINUTE) => {
const modMin = dayjs().get('minute') % cacheTime;
const minute = modMin === 0 ? currentMin : currentMin - modMin;

return `?date=${date}-${minute}`;
return {
date: `${date}-${minute}`,
};
};

const getUrl = (url: string, isBFF = false) => {
if (isBFF) {
const getUrl = (url: string, type: UrlPrefixType) => {
if (type === 'bff') {
return `${process.env.NEXT_PUBLIC_ORIGIN}/api${url}`;
}

if (type === 'blog') {
return `${process.env.NEXT_PUBLIC_BLOG_HOST}${url}`;
}

return `${process.env.NEXT_PUBLIC_API_HOST}${url}`;
};

async function api<T, K = undefined>({
url, params, config = {}, isBFF, method = 'GET',
url, params, config = {}, type = 'public', method = 'GET',
}: FetchRequest<K>): Promise<T> {
const response = await fetch(`${getUrl(url, isBFF)}?${paramsSerializer({
const response = await fetch(`${getUrl(url, type)}?${paramsSerializer({
...params,
})}`, {
...config,
method,
headers: {
...config.headers,
'Content-Type': 'application/json',
},
});

if (!response.ok) {
Expand Down
4 changes: 3 additions & 1 deletion src/app/api/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ type Method =
| 'link' | 'LINK'
| 'unlink' | 'UNLINK';

export type UrlPrefixType = 'public' | 'blog' | 'bff';

export interface FetchRequest<T = any> {
url: string;
params?: T;
method?: Method;
isBFF?: boolean;
type?: UrlPrefixType;
config?: Omit<RequestInit, 'method'>;
}
3 changes: 2 additions & 1 deletion src/app/api/projects/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextRequest, NextResponse } from 'next/server';

import { Project } from '@/lib/types/project';
import { paramsSerializer } from '@/utils';

import { getCacheDate } from '..';

Expand All @@ -12,7 +13,7 @@ export async function GET(request: NextRequest) {

const size = searchParams.get('size');

const response = await fetch(`${process.env.NEXT_PUBLIC_API_HOST}/data/project.json${getCacheDate()}`, {
const response = await fetch(`${process.env.NEXT_PUBLIC_API_HOST}/data/project.json?${paramsSerializer(getCacheDate())}`, {
method: 'GET',
});

Expand Down
4 changes: 2 additions & 2 deletions src/components/molecules/ProjectsSlider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ function ProjectsSlider() {

return (
<div className={styles.projectsSlider}>
<Marquee pauseOnHover speed={80} className={styles.marquee}>
<Marquee pauseOnHover speed={100} className={styles.marquee}>
<div className={styles.projectSliderWrapper}>
{projects.map(({ id, thumbnail, title }) => (
<Link href={`/projects/${id}`} key={id}>
<Link key={id} href={`/projects/${id}`} prefetch={false}>
<Image
key={id}
src={thumbnail}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/queries/useGetProjectsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function useGetProjectsQuery({ size }: { size?: number; } = {}) {
queryKey: ['projects'],
queryFn: () => api<ProjectType[], { size?: number; }>({
url: '/projects',
isBFF: true,
type: 'bff',
method: 'GET',
params: {
size,
Expand Down

0 comments on commit 35b4542

Please sign in to comment.