fastify-file-interceptor
Advanced tools
Comparing version 1.0.6 to 1.0.7
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.randomStringGenerator = void 0; | ||
const nanoid_1 = require("nanoid"); | ||
const randomStringGenerator = () => nanoid_1.nanoid(); | ||
const uuid_1 = require("uuid"); | ||
const randomStringGenerator = () => uuid_1.v4(); | ||
exports.randomStringGenerator = randomStringGenerator; | ||
//# sourceMappingURL=random-generator.js.map |
@@ -1,3 +0,3 @@ | ||
import { nanoid } from "nanoid"; | ||
import { v4 as uuid } from "uuid"; | ||
export const randomStringGenerator = () => nanoid(); | ||
export const randomStringGenerator = () => uuid(); |
{ | ||
"name": "fastify-file-interceptor", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"description": "This library for Nest.js using FastifyAdapter it rely on library fastify-multer and express multer", | ||
"description": "This library for Nestjs using FastifyAdapter it rely on library fastify-multer and express multer", | ||
"author": { | ||
@@ -21,2 +21,3 @@ "name": "Rom", | ||
"@types/node": "^15.0.1", | ||
"@types/uuid": "^8.3.0", | ||
"fastify": "^3.15.1", | ||
@@ -26,3 +27,5 @@ "reflect-metadata": "^0.1.13", | ||
"rxjs": "^7.0.0", | ||
"typescript": "^4.2.4" | ||
"sinon": "^11.1.1", | ||
"typescript": "^4.2.4", | ||
"uuid": "^8.3.2" | ||
}, | ||
@@ -33,4 +36,6 @@ "keywords": [ | ||
"interceptor", | ||
"upload file", | ||
"upload", | ||
"fastify file interceptor", | ||
"form", | ||
"post", | ||
"multipart", | ||
@@ -40,9 +45,8 @@ "form-data", | ||
"fastify", | ||
"@nestjs/platform-fastify" | ||
"middleware" | ||
], | ||
"dependencies": { | ||
"fastify-multer": "2.0.2", | ||
"multer": "1.4.2", | ||
"nanoid": "^3.1.25", | ||
"tslib": "2.2.0" | ||
"fastify-multer": "2.0.3", | ||
"multer": "1.4.5-lts", | ||
"tslib": "^2.2.0" | ||
}, | ||
@@ -49,0 +53,0 @@ "peerDependencies": { |
131
README.MD
@@ -1,32 +0,42 @@ | ||
**Installation** | ||
## Installation | ||
`npm install fastify-file-interceptor` | ||
</br> | ||
`yarn add fastify-file-interceptor` | ||
</br> | ||
**FileFastifyInterceptor() may not be compatible with third party cloud providers like Google Firebase or others.** | ||
**This package is base on NestJs ,fastify-multer** | ||
This libray for Nest.Js Application use FastifyAdapter that can't use | ||
@UseInterceptor or UploadFile decorator this package has provide same functionality as Nest.Js Application use ExpressAdaper,Now it support @UseInterceptor for upload file | ||
```javascript | ||
### Warning | ||
FileFastifyInterceptor() may not be compatible with third party cloud providers like Google Firebase or others ,This package is base on fastify-multer | ||
### npm | ||
```shell | ||
$ npm install fastify-file-interceptor | ||
``` | ||
### yarn | ||
```shell | ||
$ yarn add fastify-file-interceptor | ||
``` | ||
--- | ||
```typescript | ||
/* main.ts */ | ||
import "reflect-metadata"; | ||
import { | ||
FastifyAdapter, | ||
NestFastifyApplication, | ||
} from "@nestjs/platform-fastify"; | ||
import { join } from "path"; | ||
// update you can import from "fastify-file-interceptor" | ||
import { contentParser, MulterFile } from "fastify-file-interceptor"; | ||
import { contentParser } from 'fastify-file-interceptor'; | ||
import 'reflect-metadata'; | ||
import { FastifyAdapter,NestFastifyApplication } from '@nestjs/platform-fastify'; | ||
import { join } from "path" | ||
async function bootstrap(): Promise<void> { | ||
const app = | ||
(await NestFactory.create) < | ||
NestFastifyApplication > | ||
(AppModule, new FastifyAdapter()); | ||
const app = await NestFactory.create<NestFastifyApplication>( | ||
AppModule, | ||
new FastifyAdapter() | ||
); | ||
await app.listen(3000); | ||
app.register(contentParser); | ||
/* yarn add fastify-static */ | ||
app.useStaticAsset({ root: join(__dirname, "../../example") }); | ||
//add this line | ||
app.register(contentParser); yarn add fastify-multer | ||
app.useStaticAsset({root: join(__dirname,"../../example")}) yarn add fastify-static | ||
} | ||
@@ -36,4 +46,7 @@ bootstrap(); | ||
```typescript | ||
/* app.controller.ts */ | ||
import { | ||
@@ -48,3 +61,2 @@ Body, | ||
import { ApiConsumes, ApiTags } from "@nestjs/swagger"; | ||
import { diskStorage } from "multer"; | ||
import { AppService } from "./app.service"; | ||
@@ -63,2 +75,3 @@ import { | ||
FilesFastifyInterceptor, | ||
diskStorage | ||
} from "fastify-file-interceptor"; | ||
@@ -70,3 +83,3 @@ @Controller() | ||
// Single File | ||
// Upload single file | ||
@ApiConsumes("multipart/form-data") | ||
@@ -83,3 +96,6 @@ @Post("single-file") | ||
) | ||
single(@UploadedFile() file: MulterFile, @Body() body: SingleFileDto) { | ||
single( | ||
@UploadedFile() file: Express.Multer.File, | ||
@Body() body: SingleFileDto | ||
) { | ||
console.log({ ...body, photo_url: file }); | ||
@@ -89,3 +105,3 @@ return { ...body, photo_url: file }; | ||
// Multiple File | ||
// Upload multiple file | ||
@ApiConsumes("multipart/form-data") | ||
@@ -103,3 +119,3 @@ @Post("multiple-file") | ||
multiple( | ||
@UploadedFiles() files: MulterFile[], | ||
@UploadedFiles() files: Express.Multer.File[], | ||
@Body() body: MultipleFileDto | ||
@@ -111,3 +127,3 @@ ) { | ||
// Any File | ||
// Upload any file | ||
@ApiConsumes("multipart/form-data") | ||
@@ -124,3 +140,6 @@ @Post("any-file") | ||
) | ||
anyFile(@UploadedFiles() files: MulterFile, @Body() body: AnyFileDto) { | ||
anyFile( | ||
@UploadedFiles() files: Express.Multer.File, | ||
@Body() body: AnyFileDto | ||
) { | ||
console.log({ ...body, photo_url: files }); | ||
@@ -130,3 +149,3 @@ return { ...body, photo_url: files }; | ||
// File Field | ||
// Upload multiple files with different fields | ||
@ApiConsumes("multipart/form-data") | ||
@@ -162,5 +181,5 @@ @Post("fields-file") | ||
**NOTE property destination inside diskStorage is the location in our project directory where we want to store the image** | ||
</br> | ||
#### Notes : property destination inside distStorage is the location in our project directory where we want to store the image | ||
> file-upload-util.ts | ||
@@ -172,3 +191,7 @@ | ||
export const editFileName = (req: Request, file: MulterFile, callback) => { | ||
export const editFileName = ( | ||
req: Request, | ||
file: Express.Multer.File, | ||
callback | ||
) => { | ||
const name = file.originalname.split(".")[0]; | ||
@@ -179,3 +202,7 @@ const fileExtName = extname(file.originalname); | ||
export const imageFileFilter = (req: Request, file: MulterFile, callback) => { | ||
export const imageFileFilter = ( | ||
req: Request, | ||
file: Express.Multer.File, | ||
callback | ||
) => { | ||
if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) { | ||
@@ -188,7 +215,10 @@ return callback(new Error("Only image files are allowed!"), false); | ||
**Hint** | ||
save image as CDN and display in browser | ||
### Create url display image in browser | ||
#### We use interface FastifyRequest | ||
```typescript | ||
/* file-mapper.ts */ | ||
```javascript | ||
/* file-mapper.ts */ | ||
import { FastifyRequest } from "fastify"; | ||
@@ -206,2 +236,3 @@ | ||
// file (single) | ||
export const fileMapper = ({ file, req }: FileMapper) => { | ||
@@ -216,2 +247,3 @@ const image_url = `${req.protocol}://${req.headers.host}/${file.path}`; | ||
// files (multiple) | ||
export const filesMapper = ({ files, req }: FilesMapper) => { | ||
@@ -231,8 +263,7 @@ return files.map((file) => { | ||
if you already know how to use function from "@nestjs/platform-express" it's the same. | ||
### @nestjs/platform-fastify | ||
```typescript | ||
"fastify-file-interceptor"; | ||
import { | ||
FastifyMulterModule, | ||
AnyFilesFastifyInterceptor, | ||
@@ -242,7 +273,14 @@ FileFastifyInterceptor, | ||
FilesFastifyInterceptor, | ||
diskStorage, | ||
memoryStorage, | ||
MulterFile | ||
contentParser | ||
} from "fastify-file-interceptor"; | ||
``` | ||
### @nestjs/platform-express | ||
```typescript | ||
"@nestjs/platform-express"; | ||
@@ -257,3 +295,2 @@ import { | ||
You can install example test on swagger | ||
Example Repo on github: <a href="https://github.com/chanphiromsok/example-fastify-file-interceptor">https://github.com/chanphiromsok/example-fastify-file-interceptor</a> | ||
#### you can install example test on swagger |
Sorry, the diff of this file is not supported yet
44336
6
277
12
+ Added@fastify/busboy@1.2.1(transitive)
+ Addedfastify-multer@2.0.3(transitive)
+ Addedmkdirp@1.0.4(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedtext-decoding@1.0.0(transitive)
+ Addedtslib@2.8.1(transitive)
- Removednanoid@^3.1.25
- Removedbusboy@0.2.140.3.1(transitive)
- Removedconcat-stream@1.6.2(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddicer@0.2.50.3.0(transitive)
- Removedfastify-multer@2.0.2(transitive)
- Removedisarray@0.0.1(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedmulter@1.4.2(transitive)
- Removednanoid@3.3.7(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@1.1.142.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedstreamsearch@0.1.2(transitive)
- Removedstring_decoder@0.10.311.1.1(transitive)
- Removedtslib@2.2.0(transitive)
Updatedfastify-multer@2.0.3
Updatedmulter@1.4.5-lts
Updatedtslib@^2.2.0