Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 14x 14x 14x 14x 4x 4x 4x 4x 4x 4x 10x 14x 12x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 12x 14x 14x 14x 14x 14x 14x 12x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 13x 13x 14x 1x 1x 1x 1x 13x 14x 1x 1x 1x 1x 14x 12x 14x 14x 14x 14x 14x 14x 1x 1x 14x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 14x 14x 14x 14x 14x 12x 2x 2x 2x 2x 2x 12x 1x | import { EasyFetchDefaultConfig } from './createInstance';
import Interceptor from './Interceptor';
import type {
EasyFetchRequestType,
EasyFetchResponse,
} from './types/easyFetch.type';
import { convertEasyFetchResponse } from './utils/convertEasyFetchResponse';
import { hasEasyFetchResponse } from './utils/hasEasyFetchResponse';
import { hasResponseBody } from './utils/hasResponseBody';
import { mergeRequestConfig } from './utils/mergeRequestConfig';
class EasyFetch {
#baseUrl: EasyFetchRequestType[0] | undefined;
#headers: HeadersInit | undefined;
#interceptor: Interceptor;
constructor(defaultConfig?: EasyFetchDefaultConfig) {
this.#baseUrl = defaultConfig?.baseUrl;
this.#headers = defaultConfig?.headers;
this.#interceptor = new Interceptor();
}
async request<T>(
request: RequestInfo | URL,
requestInit?: EasyFetchRequestType[1]
) {
if (request instanceof Request) {
const [url, mergedRequestConfig] = await mergeRequestConfig(
request,
requestInit
);
return this.#request<T>(url, mergedRequestConfig);
}
return this.#request<T>(request, requestInit);
}
async #request<T>(
fetchURL: EasyFetchRequestType[0],
requestConfig?: EasyFetchRequestType[1]
) {
const combinedDefaultOptionWithFetchArgs = this.#combineDefaultOptions(
fetchURL,
requestConfig
);
const applyInterceptorRequest =
await this.#interceptor.flushRequestInterceptors(
Promise.resolve(combinedDefaultOptionWithFetchArgs)
);
//TODO: response값이 같지 않으면 에러발생 왜냐하면 transform result가 아니기 때문에
return this.#interceptor.flushResPonseInterceptors(
this.#dispatchFetch<T>(
applyInterceptorRequest[0],
applyInterceptorRequest[1]
)
) as Promise<EasyFetchResponse<T>>;
}
async #dispatchFetch<T>(
fetchURL: EasyFetchRequestType[0],
requestConfig?: EasyFetchRequestType[1]
): Promise<EasyFetchResponse<T>> {
const globalFetch = fetch;
const headers = new Headers(requestConfig?.headers);
headers.get('Content-Type') ??
headers.set('Content-Type', 'application/json');
try {
const res = await globalFetch(fetchURL, {
...requestConfig,
headers,
});
const easyFetchResponse = await convertEasyFetchResponse<T>(res, {
...requestConfig,
headers,
});
if (
!res.ok ||
(res.status < 500 && res.status >= 400) ||
!hasResponseBody(easyFetchResponse)
) {
throw new Error(`${res.status} Error`, {
cause: easyFetchResponse,
});
}
return Promise.resolve(easyFetchResponse);
} catch (err) {
if (err instanceof Error && hasEasyFetchResponse(err.cause)) {
return Promise.reject(err.cause);
} else {
throw err;
}
}
}
#combineDefaultOptions(
fetchURL: EasyFetchRequestType[0],
requestConfig: EasyFetchRequestType[1]
): EasyFetchRequestType {
let combinedDefaultUrl: EasyFetchRequestType[0] | undefined;
let combinedDefaultHeaders: EasyFetchRequestType[1];
if (this.#baseUrl) {
combinedDefaultUrl = new URL(fetchURL, this.#baseUrl);
}
if (this.#headers) {
const defaultHeaders = new Headers(this.#headers);
const requestConfigHeaders = new Headers(requestConfig?.headers);
for (const [key, value] of defaultHeaders.entries()) {
requestConfigHeaders.set(key, value);
}
combinedDefaultHeaders = {
...requestConfig,
headers: requestConfigHeaders,
};
}
return [
combinedDefaultUrl ?? fetchURL,
combinedDefaultHeaders ?? requestConfig,
];
}
get interceptor() {
return {
request: this.#interceptor.request,
response: this.#interceptor.response,
};
}
}
export default EasyFetch;
|