-
Notifications
You must be signed in to change notification settings - Fork 18
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 #26 from getlarge/25-feat-create-fastify-upload-lib
feat: create Fastify upload lib
- Loading branch information
Showing
38 changed files
with
1,351 additions
and
18 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
libs/microservices/shared/fastify-multipart/.eslintrc.json
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,18 @@ | ||
{ | ||
"extends": ["../../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
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,7 @@ | ||
# microservices-shared-fastify-multipart | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Running unit tests | ||
|
||
Run `nx test microservices-shared-fastify-multipart` to execute the unit tests via [Jest](https://jestjs.io). |
12 changes: 12 additions & 0 deletions
12
libs/microservices/shared/fastify-multipart/jest.config.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,12 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'microservices-shared-fastify-multipart', | ||
preset: '../../../../jest.preset.js', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: | ||
'../../../../coverage/libs/microservices/shared/fastify-multipart', | ||
}; |
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,25 @@ | ||
{ | ||
"name": "microservices-shared-fastify-multipart", | ||
"$schema": "../../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/microservices/shared/fastify-multipart/src", | ||
"projectType": "library", | ||
"targets": { | ||
"lint": { | ||
"executor": "@nx/eslint:lint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": [ | ||
"libs/microservices/shared/fastify-multipart/**/*.ts" | ||
] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/microservices/shared/fastify-multipart/jest.config.ts" | ||
} | ||
} | ||
}, | ||
"tags": ["scope:shared", "type:utils", "platform:server"] | ||
} |
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,4 @@ | ||
export * from './lib/decorators'; | ||
export * from './lib/interceptors'; | ||
export * from './lib/multipart'; | ||
export * from './lib/storage'; |
2 changes: 2 additions & 0 deletions
2
libs/microservices/shared/fastify-multipart/src/lib/decorators/index.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,2 @@ | ||
export * from './uploaded-file.decorator'; | ||
export * from './uploaded-files.decorator'; |
11 changes: 11 additions & 0 deletions
11
libs/microservices/shared/fastify-multipart/src/lib/decorators/uploaded-file.decorator.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,11 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
|
||
import { getMultipartRequest } from '../multipart/request'; | ||
import type { StorageFile } from '../storage/storage'; | ||
|
||
export const UploadedFile = createParamDecorator( | ||
(data: unknown, ctx: ExecutionContext): StorageFile | undefined => { | ||
const req = getMultipartRequest(ctx.switchToHttp()); | ||
return req?.storageFile; | ||
}, | ||
); |
14 changes: 14 additions & 0 deletions
14
libs/microservices/shared/fastify-multipart/src/lib/decorators/uploaded-files.decorator.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,14 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
|
||
import { getMultipartRequest } from '../multipart/request'; | ||
import type { StorageFile } from '../storage/storage'; | ||
|
||
export const UploadedFiles = createParamDecorator( | ||
( | ||
data: unknown, | ||
ctx: ExecutionContext, | ||
): Record<string, StorageFile[]> | StorageFile[] | undefined => { | ||
const req = getMultipartRequest(ctx.switchToHttp()); | ||
return req?.storageFiles; | ||
}, | ||
); |
51 changes: 51 additions & 0 deletions
51
libs/microservices/shared/fastify-multipart/src/lib/interceptors/any-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,51 @@ | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
mixin, | ||
NestInterceptor, | ||
Type, | ||
} from '@nestjs/common'; | ||
import { Observable, tap } from 'rxjs'; | ||
|
||
import { handleMultipartAnyFiles } from '../multipart/handlers/any-files'; | ||
import { | ||
type TransformedUploadOptions, | ||
type UploadOptions, | ||
transformUploadOptions, | ||
} from '../multipart/options'; | ||
import { getMultipartRequest } from '../multipart/request'; | ||
import type { Storage } from '../storage'; | ||
|
||
export function AnyFilesInterceptor<S extends Storage>( | ||
options?: UploadOptions<S>, | ||
): Type<NestInterceptor> { | ||
class MixinInterceptor implements NestInterceptor { | ||
private readonly options: TransformedUploadOptions<S>; | ||
|
||
constructor() { | ||
this.options = transformUploadOptions<S>(options); | ||
} | ||
|
||
async intercept( | ||
context: ExecutionContext, | ||
next: CallHandler, | ||
): Promise<Observable<void>> { | ||
const ctx = context.switchToHttp(); | ||
const req = getMultipartRequest(ctx); | ||
|
||
const { body, files, remove } = await handleMultipartAnyFiles<S>( | ||
req, | ||
this.options, | ||
); | ||
|
||
req.body = body; | ||
req.storageFiles = files; | ||
|
||
return next.handle().pipe(tap(remove)); | ||
} | ||
} | ||
|
||
const Interceptor = mixin(MixinInterceptor); | ||
|
||
return Interceptor; | ||
} |
61 changes: 61 additions & 0 deletions
61
libs/microservices/shared/fastify-multipart/src/lib/interceptors/file-fields.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,61 @@ | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
mixin, | ||
NestInterceptor, | ||
Type, | ||
} from '@nestjs/common'; | ||
import { Observable, tap } from 'rxjs'; | ||
|
||
import { | ||
handleMultipartFileFields, | ||
UploadField, | ||
UploadFieldMapEntry, | ||
uploadFieldsToMap, | ||
} from '../multipart/handlers/file-fields'; | ||
import { | ||
type TransformedUploadOptions, | ||
type UploadOptions, | ||
transformUploadOptions, | ||
} from '../multipart/options'; | ||
import { getMultipartRequest } from '../multipart/request'; | ||
import type { Storage } from '../storage'; | ||
|
||
export function FileFieldsInterceptor<S extends Storage>( | ||
uploadFields: UploadField[], | ||
options?: UploadOptions<S>, | ||
): Type<NestInterceptor> { | ||
class MixinInterceptor implements NestInterceptor { | ||
private readonly options: TransformedUploadOptions<S>; | ||
|
||
private readonly fieldsMap: Map<string, UploadFieldMapEntry>; | ||
|
||
constructor() { | ||
this.options = transformUploadOptions(options); | ||
this.fieldsMap = uploadFieldsToMap(uploadFields); | ||
} | ||
|
||
async intercept( | ||
context: ExecutionContext, | ||
next: CallHandler, | ||
): Promise<Observable<void>> { | ||
const ctx = context.switchToHttp(); | ||
const req = getMultipartRequest(ctx); | ||
|
||
const { body, files, remove } = await handleMultipartFileFields( | ||
req, | ||
this.fieldsMap, | ||
this.options, | ||
); | ||
|
||
req.body = body; | ||
req.storageFiles = files; | ||
|
||
return next.handle().pipe(tap(remove)); | ||
} | ||
} | ||
|
||
const Interceptor = mixin(MixinInterceptor); | ||
|
||
return Interceptor; | ||
} |
53 changes: 53 additions & 0 deletions
53
libs/microservices/shared/fastify-multipart/src/lib/interceptors/file.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,53 @@ | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
mixin, | ||
NestInterceptor, | ||
Type, | ||
} from '@nestjs/common'; | ||
import { Observable, tap } from 'rxjs'; | ||
|
||
import { handleMultipartSingleFile } from '../multipart/handlers/single-file'; | ||
import { | ||
type TransformedUploadOptions, | ||
type UploadOptions, | ||
transformUploadOptions, | ||
} from '../multipart/options'; | ||
import { getMultipartRequest } from '../multipart/request'; | ||
import type { Storage } from '../storage'; | ||
|
||
export function FileInterceptor<S extends Storage>( | ||
fieldname: string, | ||
options?: UploadOptions<S>, | ||
): Type<NestInterceptor> { | ||
class MixinInterceptor implements NestInterceptor { | ||
private readonly options: TransformedUploadOptions<S>; | ||
|
||
constructor() { | ||
this.options = transformUploadOptions(options); | ||
} | ||
|
||
async intercept( | ||
context: ExecutionContext, | ||
next: CallHandler, | ||
): Promise<Observable<void>> { | ||
const ctx = context.switchToHttp(); | ||
const req = getMultipartRequest(ctx); | ||
|
||
const { file, body, remove } = await handleMultipartSingleFile( | ||
req, | ||
fieldname, | ||
this.options, | ||
); | ||
|
||
req.body = body; | ||
req.storageFile = file; | ||
|
||
return next.handle().pipe(tap(remove)); | ||
} | ||
} | ||
|
||
const Interceptor = mixin(MixinInterceptor); | ||
|
||
return Interceptor; | ||
} |
55 changes: 55 additions & 0 deletions
55
libs/microservices/shared/fastify-multipart/src/lib/interceptors/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,55 @@ | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
mixin, | ||
NestInterceptor, | ||
Type, | ||
} from '@nestjs/common'; | ||
import { Observable, tap } from 'rxjs'; | ||
|
||
import { handleMultipartMultipleFiles } from '../multipart/handlers/multiple-files'; | ||
import { | ||
type TransformedUploadOptions, | ||
type UploadOptions, | ||
transformUploadOptions, | ||
} from '../multipart/options'; | ||
import { getMultipartRequest } from '../multipart/request'; | ||
import type { Storage } from '../storage'; | ||
|
||
export function FilesInterceptor<S extends Storage>( | ||
fieldname: string, | ||
maxCount = 1, | ||
options?: UploadOptions<S>, | ||
): Type<NestInterceptor> { | ||
class MixinInterceptor implements NestInterceptor { | ||
private readonly options: TransformedUploadOptions<S>; | ||
|
||
constructor() { | ||
this.options = transformUploadOptions(options); | ||
} | ||
|
||
async intercept( | ||
context: ExecutionContext, | ||
next: CallHandler, | ||
): Promise<Observable<void>> { | ||
const ctx = context.switchToHttp(); | ||
const req = getMultipartRequest(ctx); | ||
|
||
const { body, files, remove } = await handleMultipartMultipleFiles( | ||
req, | ||
fieldname, | ||
maxCount, | ||
this.options, | ||
); | ||
|
||
req.body = body; | ||
req.storageFiles = files; | ||
|
||
return next.handle().pipe(tap(remove)); | ||
} | ||
} | ||
|
||
const Interceptor = mixin(MixinInterceptor); | ||
|
||
return Interceptor; | ||
} |
4 changes: 4 additions & 0 deletions
4
libs/microservices/shared/fastify-multipart/src/lib/interceptors/index.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,4 @@ | ||
export * from './any-files.interceptor'; | ||
export * from './file.interceptor'; | ||
export * from './file-fields.interceptor'; | ||
export * from './files.interceptor'; |
36 changes: 36 additions & 0 deletions
36
libs/microservices/shared/fastify-multipart/src/lib/multipart/exceptions.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,36 @@ | ||
import { | ||
BadRequestException, | ||
HttpException, | ||
PayloadTooLargeException, | ||
} from '@nestjs/common'; | ||
|
||
export function transformException(err: undefined): undefined; | ||
export function transformException(err: HttpException): HttpException; | ||
export function transformException(err: Error): Error; | ||
export function transformException( | ||
err: Error & { code: string }, | ||
): HttpException; | ||
export function transformException( | ||
err: Error | undefined, | ||
): Error | undefined | HttpException { | ||
if ( | ||
!err || | ||
err instanceof HttpException || | ||
!('code' in err) || | ||
typeof err.code !== 'string' | ||
) { | ||
return err; | ||
} | ||
|
||
switch (err.code) { | ||
case 'FST_REQ_FILE_TOO_LARGE': | ||
return new PayloadTooLargeException(); | ||
case 'FST_PARTS_LIMIT': | ||
case 'FST_FILES_LIMIT': | ||
case 'FST_PROTO_VIOLATION': | ||
case 'FST_INVALID_MULTIPART_CONTENT_TYPE': | ||
return new BadRequestException(err.message); | ||
} | ||
|
||
return err; | ||
} |
Oops, something went wrong.