hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.63k stars 65 forks source link

How to use with graphql-upload? #134

Closed mikestecker closed 2 years ago

mikestecker commented 3 years ago

How do you use this with graphql-upload?

I tried putting in .use(graphqlUploadExpress({ maxFileSize: 10000, maxFiles: 10 })) per their docs, but I'm receiving this error:

error - TypeError: request.is is not a function
    at Array.graphqlUploadExpressMiddleware (/Volumes/Macintosh HD/Development/test/node_modules/graphql-upload/public/graphqlUploadExpress.js:54:18)
    at loop (webpack-internal:///./node_modules/next-connect/dist/index.cjs:69:47)
hoangvvo commented 3 years ago

This is because next.js does not provide request.is function. You can try adding a middleware before that route like so:

handler.use((req, res, next) => {
  req.is = (ctype) => req.headers['content-type'].includes(ctype)
  next()
})
artola commented 2 years ago

@mikestecker This is my middleware:

    (req, res, next) => {
      req.is = (ctype) => !!req.headers?.['content-type']?.includes(ctype);
      next();
    },