fetchfox-sdk 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -1
- package/src/api.js +2 -2
- package/src/configure.js +11 -4
- package/src/crawl.js +2 -2
- package/src/detach.js +39 -20
- package/src/extract.js +7 -0
- package/src/index.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fetchfox-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "AI scraper",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -28,6 +28,13 @@
|
|
|
28
28
|
"eslint-plugin-promise": "^7.2.1",
|
|
29
29
|
"husky": "^9.1.7",
|
|
30
30
|
"jest": "^30.0.4",
|
|
31
|
+
"lint-staged": "^16.1.2",
|
|
31
32
|
"prettier": "^3.6.2"
|
|
33
|
+
},
|
|
34
|
+
"lint-staged": {
|
|
35
|
+
"*.{js,jsx,ts,tsx,css,md}": "prettier --write"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"socket.io-client": "^4.8.1"
|
|
32
39
|
}
|
|
33
40
|
}
|
package/src/api.js
CHANGED
|
@@ -15,7 +15,7 @@ const FetchFoxAPIError = class extends Error {
|
|
|
15
15
|
super(JSON.stringify(errors));
|
|
16
16
|
this.errors = errors;
|
|
17
17
|
}
|
|
18
|
-
}
|
|
18
|
+
};
|
|
19
19
|
|
|
20
20
|
export const call = async (method, path, params) => {
|
|
21
21
|
const key = apiKey(params);
|
|
@@ -52,7 +52,7 @@ export const call = async (method, path, params) => {
|
|
|
52
52
|
throw new FetchFoxAPIError({
|
|
53
53
|
status: `Received status=${resp.status}`,
|
|
54
54
|
...data,
|
|
55
|
-
})
|
|
55
|
+
});
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
return camelCase(data);
|
package/src/configure.js
CHANGED
|
@@ -2,11 +2,12 @@ const config = {
|
|
|
2
2
|
host: 'https://api.fetchfox.ai',
|
|
3
3
|
};
|
|
4
4
|
|
|
5
|
-
const isNode =
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
const isNode =
|
|
6
|
+
typeof process !== 'undefined' &&
|
|
7
|
+
process.versions != null &&
|
|
8
|
+
process.versions.node != null;
|
|
8
9
|
|
|
9
|
-
const safeEnv = (key) => isNode ? process.env[key] : null;
|
|
10
|
+
const safeEnv = (key) => (isNode ? process.env[key] : null);
|
|
10
11
|
|
|
11
12
|
export const configure = ({ apiKey, host }) => {
|
|
12
13
|
if (apiKey) {
|
|
@@ -22,3 +23,9 @@ export const apiKey = (options) =>
|
|
|
22
23
|
|
|
23
24
|
export const host = (options) =>
|
|
24
25
|
options?.host || config.host || safeEnv('FETCHFOX_HOST');
|
|
26
|
+
|
|
27
|
+
export const ws = (options) =>
|
|
28
|
+
(options?.host || config.host || safeEnv('FETCHFOX_HOST')).replace(
|
|
29
|
+
'http',
|
|
30
|
+
'ws'
|
|
31
|
+
);
|
package/src/crawl.js
CHANGED
|
@@ -3,9 +3,9 @@ import { Job } from './detach.js';
|
|
|
3
3
|
|
|
4
4
|
export const crawl = async function () {
|
|
5
5
|
return call('POST', '/api/crawl', args);
|
|
6
|
-
}
|
|
6
|
+
};
|
|
7
7
|
crawl.detach = async (args) => {
|
|
8
8
|
const data = await call('POST', '/api/crawl', { ...args, detach: true });
|
|
9
9
|
console.log('detach data', data);
|
|
10
10
|
return new Job(data.jobId);
|
|
11
|
-
}
|
|
11
|
+
};
|
package/src/detach.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
import { io } from 'socket.io-client';
|
|
2
|
+
import { ws } from './configure.js';
|
|
1
3
|
import { jobs } from './jobs.js';
|
|
2
4
|
|
|
3
|
-
class FetchFoxError extends Error {}
|
|
5
|
+
class FetchFoxError extends Error {}
|
|
4
6
|
|
|
5
7
|
const interval = 1_000;
|
|
6
8
|
|
|
9
|
+
export function getSocket() {}
|
|
10
|
+
|
|
7
11
|
export const Job = class {
|
|
8
12
|
#callbacks;
|
|
13
|
+
#socket;
|
|
9
14
|
|
|
10
15
|
constructor(id) {
|
|
11
16
|
this.id = id;
|
|
@@ -16,7 +21,15 @@ export const Job = class {
|
|
|
16
21
|
progress: [],
|
|
17
22
|
};
|
|
18
23
|
|
|
19
|
-
this
|
|
24
|
+
this.#socket = new io(ws());
|
|
25
|
+
this.#socket.on('progress', (data) => {
|
|
26
|
+
this.handleProgress(data);
|
|
27
|
+
console.log('==> socket got progress', data);
|
|
28
|
+
});
|
|
29
|
+
console.log('socket emit sub', this.id);
|
|
30
|
+
this.#socket.emit('sub', this.id);
|
|
31
|
+
|
|
32
|
+
// this.poll();
|
|
20
33
|
}
|
|
21
34
|
|
|
22
35
|
get _finished() {
|
|
@@ -24,15 +37,20 @@ export const Job = class {
|
|
|
24
37
|
}
|
|
25
38
|
|
|
26
39
|
#select(data) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
const s = {};
|
|
41
|
+
for (const key of [
|
|
42
|
+
'name',
|
|
43
|
+
'state',
|
|
44
|
+
'args',
|
|
45
|
+
'metrics',
|
|
46
|
+
'progress',
|
|
47
|
+
'results',
|
|
48
|
+
'artifacts',
|
|
49
|
+
'timer',
|
|
50
|
+
]) {
|
|
51
|
+
s[key] = data[key] || this[key];
|
|
52
|
+
}
|
|
53
|
+
return s;
|
|
36
54
|
}
|
|
37
55
|
|
|
38
56
|
async get() {
|
|
@@ -44,12 +62,17 @@ export const Job = class {
|
|
|
44
62
|
return this;
|
|
45
63
|
}
|
|
46
64
|
|
|
47
|
-
|
|
65
|
+
handleProgress(data) {
|
|
48
66
|
const last = JSON.stringify(this);
|
|
49
|
-
|
|
67
|
+
|
|
68
|
+
const s = this.#select(data);
|
|
69
|
+
for (const key of Object.keys(s)) {
|
|
70
|
+
this[key] = s[key];
|
|
71
|
+
}
|
|
72
|
+
|
|
50
73
|
const didUpdate = JSON.stringify(this) != last;
|
|
51
74
|
if (didUpdate) {
|
|
52
|
-
console.log('Job progressed:', this);
|
|
75
|
+
console.log('=> Job progressed:', this);
|
|
53
76
|
this.trigger('progress');
|
|
54
77
|
|
|
55
78
|
if (this.state == 'completed') {
|
|
@@ -65,10 +88,6 @@ export const Job = class {
|
|
|
65
88
|
this.trigger('finished');
|
|
66
89
|
}
|
|
67
90
|
}
|
|
68
|
-
|
|
69
|
-
if (!this._finished) {
|
|
70
|
-
setTimeout(() => this.poll(), interval);
|
|
71
|
-
}
|
|
72
91
|
}
|
|
73
92
|
|
|
74
93
|
checkEvent(event) {
|
|
@@ -93,7 +112,7 @@ export const Job = class {
|
|
|
93
112
|
|
|
94
113
|
off(event, cb) {
|
|
95
114
|
this.checkEvent(event);
|
|
96
|
-
this.#callbacks[event] = this.#callbacks[event].filter(it => it != cb);
|
|
115
|
+
this.#callbacks[event] = this.#callbacks[event].filter((it) => it != cb);
|
|
97
116
|
}
|
|
98
117
|
|
|
99
118
|
async waitFor(event) {
|
|
@@ -117,4 +136,4 @@ export const Job = class {
|
|
|
117
136
|
async finished() {
|
|
118
137
|
return this.waitFor('finished');
|
|
119
138
|
}
|
|
120
|
-
}
|
|
139
|
+
};
|
package/src/extract.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { call } from './api.js';
|
|
2
|
+
import { Job } from './detach.js';
|
|
2
3
|
|
|
3
4
|
export const extract = async (args) => {
|
|
4
5
|
return call('POST', '/api/extract', args);
|
|
5
6
|
};
|
|
7
|
+
|
|
8
|
+
extract.detach = async (args) => {
|
|
9
|
+
const data = await call('POST', '/api/extract', { ...args, detach: true });
|
|
10
|
+
console.log('detach data', data);
|
|
11
|
+
return new Job(data.jobId);
|
|
12
|
+
};
|
package/src/index.js
CHANGED