All files / plainweb/src plain-response.ts

57.44% Statements 54/94
70% Branches 7/10
57.14% Functions 4/7
57.44% Lines 54/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 1191x 1x                         1x 4x 4x   4x 4x 4x 4x 4x         4x 4x 4x 4x 4x   4x 1x 1x       4x 3x 4x                 1x 1x 1x 1x 4x   1x                           1x                         1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   1x              
import "@kitajs/html";
import { renderToStream } from "@kitajs/html/suspense";
import type express from "express";
import type { JSONSerializable } from ".";
import type { FlashMessage } from "./flash-message";
 
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();
  }
}
 
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,
  };
}
 
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,
  };
}
 
export function redirect(
  path: string,
  opts?: { message?: FlashMessage },
): PlainResponse {
  // TODO store flash message
  return {
    _tag: "PlainResponse",
    status: 302,
    headers: {
      Location: path,
    },
  };
}
 
export function json(
  json: JSONSerializable,
  opts?: { status?: number },
): PlainResponse {
  return {
    _tag: "PlainResponse",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(json),
    status: opts?.status,
  };
}
 
export function notFound(): PlainResponse {
  return {
    _tag: "PlainResponse",
    status: 404,
    body: "Not Found",
  };
}