这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dist/cache.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare class Cache<T> {
private items;
constructor();
get(key: string): T | undefined;
set(key: string, value: T): void;
}
export default Cache;
14 changes: 14 additions & 0 deletions dist/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Cache {
constructor() {
this.items = {};
}
get(key) {
return this.items[key];
}
set(key, value) {
this.items[key] = value;
}
}
exports.default = Cache;
30 changes: 27 additions & 3 deletions dist/client.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientOptions, ForecastResponse, AlertsResponse, ForecastType, AlertOptions } from './types';
import { ClientOptions, ForecastResponse, AlertsResponse, ForecastType, StationsResponse, Station, AlertOptions } from './types';
/**
* The main client
*
Expand All @@ -8,14 +8,16 @@ import { ClientOptions, ForecastResponse, AlertsResponse, ForecastType, AlertOpt
*/
declare class Client {
private options;
private pointCache;
private stationsCache;
constructor(options?: ClientOptions);
private getPath;
private getUrl;
private getPoint;
getOptions(): ClientOptions;
setOptions(newOptions: ClientOptions): void;
/**
* Get weather alerts for a given area
* Get weather alerts for a given area.
*
* ```typescript
* const active = true;
Expand All @@ -26,7 +28,7 @@ declare class Client {
*/
getAlerts(active: boolean, options: AlertOptions): Promise<AlertsResponse>;
/**
* Get a weather forecast for a given latitude and longitude
* Get a weather forecast for a given latitude and longitude.
*
* ```typescript
* const latitude = 35.6175667;
Expand All @@ -36,5 +38,27 @@ declare class Client {
*
*/
getForecast(latitude: number, longitude: number, forecastType: ForecastType): Promise<ForecastResponse>;
/**
* Get the closest weather stations for a given latitude and longitude.
*
* ```typescript
* const latitude = 35.6175667;
* const longitude = -80.7709911;
* const stations = await client.getStations(latitude, longitude);
* ```
*
*/
getStations(latitude: number, longitude: number): Promise<StationsResponse>;
/**
* Get the closest weather station for a given latitude and longitude.
*
* ```typescript
* const latitude = 35.6175667;
* const longitude = -80.7709911;
* const stationOrNull = await client.getNearestStation(latitude, longitude);
* ```
*
*/
getNearestStation(latitude: number, longitude: number): Promise<Station | null>;
}
export { Client };
60 changes: 54 additions & 6 deletions dist/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const cache_1 = __importDefault(require("./cache"));
const defaultOptions = {
userAgent: 'weathered package'
};
Expand Down Expand Up @@ -33,6 +34,8 @@ const processOptions = (options) => {
class Client {
constructor(options) {
this.options = { ...defaultOptions, ...options };
this.pointCache = new cache_1.default();
this.stationsCache = new cache_1.default();
}
getPath(path) {
return this.getUrl(API_ROOT + path);
Expand All @@ -41,9 +44,16 @@ class Client {
const resp = await cross_fetch_1.default(url);
return await resp.json();
}
getPoint(latitude, longitude) {
async getPoint(latitude, longitude) {
const cacheKey = `${latitude},${longitude}`;
const potentialPointResponse = this.pointCache.get(cacheKey);
if (potentialPointResponse) {
return potentialPointResponse;
}
const path = `points/${latitude},${longitude}`;
return this.getPath(path);
const pointResponse = await this.getPath(path);
this.pointCache.set(cacheKey, pointResponse);
return pointResponse;
}
getOptions() {
return { ...this.options };
Expand All @@ -52,7 +62,7 @@ class Client {
this.options = { ...this.options, ...newOptions };
}
/**
* Get weather alerts for a given area
* Get weather alerts for a given area.
*
* ```typescript
* const active = true;
Expand All @@ -67,7 +77,7 @@ class Client {
return this.getPath(path);
}
/**
* Get a weather forecast for a given latitude and longitude
* Get a weather forecast for a given latitude and longitude.
*
* ```typescript
* const latitude = 35.6175667;
Expand All @@ -77,10 +87,48 @@ class Client {
*
*/
async getForecast(latitude, longitude, forecastType) {
const pointResp = await this.getPoint(latitude, longitude);
const pointResponse = await this.getPoint(latitude, longitude);
const forecastKey = forecastType === 'hourly' ? 'forecastHourly' : 'forecast';
const url = pointResp.properties[forecastKey];
const url = pointResponse.properties[forecastKey];
return this.getUrl(url);
}
/**
* Get the closest weather stations for a given latitude and longitude.
*
* ```typescript
* const latitude = 35.6175667;
* const longitude = -80.7709911;
* const stations = await client.getStations(latitude, longitude);
* ```
*
*/
async getStations(latitude, longitude) {
const pointResponse = await this.getPoint(latitude, longitude);
const stationsUrl = pointResponse.properties.observationStations;
const potentionalStationsResponse = this.stationsCache.get(stationsUrl);
if (potentionalStationsResponse) {
return potentionalStationsResponse;
}
const stationsResponse = await this.getUrl(stationsUrl);
this.stationsCache.set(stationsUrl, stationsResponse);
return stationsResponse;
}
/**
* Get the closest weather station for a given latitude and longitude.
*
* ```typescript
* const latitude = 35.6175667;
* const longitude = -80.7709911;
* const stationOrNull = await client.getNearestStation(latitude, longitude);
* ```
*
*/
async getNearestStation(latitude, longitude) {
const stationsResponse = await this.getStations(latitude, longitude);
if (stationsResponse.features.length > 0) {
return stationsResponse.features[0];
}
return null;
}
}
exports.Client = Client;
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { Client } from './client';
export { ForecastType, Area, Region, RegionType, Urgency, AlertOptions, ClientOptions, PointResponse, ForecastResponse, AlertsResponse, AlertsFeature } from './types';
export { ForecastType, Area, Region, RegionType, Urgency, AlertOptions, ClientOptions, PointResponse, ForecastResponse, ForecastProperties, AlertsResponse, Station, StationsResponse, AlertsFeature } from './types';
8 changes: 8 additions & 0 deletions dist/point_cache.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PointResponse } from './types';
declare class PointCache {
private points;
constructor();
get(latitude: number, longitude: number): PointResponse | undefined;
set(latitude: number, longitude: number, pointResponse: PointResponse): void;
}
export default PointCache;
15 changes: 15 additions & 0 deletions dist/point_cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cacheKey = (lat, lng) => `${lat},${lng}`;
class PointCache {
constructor() {
this.points = {};
}
get(latitude, longitude) {
return this.points[cacheKey(latitude, longitude)];
}
set(latitude, longitude, pointResponse) {
this.points[cacheKey(latitude, longitude)] = pointResponse;
}
}
exports.default = PointCache;
9 changes: 8 additions & 1 deletion dist/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ declare type PointResponse = {
properties: {
forecast: string;
forecastHourly: string;
observationStations: string;
};
};
declare type Station = {
id: string;
};
declare type StationsResponse = {
features: Station[];
};
declare type ForecastPeriod = {
number: number;
name: string;
Expand Down Expand Up @@ -81,4 +88,4 @@ declare type AlertsFeature = {
declare type AlertsResponse = {
features: AlertsFeature[];
};
export { ForecastType, Area, Region, RegionType, Urgency, AlertOptions, ClientOptions, PointResponse, ForecastResponse, AlertsResponse, AlertsFeature };
export { ForecastType, Area, Region, RegionType, Urgency, AlertOptions, ClientOptions, PointResponse, ForecastResponse, ForecastProperties, Station, StationsResponse, AlertsResponse, AlertsFeature };
2 changes: 1 addition & 1 deletion docs/assets/js/search.js

Large diffs are not rendered by default.

Loading