Skip to content

Commit

Permalink
Merge pull request #100 from mbti-nf-team/refactor/fetch-body-type
Browse files Browse the repository at this point in the history
refactor(@nf-team/fetch): fetch api의 body 제네릭 타입 추가
  • Loading branch information
saseungmin authored Oct 11, 2024
2 parents a5c1572 + 6fdaae6 commit df5241b
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 243 deletions.
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": "[email protected]",
"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

0 comments on commit df5241b

Please sign in to comment.