-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: feat: bff integrated file upload call (#6419)
- Loading branch information
Showing
24 changed files
with
569 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
'@modern-js/create-request': patch | ||
'@modern-js/plugin-express': patch | ||
'@modern-js/main-doc': patch | ||
'@modern-js/plugin-koa': patch | ||
'@modern-js/bff-core': patch | ||
--- | ||
|
||
feat(bff): integrated file upload call | ||
feat(bff): 支持文件上传一体化调用 |
95 changes: 95 additions & 0 deletions
95
packages/document/main-doc/docs/en/components/bff-upload.mdx
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,95 @@ | ||
BFF combined with runtime framework provides file upload capabilities, supporting integrated calls and pure function manual calls. | ||
|
||
### BFF Function | ||
|
||
First, create the `api/lambda/upload.ts` file: | ||
|
||
```ts title="api/lambda/upload.ts" | ||
export const post = async ({ formData }: {formData: Record<string, any>}) => { | ||
console.info('formData:', formData); | ||
// do somethings | ||
return { | ||
data: { | ||
code: 0, | ||
}, | ||
}; | ||
}; | ||
``` | ||
:::tip | ||
The `formData` parameter in the interface processing function can access files uploaded from the client side. It is an `Object` where the keys correspond to the field names used during the upload. | ||
::: | ||
|
||
|
||
### Integrated Calling | ||
|
||
Next, directly import and call the function in `src/routes/upload/page.tsx`: | ||
```tsx title="routes/upload/page.tsx" | ||
import { upload } from '@api/upload'; | ||
import React from 'react'; | ||
|
||
export default (): JSX.Element => { | ||
const [file, setFile] = React.useState<FileList | null>(); | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setFile(e.target.files); | ||
}; | ||
|
||
const handleUpload = () => { | ||
if (!file) { | ||
return; | ||
} | ||
upload({ | ||
files: { | ||
images: file, | ||
}, | ||
}); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<input multiple type="file" onChange={handleChange} /> | ||
<button onClick={handleUpload}>upload</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
:::tip | ||
Note: The input type must be `{ formData: FormData }` for the upload to succeed. | ||
::: | ||
|
||
|
||
### Manual Calling | ||
You can manually upload files using the `fetch API`, when calling `fetch`, set the `body` as `FormData` type and submit a post request. | ||
|
||
```tsx title="routes/upload/page.tsx" | ||
import React from 'react'; | ||
|
||
export default (): JSX.Element => { | ||
const [file, setFile] = React.useState<FileList | null>(); | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setFile(e.target.files); | ||
}; | ||
|
||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
const formData = new FormData(); | ||
if (file) { | ||
for (let i = 0; i < file.length; i++) { | ||
formData.append('images', file[i]); | ||
} | ||
await fetch('/api/upload', { | ||
method: 'POST', | ||
body: formData, | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<input multiple type="file" onChange={handleChange} /> | ||
<button type="submit">upload</button> | ||
</form> | ||
); | ||
}; | ||
``` |
2 changes: 1 addition & 1 deletion
2
packages/document/main-doc/docs/en/guides/advanced-features/bff/_meta.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 |
---|---|---|
@@ -1 +1 @@ | ||
["function", "frameworks", "extend-server", "sdk"] | ||
["function", "frameworks", "extend-server", "sdk", "upload"] |
5 changes: 5 additions & 0 deletions
5
packages/document/main-doc/docs/en/guides/advanced-features/bff/upload.mdx
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,5 @@ | ||
# File Upload | ||
|
||
import BffUpload from "@site-docs-en/components/bff-upload"; | ||
|
||
<BffUpload /> |
97 changes: 97 additions & 0 deletions
97
packages/document/main-doc/docs/zh/components/bff-upload.mdx
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,97 @@ | ||
BFF 搭配运行时框架提供了文件上传能力,支持一体化调用及纯函数手动调用。 | ||
|
||
### BFF 函数 | ||
|
||
首先创建 `api/lambda/upload.ts` 文件: | ||
|
||
```ts title="api/lambda/upload.ts" | ||
export const post = async ({ formData }: {formData: Record<string, any>}) => { | ||
console.info('formData:', formData); | ||
// do somethings | ||
return { | ||
data: { | ||
code: 0, | ||
}, | ||
}; | ||
}; | ||
``` | ||
:::tip | ||
通过接口处理函数入参中的 `formData` 可以获取客户端上传的文件。值为 `Object`,key 为上传时的字段名。 | ||
::: | ||
|
||
### 一体化调用 | ||
|
||
接着在 `src/routes/upload/page.tsx` 中直接引入函数并调用: | ||
```tsx title="routes/upload/page.tsx" | ||
import { post } from '@api/upload'; | ||
import React from 'react'; | ||
|
||
export default (): JSX.Element => { | ||
const [file, setFile] = React.useState<FileList | null>(); | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setFile(e.target.files); | ||
}; | ||
|
||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
const formData = new FormData(); | ||
if (file) { | ||
for (let i = 0; i < file.length; i++) { | ||
formData.append('images', file[i]); | ||
} | ||
post({ | ||
formData, | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<input multiple type="file" onChange={handleChange} /> | ||
<button onClick={handleUpload}>upload</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
:::tip | ||
注意:入参类型必须为:`{ formData: FormData }` 才会正确上传。 | ||
|
||
::: | ||
|
||
### 手动上传 | ||
可以基于 `fetch API` 手动上传文件,需要在调用 `fetch` 时,将 `body` 设置为 `FormData` 类型并提交 `post` 请求。 | ||
|
||
```tsx title="routes/upload/page.tsx" | ||
import React from 'react'; | ||
|
||
export default (): JSX.Element => { | ||
const [file, setFile] = React.useState<FileList | null>(); | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setFile(e.target.files); | ||
}; | ||
|
||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
const formData = new FormData(); | ||
if (file) { | ||
for (let i = 0; i < file.length; i++) { | ||
formData.append('images', file[i]); | ||
} | ||
await fetch('/api/upload', { | ||
method: 'POST', | ||
body: formData, | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<input multiple type="file" onChange={handleChange} /> | ||
<button type="submit">upload</button> | ||
</form> | ||
); | ||
}; | ||
|
||
``` |
2 changes: 1 addition & 1 deletion
2
packages/document/main-doc/docs/zh/guides/advanced-features/bff/_meta.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 |
---|---|---|
@@ -1 +1 @@ | ||
["function", "frameworks", "extend-server", "sdk"] | ||
["function", "frameworks", "extend-server", "sdk", "upload"] |
5 changes: 5 additions & 0 deletions
5
packages/document/main-doc/docs/zh/guides/advanced-features/bff/upload.mdx
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,5 @@ | ||
# 文件上传 | ||
|
||
import BffUpload from "@site-docs/components/bff-upload"; | ||
|
||
<BffUpload /> |
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
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
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
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 |
---|---|---|
|
@@ -20,4 +20,5 @@ export type APIHandlerInfo = { | |
routeName: string; | ||
// prefix+ routeName | ||
routePath: string; | ||
action?: string; | ||
}; |
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
Oops, something went wrong.