hapijs / hapi

The Simple, Secure Framework Developers Trust

Home Page:https://hapi.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do I set the types of `request.payload` for a multipart upload?

chmac opened this issue · comments

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: 16
  • module version: 21.3.0
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information:

How can we help?

  server.route({
    path: "/",
    method: "POST",
    options: {
      payload: {
        multipart: {
          output: "annotated",
        },
      },
    },
    handler: (request, h) => {
      console.log("#TGiVdD Got payload", (request.payload as any));
      return "Hello";
    },
  });

VSCode tells me that request.payload is of type string. Is there some way I can tell TypeScript what the correct type is?

Hi @chmac I think it's not well typed (if possible), but I have separate file with handlers where you can do something like (simplified version of my implementation so I hope it would work):

type GenericHapiHandler<REFS extends Hapi.ReqRef = Hapi.ReqRefDefaults> = (request: Hapi.Request<REFS>, h: Hapi.ResponseToolkit, err?: Error | undefined) => Hapi.Lifecycle.ReturnValue;

type IRouteRefs = {
  Payload: some type;
  Params:  ...,
  ...
}

export const handler: GenericHapiHandler<IRouteRefs> = async (request, h) => {

  return h.response().code(200);
};

@Radik24 Thanks. I'll close this issue now.