Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@nf-team/fetch): fetch api의 body 제네릭 타입 추가 #100

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/four-pets-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nf-team/fetch": minor
---

refactor(@nf-team/fetch): fetch api의 body 제네릭 타입 추가
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
448 changes: 224 additions & 224 deletions .yarn/releases/yarn-4.4.0.cjs → .yarn/releases/yarn-4.5.0.cjs

Large diffs are not rendered by default.

10 changes: 1 addition & 9 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,4 @@ enableGlobalCache: false

nodeLinker: node-modules

supportedArchitectures:
os:
- darwin
- linux
cpu:
- x64
- arm64

yarnPath: .yarn/releases/yarn-4.4.0.cjs
yarnPath: .yarn/releases/yarn-4.5.0.cjs
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@
"email": "dbd02169@naver.com",
"url": "https://github.com/mbti-nf-team/frontend-libraries/issues"
},
"packageManager": "yarn@4.4.0"
"packageManager": "yarn@4.5.0"
}
7 changes: 5 additions & 2 deletions packages/fetch/src/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ describe('fetchApi', () => {

context('fetch가 성공한 경우', () => {
const defaultUrl = '/test/test';
const defaultHeaders = {
'Content-Type': 'application/json',
};

context('params가 없는 경우', () => {
(window.fetch as jest.Mock) = jest.fn(() => Promise.resolve({
Expand All @@ -60,7 +63,7 @@ describe('fetchApi', () => {
expect(response).toBe(mockResponse);
expect(fetch).toBeCalledWith(defaultUrl, {
body: undefined,
headers: undefined,
headers: defaultHeaders,
method: 'GET',
signal: expect.any(AbortSignal),
});
Expand All @@ -79,7 +82,7 @@ describe('fetchApi', () => {
expect(response).toBe(mockResponse);
expect(fetch).toBeCalledWith(`${defaultUrl}?test=test`, {
body: undefined,
headers: undefined,
headers: defaultHeaders,
method: 'GET',
signal: expect.any(AbortSignal),
});
Expand Down
18 changes: 11 additions & 7 deletions packages/fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export class FetchError extends Error {

export type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';

export interface FetchApiRequest<K = unknown> {
export interface FetchApiRequest<K = unknown, B = unknown> {
url: string;
params?: K;
method?: Method;
body?: unknown;
body?: B;
timeout?: number;
config?: Omit<RequestInit, 'method' | 'body'>;
}
Expand All @@ -25,14 +25,18 @@ export const paramsSerializer = <T>(params: T): string => QueryString.stringify(
indices: false,
});

async function fetchApi<T, K = unknown>({
const defaultHeaders = {
'Content-Type': 'application/json',
};

async function fetchApi<T, K = unknown, B = unknown>({
url,
params,
body,
config = {},
timeout = TIME_OUT,
method = 'GET',
}: FetchApiRequest<K>): Promise<T> {
}: FetchApiRequest<K, B>): Promise<T> {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);

Expand All @@ -42,10 +46,10 @@ async function fetchApi<T, K = unknown>({
body: body ? JSON.stringify(body) : undefined,
method,
signal: controller.signal,
headers: body ? {
'Content-Type': 'application/json',
headers: {
...defaultHeaders,
...config.headers,
} : config.headers,
},
});

clearTimeout(id);
Expand Down
Loading