Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新依赖项,替换已弃用的包 #9

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ logs
build
.uploads
.uploads_2
nest-file-fastify
70 changes: 37 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
<div align="left">
<h1> fastify-multipart for Nest.js</h1>
</div>

[![Github Actions](https://img.shields.io/github/workflow/status/blazity/nest-file-fastify/Build?style=flat-square)](https://github.com/Blazity/nest-file-fastify)
[![NPM](https://img.shields.io/npm/v/@blazity/nest-file-fastify.svg?style=flat-square)](https://www.npmjs.com/package/@blazity/nest-file-fastify)
[![NPM](https://img.shields.io/npm/dm/@blazity/nest-file-fastify?style=flat-square)](https://www.npmjs.com/package/@blazity/nest-file-fastify)

</div>
这个库为[Nest.js](https://github.com/nestjs/nest)添加了装饰器,以支持[@fastify/multipart](https://github.com/fastify/fastify-multipart)。其 API 与官方的 Nest.js Express 文件装饰器非常相似。

This library adds decorators for [Nest.js](https://github.com/nestjs/nest) to support [@fastify/multipart](https://github.com/fastify/fastify-multipart). The API is very similar to the official Nest.js Express file decorators.

## Installation
## 安装

NPM

```bash
$ npm install @blazity/nest-file-fastify @fastify/multipart
$ npm install nest-file-fastify @fastify/multipart
```

Yarn

```bash
$ yarn add @blazity/nest-file-fastify @fastify/multipart
$ yarn add nest-file-fastify @fastify/multipart
```
pnpm

and register multpart plugin in your Nest.js application
```bash
$ pnpm install nest-file-fastify @fastify/multipart
```
并在您的 Nest.js 应用程序中注册 multipart 插件

```typescript
import fastyfyMultipart from '@fastify/multipart';
Expand All @@ -33,12 +34,18 @@ import fastyfyMultipart from '@fastify/multipart';
app.register(fastyfyMultipart);
```

## Docs
## 文档

### 单个文件

`FileInterceptor` 参数:

### Single file
- `fieldname`: string - 包含文件的字段的名称

- `options`: 可选的 [`UploadOptions`](src/multipart/options.ts#L5) 类型对象

```ts
import { FileInterceptor, UploadedFile, MemoryStorageFile } from '@blazity/nest-file-fastify';
import { FileInterceptor, UploadedFile, MemoryStorageFile } from 'nest-file-fastify';

@Post('upload')
@UseInterceptors(FileInterceptor('file'))
Expand All @@ -47,16 +54,17 @@ uploadFile(@UploadedFile() file: MemoryStorageFile) {
}
```

`FileInterceptor` arguments:

- `fieldname`: string - name of the field that holds a file
### 数组文件

- `options`: optional object of type [`UploadOptions`](src/multipart/options.ts#L4)
`FilesInterceptor` 参数:

### Array of files
- `fieldname`: string - 包含文件的字段的名称

- `maxCount`: number - 可选的数字 - 接受的文件的最大数量

- `options`: 可选的 [`UploadOptions`](src/multipart/options.ts#L5) 类型对象
```ts
import { FilesInterceptor, UploadedFiles, MemoryStorageFile } from '@blazity/nest-file-fastify';
import { FilesInterceptor, UploadedFiles, MemoryStorageFile } from 'nest-file-fastify';

@Post('upload')
@UseInterceptors(FilesInterceptor('files'))
Expand All @@ -65,18 +73,17 @@ uploadFile(@UploadedFiles() files: MemoryStorageFile[]) {
}
```

`FilesInterceptor` arguments:
### 多个文件

- `fieldname`: string - name of the field that holds files
`FileFieldsInterceptor` 参数:

- `maxCount`: optional number - maximum number of files to accept
- `uploadFields`: 类型为 [`UploadField`](src/multipart/handlers/file-fields.ts#L11) 的数组对象

- `options`: optional object of type [`UploadOptions`](src/multipart/options.ts#L4)
- `options`: 可选的 [`UploadOptions`](src/multipart/options.ts#L5) 类型对象
```ts

### Multiple files

```ts
import { FileFieldsInterceptor, UploadedFiles, MemoryStorageFile } from '@blazity/nest-file-fastify';
import { FileFieldsInterceptor, UploadedFiles, MemoryStorageFile } from 'nest-file-fastify';

@Post('upload')
@UseInterceptors(FileFieldsInterceptor([
Expand All @@ -88,16 +95,16 @@ uploadFile(@UploadedFiles() files: { avatar?: MemoryStorageFile[], background?:
}
```

`FileFieldsInterceptor` arguments:

- `uploadFields`: object of type [`UploadField`](src/multipart/handlers/file-fields.ts#L10)
### 任意文件

`AnyFilesInterceptor` 参数:

- `options`: optional object of type [`UploadOptions`](src/multipart/options.ts#L4)
- `options`: 可选的 [`UploadOptions`](src/multipart/options.ts#L5) 类型对象

### Any files

```ts
import { AnyFilesInterceptor, UploadedFiles, MemoryStorageFile } from '@blazity/nest-file-fastify';
import { AnyFilesInterceptor, UploadedFiles, MemoryStorageFile } from 'nest-file-fastify';

@Post('upload')
@UseInterceptors(AnyFilesInterceptor()
Expand All @@ -106,6 +113,3 @@ uploadFile(@UploadedFiles() files: MemoryStorageFile[]) {
}
```

`AnyFilesInterceptor` arguments:

- `options`: optional object of type [`UploadOptions`](src/multipart/options.ts#L4)
2 changes: 1 addition & 1 deletion examples/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
FastifyAdapter,
NestFastifyApplication,
} from "@nestjs/platform-fastify";
import multipart from "fastify-multipart";
import multipart from "@fastify/multipart";

import { AppModule } from "./app-module";

Expand Down
27 changes: 22 additions & 5 deletions examples/upload/upload-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,33 @@ import {
} from "../../src";

const PATH_UPLOADS = resolve(".uploads");

const FILESIZE = 1024 * 1024 * 10;
@Controller("upload")
export class UploadController {
@Post("single")
@UseInterceptors(FileInterceptor("file"))
@UseInterceptors(
FileInterceptor("file", {
limits: {
fileSize: FILESIZE,
},
}),
)
public async uploadSingle(@UploadedFile() file: MemoryStorageFile) {
console.log(file.buffer);
console.log(file);
return file;
}

@Post("single-disk")
@UseInterceptors(FileInterceptor("file", { dest: PATH_UPLOADS }))
@UseInterceptors(
FileInterceptor("file", {
dest: PATH_UPLOADS,
limits: {
fileSize: FILESIZE,
},
}),
)
public async uploadSingleToDisk(@UploadedFile() file: DiskStorageFile) {
console.log(file.path);
console.log(file);
}

@Post("array")
Expand All @@ -53,6 +67,7 @@ export class UploadController {
},
) {
console.log(files);
return files;
}

@Post("any")
Expand All @@ -62,6 +77,7 @@ export class UploadController {
files: MemoryStorageFile[],
) {
console.log(files);
return files;
}

@Post("temp")
Expand All @@ -75,5 +91,6 @@ export class UploadController {
)
public async uploadAndRemove(@UploadedFile() file: DiskStorageFile) {
console.log(file.size);
return file;
}
}
116 changes: 58 additions & 58 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
{
"name": "@blazity/nest-file-fastify",
"version": "1.0.0",
"description": "fastify-multipart decorators for Nest.js",
"license": "MIT",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
"homepage": "https://blazity.com/",
"files": [
"build/src"
],
"keywords": [
"nest",
"nestjs",
"fastify",
"multipart",
"fastify-multipart",
"upload",
"file",
"blazity"
],
"publishConfig": {
"access": "public"
},
"scripts": {
"dev": "concurrently \"tsc --watch --p tsconfig.lib.json\" \"tsc --watch --p tsconfig.examples.json\"",
"watch": "nodemon ./build/examples/index.js",
"build:ci": "tsc --p tsconfig.lib.json && tsc --p tsconfig.examples.json",
"build": "tsc --p tsconfig.lib.json",
"lint": "eslint \"src/**\" --ext \".js,.jsx,.ts,.tsx\"",
"lint:fix": "npm run lint -- --fix",
"clean": "rimraf build",
"prepublishOnly": "yarn clean && npm run build"
},
"devDependencies": {
"@nestjs/common": "^8.0.6",
"@nestjs/config": "^1.0.1",
"@nestjs/core": "^8.0.6",
"@nestjs/platform-fastify": "^8.0.6",
"@types/busboy": "^0.2.4",
"@types/node": "^14.14.10",
"@typescript-eslint/eslint-plugin": "^4.21.0",
"@typescript-eslint/parser": "^4.21.0",
"concurrently": "^6.2.1",
"cross-env": "^7.0.3",
"eslint": "^7.32.0",
"eslint-config-prettier": "^7.0.0",
"eslint-plugin-prettier": "^3.2.0",
"fastify": "^3.21.3",
"fastify-multipart": "^5.0.0",
"nodemon": "^2.0.12",
"prettier": "^2.2.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.3.0",
"typescript": "^4.1.2"
}
}
{
"name": "nest-file-fastify",
"version": "3.0.7",
"description": "@fastify/multipart decorators for Nest.js",
"license": "MIT",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
"files": [
"build/src"
],
"repository": {
"type": "git",
"url": "https://github.com/976977577/nest-file-fastify"
},
"keywords": [
"nest",
"nestjs",
"fastify",
"multipart",
"@fastify/multipart",
"upload",
"file",
"nest-file"
],
"scripts": {
"dev": "concurrently \"tsc --watch --p tsconfig.lib.json\" \"tsc --watch --p tsconfig.examples.json\"",
"watch": "nodemon ./build/examples/index.js",
"build:ci": "tsc --p tsconfig.lib.json && tsc --p tsconfig.examples.json",
"build": "tsc --p tsconfig.lib.json",
"lint": "eslint \"src/**\" --ext \".js,.jsx,.ts,.tsx\"",
"lint:fix": "npm run lint -- --fix",
"clean": "rimraf build",
"prepublishOnly": "yarn clean && npm run build"
},
"devDependencies": {
"@fastify/multipart": "^7.7.3",
"@nestjs/common": "^10.2.1",
"@nestjs/config": "^3.0.0",
"@nestjs/core": "^10.2.1",
"@nestjs/platform-fastify": "^10.2.1",
"@types/busboy": "^1.5.0",
"@types/node": "^20.5.3",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.1",
"concurrently": "^8.2.1",
"cross-env": "^7.0.3",
"eslint": "^8.47.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"fastify": "^4.21.0",
"nodemon": "^3.0.1",
"prettier": "^3.0.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^5.0.1",
"rxjs": "^7.8.1",
"typescript": "^5.1.6"
}
}
Loading