@nemigo/dadata 0.5.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.
@@ -0,0 +1,31 @@
1
+ import type { CleanPerson, CleanPhone, DaDataConfig } from "./types.js";
2
+ export interface DaDataCleanerProtocol {
3
+ /**
4
+ * @see https://dadata.ru/api/clean/phone/
5
+ */
6
+ "/phone": CleanPhone;
7
+ /**
8
+ * @see https://dadata.ru/api/clean/name/
9
+ */
10
+ "/name": CleanPerson;
11
+ }
12
+ export type DaDataCleanerProtocolEPS = keyof DaDataCleanerProtocol;
13
+ /**
14
+ * API предназначено только для серверного использования
15
+ */
16
+ export declare class DaDataCleanerAPI {
17
+ config: DaDataConfig;
18
+ headers: Record<string, string>;
19
+ constructor(config: DaDataConfig);
20
+ __service_url: string;
21
+ /**
22
+ * @throws {unknown} Сетевые ошибки или неожиданного ответа
23
+ * @throws {Error} "Invalid result", { endpoint, query, result }
24
+ */
25
+ fetch<E extends DaDataCleanerProtocolEPS>(endpoint: E, query: string, signal?: AbortSignal): Promise<DaDataCleanerProtocol[E]>;
26
+ __balance_url: string;
27
+ /**
28
+ * @throws {unknown} Сетевые ошибки или неожиданного ответа
29
+ */
30
+ balance(signal?: AbortSignal): Promise<number>;
31
+ }
@@ -0,0 +1,47 @@
1
+ import { deepCleanObject } from "@nemigo/helpers/clean";
2
+ //...
3
+ /**
4
+ * API предназначено только для серверного использования
5
+ */
6
+ export class DaDataCleanerAPI {
7
+ config;
8
+ headers;
9
+ constructor(config) {
10
+ this.config = config;
11
+ this.headers = {
12
+ Authorization: "Token " + this.config.token,
13
+ "X-Secret": this.config.secret,
14
+ Accept: "application/json",
15
+ "Content-Type": "application/json",
16
+ };
17
+ }
18
+ //...
19
+ __service_url = "https://cleaner.dadata.ru/api/v1/clean";
20
+ /**
21
+ * @throws {unknown} Сетевые ошибки или неожиданного ответа
22
+ * @throws {Error} "Invalid result", { endpoint, query, result }
23
+ */
24
+ async fetch(endpoint, query, signal) {
25
+ const response = await fetch(this.__service_url + endpoint, {
26
+ method: "POST",
27
+ headers: this.headers,
28
+ body: JSON.stringify([query]),
29
+ signal,
30
+ });
31
+ const result = await response.json();
32
+ if (Array.isArray(result) && result.length > 0) {
33
+ return deepCleanObject(result[0]);
34
+ }
35
+ throw new Error("Invalid result", { cause: { endpoint, query, result } });
36
+ }
37
+ //...
38
+ __balance_url = "https://dadata.ru/api/v2/profile/balance";
39
+ /**
40
+ * @throws {unknown} Сетевые ошибки или неожиданного ответа
41
+ */
42
+ async balance(signal) {
43
+ const response = await fetch(this.__balance_url, { method: "GET", headers: this.headers, signal });
44
+ const { balance } = (await response.json());
45
+ return balance;
46
+ }
47
+ }
@@ -0,0 +1 @@
1
+ export declare const okveds: Map<string, string>;