Skip to content

Commit 5fe4966

Browse files
committed
fetch API renamed to fetchData API
1 parent 1fe00c6 commit 5fe4966

File tree

6 files changed

+35
-35
lines changed

6 files changed

+35
-35
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Create a crawler instance via new XCrawl.
4545
class XCrawl {
4646
private readonly baseConfig
4747
constructor(baseConfig?: IXCrawlBaseConifg)
48-
fetch<T = any>(config: IFetchConfig): Promise<IFetch<T>>
48+
fetchData<T = any>(config: IFetchDataConfig): Promise<IFetchData<T>>
4949
fetchFile(config: IFetchFileConfig): Promise<IFetchFile>
5050
fetchHTML(url: string): Promise<JSDOM>
5151
}
@@ -67,14 +67,14 @@ const myXCrawl = new XCrawl({
6767
})
6868
```
6969
70-
### fetch
70+
### fetchData
7171
72-
fetch is the method of the above <a href="#myXCrawl" style="text-decoration: none">myXCrawl</a> instance, which is usually used to crawl APIs to obtain JSON data and so on.
72+
fetchData is the method of the above <a href="#myXCrawl" style="text-decoration: none">myXCrawl</a> instance, which is usually used to crawl APIs to obtain JSON data and so on.
7373
7474
- Type
7575
7676
```ts
77-
function fetch<T = any>(config: IFetchConfig): Promise<T>
77+
function fetchData <T = any>(config: IFetchDataConfig): Promise<T>
7878
```
7979

8080
- Example
@@ -86,7 +86,7 @@ const requestConifg = [
8686
{ url: '/xxxx', method: 'GET' }
8787
]
8888

89-
myXCrawl.fetch({
89+
myXCrawl.fetchData({
9090
requestConifg, // Request configuration, can be IRequestConfig | IRequestConfig[]
9191
intervalTime: 800 // Interval between next requests, multiple requests are valid
9292
}).then(res => {
@@ -188,10 +188,10 @@ interface IFetchBaseConifg {
188188
}
189189
```
190190
191-
- IFech
191+
- IFechData
192192
193193
```ts
194-
type IFetch<T> = {
194+
type IFetchData<T> = {
195195
statusCode: number | undefined
196196
headers: IncomingHttpHeaders // node:http
197197
data: T
@@ -219,10 +219,10 @@ interface IXCrawlBaseConifg {
219219
}
220220
```
221221
222-
- IFetchConfig
222+
- IFetchDataConfig
223223
224224
```ts
225-
interface IFetchConfig extends IFetchBaseConifg {
225+
interface IFetchDataConfig extends IFetchBaseConifg {
226226
}
227227
```
228228
@@ -291,7 +291,7 @@ docsXCrawl.fetchHTML('/zh/get-started').then((jsdom) => {
291291
class XCrawl {
292292
private readonly baseConfig
293293
constructor(baseConfig?: IXCrawlBaseConifg)
294-
fetch<T = any>(config: IFetchConfig): Promise<IFetch<T>>
294+
fetchData<T = any>(config: IFetchDataConfig): Promise<IFetchData<T>>
295295
fetchFile(config: IFetchFileConfig): Promise<IFetchFile>
296296
fetchHTML(url: string): Promise<JSDOM>
297297
}
@@ -313,14 +313,14 @@ const myXCrawl = new XCrawl({
313313
})
314314
```
315315

316-
### fetch
316+
### fetchData
317317

318318
fetch 是上面 <a href="#cn-myXCrawl" style="text-decoration: none">myXCrawl</a> 实例的方法,通常用于爬取 API ,可获取 JSON 数据等等。
319319

320320
- 类型
321321

322322
```ts
323-
function fetch<T = any>(config: IFetchConfig): Promise<T>
323+
function fetchData<T = any>(config: IFetchDataConfig): Promise<T>
324324
```
325325

326326
- 示例
@@ -332,7 +332,7 @@ const requestConifg = [
332332
{ url: '/xxxx', method: 'GET' }
333333
]
334334
335-
myXCrawl.fetch({
335+
myXCrawl.fetchData({
336336
requestConifg, // 请求配置, 可以是 IRequestConfig | IRequestConfig[]
337337
intervalTime: 800 // 下次请求的间隔时间, 多个请求才有效
338338
}).then(res => {
@@ -434,7 +434,7 @@ interface IFetchBaseConifg {
434434
}
435435
```
436436

437-
- IFetch
437+
- IFetchData
438438

439439
```ts
440440
type IFetch<T> = {
@@ -465,10 +465,10 @@ interface IXCrawlBaseConifg {
465465
}
466466
```
467467

468-
- IFetchConfig
468+
- IFetchDataConfig
469469

470470
```ts
471-
interface IFetchConfig extends IFetchBaseConifg {
471+
interface IFetchDataConfig extends IFetchBaseConifg {
472472
}
473473
```
474474

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "x-crawl",
4-
"version": "0.0.3",
4+
"version": "0.1.0",
55
"author": "CoderHxl",
66
"description": "XCrawl is a Nodejs multifunctional crawler library.",
77
"license": "MIT",

publish/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "x-crawl",
3-
"version": "0.0.3",
3+
"version": "0.1.0",
44
"author": "CoderHxl",
55
"description": "XCrawl is a Nodejs multifunctional crawler library.",
66
"license": "MIT",

src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { batchRequest, request } from './request'
66
import { isArray, mergeConfig } from './utils'
77

88
import {
9-
IFetch,
10-
IFetchConfig,
9+
IFetchData,
10+
IFetchDataConfig,
1111
IFetchFile,
1212
IFetchFileConfig,
1313
IRequest,
@@ -21,14 +21,14 @@ export default class XCrawl {
2121
this.baseConfig = baseConfig
2222
}
2323

24-
async fetch<T = any>(config: IFetchConfig): Promise<IFetch<T>> {
24+
async fetchData<T = any>(config: IFetchDataConfig): Promise<IFetchData<T>> {
2525
const { requestConifg, intervalTime } = mergeConfig(this.baseConfig, config)
2626

2727
const requestConfigQueue = isArray(requestConifg)
2828
? requestConifg
2929
: [requestConifg]
3030

31-
const container: IFetch<T> = []
31+
const container: IFetchData<T> = []
3232

3333
await batchRequest(requestConfigQueue, intervalTime, (requestRes) => {
3434
const data: T = JSON.parse(requestRes.data.toString())
@@ -45,6 +45,10 @@ export default class XCrawl {
4545
config
4646
)
4747

48+
const requestConfigQueue = isArray(requestConifg)
49+
? requestConifg
50+
: [requestConifg]
51+
4852
let successCount = 0
4953
const container: IFetchFile = []
5054

@@ -83,10 +87,6 @@ export default class XCrawl {
8387
})
8488
}
8589

86-
const requestConfigQueue = isArray(requestConifg)
87-
? requestConifg
88-
: [requestConifg]
89-
9090
batchRequest(requestConfigQueue, intervalTime, eachRequestResHandle)
9191
})
9292
}

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ export interface IFetchBaseConifg {
6262
intervalTime?: IIntervalTime
6363
}
6464

65-
export interface IFetchConfig extends IFetchBaseConifg {}
65+
export interface IFetchDataConfig extends IFetchBaseConifg {}
6666

6767
export interface IFetchFileConfig extends IFetchBaseConifg {
6868
fileConfig: {
6969
storeDir: string
7070
}
7171
}
7272

73-
export type IFetch<T> = {
73+
export type IFetchData<T> = {
7474
statusCode: number | undefined
7575
headers: IncomingHttpHeaders
7676
data: T

test/modal/test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function httpProtocol() {
2121
const httpXCrawl = new XCrawl()
2222

2323
httpXCrawl
24-
.fetch({
24+
.fetchData({
2525
requestConifg: {
2626
url: 'http://localhost:9001/api/home/goodprice'
2727
}
@@ -32,7 +32,7 @@ function httpProtocol() {
3232

3333
function httpsProtocol() {
3434
return new Promise((resolve) => {
35-
const httpsXCrawl = new XCrawl()
35+
const httpsXCrawl = new XCrawl({ timeout: 10000 })
3636

3737
httpsXCrawl
3838
.fetchHTML('https://docs.github.com/zh')
@@ -49,12 +49,12 @@ test('https protocol', async () => {
4949
})
5050

5151
// API
52-
function fetchAPI() {
52+
function fetchDataAPI() {
5353
return new Promise((resolve) => {
5454
const myXCrawl = new XCrawl()
5555

5656
myXCrawl
57-
.fetch({
57+
.fetchData({
5858
requestConifg: {
5959
url: 'http://localhost:9001/api/area/阳江市',
6060
method: 'POST',
@@ -80,7 +80,7 @@ function fetchFileAPI() {
8080
})
8181

8282
myXCrawl
83-
.fetch({
83+
.fetchData({
8484
requestConifg: {
8585
url: 'http://localhost:9001/api/area/阳江市',
8686
method: 'POST',
@@ -117,8 +117,8 @@ function fetchHTMLAPI() {
117117
})
118118
}
119119

120-
test('fetch API', async () => {
121-
await expect(fetchAPI()).resolves.toBe(true)
120+
test('fetchData API', async () => {
121+
await expect(fetchDataAPI()).resolves.toBe(true)
122122
})
123123

124124
test('fetchFile API', async () => {

0 commit comments

Comments
 (0)