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

TypedFormData.Body with fastify #1182

Open
tjhiggins opened this issue Jan 10, 2025 · 1 comment
Open

TypedFormData.Body with fastify #1182

tjhiggins opened this issue Jan 10, 2025 · 1 comment

Comments

@tjhiggins
Copy link

tjhiggins commented Jan 10, 2025

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

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;
}

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

1 participant