@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,62 @@
1
+ import type { DaDataAddress, DaDataBank, DaDataParty } from "./types.js";
2
+ export interface SuggsAddressRequest {
3
+ count?: number;
4
+ query: string;
5
+ }
6
+ export interface SuggsBankRequest {
7
+ /**
8
+ * Количество результатов (не более 20)
9
+ *
10
+ * @default `10`
11
+ */
12
+ count?: number;
13
+ query: string;
14
+ }
15
+ export interface SuggsPartyRequest {
16
+ /**
17
+ * Количество результатов (не более 20)
18
+ *
19
+ * @default `10`
20
+ */
21
+ count?: number;
22
+ query: string;
23
+ }
24
+ export interface DaDataSuggsProtocol {
25
+ /**
26
+ * @see https://dadata.ru/api/suggest/address/
27
+ */
28
+ "/address": {
29
+ request: SuggsAddressRequest;
30
+ response: DaDataAddress;
31
+ };
32
+ /**
33
+ * @see https://dadata.ru/api/suggest/party/
34
+ */
35
+ "/party": {
36
+ request: SuggsPartyRequest;
37
+ response: DaDataParty;
38
+ };
39
+ /**
40
+ * @see https://dadata.ru/api/suggest/bank/
41
+ */
42
+ "/bank": {
43
+ request: SuggsBankRequest;
44
+ response: DaDataBank;
45
+ };
46
+ }
47
+ export type DaDataSuggsProtocolEPS = keyof DaDataSuggsProtocol;
48
+ export declare class DaDataSuggsAPI {
49
+ config: {
50
+ token: string;
51
+ };
52
+ headers: Record<string, string>;
53
+ constructor(config: {
54
+ token: string;
55
+ });
56
+ __service_url: string;
57
+ /**
58
+ * @throws {unknown} Сетевые ошибки или неожиданного ответа
59
+ * @throws {Error} "Invalid result", { endpoint, request, result }
60
+ */
61
+ fetch<E extends DaDataSuggsProtocolEPS>(endpoint: E, request: DaDataSuggsProtocol[E]["request"], signal?: AbortSignal): Promise<DaDataSuggsProtocol[E]["response"][]>;
62
+ }
package/dist/suggs.js ADDED
@@ -0,0 +1,35 @@
1
+ import { deepCleanArray } from "@nemigo/helpers/clean";
2
+ //...
3
+ export class DaDataSuggsAPI {
4
+ config;
5
+ headers;
6
+ constructor(config) {
7
+ this.config = config;
8
+ this.headers = {
9
+ Authorization: "Token " + this.config.token,
10
+ Accept: "application/json",
11
+ "Content-Type": "application/json",
12
+ };
13
+ }
14
+ //...
15
+ __service_url = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest";
16
+ /**
17
+ * @throws {unknown} Сетевые ошибки или неожиданного ответа
18
+ * @throws {Error} "Invalid result", { endpoint, request, result }
19
+ */
20
+ async fetch(endpoint, request, signal) {
21
+ const response = await fetch(this.__service_url + endpoint, {
22
+ method: "POST",
23
+ headers: this.headers,
24
+ body: JSON.stringify(request),
25
+ signal,
26
+ });
27
+ const result = await response.json();
28
+ if (result && typeof result === "object") {
29
+ if ("suggestions" in result && Array.isArray(result.suggestions)) {
30
+ return deepCleanArray(result.suggestions);
31
+ }
32
+ }
33
+ throw new Error("Invalid result", { cause: { endpoint, request, result } });
34
+ }
35
+ }