@mharj/openweathermap 0.0.5 → 0.0.7
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.
- package/dist/OpenWeatherV2.d.ts +2 -1
- package/dist/OpenWeatherV2.js +25 -16
- package/dist/OpenWeatherV2.js.map +1 -1
- package/dist/interfaces/IOpenWeatherV2.d.ts +1 -1
- package/dist/interfaces/IOpenWeatherV2.js.map +1 -1
- package/dist/types/v2/index.d.ts +10 -10
- package/dist/types/v2/index.js +2 -2
- package/dist/types/v2/index.js.map +1 -1
- package/dist/types/v2/weatherIdGroup.d.ts +1 -1
- package/dist/types/v2/weatherIdGroup.js +2 -2
- package/dist/types/v2/weatherIdGroup.js.map +1 -1
- package/package.json +20 -20
package/dist/OpenWeatherV2.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CountryCode, LangCode, Loadable, WeatherDataV2 } from './types';
|
|
2
|
-
import { Result } from '
|
|
2
|
+
import { Result } from '@luolapeikko/result-option';
|
|
3
3
|
import type { ICacheOrAsync } from '@avanio/expire-cache';
|
|
4
4
|
import type { IOpenWeatherV2 } from './interfaces/IOpenWeatherV2';
|
|
5
5
|
/**
|
|
@@ -45,6 +45,7 @@ export declare class OpenWeatherV2 {
|
|
|
45
45
|
private cache;
|
|
46
46
|
private loadableApiKey;
|
|
47
47
|
private apiHandler;
|
|
48
|
+
private fetchPromiseMap;
|
|
48
49
|
/**
|
|
49
50
|
* OpenWeatherV2 constructor
|
|
50
51
|
* @param {Loadable<string>} loadableApiKey - Loadable API key
|
package/dist/OpenWeatherV2.js
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpenWeatherV2 = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
|
-
const
|
|
5
|
+
const result_option_1 = require("@luolapeikko/result-option");
|
|
6
6
|
const fetchUtils_1 = require("./lib/fetchUtils");
|
|
7
|
-
const fetchResult = (0,
|
|
7
|
+
const fetchResult = (0, result_option_1.safeAsyncResultBuilder)(fetch);
|
|
8
8
|
function isJson(response) {
|
|
9
9
|
const contentType = response.headers.get('content-type');
|
|
10
10
|
return (contentType && contentType.startsWith('application/json')) || false;
|
|
@@ -26,28 +26,28 @@ const defaultImplementation = {
|
|
|
26
26
|
const logUrl = buildLogUrl(params);
|
|
27
27
|
const result = await fetchResult(buildUrl(params));
|
|
28
28
|
if (!result.isOk) {
|
|
29
|
-
return (0,
|
|
29
|
+
return (0, result_option_1.Err)(result.err());
|
|
30
30
|
}
|
|
31
31
|
const res = result.ok();
|
|
32
32
|
if (!res.ok) {
|
|
33
33
|
if (isJson(res)) {
|
|
34
34
|
const data = await res.json();
|
|
35
35
|
if (isOpenWeatherError(data)) {
|
|
36
|
-
return (0,
|
|
36
|
+
return (0, result_option_1.Err)(new TypeError(`OpenWeatherV2 error: ${data.message} from ${logUrl}`));
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
return (0,
|
|
39
|
+
return (0, result_option_1.Err)(new TypeError(`OpenWeatherV2 http error: ${res.status} ${res.statusText} from ${logUrl}`));
|
|
40
40
|
}
|
|
41
41
|
if (!isJson(res)) {
|
|
42
|
-
return (0,
|
|
42
|
+
return (0, result_option_1.Err)(new TypeError(`OpenWeatherV2 response is not json payload from ${logUrl}`));
|
|
43
43
|
}
|
|
44
|
-
const jsonResult = await (0,
|
|
44
|
+
const jsonResult = await (0, result_option_1.safeAsyncResult)(res.json());
|
|
45
45
|
if (!jsonResult.isOk) {
|
|
46
|
-
return (0,
|
|
46
|
+
return (0, result_option_1.Err)(jsonResult.err());
|
|
47
47
|
}
|
|
48
48
|
const data = jsonResult.ok();
|
|
49
49
|
(0, types_1.assertWeatherDataV2)(data);
|
|
50
|
-
return (0,
|
|
50
|
+
return (0, result_option_1.Ok)(data);
|
|
51
51
|
},
|
|
52
52
|
};
|
|
53
53
|
/**
|
|
@@ -77,6 +77,7 @@ class OpenWeatherV2 {
|
|
|
77
77
|
cache;
|
|
78
78
|
loadableApiKey;
|
|
79
79
|
apiHandler;
|
|
80
|
+
fetchPromiseMap = new Map();
|
|
80
81
|
/**
|
|
81
82
|
* OpenWeatherV2 constructor
|
|
82
83
|
* @param {Loadable<string>} loadableApiKey - Loadable API key
|
|
@@ -110,10 +111,10 @@ class OpenWeatherV2 {
|
|
|
110
111
|
params.append('id', String(id));
|
|
111
112
|
cacheEntry = await this.handleFetch(cacheKey, params, opts);
|
|
112
113
|
}
|
|
113
|
-
return (0,
|
|
114
|
+
return (0, result_option_1.Ok)(cacheEntry);
|
|
114
115
|
}
|
|
115
116
|
catch (err) {
|
|
116
|
-
return (0,
|
|
117
|
+
return (0, result_option_1.Err)((0, fetchUtils_1.fetchErrorWrapper)(err));
|
|
117
118
|
}
|
|
118
119
|
}
|
|
119
120
|
/**
|
|
@@ -139,10 +140,10 @@ class OpenWeatherV2 {
|
|
|
139
140
|
params.append('q', countryCode ? `${city},${countryCode}` : city);
|
|
140
141
|
cacheEntry = await this.handleFetch(cacheKey, params, opts);
|
|
141
142
|
}
|
|
142
|
-
return (0,
|
|
143
|
+
return (0, result_option_1.Ok)(cacheEntry);
|
|
143
144
|
}
|
|
144
145
|
catch (err) {
|
|
145
|
-
return (0,
|
|
146
|
+
return (0, result_option_1.Err)((0, fetchUtils_1.fetchErrorWrapper)(err));
|
|
146
147
|
}
|
|
147
148
|
}
|
|
148
149
|
/**
|
|
@@ -169,10 +170,10 @@ class OpenWeatherV2 {
|
|
|
169
170
|
params.append('lon', String(lon));
|
|
170
171
|
cacheEntry = await this.handleFetch(cacheKey, params, opts);
|
|
171
172
|
}
|
|
172
|
-
return (0,
|
|
173
|
+
return (0, result_option_1.Ok)(cacheEntry);
|
|
173
174
|
}
|
|
174
175
|
catch (err) {
|
|
175
|
-
return (0,
|
|
176
|
+
return (0, result_option_1.Err)((0, fetchUtils_1.fetchErrorWrapper)(err));
|
|
176
177
|
}
|
|
177
178
|
}
|
|
178
179
|
async buildBaseParams({ lang, units }) {
|
|
@@ -193,7 +194,15 @@ class OpenWeatherV2 {
|
|
|
193
194
|
return `${main}:${lang}:${units}`;
|
|
194
195
|
}
|
|
195
196
|
async handleFetch(cacheKey, params, opts) {
|
|
196
|
-
|
|
197
|
+
// allow only one fetch per cacheKey until it is resolved
|
|
198
|
+
let promiseResult = this.fetchPromiseMap.get(cacheKey);
|
|
199
|
+
if (!promiseResult) {
|
|
200
|
+
promiseResult = this.apiHandler.dataWeatherApi(params);
|
|
201
|
+
this.fetchPromiseMap.set(cacheKey, promiseResult);
|
|
202
|
+
await promiseResult;
|
|
203
|
+
this.fetchPromiseMap.delete(cacheKey); // clear promise from map
|
|
204
|
+
}
|
|
205
|
+
const dataApiResult = await promiseResult;
|
|
197
206
|
const data = dataApiResult.unwrap();
|
|
198
207
|
(0, types_1.assertWeatherDataV2)(data);
|
|
199
208
|
if (this.cache) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OpenWeatherV2.js","sourceRoot":"./src/","sources":["OpenWeatherV2.ts"],"names":[],"mappings":";;;AAAA,mCAA4F;AAC5F,+CAAsF;AACtF,iDAAmD;AAInD,MAAM,WAAW,GAAG,IAAA,qCAAsB,EAA8D,KAAK,CAAC,CAAC;AAE/G,SAAS,MAAM,CAAC,QAAkB;IACjC,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IACzD,OAAO,CAAC,WAAW,IAAI,WAAW,CAAC,UAAU,CAAC,kBAAkB,CAAC,CAAC,IAAI,KAAK,CAAC;AAC7E,CAAC;AAED,SAAS,kBAAkB,CAAC,IAAa;IACxC,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,SAAS,IAAI,IAAI,CAAC;AACxF,CAAC;AAmBD,MAAM,QAAQ,GAAG,iDAAiD,CAAC;AAEnE,SAAS,QAAQ,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;QAC/B,OAAO,MAAM,CAAC;IACf,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,IAAY,EAAE,EAAC,IAAI,EAAE,KAAK,EAA6B;QAChF,OAAO,GAAG,IAAI,IAAI,IAAI,IAAI,KAAK,EAAE,CAAC;IACnC,CAAC;IAEO,KAAK,CAAC,WAAW,CAAC,QAAgB,EAAE,MAAuB,EAAE,IAAgC;QACpG,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QACnE,MAAM,IAAI,GAAkB,aAAa,CAAC,MAAM,EAAE,CAAC;QACnD,IAAA,2BAAmB,EAAC,IAAI,CAAC,CAAC;QAC1B,IAAI,IAAI,CAAC,KAAK,EAAE;YACf,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;YACrC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE;gBAChC,sBAAsB;gBACtB,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,iBAAiB,CAAC,MAAM,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC;aAC1E;SACD;QACD,OAAO,IAAI,CAAC;IACb,CAAC;CACD;AA3ID,sCA2IC","sourcesContent":["import {assertWeatherDataV2, CountryCode, LangCode, Loadable, WeatherDataV2} from './types';\nimport {Err, Ok, Result, safeAsyncResult, safeAsyncResultBuilder} from 'mharj-result';\nimport {fetchErrorWrapper} from './lib/fetchUtils';\nimport type {ICacheOrAsync} from '@avanio/expire-cache';\nimport type {IOpenWeatherV2} from './interfaces/IOpenWeatherV2';\n\nconst fetchResult = safeAsyncResultBuilder<Parameters<typeof fetch>, Response, SyntaxError | TypeError>(fetch);\n\nfunction isJson(response: Response): boolean {\n\tconst contentType = response.headers.get('content-type');\n\treturn (contentType && contentType.startsWith('application/json')) || false;\n}\n\nfunction isOpenWeatherError(data: unknown): data is {cod: string; message: string} {\n\treturn typeof data === 'object' && data !== null && 'cod' in data && 'message' in data;\n}\n\n/**\n * Open Weather V2 API Common Options\n * @default {lang: 'en', units: 'standard'} in API\n * @example\n * {lang: 'fi', units: 'metric'}\n */\nexport type OpenWeatherV2CommonOptions = {\n\t/**\n\t * Language code\n\t */\n\tlang?: LangCode;\n\t/**\n\t * Weather units\n\t */\n\tunits?: 'standard' | 'metric' | 'imperial';\n};\n\nconst basePath = 'https://api.openweathermap.org/data/2.5/weather';\n\nfunction buildUrl(params: URLSearchParams): string {\n\treturn `${basePath}?${params.toString()}`;\n}\n\nfunction buildLogUrl(params: URLSearchParams): string {\n\tconst logParams = new URLSearchParams(params);\n\tlogParams.set('appid', '***');\n\treturn buildUrl(logParams);\n}\n\nconst defaultImplementation: IOpenWeatherV2 = {\n\tdataWeatherApi: async (params: URLSearchParams): Promise<Result<WeatherDataV2, SyntaxError | TypeError>> => {\n\t\tconst logUrl = buildLogUrl(params);\n\t\tconst result = await fetchResult(buildUrl(params));\n\t\tif (!result.isOk) {\n\t\t\treturn Err(result.err());\n\t\t}\n\t\tconst res = result.ok();\n\t\tif (!res.ok) {\n\t\t\tif (isJson(res)) {\n\t\t\t\tconst data: unknown = await res.json();\n\t\t\t\tif (isOpenWeatherError(data)) {\n\t\t\t\t\treturn Err(new TypeError(`OpenWeatherV2 error: ${data.message} from ${logUrl}`));\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn Err(new TypeError(`OpenWeatherV2 http error: ${res.status} ${res.statusText} from ${logUrl}`));\n\t\t}\n\t\tif (!isJson(res)) {\n\t\t\treturn Err(new TypeError(`OpenWeatherV2 response is not json payload from ${logUrl}`));\n\t\t}\n\t\tconst jsonResult = await safeAsyncResult<unknown, SyntaxError>(res.json());\n\t\tif (!jsonResult.isOk) {\n\t\t\treturn Err(jsonResult.err());\n\t\t}\n\t\tconst data = jsonResult.ok();\n\t\tassertWeatherDataV2(data);\n\t\treturn Ok<WeatherDataV2, SyntaxError | TypeError>(data);\n\t},\n};\n\n/**\n * Open Weather V2 API\n * @example\n * const weather = new OpenWeatherV2('your-api-key');\n *\n * const cache = new ExpireCache<WeatherDataV2>(undefined, undefined, 900000); // data 15 minutes in cache\n * const weather = new OpenWeatherV2(() => Promise.resolve('your-api-key'), cache);\n *\n * const data: WeatherDataV2 = (await weather.getWeatherById(2643743)).unwrap(); // throws if error\n * const data: WeatherDataV2 | undefined = (await weather.getWeatherByCity('Helsinki', 'fi')).ok();\n *\n * const result: Result<WeatherDataV2> = await weather.getWeatherByLatLon(60.1699, 24.9384);\n * result.match({\n * Ok: (data: WeatherDataV2) => console.log(data),\n * Err: (err: DOMException | TypeError) => console.error(err),\n * });\n *\n * if(result.isOk) {\n * const data: WeatherDataV2 = data.ok();\n * } else {\n * const err: DOMException | TypeError = data.err();\n * }\n */\nexport class OpenWeatherV2 {\n\tprivate cache: ICacheOrAsync<WeatherDataV2> | undefined;\n\tprivate loadableApiKey: Loadable<string>;\n\tprivate apiHandler: IOpenWeatherV2;\n\t/**\n\t * OpenWeatherV2 constructor\n\t * @param {Loadable<string>} loadableApiKey - Loadable API key\n\t * @param {ICacheOrAsync<WeatherDataV2>=} cache - optional async cache implementation\n\t * @param {IOpenWeatherV2=} apiHandler - optional API handler implementation for mocking\n\t */\n\tconstructor(loadableApiKey: Loadable<string>, cache?: ICacheOrAsync<WeatherDataV2>, apiHandler: IOpenWeatherV2 = defaultImplementation) {\n\t\tthis.loadableApiKey = loadableApiKey;\n\t\tthis.cache = cache;\n\t\tthis.apiHandler = apiHandler;\n\t}\n\n\t/**\n\t * get weather by Id\n\t * @param {number} id - Weather station ID\n\t * @param {OpenWeatherV2CommonOptions=} opts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```\n\t * @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise\n\t * @example\n\t * const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherResultById(id: 564, {lang: 'fi'});\n\t * if (result.isOk) {\n\t * const weatherData: WeatherDataV2 = result.ok();\n\t * } else {\n\t * const error: DOMException | TypeError = result.err();\n\t * }\n\t */\n\tpublic async getWeatherById(id: number, opts: OpenWeatherV2CommonOptions = {}): Promise<Result<WeatherDataV2, DOMException | TypeError>> {\n\t\ttry {\n\t\t\tconst cacheKey = this.buildBaseCacheKey(`id:${id}`, opts);\n\t\t\tlet cacheEntry = this.cache && (await this.cache.get(cacheKey));\n\t\t\tif (!cacheEntry) {\n\t\t\t\tconst params = await this.buildBaseParams(opts);\n\t\t\t\tparams.append('id', String(id));\n\t\t\t\tcacheEntry = await this.handleFetch(cacheKey, params, opts);\n\t\t\t}\n\t\t\treturn Ok(cacheEntry);\n\t\t} catch (err) {\n\t\t\treturn Err(fetchErrorWrapper(err));\n\t\t}\n\t}\n\n\t/**\n\t * get weather with city name and optional country code\n\t * @param {string} city - City name\n\t * @param {countryCode=} countryCode - Optional Country code\n\t * @param {OpenWeatherV2CommonOptions=} opts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```\n\t * @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise\n\t * @example\n\t * const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherByCity('Helsinki', 'fi', {lang: 'fi'});\n\t * if (result.isOk) {\n\t * const weatherData: WeatherDataV2 = result.ok();\n\t * } else {\n\t * const error: DOMException | TypeError = result.err();\n\t * }\n\t */\n\tpublic async getWeatherByCity(\n\t\tcity: string,\n\t\tcountryCode?: CountryCode,\n\t\topts: OpenWeatherV2CommonOptions = {},\n\t): Promise<Result<WeatherDataV2, DOMException | TypeError>> {\n\t\ttry {\n\t\t\tconst cacheKey = this.buildBaseCacheKey(`q:${city}:${countryCode}`, opts);\n\t\t\tlet cacheEntry = this.cache && (await this.cache.get(cacheKey));\n\t\t\tif (!cacheEntry) {\n\t\t\t\tconst params = await this.buildBaseParams(opts);\n\t\t\t\tparams.append('q', countryCode ? `${city},${countryCode}` : city);\n\t\t\t\tcacheEntry = await this.handleFetch(cacheKey, params, opts);\n\t\t\t}\n\t\t\treturn Ok(cacheEntry);\n\t\t} catch (err) {\n\t\t\treturn Err(fetchErrorWrapper(err));\n\t\t}\n\t}\n\n\t/**\n\t * get weather with latitude and longitude with Result\n\t * @param {number} lat - Latitude\n\t * @param {number} lon - Longitude\n\t * @param {OpenWeatherV2CommonOptions=} opts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```\n\t * @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise\n\t * @example\n\t * const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherByLatLon(60.1699, 24.9384, {lang: 'fi'});\n\t * if (result.isOk) {\n\t * const weatherData: WeatherDataV2 = result.ok();\n\t * } else {\n\t * const error: DOMException | TypeError = result.err();\n\t * }\n\t */\n\tpublic async getWeatherByLatLon(lat: number, lon: number, opts: OpenWeatherV2CommonOptions = {}): Promise<Result<WeatherDataV2, DOMException | TypeError>> {\n\t\ttry {\n\t\t\tconst cacheKey = this.buildBaseCacheKey(`latlon:${lat}:${lon}`, opts);\n\t\t\tlet cacheEntry = this.cache && (await this.cache.get(cacheKey));\n\t\t\tif (!cacheEntry) {\n\t\t\t\tconst params = await this.buildBaseParams(opts);\n\t\t\t\tparams.append('lat', String(lat));\n\t\t\t\tparams.append('lon', String(lon));\n\t\t\t\tcacheEntry = await this.handleFetch(cacheKey, params, opts);\n\t\t\t}\n\t\t\treturn Ok(cacheEntry);\n\t\t} catch (err) {\n\t\t\treturn Err(fetchErrorWrapper(err));\n\t\t}\n\t}\n\n\tprivate async buildBaseParams({lang, units}: OpenWeatherV2CommonOptions): Promise<URLSearchParams> {\n\t\tconst apiKey = await (typeof this.loadableApiKey === 'function' ? this.loadableApiKey() : this.loadableApiKey);\n\t\tconst params = new URLSearchParams();\n\t\tlang && params.append('lang', lang);\n\t\tunits && params.append('units', units);\n\t\tparams.append('appid', apiKey);\n\t\treturn params;\n\t}\n\n\t/**\n\t * build base cache key\n\t * @param main - main cache key prefix\n\t * @param opts - OpenWeatherV2CommonOptions\n\t * @returns {CacheKey}\n\t */\n\tprivate buildBaseCacheKey(main: string, {lang, units}: OpenWeatherV2CommonOptions): string {\n\t\treturn `${main}:${lang}:${units}`;\n\t}\n\n\tprivate async handleFetch(cacheKey: string, params: URLSearchParams, opts: OpenWeatherV2CommonOptions): Promise<WeatherDataV2> {\n\t\tconst dataApiResult = await this.apiHandler.dataWeatherApi(params);\n\t\tconst data: WeatherDataV2 = dataApiResult.unwrap();\n\t\tassertWeatherDataV2(data);\n\t\tif (this.cache) {\n\t\t\tawait this.cache.set(cacheKey, data);\n\t\t\tif (!cacheKey.startsWith('id:')) {\n\t\t\t\t// update id cache too\n\t\t\t\tawait this.cache.set(this.buildBaseCacheKey(`id:${data.id}`, opts), data);\n\t\t\t}\n\t\t}\n\t\treturn data;\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"OpenWeatherV2.js","sourceRoot":"./src/","sources":["OpenWeatherV2.ts"],"names":[],"mappings":";;;AAAA,mCAA4F;AAC5F,8DAAoG;AACpG,iDAAmD;AAInD,MAAM,WAAW,GAAG,IAAA,sCAAsB,EAA8D,KAAK,CAAC,CAAC;AAE/G,SAAS,MAAM,CAAC,QAAkB;IACjC,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IACzD,OAAO,CAAC,WAAW,IAAI,WAAW,CAAC,UAAU,CAAC,kBAAkB,CAAC,CAAC,IAAI,KAAK,CAAC;AAC7E,CAAC;AAED,SAAS,kBAAkB,CAAC,IAAa;IACxC,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,SAAS,IAAI,IAAI,CAAC;AACxF,CAAC;AAmBD,MAAM,QAAQ,GAA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AAC;CACD;AApJD,sCAoJC","sourcesContent":["import {assertWeatherDataV2, CountryCode, LangCode, Loadable, WeatherDataV2} from './types';\nimport {Err, Ok, Result, safeAsyncResult, safeAsyncResultBuilder} from '@luolapeikko/result-option';\nimport {fetchErrorWrapper} from './lib/fetchUtils';\nimport type {ICacheOrAsync} from '@avanio/expire-cache';\nimport type {IOpenWeatherV2} from './interfaces/IOpenWeatherV2';\n\nconst fetchResult = safeAsyncResultBuilder<Parameters<typeof fetch>, Response, SyntaxError | TypeError>(fetch);\n\nfunction isJson(response: Response): boolean {\n\tconst contentType = response.headers.get('content-type');\n\treturn (contentType && contentType.startsWith('application/json')) || false;\n}\n\nfunction isOpenWeatherError(data: unknown): data is {cod: string; message: string} {\n\treturn typeof data === 'object' && data !== null && 'cod' in data && 'message' in data;\n}\n\n/**\n * Open Weather V2 API Common Options\n * @default {lang: 'en', units: 'standard'} in API\n * @example\n * {lang: 'fi', units: 'metric'}\n */\nexport type OpenWeatherV2CommonOptions = {\n\t/**\n\t * Language code\n\t */\n\tlang?: LangCode;\n\t/**\n\t * Weather units\n\t */\n\tunits?: 'standard' | 'metric' | 'imperial';\n};\n\nconst basePath = 'https://api.openweathermap.org/data/2.5/weather';\n\nfunction buildUrl(params: URLSearchParams): string {\n\treturn `${basePath}?${params.toString()}`;\n}\n\nfunction buildLogUrl(params: URLSearchParams): string {\n\tconst logParams = new URLSearchParams(params);\n\tlogParams.set('appid', '***');\n\treturn buildUrl(logParams);\n}\n\nconst defaultImplementation: IOpenWeatherV2 = {\n\tdataWeatherApi: async (params: URLSearchParams): Promise<Result<WeatherDataV2, SyntaxError | TypeError>> => {\n\t\tconst logUrl = buildLogUrl(params);\n\t\tconst result = await fetchResult(buildUrl(params));\n\t\tif (!result.isOk) {\n\t\t\treturn Err(result.err());\n\t\t}\n\t\tconst res = result.ok();\n\t\tif (!res.ok) {\n\t\t\tif (isJson(res)) {\n\t\t\t\tconst data: unknown = await res.json();\n\t\t\t\tif (isOpenWeatherError(data)) {\n\t\t\t\t\treturn Err(new TypeError(`OpenWeatherV2 error: ${data.message} from ${logUrl}`));\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn Err(new TypeError(`OpenWeatherV2 http error: ${res.status} ${res.statusText} from ${logUrl}`));\n\t\t}\n\t\tif (!isJson(res)) {\n\t\t\treturn Err(new TypeError(`OpenWeatherV2 response is not json payload from ${logUrl}`));\n\t\t}\n\t\tconst jsonResult = await safeAsyncResult<unknown, SyntaxError>(res.json());\n\t\tif (!jsonResult.isOk) {\n\t\t\treturn Err(jsonResult.err());\n\t\t}\n\t\tconst data = jsonResult.ok();\n\t\tassertWeatherDataV2(data);\n\t\treturn Ok<WeatherDataV2, SyntaxError | TypeError>(data);\n\t},\n};\n\n/**\n * Open Weather V2 API\n * @example\n * const weather = new OpenWeatherV2('your-api-key');\n *\n * const cache = new ExpireCache<WeatherDataV2>(undefined, undefined, 900000); // data 15 minutes in cache\n * const weather = new OpenWeatherV2(() => Promise.resolve('your-api-key'), cache);\n *\n * const data: WeatherDataV2 = (await weather.getWeatherById(2643743)).unwrap(); // throws if error\n * const data: WeatherDataV2 | undefined = (await weather.getWeatherByCity('Helsinki', 'fi')).ok();\n *\n * const result: Result<WeatherDataV2> = await weather.getWeatherByLatLon(60.1699, 24.9384);\n * result.match({\n * Ok: (data: WeatherDataV2) => console.log(data),\n * Err: (err: DOMException | TypeError) => console.error(err),\n * });\n *\n * if(result.isOk) {\n * const data: WeatherDataV2 = data.ok();\n * } else {\n * const err: DOMException | TypeError = data.err();\n * }\n */\nexport class OpenWeatherV2 {\n\tprivate cache: ICacheOrAsync<WeatherDataV2> | undefined;\n\tprivate loadableApiKey: Loadable<string>;\n\tprivate apiHandler: IOpenWeatherV2;\n\tprivate fetchPromiseMap = new Map<string, Promise<Result<WeatherDataV2, DOMException | TypeError>>>();\n\t/**\n\t * OpenWeatherV2 constructor\n\t * @param {Loadable<string>} loadableApiKey - Loadable API key\n\t * @param {ICacheOrAsync<WeatherDataV2>=} cache - optional async cache implementation\n\t * @param {IOpenWeatherV2=} apiHandler - optional API handler implementation for mocking\n\t */\n\tconstructor(loadableApiKey: Loadable<string>, cache?: ICacheOrAsync<WeatherDataV2>, apiHandler: IOpenWeatherV2 = defaultImplementation) {\n\t\tthis.loadableApiKey = loadableApiKey;\n\t\tthis.cache = cache;\n\t\tthis.apiHandler = apiHandler;\n\t}\n\n\t/**\n\t * get weather by Id\n\t * @param {number} id - Weather station ID\n\t * @param {OpenWeatherV2CommonOptions=} opts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```\n\t * @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise\n\t * @example\n\t * const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherResultById(id: 564, {lang: 'fi'});\n\t * if (result.isOk) {\n\t * const weatherData: WeatherDataV2 = result.ok();\n\t * } else {\n\t * const error: DOMException | TypeError = result.err();\n\t * }\n\t */\n\tpublic async getWeatherById(id: number, opts: OpenWeatherV2CommonOptions = {}): Promise<Result<WeatherDataV2, DOMException | TypeError>> {\n\t\ttry {\n\t\t\tconst cacheKey = this.buildBaseCacheKey(`id:${id}`, opts);\n\t\t\tlet cacheEntry = this.cache && (await this.cache.get(cacheKey));\n\t\t\tif (!cacheEntry) {\n\t\t\t\tconst params = await this.buildBaseParams(opts);\n\t\t\t\tparams.append('id', String(id));\n\t\t\t\tcacheEntry = await this.handleFetch(cacheKey, params, opts);\n\t\t\t}\n\t\t\treturn Ok(cacheEntry);\n\t\t} catch (err) {\n\t\t\treturn Err(fetchErrorWrapper(err));\n\t\t}\n\t}\n\n\t/**\n\t * get weather with city name and optional country code\n\t * @param {string} city - City name\n\t * @param {countryCode=} countryCode - Optional Country code\n\t * @param {OpenWeatherV2CommonOptions=} opts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```\n\t * @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise\n\t * @example\n\t * const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherByCity('Helsinki', 'fi', {lang: 'fi'});\n\t * if (result.isOk) {\n\t * const weatherData: WeatherDataV2 = result.ok();\n\t * } else {\n\t * const error: DOMException | TypeError = result.err();\n\t * }\n\t */\n\tpublic async getWeatherByCity(\n\t\tcity: string,\n\t\tcountryCode?: CountryCode,\n\t\topts: OpenWeatherV2CommonOptions = {},\n\t): Promise<Result<WeatherDataV2, DOMException | TypeError>> {\n\t\ttry {\n\t\t\tconst cacheKey = this.buildBaseCacheKey(`q:${city}:${countryCode}`, opts);\n\t\t\tlet cacheEntry = this.cache && (await this.cache.get(cacheKey));\n\t\t\tif (!cacheEntry) {\n\t\t\t\tconst params = await this.buildBaseParams(opts);\n\t\t\t\tparams.append('q', countryCode ? `${city},${countryCode}` : city);\n\t\t\t\tcacheEntry = await this.handleFetch(cacheKey, params, opts);\n\t\t\t}\n\t\t\treturn Ok(cacheEntry);\n\t\t} catch (err) {\n\t\t\treturn Err(fetchErrorWrapper(err));\n\t\t}\n\t}\n\n\t/**\n\t * get weather with latitude and longitude with Result\n\t * @param {number} lat - Latitude\n\t * @param {number} lon - Longitude\n\t * @param {OpenWeatherV2CommonOptions=} opts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```\n\t * @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise\n\t * @example\n\t * const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherByLatLon(60.1699, 24.9384, {lang: 'fi'});\n\t * if (result.isOk) {\n\t * const weatherData: WeatherDataV2 = result.ok();\n\t * } else {\n\t * const error: DOMException | TypeError = result.err();\n\t * }\n\t */\n\tpublic async getWeatherByLatLon(lat: number, lon: number, opts: OpenWeatherV2CommonOptions = {}): Promise<Result<WeatherDataV2, DOMException | TypeError>> {\n\t\ttry {\n\t\t\tconst cacheKey = this.buildBaseCacheKey(`latlon:${lat}:${lon}`, opts);\n\t\t\tlet cacheEntry = this.cache && (await this.cache.get(cacheKey));\n\t\t\tif (!cacheEntry) {\n\t\t\t\tconst params = await this.buildBaseParams(opts);\n\t\t\t\tparams.append('lat', String(lat));\n\t\t\t\tparams.append('lon', String(lon));\n\t\t\t\tcacheEntry = await this.handleFetch(cacheKey, params, opts);\n\t\t\t}\n\t\t\treturn Ok(cacheEntry);\n\t\t} catch (err) {\n\t\t\treturn Err(fetchErrorWrapper(err));\n\t\t}\n\t}\n\n\tprivate async buildBaseParams({lang, units}: OpenWeatherV2CommonOptions): Promise<URLSearchParams> {\n\t\tconst apiKey = await (typeof this.loadableApiKey === 'function' ? this.loadableApiKey() : this.loadableApiKey);\n\t\tconst params = new URLSearchParams();\n\t\tlang && params.append('lang', lang);\n\t\tunits && params.append('units', units);\n\t\tparams.append('appid', apiKey);\n\t\treturn params;\n\t}\n\n\t/**\n\t * build base cache key\n\t * @param main - main cache key prefix\n\t * @param opts - OpenWeatherV2CommonOptions\n\t * @returns {CacheKey}\n\t */\n\tprivate buildBaseCacheKey(main: string, {lang, units}: OpenWeatherV2CommonOptions): string {\n\t\treturn `${main}:${lang}:${units}`;\n\t}\n\n\tprivate async handleFetch(cacheKey: string, params: URLSearchParams, opts: OpenWeatherV2CommonOptions): Promise<WeatherDataV2> {\n\t\t// allow only one fetch per cacheKey until it is resolved\n\t\tlet promiseResult = this.fetchPromiseMap.get(cacheKey);\n\t\tif (!promiseResult) {\n\t\t\tpromiseResult = this.apiHandler.dataWeatherApi(params);\n\t\t\tthis.fetchPromiseMap.set(cacheKey, promiseResult);\n\t\t\tawait promiseResult;\n\t\t\tthis.fetchPromiseMap.delete(cacheKey); // clear promise from map\n\t\t}\n\t\tconst dataApiResult = await promiseResult;\n\t\tconst data: WeatherDataV2 = dataApiResult.unwrap();\n\t\tassertWeatherDataV2(data);\n\t\tif (this.cache) {\n\t\t\tawait this.cache.set(cacheKey, data);\n\t\t\tif (!cacheKey.startsWith('id:')) {\n\t\t\t\t// update id cache too\n\t\t\t\tawait this.cache.set(this.buildBaseCacheKey(`id:${data.id}`, opts), data);\n\t\t\t}\n\t\t}\n\t\treturn data;\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IOpenWeatherV2.js","sourceRoot":"./src/","sources":["interfaces/IOpenWeatherV2.ts"],"names":[],"mappings":"","sourcesContent":["import {Result} from '
|
|
1
|
+
{"version":3,"file":"IOpenWeatherV2.js","sourceRoot":"./src/","sources":["interfaces/IOpenWeatherV2.ts"],"names":[],"mappings":"","sourcesContent":["import {Result} from '@luolapeikko/result-option';\nimport {WeatherDataV2} from '../types/v2';\n\n/**\n * Interface for OpenWeatherMap API v2 implementation.\n */\nexport interface IOpenWeatherV2 {\n\tdataWeatherApi: (params: URLSearchParams) => Promise<Result<WeatherDataV2, SyntaxError | TypeError>>;\n}\n"]}
|
package/dist/types/v2/index.d.ts
CHANGED
|
@@ -75,25 +75,25 @@ export declare const weatherDataV2Schema: z.ZodObject<{
|
|
|
75
75
|
}>>;
|
|
76
76
|
sys: z.ZodObject<{
|
|
77
77
|
country: z.ZodString;
|
|
78
|
-
id: z.ZodNumber
|
|
78
|
+
id: z.ZodOptional<z.ZodNumber>;
|
|
79
79
|
message: z.ZodOptional<z.ZodNumber>;
|
|
80
80
|
sunrise: z.ZodNumber;
|
|
81
81
|
sunset: z.ZodNumber;
|
|
82
|
-
type: z.ZodNumber
|
|
82
|
+
type: z.ZodOptional<z.ZodNumber>;
|
|
83
83
|
}, "strip", z.ZodTypeAny, {
|
|
84
|
-
type: number;
|
|
85
|
-
id: number;
|
|
86
84
|
country: string;
|
|
87
85
|
sunrise: number;
|
|
88
86
|
sunset: number;
|
|
87
|
+
type?: number | undefined;
|
|
89
88
|
message?: number | undefined;
|
|
89
|
+
id?: number | undefined;
|
|
90
90
|
}, {
|
|
91
|
-
type: number;
|
|
92
|
-
id: number;
|
|
93
91
|
country: string;
|
|
94
92
|
sunrise: number;
|
|
95
93
|
sunset: number;
|
|
94
|
+
type?: number | undefined;
|
|
96
95
|
message?: number | undefined;
|
|
96
|
+
id?: number | undefined;
|
|
97
97
|
}>;
|
|
98
98
|
timezone: z.ZodNumber;
|
|
99
99
|
visibility: z.ZodNumber;
|
|
@@ -147,12 +147,12 @@ export declare const weatherDataV2Schema: z.ZodObject<{
|
|
|
147
147
|
lat: number;
|
|
148
148
|
};
|
|
149
149
|
sys: {
|
|
150
|
-
type: number;
|
|
151
|
-
id: number;
|
|
152
150
|
country: string;
|
|
153
151
|
sunrise: number;
|
|
154
152
|
sunset: number;
|
|
153
|
+
type?: number | undefined;
|
|
155
154
|
message?: number | undefined;
|
|
155
|
+
id?: number | undefined;
|
|
156
156
|
};
|
|
157
157
|
timezone: number;
|
|
158
158
|
weather: {
|
|
@@ -197,12 +197,12 @@ export declare const weatherDataV2Schema: z.ZodObject<{
|
|
|
197
197
|
lat: number;
|
|
198
198
|
};
|
|
199
199
|
sys: {
|
|
200
|
-
type: number;
|
|
201
|
-
id: number;
|
|
202
200
|
country: string;
|
|
203
201
|
sunrise: number;
|
|
204
202
|
sunset: number;
|
|
203
|
+
type?: number | undefined;
|
|
205
204
|
message?: number | undefined;
|
|
205
|
+
id?: number | undefined;
|
|
206
206
|
};
|
|
207
207
|
timezone: number;
|
|
208
208
|
weather: {
|
package/dist/types/v2/index.js
CHANGED
|
@@ -41,11 +41,11 @@ const snowSchema = zod_1.z.object({
|
|
|
41
41
|
});
|
|
42
42
|
const sysSchema = zod_1.z.object({
|
|
43
43
|
country: zod_1.z.string(),
|
|
44
|
-
id: zod_1.z.number(),
|
|
44
|
+
id: zod_1.z.number().optional(),
|
|
45
45
|
message: zod_1.z.number().optional(),
|
|
46
46
|
sunrise: zod_1.z.number(),
|
|
47
47
|
sunset: zod_1.z.number(),
|
|
48
|
-
type: zod_1.z.number(),
|
|
48
|
+
type: zod_1.z.number().optional(),
|
|
49
49
|
});
|
|
50
50
|
/**
|
|
51
51
|
* @internal
|
|
@@ -1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,EAAE,WAAW;IAClB,EAAE,EAAE,OAAC,CAAC,MAAM,EAAE;IACd,EAAE,EAAE,OAAC,CAAC,MAAM,EAAE;IACd,IAAI,EAAE,UAAU;IAChB,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE;IAChB,IAAI,EAAE,UAAU,CAAC,QAAQ,EAAE;IAC3B,IAAI,EAAE,UAAU,CAAC,QAAQ,EAAE;IAC3B,GAAG,EAAE,SAAS;IACd,QAAQ,EAAE,OAAC,CAAC,MAAM,EAAE;IACpB,UAAU,EAAE,OAAC,CAAC,MAAM,EAAE;IACtB,OAAO,EAAE,OAAC,CAAC,KAAK,CAAC,aAAa,CAAC;IAC/B,IAAI,EAAE,UAAU;CAChB,CAAC,CAAC;AAIH,SAAgB,eAAe,CAAC,IAAa;IAC5C,OAAO,2BAAmB,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC;AACpD,CAAC;AAFD,0CAEC;AAED,SAAgB,mBAAmB,CAAC,IAAa;IAChD,2BAAmB,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AACjC,CAAC;AAFD,kDAEC","sourcesContent":["import {iconSchema} from './Icon';\nimport {weatherIdSchema} from './weatherIdGroup';\nimport {z} from 'zod';\nexport * from './Icon';\nexport * from './Language';\nexport * from './weatherIdGroup';\n\nconst coordSchema = z.object({\n\tlon: z.number(),\n\tlat: z.number(),\n});\n\nconst weatherSchema = z.object({\n\tdescription: z.string(),\n\ticon: iconSchema,\n\tid: weatherIdSchema,\n\tmain: z.string(),\n});\n\nconst mainSchema = z.object({\n\tgrnd_level: z.number().optional(),\n\thumidity: z.number(),\n\tpressure: z.number(),\n\tsea_level: z.number().optional(),\n\ttemp: z.number(),\n\ttemp_max: z.number(),\n\ttemp_min: z.number(),\n});\n\nconst windSchema = z.object({\n\tspeed: z.number(),\n\tdeg: z.number(),\n});\n\nconst rainSchema = z.object({\n\t'1h': z.number().optional(),\n\t'3h': z.number().optional(),\n});\n\nconst snowSchema = z.object({\n\t'1h': z.number().optional(),\n\t'3h': z.number().optional(),\n});\n\nconst sysSchema = z.object({\n\tcountry: z.string(),\n\tid: z.number().optional(),\n\tmessage: z.number().optional(),\n\tsunrise: z.number(),\n\tsunset: z.number(),\n\ttype: z.number().optional(),\n});\n\n/**\n * @internal\n */\nexport const weatherDataV2Schema = z.object({\n\tbase: z.string(),\n\tclouds: z.object({\n\t\tall: z.number(),\n\t}),\n\tcod: z.number(),\n\tcoord: coordSchema,\n\tdt: z.number(),\n\tid: z.number(),\n\tmain: mainSchema,\n\tname: z.string(),\n\train: rainSchema.optional(),\n\tsnow: snowSchema.optional(),\n\tsys: sysSchema,\n\ttimezone: z.number(),\n\tvisibility: z.number(),\n\tweather: z.array(weatherSchema),\n\twind: windSchema,\n});\n\nexport type WeatherDataV2 = z.infer<typeof weatherDataV2Schema>;\n\nexport function isWeatherDataV2(data: unknown): data is WeatherDataV2 {\n\treturn weatherDataV2Schema.safeParse(data).success;\n}\n\nexport function assertWeatherDataV2(data: unknown): asserts data is WeatherDataV2 {\n\tweatherDataV2Schema.parse(data);\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getWeatherV2Description = exports.weatherIdSchema = exports.weatherIdGroup = void 0;
|
|
4
|
-
const
|
|
4
|
+
const result_option_1 = require("@luolapeikko/result-option");
|
|
5
5
|
const zod_1 = require("zod");
|
|
6
6
|
/**
|
|
7
7
|
* This is a list of weather ids, groups and descriptions from OpenWeatherMap API
|
|
@@ -311,7 +311,7 @@ exports.weatherIdSchema = zod_1.z.custom((val) => {
|
|
|
311
311
|
* }
|
|
312
312
|
*/
|
|
313
313
|
function getWeatherV2Description(id) {
|
|
314
|
-
return (0,
|
|
314
|
+
return (0, result_option_1.undefinedOptionWrap)(exports.weatherIdGroup.find((x) => x.id === id)?.description);
|
|
315
315
|
}
|
|
316
316
|
exports.getWeatherV2Description = getWeatherV2Description;
|
|
317
317
|
//# sourceMappingURL=weatherIdGroup.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"weatherIdGroup.js","sourceRoot":"./src/","sources":["types/v2/weatherIdGroup.ts"],"names":[],"mappings":";;;AAAA,+CAAyD;AACzD,6BAAsB;AAEtB;;GAEG;AACU,QAAA,cAAc,GAAG;IAC7B;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,8BAA8B;KAC3C;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,wBAAwB;KACrC;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,8BAA8B;KAC3C;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,oBAAoB;KACjC;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,cAAc;KAC3B;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,oBAAoB;KACjC;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,qBAAqB;KAClC;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,iCAAiC;KAC9C;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,2BAA2B;KACxC;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,iCAAiC;KAC9C;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,SAAS;QAChB,WAAW,EAAE,yBAAyB;KACtC;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,SAAS;QAChB,WAAW,EAAE,SAAS;KACtB;IACD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,OAAO,EAAE,KAAK,EAAC,CAAC;KACtD;IACD,IAAI,CAAC,sBAAc,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE;QAC9C,OAAO,EAAC,OAAO,EAAE,6BAA6B,EAAE,OAAO,EAAE,KAAK,EAAC,CAAC;KAChE;IACD,OAAO,GAAG,CAAC;AACZ,CAAC,CAAC,CAAC;AAEH;;;;;;;;;;;;;GAaG;AACH,SAAgB,uBAAuB,CAAC,EAAyB;IAChE,OAAO,IAAA,kCAAmB,EAAC,sBAAc,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,WAAW,CAAC,CAAC;AAClF,CAAC;AAFD,0DAEC","sourcesContent":["import {Option, undefinedOptionWrap} from 'mharj-result';\nimport {z} from 'zod';\n\n/**\n * This is a list of weather ids, groups and descriptions from OpenWeatherMap API\n */\nexport const weatherIdGroup = [\n\t{\n\t\tid: 200,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm_with_light_rain',\n\t},\n\t{\n\t\tid: 201,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm_with_rain',\n\t},\n\t{\n\t\tid: 202,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm_with_heavy_rain',\n\t},\n\t{\n\t\tid: 210,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'light_thunderstorm',\n\t},\n\t{\n\t\tid: 211,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm',\n\t},\n\t{\n\t\tid: 212,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'heavy_thunderstorm',\n\t},\n\t{\n\t\tid: 221,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'ragged_thunderstorm',\n\t},\n\t{\n\t\tid: 230,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm_with_light_drizzle',\n\t},\n\t{\n\t\tid: 231,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm_with_drizzle',\n\t},\n\t{\n\t\tid: 232,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm_with_heavy_drizzle',\n\t},\n\t{\n\t\tid: 300,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'light_intensity_drizzle',\n\t},\n\t{\n\t\tid: 301,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'drizzle',\n\t},\n\t{\n\t\tid: 302,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'heavy_intensity_drizzle',\n\t},\n\t{\n\t\tid: 310,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'light_intensity_drizzle_rain',\n\t},\n\t{\n\t\tid: 311,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'drizzle_rain',\n\t},\n\t{\n\t\tid: 312,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'heavy_intensity_drizzle_rain',\n\t},\n\t{\n\t\tid: 313,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'shower_rain_and_drizzle',\n\t},\n\t{\n\t\tid: 314,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'heavy_shower_rain_and_drizzle',\n\t},\n\t{\n\t\tid: 321,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'shower_drizzle',\n\t},\n\t{\n\t\tid: 500,\n\t\tgroup: 'rain',\n\t\tdescription: 'light_rain',\n\t},\n\t{\n\t\tid: 501,\n\t\tgroup: 'rain',\n\t\tdescription: 'moderate_rain',\n\t},\n\t{\n\t\tid: 502,\n\t\tgroup: 'rain',\n\t\tdescription: 'heavy_intensity_rain',\n\t},\n\t{\n\t\tid: 503,\n\t\tgroup: 'rain',\n\t\tdescription: 'very_heavy_rain',\n\t},\n\t{\n\t\tid: 504,\n\t\tgroup: 'rain',\n\t\tdescription: 'extreme_rain',\n\t},\n\t{\n\t\tid: 511,\n\t\tgroup: 'rain',\n\t\tdescription: 'freezing_rain',\n\t},\n\t{\n\t\tid: 520,\n\t\tgroup: 'rain',\n\t\tdescription: 'light_intensity_shower_rain',\n\t},\n\t{\n\t\tid: 521,\n\t\tgroup: 'rain',\n\t\tdescription: 'shower_rain',\n\t},\n\t{\n\t\tid: 522,\n\t\tgroup: 'rain',\n\t\tdescription: 'heavy_intensity_shower_rain',\n\t},\n\t{\n\t\tid: 531,\n\t\tgroup: 'rain',\n\t\tdescription: 'ragged_shower_rain',\n\t},\n\t{\n\t\tid: 600,\n\t\tgroup: 'snow',\n\t\tdescription: 'light_snow',\n\t},\n\t{\n\t\tid: 601,\n\t\tgroup: 'snow',\n\t\tdescription: 'snow',\n\t},\n\t{\n\t\tid: 602,\n\t\tgroup: 'snow',\n\t\tdescription: 'heavy_snow',\n\t},\n\t{\n\t\tid: 611,\n\t\tgroup: 'snow',\n\t\tdescription: 'sleet',\n\t},\n\t{\n\t\tid: 612,\n\t\tgroup: 'snow',\n\t\tdescription: 'light_shower_sleet',\n\t},\n\t{\n\t\tid: 613,\n\t\tgroup: 'snow',\n\t\tdescription: 'shower_sleet',\n\t},\n\t{\n\t\tid: 615,\n\t\tgroup: 'snow',\n\t\tdescription: 'light_rain_and_snow',\n\t},\n\t{\n\t\tid: 616,\n\t\tgroup: 'snow',\n\t\tdescription: 'rain_and_snow',\n\t},\n\t{\n\t\tid: 620,\n\t\tgroup: 'snow',\n\t\tdescription: 'light_shower_snow',\n\t},\n\t{\n\t\tid: 621,\n\t\tgroup: 'snow',\n\t\tdescription: 'shower_snow',\n\t},\n\t{\n\t\tid: 622,\n\t\tgroup: 'snow',\n\t\tdescription: 'heavy_shower_snow',\n\t},\n\t{\n\t\tid: 701,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'mist',\n\t},\n\t{\n\t\tid: 711,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'smoke',\n\t},\n\t{\n\t\tid: 721,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'haze',\n\t},\n\t{\n\t\tid: 731,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'sand_dust_whirls',\n\t},\n\t{\n\t\tid: 741,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'fog',\n\t},\n\t{\n\t\tid: 751,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'sand',\n\t},\n\t{\n\t\tid: 761,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'dust',\n\t},\n\t{\n\t\tid: 762,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'volcanic_ash',\n\t},\n\t{\n\t\tid: 771,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'squalls',\n\t},\n\t{\n\t\tid: 781,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'tornado',\n\t},\n\t{\n\t\tid: 800,\n\t\tgroup: 'clear',\n\t\tdescription: 'clear_sky',\n\t},\n\t{\n\t\tid: 801,\n\t\tgroup: 'clouds',\n\t\tdescription: 'few_clouds_11-25_percent',\n\t},\n\t{\n\t\tid: 802,\n\t\tgroup: 'clouds',\n\t\tdescription: 'scattered_clouds_25-50_percent',\n\t},\n\t{\n\t\tid: 803,\n\t\tgroup: 'clouds',\n\t\tdescription: 'broken_clouds_51-84_percent',\n\t},\n\t{\n\t\tid: 804,\n\t\tgroup: 'clouds',\n\t\tdescription: 'overcast_clouds_85-100_percent',\n\t},\n] as const;\n\nexport type WeatherID = (typeof weatherIdGroup)[number]['id'];\n\n/**\n * WeatherGroup: \"clouds\" | \"rain\" | \"snow\" | \"thunderstorm\" | \"drizzle\" | \"atmosphere\" | \"clear\"\n */\nexport type WeatherGroup = (typeof weatherIdGroup)[number]['group'];\n\n/**\n * List for weather description key types from OpenWeatherMap API based on weather id.\n *\n * This list of keys can be used to translate weather id to human readable description with different languages.\n * @example\n * const i18Weather: Record<WeatherDescription, string> = {\n * clear_sky: 'Clear sky',\n * ...\n * }\n */\nexport type WeatherDescription = (typeof weatherIdGroup)[number]['description'];\n\n/**\n * Weather id schema\n * @internal\n */\nexport const weatherIdSchema = z.custom<WeatherID>((val) => {\n\tif (typeof val !== 'number') {\n\t\treturn {message: 'Expected a number', success: false};\n\t}\n\tif (!weatherIdGroup.find((x) => x.id === val)) {\n\t\treturn {message: 'Expected a valid weather id', success: false};\n\t}\n\treturn val;\n});\n\n/**\n * get weather description key from weather id\n * @param id - weather id\n * @returns {Option<WeatherDescription>} option for weather description key\n * @example\n * const weatherComponent = ({data}) => {\n * const key = getWeatherV2Description(data.weather[0]?.id).unwrapOr('unknown');\n * return (\n * <div>\n * {t(`weather:${key}`}\n * </div>\n * );\n * }\n */\nexport function getWeatherV2Description(id: WeatherID | undefined): Option<WeatherDescription> {\n\treturn undefinedOptionWrap(weatherIdGroup.find((x) => x.id === id)?.description);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"weatherIdGroup.js","sourceRoot":"./src/","sources":["types/v2/weatherIdGroup.ts"],"names":[],"mappings":";;;AAAA,8DAAuE;AACvE,6BAAsB;AAEtB;;GAEG;AACU,QAAA,cAAc,GAAG;IAC7B;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,8BAA8B;KAC3C;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,wBAAwB;KACrC;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,8BAA8B;KAC3C;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,oBAAoB;KACjC;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,cAAc;KAC3B;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,oBAAoB;KACjC;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,cAAc;QACrB,WAAW,EAAE,qBAAqB;KAClC;IACD;QACC,EAAE,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,EAAE,QAAQ;QACf,WAAW,EAAE,gCAAgC;KAC7C;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,QAAQ;QACf,WAAW,EAAE,6BAA6B;KAC1C;IACD;QACC,EAAE,EAAE,GAAG;QACP,KAAK,EAAE,QAAQ;QACf,WAAW,EAAE,gCAAgC;KAC7C;CACQ,CAAC;AAqBX;;;GAGG;AACU,QAAA,eAAe,GAAG,OAAC,CAAC,MAAM,CAAY,CAAC,GAAG,EAAE,EAAE;IAC1D,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;QAC5B,OAAO,EAAC,OAAO,EAAE,mBAAmB,EAAE,OAAO,EAAE,KAAK,EAAC,CAAC;KACtD;IACD,IAAI,CAAC,sBAAc,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE;QAC9C,OAAO,EAAC,OAAO,EAAE,6BAA6B,EAAE,OAAO,EAAE,KAAK,EAAC,CAAC;KAChE;IACD,OAAO,GAAG,CAAC;AACZ,CAAC,CAAC,CAAC;AAEH;;;;;;;;;;;;;GAaG;AACH,SAAgB,uBAAuB,CAAC,EAAyB;IAChE,OAAO,IAAA,mCAAmB,EAAC,sBAAc,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,WAAW,CAAC,CAAC;AAClF,CAAC;AAFD,0DAEC","sourcesContent":["import {Option, undefinedOptionWrap} from '@luolapeikko/result-option';\nimport {z} from 'zod';\n\n/**\n * This is a list of weather ids, groups and descriptions from OpenWeatherMap API\n */\nexport const weatherIdGroup = [\n\t{\n\t\tid: 200,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm_with_light_rain',\n\t},\n\t{\n\t\tid: 201,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm_with_rain',\n\t},\n\t{\n\t\tid: 202,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm_with_heavy_rain',\n\t},\n\t{\n\t\tid: 210,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'light_thunderstorm',\n\t},\n\t{\n\t\tid: 211,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm',\n\t},\n\t{\n\t\tid: 212,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'heavy_thunderstorm',\n\t},\n\t{\n\t\tid: 221,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'ragged_thunderstorm',\n\t},\n\t{\n\t\tid: 230,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm_with_light_drizzle',\n\t},\n\t{\n\t\tid: 231,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm_with_drizzle',\n\t},\n\t{\n\t\tid: 232,\n\t\tgroup: 'thunderstorm',\n\t\tdescription: 'thunderstorm_with_heavy_drizzle',\n\t},\n\t{\n\t\tid: 300,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'light_intensity_drizzle',\n\t},\n\t{\n\t\tid: 301,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'drizzle',\n\t},\n\t{\n\t\tid: 302,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'heavy_intensity_drizzle',\n\t},\n\t{\n\t\tid: 310,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'light_intensity_drizzle_rain',\n\t},\n\t{\n\t\tid: 311,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'drizzle_rain',\n\t},\n\t{\n\t\tid: 312,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'heavy_intensity_drizzle_rain',\n\t},\n\t{\n\t\tid: 313,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'shower_rain_and_drizzle',\n\t},\n\t{\n\t\tid: 314,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'heavy_shower_rain_and_drizzle',\n\t},\n\t{\n\t\tid: 321,\n\t\tgroup: 'drizzle',\n\t\tdescription: 'shower_drizzle',\n\t},\n\t{\n\t\tid: 500,\n\t\tgroup: 'rain',\n\t\tdescription: 'light_rain',\n\t},\n\t{\n\t\tid: 501,\n\t\tgroup: 'rain',\n\t\tdescription: 'moderate_rain',\n\t},\n\t{\n\t\tid: 502,\n\t\tgroup: 'rain',\n\t\tdescription: 'heavy_intensity_rain',\n\t},\n\t{\n\t\tid: 503,\n\t\tgroup: 'rain',\n\t\tdescription: 'very_heavy_rain',\n\t},\n\t{\n\t\tid: 504,\n\t\tgroup: 'rain',\n\t\tdescription: 'extreme_rain',\n\t},\n\t{\n\t\tid: 511,\n\t\tgroup: 'rain',\n\t\tdescription: 'freezing_rain',\n\t},\n\t{\n\t\tid: 520,\n\t\tgroup: 'rain',\n\t\tdescription: 'light_intensity_shower_rain',\n\t},\n\t{\n\t\tid: 521,\n\t\tgroup: 'rain',\n\t\tdescription: 'shower_rain',\n\t},\n\t{\n\t\tid: 522,\n\t\tgroup: 'rain',\n\t\tdescription: 'heavy_intensity_shower_rain',\n\t},\n\t{\n\t\tid: 531,\n\t\tgroup: 'rain',\n\t\tdescription: 'ragged_shower_rain',\n\t},\n\t{\n\t\tid: 600,\n\t\tgroup: 'snow',\n\t\tdescription: 'light_snow',\n\t},\n\t{\n\t\tid: 601,\n\t\tgroup: 'snow',\n\t\tdescription: 'snow',\n\t},\n\t{\n\t\tid: 602,\n\t\tgroup: 'snow',\n\t\tdescription: 'heavy_snow',\n\t},\n\t{\n\t\tid: 611,\n\t\tgroup: 'snow',\n\t\tdescription: 'sleet',\n\t},\n\t{\n\t\tid: 612,\n\t\tgroup: 'snow',\n\t\tdescription: 'light_shower_sleet',\n\t},\n\t{\n\t\tid: 613,\n\t\tgroup: 'snow',\n\t\tdescription: 'shower_sleet',\n\t},\n\t{\n\t\tid: 615,\n\t\tgroup: 'snow',\n\t\tdescription: 'light_rain_and_snow',\n\t},\n\t{\n\t\tid: 616,\n\t\tgroup: 'snow',\n\t\tdescription: 'rain_and_snow',\n\t},\n\t{\n\t\tid: 620,\n\t\tgroup: 'snow',\n\t\tdescription: 'light_shower_snow',\n\t},\n\t{\n\t\tid: 621,\n\t\tgroup: 'snow',\n\t\tdescription: 'shower_snow',\n\t},\n\t{\n\t\tid: 622,\n\t\tgroup: 'snow',\n\t\tdescription: 'heavy_shower_snow',\n\t},\n\t{\n\t\tid: 701,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'mist',\n\t},\n\t{\n\t\tid: 711,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'smoke',\n\t},\n\t{\n\t\tid: 721,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'haze',\n\t},\n\t{\n\t\tid: 731,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'sand_dust_whirls',\n\t},\n\t{\n\t\tid: 741,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'fog',\n\t},\n\t{\n\t\tid: 751,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'sand',\n\t},\n\t{\n\t\tid: 761,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'dust',\n\t},\n\t{\n\t\tid: 762,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'volcanic_ash',\n\t},\n\t{\n\t\tid: 771,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'squalls',\n\t},\n\t{\n\t\tid: 781,\n\t\tgroup: 'atmosphere',\n\t\tdescription: 'tornado',\n\t},\n\t{\n\t\tid: 800,\n\t\tgroup: 'clear',\n\t\tdescription: 'clear_sky',\n\t},\n\t{\n\t\tid: 801,\n\t\tgroup: 'clouds',\n\t\tdescription: 'few_clouds_11-25_percent',\n\t},\n\t{\n\t\tid: 802,\n\t\tgroup: 'clouds',\n\t\tdescription: 'scattered_clouds_25-50_percent',\n\t},\n\t{\n\t\tid: 803,\n\t\tgroup: 'clouds',\n\t\tdescription: 'broken_clouds_51-84_percent',\n\t},\n\t{\n\t\tid: 804,\n\t\tgroup: 'clouds',\n\t\tdescription: 'overcast_clouds_85-100_percent',\n\t},\n] as const;\n\nexport type WeatherID = (typeof weatherIdGroup)[number]['id'];\n\n/**\n * WeatherGroup: \"clouds\" | \"rain\" | \"snow\" | \"thunderstorm\" | \"drizzle\" | \"atmosphere\" | \"clear\"\n */\nexport type WeatherGroup = (typeof weatherIdGroup)[number]['group'];\n\n/**\n * List for weather description key types from OpenWeatherMap API based on weather id.\n *\n * This list of keys can be used to translate weather id to human readable description with different languages.\n * @example\n * const i18Weather: Record<WeatherDescription, string> = {\n * clear_sky: 'Clear sky',\n * ...\n * }\n */\nexport type WeatherDescription = (typeof weatherIdGroup)[number]['description'];\n\n/**\n * Weather id schema\n * @internal\n */\nexport const weatherIdSchema = z.custom<WeatherID>((val) => {\n\tif (typeof val !== 'number') {\n\t\treturn {message: 'Expected a number', success: false};\n\t}\n\tif (!weatherIdGroup.find((x) => x.id === val)) {\n\t\treturn {message: 'Expected a valid weather id', success: false};\n\t}\n\treturn val;\n});\n\n/**\n * get weather description key from weather id\n * @param id - weather id\n * @returns {Option<WeatherDescription>} option for weather description key\n * @example\n * const weatherComponent = ({data}) => {\n * const key = getWeatherV2Description(data.weather[0]?.id).unwrapOr('unknown');\n * return (\n * <div>\n * {t(`weather:${key}`}\n * </div>\n * );\n * }\n */\nexport function getWeatherV2Description(id: WeatherID | undefined): Option<WeatherDescription> {\n\treturn undefinedOptionWrap(weatherIdGroup.find((x) => x.id === id)?.description);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mharj/openweathermap",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "Open Weather API Client",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -59,30 +59,30 @@
|
|
|
59
59
|
},
|
|
60
60
|
"homepage": "https://github.com/mharj/ts-openweather#readme",
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@avanio/expire-cache": "^0.3.
|
|
63
|
-
"
|
|
64
|
-
"tslib": "^2.6.
|
|
65
|
-
"zod": "^3.
|
|
62
|
+
"@avanio/expire-cache": "^0.3.3",
|
|
63
|
+
"@luolapeikko/result-option": "^0.5.3",
|
|
64
|
+
"tslib": "^2.6.2",
|
|
65
|
+
"zod": "^3.23.0"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
|
-
"@types/chai": "^4.3.
|
|
69
|
-
"@types/mocha": "^10.0.
|
|
70
|
-
"@types/node": "^18.
|
|
71
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
72
|
-
"@typescript-eslint/parser": "^
|
|
73
|
-
"chai": "^4.
|
|
74
|
-
"eslint": "^8.
|
|
75
|
-
"eslint-config-prettier": "^
|
|
68
|
+
"@types/chai": "^4.3.14",
|
|
69
|
+
"@types/mocha": "^10.0.6",
|
|
70
|
+
"@types/node": "^18.19.31",
|
|
71
|
+
"@typescript-eslint/eslint-plugin": "^7.7.0",
|
|
72
|
+
"@typescript-eslint/parser": "^7.7.0",
|
|
73
|
+
"chai": "^4.4.1",
|
|
74
|
+
"eslint": "^8.57.0",
|
|
75
|
+
"eslint-config-prettier": "^9.1.0",
|
|
76
76
|
"eslint-config-standard": "^17.1.0",
|
|
77
|
-
"eslint-plugin-deprecation": "^
|
|
78
|
-
"eslint-plugin-prettier": "^5.
|
|
77
|
+
"eslint-plugin-deprecation": "^2.0.0",
|
|
78
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
79
79
|
"eslint-plugin-sonarjs": "^0.19.0",
|
|
80
|
-
"mocha": "^10.
|
|
80
|
+
"mocha": "^10.4.0",
|
|
81
81
|
"nyc": "^15.1.0",
|
|
82
|
-
"prettier": "^3.
|
|
82
|
+
"prettier": "^3.2.5",
|
|
83
83
|
"source-map-support": "^0.5.21",
|
|
84
|
-
"ts-node": "^10.9.
|
|
85
|
-
"typedoc": "^0.
|
|
86
|
-
"typedoc-plugin-zod": "^1.
|
|
84
|
+
"ts-node": "^10.9.2",
|
|
85
|
+
"typedoc": "^0.25.13",
|
|
86
|
+
"typedoc-plugin-zod": "^1.1.2"
|
|
87
87
|
}
|
|
88
88
|
}
|