From 50d84576fce3f88da929c02d4e476cc968e1c0d4 Mon Sep 17 00:00:00 2001 From: Inga Date: Tue, 24 Oct 2023 00:32:33 +0000 Subject: [PATCH] improved types --- src/clients/geocoding/osm.ts | 5 +++-- src/clients/geocoding/types.ts | 6 ++++++ src/storage/cache.ts | 6 +++--- src/storage/inMemoryDB.ts | 12 ++++++------ src/utils/throttle.ts | 4 ++-- 5 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 src/clients/geocoding/types.ts diff --git a/src/clients/geocoding/osm.ts b/src/clients/geocoding/osm.ts index 12e28cd..deceb47 100644 --- a/src/clients/geocoding/osm.ts +++ b/src/clients/geocoding/osm.ts @@ -3,8 +3,9 @@ import NodeGeocoder from 'node-geocoder'; import fetch from 'node-fetch'; import PQueue from 'p-queue'; import { mean } from '../../utils/math'; +import type { Geocoder } from './types'; -export const createOsmClient = () => { +export const createOsmClient = (): Geocoder => { const geocoder = NodeGeocoder({ provider: 'openstreetmap', fetch: (url, options) => { @@ -28,7 +29,7 @@ export const createOsmClient = () => { }); return { - geocode: async (query: string) => { + geocode: async (query) => { const result = await queue.add(() => geocoder.geocode(query)); if (!result.length) { diff --git a/src/clients/geocoding/types.ts b/src/clients/geocoding/types.ts new file mode 100644 index 0000000..ffc8086 --- /dev/null +++ b/src/clients/geocoding/types.ts @@ -0,0 +1,6 @@ +export type Geocoder = { + geocode(query: string): Promise<{ + longitude: number; + latitude: number; + }>; +}; diff --git a/src/storage/cache.ts b/src/storage/cache.ts index 04eeacb..03c2228 100644 --- a/src/storage/cache.ts +++ b/src/storage/cache.ts @@ -1,6 +1,6 @@ import { throttle } from '../utils/throttle'; -import { StringifiableValue, createSerializer } from '../utils/serializer'; -import { ClearableKeyValueStorage } from './types'; +import { type StringifiableValue, createSerializer } from '../utils/serializer'; +import type { ClearableKeyValueStorage } from './types'; type DataProvider = (key: TKey) => Promise; @@ -20,7 +20,7 @@ export const createCachedDataProvider = < cacheStorage: ClearableKeyValueStorage>; getNewValue: DataProvider; ttlMs: number; -}) => { +}): DataProvider => { const keySerializer = createSerializer(); const unsafeGet = async (key: TKey) => { const cacheEntry = await cacheStorage.get(keySerializer.stringify(key)); diff --git a/src/storage/inMemoryDB.ts b/src/storage/inMemoryDB.ts index 432d3a6..ebc1626 100644 --- a/src/storage/inMemoryDB.ts +++ b/src/storage/inMemoryDB.ts @@ -1,10 +1,10 @@ import { sleep } from '../utils/eventLoop'; -import { StringifiableValue, createSerializer } from '../utils/serializer'; -import { ClearableKeyValueStorage } from './types'; +import { type StringifiableValue, createSerializer } from '../utils/serializer'; +import type { ClearableKeyValueStorage } from './types'; export const createKeyValueStorage = ( latencyMs: number, -) => { +): ClearableKeyValueStorage => { const withSimulatedLatency = async ( f: () => Promise, ): Promise => { @@ -17,7 +17,7 @@ export const createKeyValueStorage = ( const serializer = createSerializer(); const storage = new Map(); return { - get: (key: string) => + get: (key) => withSimulatedLatency(() => Promise.resolve( storage.has(key) @@ -32,7 +32,7 @@ export const createKeyValueStorage = ( }, ), ), - set: (key: string, value: TValue) => + set: (key, value) => withSimulatedLatency(() => Promise.resolve( void storage.set(key, serializer.stringify(value)), @@ -41,5 +41,5 @@ export const createKeyValueStorage = ( clear: () => // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression withSimulatedLatency(() => Promise.resolve(storage.clear())), - } as ClearableKeyValueStorage; + }; }; diff --git a/src/utils/throttle.ts b/src/utils/throttle.ts index 4db2f03..8e10bd6 100644 --- a/src/utils/throttle.ts +++ b/src/utils/throttle.ts @@ -23,10 +23,10 @@ import { StringifiableValue, createSerializer } from './serializer'; */ export const throttle = ( f: (...args: TArgs) => Promise, -) => { +): ((...args: TArgs) => Promise) => { const argsSerializer = createSerializer(); const promises = new Map>(); - return (...args: TArgs) => { + return (...args) => { const promiseKey = argsSerializer.stringify(args); if (!promises.has(promiseKey)) { promises.set(