Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11667 from mathewtrivett/support-multer-none
feat(express): add support for multer.none
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
56 changes: 56 additions & 0 deletions
56
packages/platform-express/multer/interceptors/no-files.interceptor.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
Inject, | ||
mixin, | ||
NestInterceptor, | ||
Optional, | ||
Type, | ||
} from '@nestjs/common'; | ||
import * as multer from 'multer'; | ||
import { Observable } from 'rxjs'; | ||
import { MULTER_MODULE_OPTIONS } from '../files.constants'; | ||
import { MulterModuleOptions } from '../interfaces'; | ||
import { MulterOptions } from '../interfaces/multer-options.interface'; | ||
import { transformException } from '../multer/multer.utils'; | ||
|
||
type MulterInstance = any; | ||
|
||
export function NoFilesInterceptor( | ||
localOptions?: MulterOptions, | ||
): Type<NestInterceptor> { | ||
class MixinInterceptor implements NestInterceptor { | ||
protected multer: MulterInstance; | ||
|
||
constructor( | ||
@Optional() | ||
@Inject(MULTER_MODULE_OPTIONS) | ||
options: MulterModuleOptions = {}, | ||
) { | ||
this.multer = (multer as any)({ | ||
...options, | ||
...localOptions, | ||
}); | ||
} | ||
|
||
async intercept( | ||
context: ExecutionContext, | ||
next: CallHandler, | ||
): Promise<Observable<any>> { | ||
const ctx = context.switchToHttp(); | ||
|
||
await new Promise<void>((resolve, reject) => | ||
this.multer.none()(ctx.getRequest(), ctx.getResponse(), (err: any) => { | ||
if (err) { | ||
const error = transformException(err); | ||
return reject(error); | ||
} | ||
resolve(); | ||
}), | ||
); | ||
return next.handle(); | ||
} | ||
} | ||
const Interceptor = mixin(MixinInterceptor); | ||
return Interceptor; | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/platform-express/test/multer/interceptors/no-files.inteceptor.spec.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { CallHandler } 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 { NoFilesInterceptor } from '../../../multer/interceptors/no-files.interceptor'; | ||
|
||
describe('NoFilesInterceptor', () => { | ||
it('should return metatype with expected structure', async () => { | ||
const targetClass = NoFilesInterceptor(); | ||
expect(targetClass.prototype.intercept).to.not.be.undefined; | ||
}); | ||
describe('intercept', () => { | ||
let handler: CallHandler; | ||
beforeEach(() => { | ||
handler = { | ||
handle: () => of('test'), | ||
}; | ||
}); | ||
it('should call none() with expected params', async () => { | ||
const target = new (NoFilesInterceptor())(); | ||
|
||
const callback = (req, res, next) => next(); | ||
const noneSpy = sinon | ||
.stub((target as any).multer, 'none') | ||
.returns(callback); | ||
|
||
await target.intercept(new ExecutionContextHost([]), handler); | ||
|
||
expect(noneSpy.called).to.be.true; | ||
}); | ||
it('should transform exception', async () => { | ||
const target = new (NoFilesInterceptor())(); | ||
const err = {}; | ||
const callback = (req, res, next) => next(err); | ||
(target as any).multer = { | ||
none: () => callback, | ||
}; | ||
(target.intercept(new ExecutionContextHost([]), handler) as any).catch( | ||
error => expect(error).to.not.be.undefined, | ||
); | ||
}); | ||
}); | ||
}); |