forked from nitrictech/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-response.ts
More file actions
34 lines (28 loc) · 886 Bytes
/
generate-response.ts
File metadata and controls
34 lines (28 loc) · 886 Bytes
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
import { formatJSON, headersToObject } from '../utils'
export const generateResponse = async (res: Response, startTime: number) => {
const contentType = res.headers.get('Content-Type')
let data
if (contentType === 'application/json') {
data = formatJSON(await res.text())
} else if (
contentType?.startsWith('image/') ||
contentType?.startsWith('video/') ||
contentType?.startsWith('audio/') ||
contentType?.startsWith('application')
) {
const blob = await res.blob()
const url = URL.createObjectURL(blob)
data = url
} else {
data = await res.text()
}
const endTime = window.performance.now()
const responseSize = res.headers.get('Content-Length')
return {
data,
time: endTime - startTime,
status: res.status,
size: responseSize ? parseInt(responseSize) : 0,
headers: headersToObject(res.headers),
}
}