parreq-client 1.0.0 → 1.1.0
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/README.md +8 -8
- package/index.d.ts +12 -9
- package/index.js +19 -14
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# parreq
|
|
2
2
|
|
|
3
|
-
Клиент [
|
|
3
|
+
Клиент [Parreq](https://req.akuraq.dev) — поисковая выдача Google и Яндекса в
|
|
4
4
|
JSON. Без зависимостей, на встроенном `fetch` (нужен Node 18+). Типы в комплекте.
|
|
5
5
|
|
|
6
6
|
```bash
|
|
7
|
-
npm i parreq
|
|
7
|
+
npm i parreq-client
|
|
8
8
|
```
|
|
9
9
|
|
|
10
10
|
```js
|
|
11
|
-
import {
|
|
11
|
+
import { Parreq } from "parreq-client";
|
|
12
12
|
|
|
13
|
-
const client = new
|
|
13
|
+
const client = new Parreq({ apiKey: "pr_ВАШКЛЮЧ" });
|
|
14
14
|
|
|
15
15
|
const res = await client.yandex({ q: "кофемашина", gl: "by", hl: "ru",
|
|
16
16
|
include: ["ads", "shopping"] });
|
|
@@ -49,14 +49,14 @@ res.ads; res.shopping; res.videos; res.related;
|
|
|
49
49
|
## Ошибки
|
|
50
50
|
|
|
51
51
|
```js
|
|
52
|
-
import {
|
|
52
|
+
import { Parreq, ParreqError, NoWorkers, RateLimited } from "parreq-client";
|
|
53
53
|
|
|
54
54
|
try {
|
|
55
55
|
const res = await client.google({ q: "coffee", gl: "us" });
|
|
56
56
|
} catch (err) {
|
|
57
57
|
if (err instanceof NoWorkers) console.log("повторить через", err.retryAfter);
|
|
58
58
|
else if (err instanceof RateLimited) console.log(err.code, err.retryAfter);
|
|
59
|
-
else if (err instanceof
|
|
59
|
+
else if (err instanceof ParreqError) console.log(err.status, err.code, err.requestId);
|
|
60
60
|
else throw err;
|
|
61
61
|
}
|
|
62
62
|
```
|
|
@@ -65,8 +65,8 @@ try {
|
|
|
65
65
|
столько, сколько просит сервер в `Retry-After`. По умолчанию три попытки:
|
|
66
66
|
|
|
67
67
|
```js
|
|
68
|
-
new
|
|
69
|
-
new
|
|
68
|
+
new Parreq({ apiKey: "pr_…", retries: 0 }); // выключить повторы
|
|
69
|
+
new Parreq({ apiKey: "pr_…", timeoutMs: 240_000 }); // запрос с капчей бывает долгим
|
|
70
70
|
```
|
|
71
71
|
|
|
72
72
|
## Свой расход
|
package/index.d.ts
CHANGED
|
@@ -56,7 +56,7 @@ export declare class SearchResult {
|
|
|
56
56
|
readonly totalResults: number | null;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
export declare class
|
|
59
|
+
export declare class ParreqError extends Error {
|
|
60
60
|
status: number;
|
|
61
61
|
code: string;
|
|
62
62
|
detail: string;
|
|
@@ -65,14 +65,14 @@ export declare class ParReqError extends Error {
|
|
|
65
65
|
details: Record<string, any>;
|
|
66
66
|
readonly retriable: boolean;
|
|
67
67
|
}
|
|
68
|
-
export declare class BadRequest extends
|
|
69
|
-
export declare class AuthError extends
|
|
70
|
-
export declare class RateLimited extends
|
|
71
|
-
export declare class NoWorkers extends
|
|
72
|
-
export declare class SearchBlocked extends
|
|
73
|
-
export declare class ServerError extends
|
|
68
|
+
export declare class BadRequest extends ParreqError {}
|
|
69
|
+
export declare class AuthError extends ParreqError {}
|
|
70
|
+
export declare class RateLimited extends ParreqError {}
|
|
71
|
+
export declare class NoWorkers extends ParreqError {}
|
|
72
|
+
export declare class SearchBlocked extends ParreqError {}
|
|
73
|
+
export declare class ServerError extends ParreqError {}
|
|
74
74
|
|
|
75
|
-
export declare class
|
|
75
|
+
export declare class Parreq {
|
|
76
76
|
constructor(options: ClientOptions | string);
|
|
77
77
|
search(options: SearchOptions): Promise<SearchResult>;
|
|
78
78
|
google(options: EngineOptions): Promise<SearchResult>;
|
|
@@ -83,4 +83,7 @@ export declare class ParReq {
|
|
|
83
83
|
|
|
84
84
|
export declare const DEFAULT_BASE_URL: string;
|
|
85
85
|
export declare const DEFAULT_TIMEOUT_MS: number;
|
|
86
|
-
export default
|
|
86
|
+
export default Parreq;
|
|
87
|
+
|
|
88
|
+
/** Имена из 1.0.0, оставлены псевдонимами. */
|
|
89
|
+
export { Parreq as ParReq, ParreqError as ParReqError };
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Parreq — клиент поисковой выдачи Google и Яндекса.
|
|
3
3
|
*
|
|
4
4
|
* Зависимостей нет: используется встроенный fetch, поэтому нужен Node 18+.
|
|
5
5
|
* Тянуть axios ради одного запроса значит навязывать чужому проекту лишнее.
|
|
@@ -12,13 +12,13 @@ export const DEFAULT_TIMEOUT_MS = 180_000;
|
|
|
12
12
|
// Коды, которые лечатся повтором. Остальные повторять бессмысленно: неверный
|
|
13
13
|
// параметр и отозванный ключ от этого не исправятся.
|
|
14
14
|
const RETRIABLE = new Set([429, 502, 503, 504]);
|
|
15
|
-
const USER_AGENT = "parreq-node/1.
|
|
15
|
+
const USER_AGENT = "parreq-node/1.1";
|
|
16
16
|
|
|
17
17
|
/** Ошибка API. Разбирайте `code`, а не текст: текст переписывается. */
|
|
18
|
-
export class
|
|
18
|
+
export class ParreqError extends Error {
|
|
19
19
|
constructor(status, code, message, { requestId = "", retryAfter = null, details = {} } = {}) {
|
|
20
20
|
super(`${code}: ${message}`);
|
|
21
|
-
this.name = "
|
|
21
|
+
this.name = "ParreqError";
|
|
22
22
|
this.status = status;
|
|
23
23
|
this.code = code;
|
|
24
24
|
this.detail = message;
|
|
@@ -33,12 +33,12 @@ export class ParReqError extends Error {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
export class BadRequest extends
|
|
37
|
-
export class AuthError extends
|
|
38
|
-
export class RateLimited extends
|
|
39
|
-
export class NoWorkers extends
|
|
40
|
-
export class SearchBlocked extends
|
|
41
|
-
export class ServerError extends
|
|
36
|
+
export class BadRequest extends ParreqError {}
|
|
37
|
+
export class AuthError extends ParreqError {}
|
|
38
|
+
export class RateLimited extends ParreqError {}
|
|
39
|
+
export class NoWorkers extends ParreqError {}
|
|
40
|
+
export class SearchBlocked extends ParreqError {}
|
|
41
|
+
export class ServerError extends ParreqError {}
|
|
42
42
|
|
|
43
43
|
const BY_CODE = {
|
|
44
44
|
invalid_request: BadRequest,
|
|
@@ -92,7 +92,7 @@ export class SearchResult {
|
|
|
92
92
|
get totalResults() { return this.raw.total_results ?? null; }
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
export class
|
|
95
|
+
export class Parreq {
|
|
96
96
|
/**
|
|
97
97
|
* @param {object|string} options ключ строкой либо объект настроек
|
|
98
98
|
* @param {string} options.apiKey ключ вида `pr_…`
|
|
@@ -102,7 +102,7 @@ export class ParReq {
|
|
|
102
102
|
*/
|
|
103
103
|
constructor(options) {
|
|
104
104
|
const opts = typeof options === "string" ? { apiKey: options } : options || {};
|
|
105
|
-
if (!opts.apiKey) throw new Error("нужен ключ
|
|
105
|
+
if (!opts.apiKey) throw new Error("нужен ключ Parreq");
|
|
106
106
|
this.apiKey = opts.apiKey;
|
|
107
107
|
this.baseUrl = (opts.baseUrl || DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
108
108
|
this.timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
@@ -146,7 +146,7 @@ export class ParReq {
|
|
|
146
146
|
try {
|
|
147
147
|
return await this.#once(method, url);
|
|
148
148
|
} catch (err) {
|
|
149
|
-
if (!(err instanceof
|
|
149
|
+
if (!(err instanceof ParreqError) || !err.retriable || attempt === this.retries) throw err;
|
|
150
150
|
last = err;
|
|
151
151
|
// ждём столько, сколько попросил сервер: у суточной квоты это время до
|
|
152
152
|
// полуночи, и «подождать 5 секунд» там ничего не изменит
|
|
@@ -192,4 +192,9 @@ export class ParReq {
|
|
|
192
192
|
}
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
-
export default
|
|
195
|
+
export default Parreq;
|
|
196
|
+
|
|
197
|
+
// --- совместимость с 1.0.0 -------------------------------------------------
|
|
198
|
+
// Тогда класс назывался ParReq — с заглавной R. Написание бренда изменилось на
|
|
199
|
+
// Parreq, но ломать чужой импорт из-за этого нельзя.
|
|
200
|
+
export { Parreq as ParReq, ParreqError as ParReqError };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "parreq-client",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Клиент
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Клиент Parreq: поисковая выдача Google и Яндекса в JSON",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
7
7
|
"types": "./index.d.ts",
|