A simple typescript client for Dark Sky API.
All types based off what is found in the official API documentation.
DISCLAIMER: This does not use a proxy and as such does not hide your api key during requests. If you would like to use a proxy approach there are several other packages that offer it.
npm install dark-sky-client-ts --save
import { DarkSkyApiClient } from "dark-sky-client-ts"; // And any other types needed.
const dsApi = new DarkSkyApiClient(apiKey, params);
async getWeather() {
const weatherIsh = await dsApi.get(); // requires type assertion.
const weather = await dsApi.getWeather(); // resolves type.
}
async getHourly() {
const hourlyIsh = await dsApi.get("hourly"); // requires type assertion.
const hourly = await dsApi.getHourly(); // resolves type.
}
async getDaily() {
const daily = await dsApi.get("daily", {
latitude: -38.590451,
longitude: 175.541561,
lang: "zh",
units: "uk2"
});
}
const dsApi = new DarkSkyApiClient(apiKey, params);
- apiKey (
string
) - Dark Sky API key. - params (
DarkSkyRequestObject
) - optional params for API request.
async get(
field?: DarkSkyRootField, // Which section of the data you want. If ommitted, returns all.
params?: DarkSkyRequestObject // If ommitted, uses client's declared params.
)
Note: Responses are stored for the duration of the refresh rate, unless it is a Time Machine request.
getWeather(): Promise<DarkSkyResponseObject> // Returns full api response.
getCurrently(): Promise<DarkSkyCurrentlyResponse> // Returns currently field from api response.
getMinutely(): Promise<DarkSkyDataBlockObject> // Returns minutely field from api response.
getHourly(): Promise<DarkSkyHourlyBlockObject> // Returns hourly field from api response.
getDaily(): Promise<DarkSkyDailyBlockObject> // Returns daily field from api response.
getAlerts(): Promise<DarkSkyAlertObject[]> // Returns alerts field from api response.
getFlags(): Promise<DarkSkyFlagsObject> // Returns flags field from api response.
getRequestParams(): DarkSkyRequestObject // Returns current request params.
setRequestParams(request: DarkSkyRequestObject) // Sets current request params.
getRefreshRate(): number // Gets current refresh rate for data in minutes.
setRefreshRate(refreshRate: number) // Sets current refresh rate for data in minutes. Minimum is 30.
setCoords(lat: number, lng: number) // Sets current request latitude & longitude.
setExcludes(excludes: DarkSkyRootField[]) // Sets which root field to exclude from response.
setLang(lang: DarkSkySupportedLanguage) // Sets the preferred language from supported languages.
setUnits(units: DarkSkyUnitType) // Sets the preferred units of measurements from supported units.
setTime(time: number | string) // Sets time for Time Machine requests, parsed internally.
// Number should be milliseconds.
// String should be ISO string.
{
latitude?: number; // Required. Set either in constructor or using setCoords client method.
longitude?: number; // Required. Set either in constructor or using setCoords client method.
exclude?: DarkSkyRootField[]; // List of root fields to exclude from returned data.
lang?: DarkSkySupportedLanguage; // Language string that matches supported list.
units?: DarkSkyUnitType; // Unit of measurement that matches supported list.
timeSeconds?: number; // Used for time machine requests. Pass in milliseconds,
// as conversion to UNIX is handled internally.
timeString?: string; // Used for time machine requests. Pass in ISO string.
// Setting of this field and timeSeconds are exclusive to each other.
}
For the sake of brevity, I'm going to go ahead and say that all reponses should match what is outlined in the official API documentation. But for a general type skeleton:
DarkSkyResponseObject {
...
DarkSkyCurrentlyResponse // currently
DarkSkyDataBlockObject { // minutely
...
DarkSkyDataPointObject[]
}
DarkSkyHourlyBlockObject { // hourly
...
DarkSkyHourlyResponse[]
}
DarkSkyDailyBlockObject { // daily
...
DarkSkyDailyResponse[]
}
DarkSkyAlertObject[] // alerts
DarkSkyFlagsObject // flags
}