All files plain-response.ts

52.12% Statements 49/94
44.44% Branches 4/9
57.14% Functions 4/7
52.12% Lines 49/94

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 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 1301x 1x                         1x 11x 11x   11x 11x 11x 11x 11x         11x 11x 11x 11x 11x   11x           11x 11x 11x                         11x     1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x           1x                                 1x                             1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x     1x              
import "@kitajs/html";
import { renderToStream } from "@kitajs/html/suspense";
import type express from "express";
import type { FlashMessage } from "./flash-message";
import type { JSONSerializable } from "./handler";
 
export interface PlainResponse {
  _tag: "PlainResponse";
  headers?: Record<string, string>;
  status?: number;
  body?: string | Promise<string>;
  htmlStream?: (rid: number | string) => JSX.Element;
}
 
export function isPlainResponse(r: unknown): r is PlainResponse {
  return (r as PlainResponse)._tag === "PlainResponse";
}
 
export async function sendPlainResponse(
  res: express.Response,
  plainResponse: PlainResponse,
) {
  if (res.headersSent) {
    console.warn("Headers already sent, cannot send response");
    return;
  }
 
  if (plainResponse.headers) {
    for (const [key, value] of Object.entries(plainResponse.headers)) {
      res.setHeader(key, value);
    }
  }
 
  if (plainResponse.status) {
    res.status(plainResponse.status);
  }
 
  // body
 
  if (plainResponse.body) {
    res.send(await plainResponse.body);
  } else if (plainResponse.htmlStream) {
    const htmlStream = renderToStream(plainResponse.htmlStream);
    htmlStream.pipe(res);
    htmlStream.on("end", () => {
      res.end();
    });
    htmlStream.on("error", (err) => {
      console.error("Error streaming response", err);
    });
  } else {
    console.error("No body or htmlStream provided");
    res.end();
  }
}
 
/** Return a HTML response with Content-Type set to text/html. */
export function html(
  html: Promise<string> | string,
  opts?: { status?: number },
): PlainResponse {
  return {
    _tag: "PlainResponse",
    headers: {
      "Content-Type": "text/html; charset=utf-8",
    },
    body: html,
    status: opts?.status,
  };
}
 
/**
 * Stream a HTML response with Content-Type set to text/html.
 * Cache-Control is set to no-transform to prevent caching.
 */
export function stream(
  htmlStream: (rid: number | string) => JSX.Element,
): PlainResponse {
  return {
    _tag: "PlainResponse",
    headers: {
      "Content-Type": "text/html; charset=utf-8",
      "Cache-Control": "no-transform",
    },
    htmlStream,
  };
}
 
/**
 * Redirect to a new path.
 * The status code defaults to 302.
 */
export function redirect(
  path: string,
  opts?: { message?: FlashMessage; status?: number },
): PlainResponse {
  // TODO store flash message
  return {
    _tag: "PlainResponse",
    status: opts?.status ?? 302,
    headers: {
      Location: path,
    },
  };
}
 
/** Return a JSON response with Content-Type set to application/json. */
export function json(
  json: JSONSerializable,
  opts?: { status?: number },
): PlainResponse {
  return {
    _tag: "PlainResponse",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(json),
    status: opts?.status,
  };
}
 
/** Return a 404 response. */
export function notFound(): PlainResponse {
  return {
    _tag: "PlainResponse",
    status: 404,
    body: "Not Found",
  };
}