Spaces:
Sleeping
Sleeping
File size: 1,707 Bytes
cd6720a | 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 | import { isObject } from './isObject'
import { isPropertyAccessible } from './isPropertyAccessible'
/**
* Creates a generic 500 Unhandled Exception response.
*/
export function createServerErrorResponse(body: unknown): Response {
return new Response(
JSON.stringify(
body instanceof Error
? {
name: body.name,
message: body.message,
stack: body.stack,
}
: body
),
{
status: 500,
statusText: 'Unhandled Exception',
headers: {
'Content-Type': 'application/json',
},
}
)
}
export type ResponseError = Response & { type: 'error' }
/**
* Check if the given response is a `Response.error()`.
*
* @note Some environments, like Miniflare (Cloudflare) do not
* implement the "Response.type" property and throw on its access.
* Safely check if we can access "type" on "Response" before continuing.
* @see https://github.com/mswjs/msw/issues/1834
*/
export function isResponseError(response: unknown): response is ResponseError {
return (
response != null &&
response instanceof Response &&
isPropertyAccessible(response, 'type') &&
response.type === 'error'
)
}
/**
* Check if the given value is a `Response` or a Response-like object.
* This is different from `value instanceof Response` because it supports
* custom `Response` constructors, like the one when using Undici directly.
*/
export function isResponseLike(value: unknown): value is Response {
return (
isObject<Record<string, any>>(value, true) &&
isPropertyAccessible(value, 'status') &&
isPropertyAccessible(value, 'statusText') &&
isPropertyAccessible(value, 'bodyUsed')
)
}
|