fox1t / fastify-multer

Fastify plugin for handling multipart/form-data
MIT License
89 stars 14 forks source link

FastifyRequest is missing the fields attribute in TS #16

Open benediktdertinger opened 4 years ago

benediktdertinger commented 4 years ago

🐛 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

fox1t commented 4 years ago

Would you mind to send a PR to address this?

benediktdertinger commented 4 years ago

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?

fox1t commented 4 years ago

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.

benediktdertinger commented 4 years ago

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

pluma commented 3 years ago

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.