Как передать токен доступа и другие данные формы при загрузке нескольких файлов в Nest JS с помощью swagger api - PullRequest
0 голосов
/ 09 апреля 2020

Мой файл стратегии jwt выглядит так:

import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy, VerifiedCallback } from 'passport-jwt';
import { Configuration } from '../../configuraiton/configuration.enum';
import { ConfigurationService } from '../../configuraiton/configuration.service';
import { AuthService } from '../auth.service';
import { JwtPayload } from '../jwt-payload';

@Injectable()
export class JwtStrategyService extends PassportStrategy(Strategy) {
    constructor(
        private readonly _authService: AuthService,
        private readonly _configurationService: ConfigurationService,
    ) {
        super({
            jwtFromRequest: ExtractJwt.fromBodyField('access_token'),
            ignoreExpiration: false,
            secretOrKey: _configurationService.get(Configuration.JWT_KEY),
        });

    }

    async validate(payload: JwtPayload, done: VerifiedCallback) {
        const user = await this._authService.validatePayload(payload);

        if (!user) {

            return done(new HttpException({}, HttpStatus.UNAUTHORIZED), false);
        }

        return done(null, user, payload.iat);
    }
}

, поэтому jwt извлекается из полей тела. Сейчас я пытаюсь реализовать метод, при котором пользователь загружает несколько изображений и создает галерею, как показано ниже:

@Post('upload')
@ApiConsumes('multipart/form-data')
@ApiMultiFile("gallery") 
@UseInterceptors( FilesInterceptor('files', 9, { fileFilter: imageFileFilter }))
async updateGallery( @UploadedFiles() gallery, @Body() galleryParams:GalleryParamsVm) {

    console.log(gallery)
    console.log(galleryParams)
}

Это код объекта body GalleryParamVm:

import { ApiPropertyOptional, ApiProperty } from "@nestjs/swagger";

export class GalleryParamsVm {

    @ApiProperty({required:true})
    galleryName: string;

    @ApiPropertyOptional()
    tag?: string;
}

Это код декоратора ApiMultiFile:

import { ApiBody } from '@nestjs/swagger';

export const ApiMultiFile = (fileName: string = 'files'): MethodDecorator => (
    target: any,
    propertyKey: string,
    descriptor: PropertyDescriptor,
) => {
    ApiBody({
        type: 'multipart/form-data',
        required: true,
        schema: {
            type: 'object',
            properties: {
                [fileName]: {
                    type: 'array',
                    items: {
                        type: 'string',
                        format: 'binary',
                    },
                },
            },
        },
    })(target, propertyKey, descriptor);
};

но на панели swagger я получаю это: enter image description here

Я не уверен, что я делаю неправильно или Как я могу отправить JWT access_token. Есть ли способ, которым я могу достичь этого?

...