RequestInit
를 implements
한 class Request
에 있다.private _method;
private _mode;
private _cache;
private _credentials;
private _headers;
private _body;
private _redirect;
private _referrer;
RequestInit
에는 다음과 같이 각 메서드들의 타입들이 지정되어 있다.interface RequestInit {
body?: BodyInit | null;
cache?: RequestCache;
...
}
여기서, BodyInit
의 타입을 가져오고 싶으면, 다음과 같이 타입이 정해져 있는 것을 network.ts
로 가져와서, export
해와야 한다.
type BodyInit = Blob | BufferSource | FormData | URLSearchParams | ReadableStream<Uint8Array> | string;
export type BodyInit =
| Blob
| BufferSource
| FormData
| URLSearchParams
| ReadableStream<Uint8Array>
| string;
그 후에, 타입을 지정해준다.
private _method: Methods;
private _mode: RequestMode;
private _cache: RequestCache;
private _credentials: RequestCredentials;
private _headers: HeadersInit;
private _body: BodyInit | null;
private _redirect: RequestRedirect;
private _referrer: string;
그러면, constructor은 다음과 같다.
특히, python과 비슷하게 파라메타를 순서 없이 가져오기 위해서 지정해주었다.
constructor({
cache = 'no-cache',
body = null,
} = {}) {
this._cache = cache as RequestCache;
this._body = body ? JSON.stringify(body) : null;
}
private로 지정해줬기 때문에, getter와 setter를 작성하고 이도 마찬가지로 타입을 지정해준다.
get cache(): RequestCache {
return this._cache;
}
get body(): BodyInit | null {
return this._body;
}
set body(body: BodyInit | null) {
this._body = body;
}