fastify-multer icon indicating copy to clipboard operation
fastify-multer copied to clipboard

FastifyRequest is missing the fields attribute in TS

Open benediktdertinger opened this issue 4 years ago • 5 comments

🐛 Bug Report

If I use the example

  ...
  preHandler: upload.array('photos', 12),
  handler: function(request, reply) {
    // request.files is array of `photos` files
  }

from the README the request.files attribute is missing and TS is complaining TS2339: Property 'files' does not exist on type 'FastifyRequest '..

To Reproduce

Steps to reproduce the behavior:

server.route({
  method: 'POST',
  url: '/photos/upload',
  preHandler: upload.array('photos', 12),
  handler: function(request, reply) {
    // request.files is array of `photos` files
    // request.body will contain the text fields, if there were any
    reply.code(200).send('SUCCESS')
  }
})

Expected behavior

request.files is typed as File[]

  ...
  preHandler: upload.array('photos', 12),
  handler: function(request, reply) {
    // request.files is typed as File[]
  }

I fixed it extending the fastify module via:

import { FastifyInstance } from 'fastify';
import { File } from 'fastify-multer/lib/interfaces';

declare module 'fastify' {
    export interface FastifyRequest {
        files: File[];
    }
}

Your Environment

  • node version: 12
  • "fastify": "^3.3.0",
  • "fastify-multer": "^2.0.2",

benediktdertinger avatar Sep 03 '20 14:09 benediktdertinger

Would you mind to send a PR to address this?

fox1t avatar Sep 08 '20 09:09 fox1t

Hi @fox1t, I checked your code and found an existing declaration here https://github.com/nualabs/fastify-multer/blob/master/typings/fastify/index.d.ts

Replacing my workaround with your more sophisticated version works perfectly fine for me:

import 'fastify'
import { isMultipart } from '../../src/lib/content-parser'
import { File, FilesObject } from '../../src/interfaces'

type FilesInRequest = FilesObject | Partial<File>[]

declare module 'fastify' {
  interface FastifyRequest {
    isMultipart: typeof isMultipart
    file: File
    files: FilesInRequest
  }
}

As I'm not very familiar with building fastify plugins: Is this declaration maybe not loaded/initialized correctly?

benediktdertinger avatar Sep 18 '20 17:09 benediktdertinger

Very very strange. Can you make a repro repository so we can look further into it? I suppose it is not loaded for some reason.

fox1t avatar Sep 25 '20 08:09 fox1t

Hi @fox1t I created this repo for you real quick: https://github.com/nualabs/fastify-multer-ts-files-typings you will find the neccessary steps in the README. I haven't had the time to check everythig in detail so it can also be due to a mistake I made. I'm now off for some days and will reply in mid october. Have a good time :)

benediktdertinger avatar Sep 28 '20 21:09 benediktdertinger

One thing I noticed is that types in package.json is normally supposed to point at a file but in this case it points at the lib folder (and the fastify type overrides are outside that folder). I'm guessing it might simply infer the location of the types for the index.js and resolve the types via imports from there but as the fastify overrides are not imported by anything they have no effect.

pluma avatar Oct 07 '20 16:10 pluma