All files / util resolveWithStatus.server.ts

100% Statements 25/25
100% Branches 1/1
100% Functions 1/1
100% Lines 25/25

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 261x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x  
import type { RequestEvent } from "@sveltejs/kit";
 
// We don't use SvelteKit's built-in error catching to catch preview authentication errors, because
// we want to control that UI and you can't throw errors from hooks or root layouts and still catch
// them within Svelte. This means that our responses will be 200 OK, even if there was a preview
// authentication error that's making the UI show a 401 Unauthorized. This function copies the response
// and sets the status explicitly, fixing this issue. This is probably inefficient, but this is only
// used to serve error pages which are quite small relative to normal pages.
 
const resolveWithStatus = async <T extends RequestEvent>(
  status: number,
  statusText: string,
  resolve: (event: T) => Response | Promise<Response>,
  event: T,
) => {
  const response = await resolve(event);
  const wrappedResponse = new Response(response.body, {
    headers: response.headers,
    status,
    statusText,
  });
  return wrappedResponse;
};
 
export default resolveWithStatus;