1.0.16 • Published 5 years ago

dark-sky-client-ts v1.0.16

Weekly downloads
14
License
UNLICENSED
Repository
github
Last release
5 years ago

dark-sky-client-ts

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.

Installation

npm install dark-sky-client-ts --save

Usage

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"
    });
}

DarkSkyApiClient

const dsApi = new DarkSkyApiClient(apiKey, params);
  • apiKey (string) - Dark Sky API key.
  • params (DarkSkyRequestObject) - optional params for API request.

Methods

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.
Note: minutely is excluded by default, because c'mon, really?
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.
Note: time is used for Time Machine requests and is reset to null after each call.

DarkSkyRequestObject

{
  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.
}

DarkSkyResponseObject

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
}
1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago