0

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()

Akza
  • 1,033
  • 3
  • 19
  • 37

2 Answers2

1

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.

Ankh
  • 5,478
  • 3
  • 36
  • 40
  • sorry but I still couldn't find appropriate type. I use interface Stream but there is no ._data property on it – Akza Feb 11 '20 at 03:19
  • 1
    There won't be, you shouldn't be accessing private variables. – Ankh Feb 11 '20 at 07:43
0

Multipart requests combine one or more sets of data into a single body, separated by boundaries.

I think you are looking for

    const filename = file.hapi.filename
    console.log(filename )
    const data = file._data

then you can do this

   fs.writeFile('./upload/' + filename, data, err => {
      if (err) {
        reject(err)
      }
      resolve({ message: 'Upload successfully!' })
    })
Manjeet Thakur
  • 2,288
  • 1
  • 16
  • 35
  • I am looking for the type definition. Not how to get the data. I know how to get the data. Yes I am using inert – Akza Feb 11 '20 at 03:03
  • @Akza there is no new definition of multipart form data in hapijs and I hope you have knowledge of Multipart requests. but there is configuration of payload output 'data', 'stream', 'file' [routeoptionspayloadmultipart](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionspayloadmultipart) – Manjeet Thakur Feb 11 '20 at 04:37