-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: api 구현 및 /projects api route 적용 (#27)
- install qs, dayjs dependency 설치 - api 구현 및 /projects api route 적용
- Loading branch information
1 parent
76da268
commit 99daeb0
Showing
11 changed files
with
163 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ yarn-error.log* | |
|
||
# local env files | ||
.env*.local | ||
.env | ||
|
||
# vercel | ||
.vercel | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
namespace NodeJS { | ||
interface ProcessEnv extends NodeJS.ProcessEnv { | ||
NEXT_PUBLIC_API_HOST: string; | ||
NEXT_PUBLIC_ORIGIN: string; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import dayjs from 'dayjs'; | ||
|
||
import { paramsSerializer } from '@/utils'; | ||
|
||
import { FetchRequest } from './model'; | ||
|
||
const CACHE_MINUTE = 5; | ||
|
||
// TODO - fetch error 수정 필요 | ||
export class FetchError extends Error { | ||
constructor( | ||
response: Response, | ||
) { | ||
super(); | ||
this.response = response; | ||
} | ||
|
||
response?: Response; | ||
} | ||
|
||
export const getCacheDate = (cacheTime = CACHE_MINUTE) => { | ||
const date = dayjs().format('YYYY-MM-DD-HH'); | ||
const currentMin = dayjs().get('minute'); | ||
const modMin = dayjs().get('minute') % cacheTime; | ||
const minute = modMin === 0 ? currentMin : currentMin - modMin; | ||
|
||
return `?date=${date}-${minute}`; | ||
}; | ||
|
||
const getUrl = (url: string, isBFF = false) => { | ||
if (isBFF) { | ||
return `${process.env.NEXT_PUBLIC_ORIGIN}/api${url}`; | ||
} | ||
|
||
return `${process.env.NEXT_PUBLIC_API_HOST}${url}`; | ||
}; | ||
|
||
async function api<T, K>({ | ||
url, params, config = {}, isBFF, method = 'GET', | ||
}: FetchRequest<K>): Promise<T> { | ||
const response = await fetch(`${getUrl(url, isBFF)}?${paramsSerializer({ | ||
...params, | ||
})}`, { | ||
...config, | ||
method, | ||
headers: { | ||
...config.headers, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new FetchError(response); | ||
} | ||
|
||
const data = await response.json() as Promise<T>; | ||
|
||
return data; | ||
} | ||
|
||
export default api; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
type Method = | ||
| 'get' | 'GET' | ||
| 'delete' | 'DELETE' | ||
| 'head' | 'HEAD' | ||
| 'options' | 'OPTIONS' | ||
| 'post' | 'POST' | ||
| 'put' | 'PUT' | ||
| 'patch' | 'PATCH' | ||
| 'purge' | 'PURGE' | ||
| 'link' | 'LINK' | ||
| 'unlink' | 'UNLINK'; | ||
|
||
export interface FetchRequest<T = any> { | ||
url: string; | ||
params?: T; | ||
method?: Method; | ||
isBFF?: boolean; | ||
config?: Omit<RequestInit, 'method'>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
import { getCacheDate } from '..'; | ||
|
||
export const runtime = 'edge'; | ||
|
||
export async function GET(request: NextRequest) { | ||
const requestHeaders = new Headers(request.headers); | ||
|
||
const response = await fetch(`${process.env.NEXT_PUBLIC_API_HOST}/data/project.json${getCacheDate()}`); | ||
|
||
if (!response.ok) { | ||
return NextResponse.json(null, { | ||
status: response.status, | ||
statusText: response.statusText, | ||
}); | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
return NextResponse.json(data, { | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers: requestHeaders, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import qs from 'qs'; | ||
|
||
import { paramsSerializer } from '.'; | ||
|
||
describe('paramsSerializer', () => { | ||
it('"qs.stringify"를 호출해야만 한다', () => { | ||
const qsSpyOn = jest.spyOn(qs, 'stringify'); | ||
const params = { | ||
param1: 'apple', | ||
param2: 'banana', | ||
param3: 'orange', | ||
}; | ||
|
||
const result = paramsSerializer(params); | ||
|
||
expect(result).toBe('param1=apple¶m2=banana¶m3=orange'); | ||
expect(qsSpyOn).toHaveBeenCalledWith(params, { | ||
indices: false, | ||
arrayFormat: 'comma', | ||
}); | ||
|
||
qsSpyOn.mockRestore(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import qs from 'qs'; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const paramsSerializer = <T>(params: T): string => qs.stringify(params, { | ||
arrayFormat: 'comma', | ||
indices: false, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters