Skip to content

Instantly share code, notes, and snippets.

@MohammedALREAI
Created January 5, 2023 09:42
Show Gist options
  • Save MohammedALREAI/95c8ae297bb4d90eed3f576ec6450a8f to your computer and use it in GitHub Desktop.
Save MohammedALREAI/95c8ae297bb4d90eed3f576ec6450a8f to your computer and use it in GitHub Desktop.
ApiFiles swigger multer
import { ApiBody } from '@nestjs/swagger';
interface IApiFileSwigger {
files: Array < { name: string, description: string, required: boolean }>;
}
export const ApiFiles = ({files}: IApiFileSwigger): MethodDecorator => (
target:any, propertyKey : string | symbol,
descriptor: any) => {
let schema: { type: string, properties: any, required: string[] } = { type: 'object', properties: {}, required: []};
for (const file of files) {
schema.properties[file.name] = { type: 'string', description: file.description ,format: 'binary'};
if (file.required) {
schema.required.push(file.name);
}
return ApiBody({
type: 'multipart/form-data',
schema,
required: true,
isArray: true,
})(target, propertyKey, descriptor);
}
}
to used it in controller like this :
ApiConsumes('multipart/form-data')
ApiFiles({
files: [
{ name: 'file', description: 'file', required: true },
{ name: 'file2', description: 'file2', required: false },
],})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment