-
Notifications
You must be signed in to change notification settings - Fork 3
/
httpResponse.ts
35 lines (33 loc) · 1.03 KB
/
httpResponse.ts
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
import { HttpHeaders, HttpResponse, NormalizedHttpBody, RawHttpResponse } from './httpClient.types';
import { bodyParser } from './bodyParser';
import { encodeArrayBuffer, encodeText } from './helpers/encoder.helper';
import { HttpStatusText } from './httpCodes';
import { HttpResponseHeaders } from './httpResponseHeaders';
interface HttpResponseOptions {
url?: string;
headers?: HttpHeaders;
status: number;
body?: NormalizedHttpBody;
}
export const createHttpResponse = ({
url = '',
headers,
body,
status,
}: HttpResponseOptions): HttpResponse => {
const arrayBuffer = encodeArrayBuffer(body);
const rawResponse: RawHttpResponse = {
arrayBuffer: async () => arrayBuffer,
text: async () => arrayBuffer && encodeText(arrayBuffer),
abort: () => {
throw new Error('Can not abort manually created response');
},
url,
status,
statusText: HttpStatusText[status],
redirected: false,
headers: new HttpResponseHeaders(headers),
ok: status < 400,
};
return bodyParser()(rawResponse);
};