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

doesn't work with custom fastify-multipart #14

Open
andresribeiro opened this issue Apr 23, 2023 · 3 comments
Open

doesn't work with custom fastify-multipart #14

andresribeiro opened this issue Apr 23, 2023 · 3 comments

Comments

@andresribeiro
Copy link

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

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

ai-leonid commented Jul 10, 2023

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:

await fastifyApp.register(fastifyMultipart, { attachFieldsToBody: true });

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

vinaysshenoy commented Apr 29, 2024

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

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