diff --git a/src/gismeteo.ts b/src/gismeteo.ts index f2ab03a..00cb335 100644 --- a/src/gismeteo.ts +++ b/src/gismeteo.ts @@ -20,6 +20,10 @@ export class Gismeteo { 'user-agent': new UserAgent().toString(), }, } + private _city_cache = { + city: '', + city_uri: '' as CityUri, + } constructor(options?: GismeteoOptions) { this._base_url = options?.lang === 'en' ? Endpoint.BASE_EN : Endpoint.BASE_RU @@ -183,10 +187,18 @@ export class Gismeteo { } private async getCityUri(city: string): Promise { + if (city === this._city_cache.city) { + return Promise.resolve(this._city_cache.city_uri) + } + return axios.get(`${Endpoint.SEARCH}${encodeURIComponent(city)}/9/`, this._axios_config).then(({ data }) => { if (data.data.length === 0 || data.data[0]?.url === undefined) { throw new GismeteoCityError('Unable to find uri for given city name') } + this._city_cache = { + city: city, + city_uri: data.data[0].url, + } return data.data[0].url }) } diff --git a/test/gismeteo.test.ts b/test/gismeteo.test.ts index b705a6a..31c90f4 100644 --- a/test/gismeteo.test.ts +++ b/test/gismeteo.test.ts @@ -25,7 +25,7 @@ describe('Gismeteo', () => { describe('getTwoWeeks', () => { test('should not be null and contain 14 items', async () => { - const result = await gismeteo.getTwoWeeks('Москва') + const result = await gismeteo.getTwoWeeks('Moscow') expect(result).not.toBeNull() expect(result.length).toBe(14)