SoftwareBrothers / adminjs-fastify

MIT License
6 stars 13 forks source link

doesn't work with custom fastify-multipart #14

Open andresribeiro opened 1 year ago

andresribeiro commented 1 year ago

buildRouter already setup it's own fastify-multipart, so i can't do it again on my own, because it throws an error:

FastifyError [Error]: The decorator 'multipartErrors' has already been added!

that's alright, but in that way i can' pass my custom params. my solution is to add a param disableMultipart or something like that

ai-leonid commented 1 year ago

+1 same problem. If I try to add code like this

fastify.register(multipart);

await AdminJSFastify.buildRouter(
    admin,
    fastify,
)

I will have error: FastifyError [FST_ERR_DEC_ALREADY_PRESENT]: The decorator 'multipartErrors' has already been added!

but after disabling fastify.register(multipart); all is good

ai-leonid commented 1 year ago

One of possible hack-solution if you really need adminjs is disabling your own multipart and using multipart in adminjs.

Because in this line of buildRouter func is using multipart plugin like: https://github.com/SoftwareBrothers/adminjs-fastify/blob/04d94160fb05add1a30ab4e00a0030deee3555fe/src/buildRouter.ts#L38

What you need to know: 1) You can't use your own options for multipart 2) By default is only attachFieldsToBody: true 3) Default limits for file uploads is (from fastify-multipart documentation)

fastify.register(require('@fastify/multipart'), {
  limits: {
    fieldNameSize: 100, // Max field name size in bytes
    fieldSize: 100,     // Max field value size in bytes
    fields: 10,         // Max number of non-file fields
    fileSize: 1000000,  // For multipart forms, the max file size in bytes
    files: 1,           // Max number of file fields
    headerPairs: 2000,  // Max number of header key=>value pairs
    parts: 1000         // For multipart forms, the max number of parts (fields + files)
  }
});

For fixing the bug need something as issue-starter says disableMultipart option. Or extra-check of existing multipart plugin in fastify[Symbol.for('registered-plugin')] and set disablingMultipart automatically underhood

vinaysshenoy commented 4 months ago

If anyone else is running into this problem, you can work around it by registering adminjs as part of a different fastify scope:

const fastifyApp = fastify({})

fastifyApp.register(async (server) => {
  // Register routes here with their own multipart handlers
})

fastifyApp.register(async (server) => {
  const admin = new AdminJS({
    databases: [],
    rootPath: '/admin'
  })

  // Using the server instance received in the callback is important!
  await AdminJSFastify.buildRouter(admin, server)
})

// Start fastify app