nestia icon indicating copy to clipboard operation
nestia copied to clipboard

TypedFormData.Body with fastify

Open tjhiggins opened this issue 11 months ago • 2 comments

Feedback

I upgraded to the latest version of nestia and wasn't thrilled about having to use fastify-multer with TypedFormData.Body. Since fastify-multer is just an unmaintained port that was last published 3 years ago.

I wanted to keep using @fastify/multipart so I hacked around and got it working how I wanted with nestia. Figured I'd share since I think the new interface is less desirable for fastify users.

The nice part is that it uses the normal nestjs body and the validation pipes apply to it because of attachFieldsToBody. Obviously super hacky, but maybe you could add first class support for @fastify/multipart? Also please let me know if there was a better way.



export type FileUpload = {
  name: string;
  type: string;
  value: Buffer;
};

// main.ts
import multipart from "@fastify/multipart";

app.register(multipart, {
  attachFieldsToBody: "keyValues",
  onFile: async (part) => {
    const file: FileUpload = {
      name: part.filename,
      type: part.mimetype,
      value: await part.toBuffer(),
    };
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-expect-error
    part.value = file;
  },
});

// dto.ts
export class ExampleDto {
  @IsDefined()
  file: FileUpload;

  @IsString()
  name: string;
}

// controller.ts
@Post('/example')
@SwaggerCustomizerFile()
async uploadFile(@Body() dto: ExampleDto) {
  console.log(dto.file)
}

// swagger.ts (super hacky)
import { SwaggerCustomizer } from '@nestia/core';

export function SwaggerCustomizerFile() {
  return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    SwaggerCustomizer((props: SwaggerCustomizer.IProps) => {
      const neighbor = props.at(descriptor.value);
      const content = neighbor?.route.requestBody?.content;
      if (content) {
        const oldValue = content['application/json'];

        const schema = oldValue?.schema;
        if (schema && '$ref' in schema) {
          const componentName = schema['$ref'].split('/').pop()!;
          const component = props.swagger.components.schemas![componentName];

          if ('properties' in component && component.properties?.file) {
            component.properties.file = { type: 'file' } as any;
          }
        }

        delete content['application/json'];
        content['multipart/form-data'] = oldValue;
      }
    })(target, propertyKey, descriptor);
  };
}

tjhiggins avatar Jan 10 '25 15:01 tjhiggins

The best case scenario for me would be if nestia was okay with having the File type in a dto. Also it would update the content type to be multipart/form-data if a file type existed in the dto.

export class ExampleDto {
  @IsDefined()
  file: File;

  @IsString()
  name: string;
}

tjhiggins avatar Jan 10 '25 15:01 tjhiggins

How about making your new library? As you've succeeded to hacking the @fastify/multipart, you can make it by yourself.

After you've succeeded, @nestia will accept your new library.

samchon avatar Aug 08 '25 16:08 samchon