Skip to content

Commit

Permalink
feat(platform-express): Allowing pipes for FileInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
lucavb committed Feb 7, 2021
1 parent d5c51c1 commit fda6664
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/platform-express/multer/interceptors/file.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
mixin,
NestInterceptor,
Optional,
PipeTransform,
Type,
} from '@nestjs/common';
import * as multer from 'multer';
Expand All @@ -19,6 +20,7 @@ type MulterInstance = any;
export function FileInterceptor(
fieldName: string,
localOptions?: MulterOptions,
...pipes: (Type<PipeTransform> | PipeTransform)[]
): Type<NestInterceptor> {
class MixinInterceptor implements NestInterceptor {
protected multer: MulterInstance;
Expand Down Expand Up @@ -49,6 +51,21 @@ export function FileInterceptor(
const error = transformException(err);
return reject(error);
}

if (pipes.length > 0) {
const request = ctx.getRequest();
request.file = pipes.reduce(
(previousFile, Pipe: Type<PipeTransform> | PipeTransform) => {
const pipeInstance: PipeTransform =
typeof Pipe === 'function' ? new Pipe() : Pipe;
return pipeInstance.transform(previousFile, {
type: 'custom',
});
},
request.file,
);
}

resolve();
},
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { CallHandler } from '@nestjs/common';
import { CallHandler, PipeTransform } from '@nestjs/common';
import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context-host';
import { expect } from 'chai';
import { of } from 'rxjs';
import * as sinon from 'sinon';
import { FileInterceptor } from '../../../multer/interceptors/file.interceptor';

const createFilePipe = (): PipeTransform => ({
transform: (): any => undefined,
});

describe('FileInterceptor', () => {
it('should return metatype with expected structure', async () => {
const targetClass = FileInterceptor('file');
Expand Down Expand Up @@ -42,5 +46,32 @@ describe('FileInterceptor', () => {
error => expect(error).to.not.be.undefined,
);
});
it('should accept pipes and call them', async () => {
const fieldName = 'file';
const pipe = createFilePipe();
const secondPipe = createFilePipe();
const myFakeFile: any = {};
const transformSpy = sinon.stub(pipe, 'transform').returns(myFakeFile);
const secondTransformSpy = sinon
.stub(secondPipe, 'transform')
.returns(undefined);
const target = new (FileInterceptor(
fieldName,
undefined,
pipe,
secondPipe,
))();
const callback = (req, res, next) => next();
sinon.stub((target as any).multer, 'single').returns(callback);

await target.intercept(
new ExecutionContextHost([{ file: myFakeFile }]),
handler,
);

expect(transformSpy.called).to.be.true;
expect(transformSpy.calledWith(myFakeFile)).to.be.true;
expect(secondTransformSpy.called).to.be.true;
});
});
});

0 comments on commit fda6664

Please sign in to comment.