Skip to content

Commit 40464cb

Browse files
committed
调整参数和类型
1 parent 4f0e604 commit 40464cb

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
},
1313
plugins: ['@typescript-eslint'],
1414
rules: {
15-
'@typescript-eslint/no-explicit-any': 'off'
15+
'@typescript-eslint/no-explicit-any': 'off',
16+
'@typescript-eslint/no-empty-interface': 'off'
1617
}
1718
}

src/service.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ function request(config: IRequestConfig) {
1616
const handleConfigRes = handleConfig(config)
1717

1818
const req = https.request(handleConfigRes, (res) => {
19+
const { headers } = res
20+
1921
const container: Buffer[] = []
2022

2123
res.on('data', (chunk) => container.push(chunk))
2224

2325
res.on('end', () => {
2426
const data = Buffer.concat(container)
2527
const resolveRes: IRequest = {
26-
contentType: res.headers['content-type'],
27-
contentLength: res.headers['content-length'],
28+
headers,
2829
data
2930
}
31+
3032
resolve(resolveRes)
3133
})
3234
})
@@ -98,8 +100,10 @@ export async function fetchFile(config: IFetchFileConfig) {
98100

99101
const requestRes = await request(item)
100102

101-
const { contentType, data } = requestRes
102-
const filename = `${new Date().getTime()}.${contentType?.split('/').pop()}`
103+
const { headers, data } = requestRes
104+
const filename = `${new Date().getTime()}.${headers['content-type']
105+
?.split('/')
106+
.pop()}`
103107
const path = `${fileConfig.storeDir}/${filename}`
104108

105109
fs.createWriteStream(path, 'binary').write(data, (err) => {

src/types.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'node:http'
2+
13
export interface IAnyObject extends Object {
24
[key: string | number | symbol]: any
35
}
@@ -10,6 +12,11 @@ export type IMapTypeEmptyObject<T extends object, E extends string = ''> = {
1012
[P in keyof T as Exclude<P, E>]?: T[P]
1113
}
1214

15+
export interface IRequest {
16+
headers: IncomingHttpHeaders
17+
data: Buffer
18+
}
19+
1320
export type IMethod =
1421
| 'get'
1522
| 'GET'
@@ -35,9 +42,7 @@ export type IMethod =
3542
export interface IRequestConfig {
3643
url: string
3744
method: IMethod
38-
headers?: {
39-
[key: string]: number | string | string[]
40-
}
45+
headers?: OutgoingHttpHeaders
4146
params?: IAnyObject
4247
data?: any
4348
timeout?: number
@@ -53,16 +58,10 @@ export interface IFetchBaseConifg {
5358
}
5459
}
5560

56-
export type IFetchConfig = IFetchBaseConifg
61+
export interface IFetchConfig extends IFetchBaseConifg {}
5762

5863
export interface IFetchFileConfig extends IFetchBaseConifg {
5964
fileConfig: {
6065
storeDir: string
6166
}
6267
}
63-
64-
export interface IRequest {
65-
contentType: string | undefined
66-
contentLength: string | undefined
67-
data: Buffer
68-
}

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RequestOptions } from 'https'
1+
import { RequestOptions } from 'http'
22

33
import Url, { URL } from 'node:url'
44
import { IAnyObject, IMapTypeEmptyObject, IRequestConfig } from './types'

test/start/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)