npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@shootismoke/dataproviders

v0.10.0

Published

Helpers to fetch data from air quality data providers

Downloads

38

Readme

npm (scoped) dependencies Status Maintainability Test Coverage

@shootismoke/dataproviders

A library to fetch air quality data from various providers (AQICN, OpenAQ...) and normalizing data into one common format: the openaq-data-format.

Supported Air Quality Providers

| Provider | Provider Code | Website | Notes | | --------- | ------------- | ------------------ | -------------------------------------------------------------------------------- | | AQICN | aqicn | https://aqicn.org | API token needed, get one for free here | | WAQI | waqi | https://waqi.info/ | Same institution as AqiCN | | OpenAQ v2 | openaq | https://openaq.org | |

We plan to support more air quality providers in the future see issue #29.

⚡ Get Started

Install the package:

yarn install @shootismoke/dataproviders

The package exposes a couple of data providers (see list above), and for each data provider, there are two main functions:

  • fetchByGps({ latitude, longitude }, options?) - Fetch air quality data by GPS coordinates.
  • fetchByStation(stationId, options?) - Fetch air quality data by station ID.
// Retrieve the providers by provider code.
import { aqicn, openaq } from '@shootismoke/dataproviders';

async function main() {
	// Fetch the station 1045 on AQICN.
	const data = await aqicn.fetchByStation(1045);
	console.log(data.dominentpol); // `dominentpol` is a field specific to AQICN. This logs "pm25".

	const results = aqicn.normalizeByStation(data); // `results` is an array of normalized OpenAQResults.
	const res = results[0];
	console.log(`${res.parameter}: ${res.value} ${res.unit}`); // Logs "pm25: 34.5 µg/m³".

	// Note how `data.dominentpol` has been converted to `res.parameter`: this is
	// what we call "normalization" in @shootismoke/providers.
}

Usage with fp-ts

The codebase uses io-ts under the hood to perform runtime data validation, the results are functional programming datatypes (Either<E,A>, Task<E,A>...). If you are already using io-ts and fp-ts](https://github.com/gcanti/fp-ts) in your project, it's recommended to import from the /lib/fp subfolder directly.

// Retrieve the providers by provider code, using fp-ts.
import { aqicn, openaq } from '@shootismoke/dataproviders/lib/fp';

aqicn.fetchByGps({ latitude: 45, longitude: 23 }); // Returns a TaskEither<Error, AqicnData>

Here's a real-world example:

import { pipe } from 'fp-ts/lib/pipeable';
import * as TE from 'fp-ts/lib/TaskEither';

pipe(
	// Fetch data from station 'Coyhaique' on the OpenAQ platform.
	openaq.fetchByStation('Coyhaique'),
	// Normalize the data.
	TE.chain((response) => TE.fromEither(normalize(response))),
	// Depending on error/result case, do different stuff
	TE.fold(
		// Do on error:
		(error) => {
			console.error(error);

			// -- snip --
		},
		// Do on success:
		(results) => {
			const res = results[0]; // `results` is an array of normalized OpenAQ objects
			console.log(`${res.value} ${res.unit}`); // Logs "34.5 µg/m³"

			// -- snip --
		}
	)
);

Normalized Data Format

If you use the .normalizeByGps or .normalizeByStation functions, the output will be normalized, i.e. in the same format no matter which provider you use. We follow the openaq-data-format, below are some of its most frequently used fields:

/**
 * The OpenAQ data format. One such object represents one air quality
 * measurement
 */
interface OpenAQResult {
	/**
	 * City (or regional approximation) containing location
	 */
	city?: string;
	/**
	 * Coordinates where the measurement took place. Note that in the
	 * openaq-data-format, this field is optional. Using this library, this field
	 * will **always** be populated
	 */
	coordinates?: {
		latitude: number;
		longitude: number;
	};
	/**
	 * Country containing location in two letter ISO format
	 */
	country: string;
	/**
	 * Time of measurement including both local time and UTC time.
	 */
	date: {
		local: string;
		utc: string;
	};
	/**
	 * A unique ID representing the location of a measurement (can be a station
	 * ID, a place...)
	 */
	location: string;
	/**
	 * The pollutant id (pm25, pm10, o3...)
	 */
	parameter: Pollutant;
	/**
	 * The value of the measurement
	 */
	value: number;
	/**
	 * The unit the value is measured in (µg/m³, ppm)
	 */
	unit: Unit;
	// -- snip --
	// There are some other optional fields, see docs for more details.
}

/**
 * The normalized data is an array of OpenAQ results. We ensure there is
 * always at least one element in the Normalized array.
 */
type OpenAQResults = OpenAQResult[];

See openaq-data-format for more information.

Full Documentation

See the API reference documentation.

:raising_hand: Contribute

  1. Fork the repo
  2. Make your changes in your own fork
  3. Make sure yarn lint and yarn test pass
  4. Create a Pull Request on this repo

Tests

Look out for *.spec.ts in the codebase. Run:

yarn test

:newspaper: License

GPL-3.0. See LICENSE file for more information.