Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FastifyRequest is missing the fields attribute in TS #16

Open
benediktdertinger opened this issue Sep 3, 2020 · 5 comments
Open

FastifyRequest is missing the fields attribute in TS #16

benediktdertinger opened this issue Sep 3, 2020 · 5 comments

Comments

@benediktdertinger
Copy link

🐛 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",
@fox1t
Copy link
Owner

fox1t commented Sep 8, 2020

Would you mind to send a PR to address this?

@benediktdertinger
Copy link
Author

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
Copy link
Owner

fox1t commented Sep 25, 2020

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
Copy link
Author

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
Copy link

pluma commented Oct 7, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants