0.2.0 • Published 5 years ago

@paxperscientiam/nation-weather-service-api.ts v0.2.0

Weekly downloads
61
License
MIT
Repository
-
Last release
5 years ago

Provides types for the US National Weather Service's forecast API and a helpful URI builder.

Motivation

TypeScript makes building API requests more straightforward.

Installation

pnpm install @paxperscientiam/nation-weather-service-api.ts

or ...

npm install @paxperscientiam/nation-weather-service-api.ts

or ...

yarn add @paxperscientiam/nation-weather-service-api.ts

Importing

import {
    NationalWeatherServicePublicDataAPIServices as NWS
} from "@paxperscientiam/nation-weather-service-api.ts"

const {
    NationalWeatherServicePublicDataAPIServices
} = require("@paxperscientiam/nation-weather-service-api.ts")

Under the hood

For example, say you want to make a request to the "Alerts" endpoint of the NWS forecast API (I.E. https://api.weather.gov/alerts). This endpoint accepts URL queries.

Here's the interface for those parameters:

declare namespace NationalWeatherServicePublicDataAPI {
    namespace Alerts {
        export interface IQueryParameters {
            active?: Parameters.IActive;
            start?: Parameters.IStart; // date-time
            end?: Parameters.IEnd; // date-time
            status?: Parameters.IStatus;
            message_type?: Parameters.IMessageType;
            event?: Parameters.IEvent;
            code?: Parameters.ICode;
            region_type?: Parameters.IRegionType;
            point?: Parameters.IPoint; // ^(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)$
            region?: Parameters.IRegion;
            area?: Parameters.IArea;
            zone?: Parameters.IZone;
            urgency?: Parameters.IUrgency;
            severity?: Parameters.ISeverity;
            certainty?: Parameters.ICertainty;
            limit?: Parameters.ILimit;
            cursor?: Parameters.ICursor;
        }
        namespace Parameters {
            export type IActive = boolean;
            export type IArea = ("AL" | "AK" | "AS" | "AR" | "AZ" | "CA" | "CO" | "CT" | "DE" | "DC" | "FL" | "GA" | "GU" | "HI" | "ID" | "IL" | "IN" | "IA" | "KS" | "KY" | "LA" | "ME" | "MD" | "MA" | "MI" | "MN" | "MS" | "MO" | "MT" | "NE" | "NV" | "NH" | "NJ" | "NM" | "NY" | "NC" | "ND" | "OH" | "OK" | "OR" | "PA" | "PR" | "RI" | "SC" | "SD" | "TN" | "TX" | "UT" | "VT" | "VI" | "VA" | "WA" | "WV" | "WI" | "WY" | "PZ" | "PK" | "PH" | "PS" | "PM" | "AN" | "AM" | "GM" | "LS" | "LM" | "LH" | "LC" | "LE" | "LO")[];
            export type ICertainty = ("unknown" | "unlikely" | "possible" | "likely" | "observed")[];
            export type ICode = string /* ^\w{3}$ */ [];
            export type ICursor = string;
            export type IEnd = string; // date-time
            export type IEvent = string /* ^[A-Za-z0-9 ]+$ */ [];
            export type ILimit = number;
            export type IMessageType = ("alert" | "update" | "cancel")[];
            export type IPoint = string; // ^(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)$
            export type IRegion = ("AL" | "AT" | "GL" | "GM" | "PA" | "PI")[];
            export type IRegionType = "land" | "marine";
            export type ISeverity = ("unknown" | "minor" | "moderate" | "severe" | "extreme")[];
            export type IStart = string; // date-time
            export type IStatus = ("actual" | "exercise" | "system" | "test" | "draft")[];
            export type IUrgency = ("unknown" | "past" | "future" | "expected" | "immediate")[];
            export type IZone = string /* ^\w{2}[CZ]\d{3}$ */ [];
        }
        namespace Responses {
            export type IDefault = Components.Responses.IError;
        }
    }

The exposed class is NationalWeatherServicePublicDataAPIServices. The method corresponding to the https://api.weather.gov/alerts endpoint looks like this:

export class NationalWeatherServicePublicDataAPIServices {
    private domain: string = "https://api.weather.gov"
    private query: any
    private pathString: string = ""

    constructor() {
        return this
    }
    // Returns all alerts
    AlertsService(query?: NationalWeatherServicePublicDataAPI.Alerts.IQueryParameters) {
        if (query) {
            this.query = query
        }
        this.pathString = `/alerts`
        return this
    }

Example

With all this said, here's how you might use this:

import { NationalWeatherServicePublicDataAPIServices } from "@paxperscientiam/nation-weather-service-api.ts"

const NWS = new NationalWeatherServicePublicDataAPIServices()

const alerts = NWS.AlertsService({
    active: true,
})

const url = alerts.buildURI()
// => "https://api.weather.gov/alerts?active=true"

Humans

0.2.0

5 years ago

0.1.9-beta.15

5 years ago

0.1.9-beta.14

5 years ago

0.1.9-beta.13

5 years ago

0.1.9-beta.12

5 years ago

0.1.9-beta.11

5 years ago

0.1.9-beta.10

5 years ago

0.1.9-beta.9

5 years ago

0.1.9-beta.8

5 years ago

0.1.9-beta.7

5 years ago

0.1.9-beta.6

5 years ago

0.1.9-beta.5

5 years ago

0.1.9-beta.3

5 years ago

0.1.9-beta.2

5 years ago

0.1.9-beta.0

5 years ago

0.1.8

5 years ago

0.1.8-beta.21

5 years ago

0.1.8-beta.20

5 years ago

0.1.8-beta.19

5 years ago

0.1.8-beta.18

5 years ago

0.1.8-beta.17

5 years ago

0.1.8-beta.16

5 years ago

0.1.8-beta.15

5 years ago

0.1.8-beta.14

5 years ago

0.1.8-beta.13

5 years ago

0.1.8-beta.12

5 years ago

0.1.8-beta.11

5 years ago

0.1.8-beta.10

5 years ago

0.1.8-beta.9

5 years ago

0.1.8-beta.8

5 years ago

0.1.8-beta.6

5 years ago

0.1.8-beta.5

5 years ago

0.1.8-beta.4

5 years ago

0.1.8-beta.3

5 years ago

0.1.8-beta.2

5 years ago

0.1.8-beta.1

5 years ago

0.1.8-beta.0

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5-beta.14

5 years ago

0.1.5-beta.13

5 years ago

0.1.5-beta.12

5 years ago

0.1.5-beta.11

5 years ago

0.1.5-beta.10

5 years ago

0.1.5-beta.9

5 years ago

0.1.5-beta.8

5 years ago

0.1.5-beta.7

5 years ago

0.1.5-beta.6

5 years ago

0.1.5-beta.5

5 years ago

0.1.5-beta.4

5 years ago

0.1.5-beta.3

5 years ago

0.1.5-beta.2

5 years ago

0.1.5-beta.1

5 years ago

0.1.5-beta.0

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago