Skip to content

Commit

Permalink
added gelocate address
Browse files Browse the repository at this point in the history
  • Loading branch information
darkxanter committed Apr 30, 2022
1 parent a0214cb commit 95c9ccc
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
29 changes: 21 additions & 8 deletions src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ import { DaDataAddressInfo } from '../models/address'
import { DaDataBankInfo } from '../models/bank'
import { DaDataOrganizationInfo } from '../models/organization'
import { DaDataSuggestion } from '../models/suggestion'
import { validateSuggestions } from '../utils/guards'
import { isObjectEmpty, isRecord, validateSuggestions } from '../utils/guards'
import simpleHash from '../utils/hash'
import { camelCaseReviver } from '../utils/json'
import { DaDataAddressRequest, DaDataBankRequest, DaDataOrganizationRequest, DaDataQuery } from './requests'
import {
DaDataAddressRequest,
DaDataBankRequest,
DaDataOrganizationRequest,
DaDataQuery,
DaDataReverseGeolocateRequest,
} from '../models/requests'

export interface ApiClientOptions {
/**
Expand All @@ -17,9 +23,9 @@ export interface ApiClientOptions {
cache?: boolean
}

type MethodType = 'suggest' | 'findById'
type SuggestionsType = 'party' | 'bank'
export type DaDataSuggestionsMethod = `${MethodType}/${SuggestionsType}` | 'suggest/address'
export type DaDataSuggestionsMethod =
| `${'suggest' | 'findById'}/${'party' | 'bank'}`
| `${'suggest' | 'findById' | 'geolocate'}/address`

export default class ApiClient {
readonly endpoint: string
Expand All @@ -41,6 +47,13 @@ export default class ApiClient {
return this.request('suggest/address', { ...options, query })
}

/**
* Находит ближайшие адреса (дома, улицы, города) по географическим координатам. Только для России.
*/
geolocateAddress(request: DaDataReverseGeolocateRequest): Promise<DaDataSuggestion<DaDataAddressInfo>[]> {
return this.request('geolocate/address', request)
}

/**
* Что умеет
* - Ищет кредитные организации:
Expand Down Expand Up @@ -105,11 +118,11 @@ export default class ApiClient {
this.cache.clear()
}

async request<TResult extends object>(
async request<TResult extends object, TParams extends object>(
method: DaDataSuggestionsMethod,
params: DaDataQuery & Record<string, unknown>,
params: TParams,
): Promise<DaDataSuggestion<TResult>[]> {
if (!params.query) {
if (!isRecord(params) || isObjectEmpty(params)) {
return []
}
if (this.useCache) {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import ApiClient from './api/client'

export { ApiClient }

export * from './api/requests'
export * from './models/requests'
export * from './models/common'
export * from './models/suggestion'
export * from './models/bank'
export * from './models/organization'
export * from './models/address'
4 changes: 4 additions & 0 deletions src/api/requests.ts → src/models/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,7 @@ export type DaDataAddressRequestGranularAddressType =
| 'settlement'
| 'street'
| 'house'

export interface DaDataReverseGeolocateRequest extends DaDataRequest, DaDataAddressRequestLocationGeo {
language?: 'ru' | 'en'
}
8 changes: 8 additions & 0 deletions src/utils/guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ export function validateSuggestions(suggestions: unknown): suggestions is DaData
}
return false
}

export function isRecord(value: unknown): value is Record<string, unknown> {
return value != null && typeof value === 'object' && !Array.isArray(value)
}

export function isObjectEmpty(value: object): boolean {
return Object.keys(value).length === 0
}
27 changes: 20 additions & 7 deletions tests/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
import { describe, it, expect } from 'vitest'
import { getApiClient } from './utils'
import { checkResponse, getApiClient } from './utils'

describe('api test', () => {
it('suggest/bank', async () => {
const apiClient = getApiClient()
const suggestions = await apiClient.suggestBank('сбер')
expect(Array.isArray(suggestions)).toBeTruthy()
checkResponse(suggestions)
})

it('findById/bank', async () => {
const apiClient = getApiClient()
const suggestions = await apiClient.findBankById('773601001')
expect(Array.isArray(suggestions)).toBeTruthy()
const suggestions = await apiClient.findBankById('044525225')
checkResponse(suggestions)
})

it('suggest/party', async () => {
const apiClient = getApiClient()
const suggestions = await apiClient.suggestOrganization('сбер')
expect(Array.isArray(suggestions)).toBeTruthy()
checkResponse(suggestions)
})

it('findById/party', async () => {
const apiClient = getApiClient()
const suggestions = await apiClient.findOrganizationById('773601001')
expect(Array.isArray(suggestions)).toBeTruthy()
const suggestions = await apiClient.findOrganizationById('7707083893')
console.log(suggestions)
checkResponse(suggestions)
})

it('suggest/address', async () => {
const apiClient = getApiClient()
const suggestions = await apiClient.suggestAddress('г Москва, ул Сухонская, д 9')
checkResponse(suggestions)
})

it('geolocate/address', async () => {
const apiClient = getApiClient()
const suggestions = await apiClient.geolocateAddress({ lat: 55.878, lon: 37.653 })
checkResponse(suggestions)
})
})
6 changes: 6 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import ApiClient from '../src/api/client'

import fetch from 'node-fetch'
import { expect } from 'vitest'
// @ts-expect-error fetch
globalThis.fetch = fetch

export function getApiClient() {
return new ApiClient({ token: import.meta.env.VITE_DADATA_TOKEN as string })
}

export function checkResponse(suggestions: unknown[]) {
expect(Array.isArray(suggestions)).toBeTruthy()
expect(suggestions.length).toBeGreaterThan(0)
}

0 comments on commit 95c9ccc

Please sign in to comment.