1

I have been playing with Deno Oak. Looked at some of the basic routing examples and none of them are using types for request or response.

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",
    };
  }

In above example, request and response is of any type. I tried to replace it with following types which are not compatible for body?

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

I'll appreciate if someone can point me to a relevant example or shed some light on this.

Gurpreet Singh
  • 20,907
  • 5
  • 44
  • 60

1 Answers1

2

ServerRequest & ServerResponse are types used by Deno net. Oak uses Request & Response

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

You can see that Oak Response, has a method toServerResponse that converts from Oak's Response to 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> {
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98