Created
January 5, 2023 09:42
-
-
Save MohammedALREAI/95c8ae297bb4d90eed3f576ec6450a8f to your computer and use it in GitHub Desktop.
ApiFiles swigger multer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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