Skip to content

Commit

Permalink
test(fetcher): lodash 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
Si-off committed Jul 8, 2024
1 parent 207d7d3 commit 0698372
Showing 1 changed file with 14 additions and 36 deletions.
50 changes: 14 additions & 36 deletions src/lib/fetcher/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import merge from 'lodash/merge';
import qs from 'qs';

type FetcherRequestInit = Omit<RequestInit, 'method'>;
Expand All @@ -17,23 +16,26 @@ class Fetcher {
this.defaultRequestInit = config?.defaultRequestInit;
}

// TODO: 리팩토링 필요
optionMerge(options?: FetcherRequestInit) {
if (!options) {
return this.defaultRequestInit;
} else {
return { ...this.defaultRequestInit, ...options };
}
}

async get<T>(endpoint: string, params?: object, options?: FetcherRequestInit): Promise<T> {
try {
const url = new URL(endpoint, this.baseURL);

let requestInit = this.defaultRequestInit;

if (params) {
url.search = qs.stringify(params);
}

if (options) {
requestInit = merge(this.defaultRequestInit, options);
}

const res = await fetch(url, {
method: 'GET',
...requestInit,
...this.optionMerge(options),
});
const data: T = await res.json();

Expand All @@ -47,16 +49,10 @@ class Fetcher {
try {
const url = new URL(endpoint, this.baseURL);

let requestInit = this.defaultRequestInit;

if (options) {
requestInit = merge(this.defaultRequestInit, options);
}

const res = await fetch(url, {
method: 'POST',
body: JSON.stringify(params),
...requestInit,
...this.optionMerge(options),
});

const data: T = await res.json();
Expand All @@ -70,16 +66,10 @@ class Fetcher {
try {
const url = new URL(endpoint, this.baseURL);

let requestInit = this.defaultRequestInit;

if (options) {
requestInit = merge(this.defaultRequestInit, options);
}

const res = await fetch(url, {
method: 'PUT',
body: JSON.stringify(params),
...requestInit,
...this.optionMerge(options),
});

const data: T = await res.json();
Expand All @@ -93,16 +83,10 @@ class Fetcher {
try {
const url = new URL(endpoint, this.baseURL);

let requestInit = this.defaultRequestInit;

if (options) {
requestInit = merge(this.defaultRequestInit, options);
}

const res = await fetch(url, {
method: 'PATCH',
body: JSON.stringify(params),
...requestInit,
...this.optionMerge(options),
});

const data: T = await res.json();
Expand All @@ -116,15 +100,9 @@ class Fetcher {
try {
const url = new URL(endpoint, this.baseURL);

let requestInit = this.defaultRequestInit;

if (options) {
requestInit = merge(this.defaultRequestInit, options);
}

const res = await fetch(url, {
method: 'DELETE',
...requestInit,
...this.optionMerge(options),
});

const data: T = await res.json();
Expand Down

0 comments on commit 0698372

Please sign in to comment.