用于请求和响应的 Deno oak 兼容类型

Deno oak compatible types for request and response

我一直在玩 Deno Oak。查看了一些基本路由示例,其中 none 正在使用请求或响应类型。

router
  .post("/api/v1/products", addProduct)


const addProduct = async (
  { request, response }: {
    request: any;
    response: any;
  },
) => {
  const body = await request.body();

  if (!body.value) {
    response.status = 404;
    response.body = {
      success: false,
      msg: "No data",
    };
  }

在上面的例子中,请求和响应是any类型。我尝试将其替换为以下与 body 不兼容的类型?

import { ServerRequest, ServerResponse } from "http://deno.land/x/oak/mod.ts";

如果有人能指出相关示例或阐明这一点,我将不胜感激。

ServerRequest & ServerResponse 是 Deno net 使用的类型。橡树使用 Request & Response

const addProduct = async (
  { request, response }: {
    request: Request;
    response: Response;
  },
)

您可以看到 Oak Response 有一个方法 toServerResponse 可以将 Oak 的 Response 转换为 Deno net ServerResponse。

/** Take this response and convert it to the response used by the Deno net
   * server.  Calling this will set the response to not be writable.
   * 
   * Most users will have no need to call this method. */
  async toServerResponse(): Promise<ServerResponse> {