Skip to content

Commit 1b379f7

Browse files
committed
fix(api): upload form-data
1 parent 92faf70 commit 1b379f7

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

src/api/upload.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ export const token = <T = Upload.TokenResponse>(md5: string) =>
1010
)
1111

1212
export const upload = async (image: File | Buffer, token: string) => {
13+
let file: File
14+
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(image)) {
15+
file = new File([image.buffer], '?')
16+
} else {
17+
file = image as File
18+
}
1319
const formData = new FormData()
14-
formData.append('file', image as any)
20+
formData.append('file', file)
1521
formData.append('token', token)
1622
const result = await ky
1723
.post('https://upload.qiniup.com/', {

src/node-shim.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Headers,
55
Request,
66
Response,
7+
File,
78
// @ts-expect-error
89
FormData,
910
} from 'node-fetch'
@@ -19,6 +20,7 @@ if (!globals.fetch) {
1920
}
2021

2122
if (!globals.FormData) {
23+
globals.File = File
2224
globals.FormData = FormData
2325
}
2426

src/request.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { defaultEnvironment } from './constants'
44
import { generateUUID } from './utils'
55
import type { KyInstance } from 'ky/distribution/types/ky'
66
import type { BeforeRetryState } from 'ky/distribution/types/hooks'
7-
import type { ResponsePromise } from 'ky'
7+
import type { ResponsePromise, Options } from 'ky'
88

99
/**
1010
* API 配置
@@ -64,7 +64,11 @@ let apiConfig: ApiConfigResolved = undefined as any // MUST BE SET FIRST
6464
*/
6565
export const setApiConfig = (config: ApiConfig) => {
6666
apiConfig = resolveApiConfig(config)
67-
_request = ky.create({
67+
_request = ky.create(resolveKyOptions())
68+
}
69+
70+
export const resolveKyOptions = (): Options => {
71+
return {
6872
prefixUrl: apiConfig.endpointUrl,
6973
headers: {
7074
manufacturer: defaultEnvironment.manufacturer,
@@ -107,7 +111,7 @@ export const setApiConfig = (config: ApiConfig) => {
107111
},
108112
],
109113
},
110-
})
114+
}
111115
}
112116

113117
/** 获取 API 配置 */

tests/api/posts.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { setApiConfig, api } from '../../src'
33
import { PostType } from '../../src/types/options'
44
import { config } from '../config'
55

6+
setApiConfig(config)
7+
68
describe('posts should work', () => {
7-
setApiConfig(config)
89
const id = '61b8b9d298f39200100ba010'
910

1011
it('share should work', async () => {

0 commit comments

Comments
 (0)