Search code examples
typescripthapi.jshapi

What is the type definition of file payload with multipart/form-data in hapijs?


I am using hapijs with typescript. I upload file in multipart/fom-data. I want to define the type of the req.payload.file. But unfortunately, I couldn't find any documentation to know the type. here is the result of console.log(req.payload.file);

Readable {
  _readableState:
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: null, tail: null, length: 0 },
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: null,
     ended: false,
     endEmitted: false,
     reading: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     resumeScheduled: false,
     paused: true,
     emitClose: true,
     autoDestroy: false,
     destroyed: false,
     defaultEncoding: 'utf8',
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: true,
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  _data:
   <Buffer 50 4b 03 04 14 00 06 00 08 00 00 00 21 00 48 41 42 ca 71 01 00 00 b0 06 00 00 13 00 08 02 5b 43 6f 6e 74 65 6e 74 5f 54 79 70 65 73 5d 2e 78 6d 6c 20 ... >,
  _position: 0,
  _encoding: 'utf8',
  hapi:
   { filename: 'file.xlsx',
     headers:
      { 'content-disposition': 'form-data; name="file"; filename="file.xlsx"',
        'content-type':
         'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' } } }

I want to make this:

const file: <ApropriateTypeHere> = req.payload.file;

so I could see all property inside the file as printed in the console.log()


Solution

  • From the documentation:

    ...the incoming payload is made available via a Stream.Readable interface.

    For which you can find more information in the Node.js documentation. The typings can be found in @types/node.