DEV Community

Discussion on: Nestjs(fastify, multer). Uploading & cropping image.

Collapse
 
aayani profile image
Wahaj Aayani

Couldn't get it running. How to export the UploadImageFactory to a module?

Collapse
 
sergey_telpuk profile image
Sergey Telpuk

Hi, You can do it like below:

import { Module } from '@nestjs/common';

export const UploadImageFactory: FactoryProvider = {
    provide: 'IUploadImage',
    useFactory: () => {
        return StorageFactory.createStorageFromType(TYPE_STORAGE);
    },
};

@Module({
  controllers: [],
  providers: [UploadImageFactory],
})
export class CatsModule {}
Collapse
 
aayani profile image
Wahaj Aayani • Edited

Thanks for your response. Figured that out earlier. I was somewhat trying to make it a dynamic module to be exported and used elsewhere in the code. I can see that you have hard-coded AWS config in it's adapter itself which is ofc not the best of practices, and now if you want to take it out of there and pass in from the module itself. How would you recommend doing that?!

Thread Thread
 
sergey_telpuk profile image
Sergey Telpuk

To be honest, I made this solution in a hurry. You know, I start thinking about making a library MulterCropper.
You are right about hard-coded config, This solution sucks, As for me the best way is to take it out and pass via a constructor.

Thread Thread
 
aayani profile image
Wahaj Aayani

It really helped me to say the least. Thank you. That's a nice idea for a library though.