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

@firstclasspostcodes/js

v1.6.2

Published

Firstclasspostcodes API Wrapper

Downloads

21

Readme

Firstclasspostcodes

Build, test and release

The Firstclasspostcodes Javascript library is compatible with both Node.JS and Browsers.

The library will work in all modern browsers and IE9+.

An identical API is provided for both the client and server side.

Documentation

See @firstclasspostcodes/js API docs for detailed usage and examples.

Installation

Install the package with:

npm install @firstclasspostcodes/js

or directly in HTML with:

<script src="https://js.firstclasspostcodes.com/v1.6.2.js"></script>

Note on older browsers: We recommend using a polyfill service, the following example covers all of the required language features:

<script src="https://polyfill.io/v3/polyfill.min.js?features=Promise%2Cfetch%2CObject.assign"></script>

Security

Where the libary is loaded on pages including sensitive information, we recommend using the Subresource Integrity security feature.

Every version of the library is accompanied by an SRI hash file, the hash can be accessed directly using:

$ curl https://js.firstclasspostcodes.com/v1.6.2.sri.txt # => "sha256-45tfd... sha384-43567ytr..."

You can then update the above <script> tag, adding the integrity attribute:

<script src="https://js.firstclasspostcodes.com/v1.6.2.js"
        integrity="sha256-45tfd... sha384-43567ytr..."
        crossorigin="anonymous"></script>

Usage

You need to configure the library to use your API Key which is available on your dashboard. Require the library using the key:

const client = require('@firstclasspostcodes/js')('fg3rfgy3345tgfAt3r');

const postcodeData = await client.getPostcode('sw13 8gh');

Using ES modules looks a little different:

import Firstclasspostcodes from '@firstclasspostcodes/js';

const client = Firstclasspostcodes('fg3rfgy3345tgfAt3r');

// ...

Configuration

The library can be initialized with several options:

const client = Firstclasspostcodes('fg3r...', {
  endpoint: 'https://api.firstclasspostcodes.com/data',
  content: 'json',
});

| Property | Default | Description | |:-----|:-----|:-----| | endpoint | https://api.firstclasspostcodes.com/data | The endpoint to be used. This can be overridden to use a private endpoint, or for testing purposes. | | content | 'json' | The content key controls the type of response being received. geo+json can be used to receives responses in GeoJSON.

Events

Event handlers can be attached to the library using the Node.JS EventEmitter pattern.

The library does not currently support wildcards or regular expressions.

const client = Firstclasspostcodes('....');

const handler = (requestObj) => {
  console.log(requestObj);
};

client.on('request', handler);

client.once('response', (responseObj) => {
  sendSignal(responseObj);
});

client.off('request', handler);

| Event name | Description | |:-----|:-----| | request | Triggered before a request is sent. The request object to be sent is passed to the event handler. | | response | Triggered with the parsed JSON response body upon a successful reques. | | error | Triggered with a client error when the request fails. | | operation:{name} | Triggered by an operation with the parameter object. |

Note: {name} is replaced with the operation name of the method, as defined inside the OpenAPI specification.

Debug

Once the library has been initialized, various debug statements are logged as requests are sent and responses received.

Enabling debug mode:

const client = Firstclasspostcodes('....');

client.debugging = true;

Integration / Testing

We provide a mock service of our API as a docker container available here. Once the container is running, the library can be easily configured to use it:

const Firstclasspostcodes = require('@firstclasspostcodes/js')

const MOCK_API_URL = 'http://localhost:3000';

// The mock API key is always 111111111111 ("12x1")
const MOCK_API_KEY = '111111111111'

const client = Firstclasspostcodes(MOCK_API_KEY, {
  endpoint: MOCK_API_URL,
});

Development

Run all linting and tests:

$ npm ci
$ npm run lint
$ npm test

We use Cypress to ensure that our library is working correctly inside the browser, this can be executed locally using:

$ npm run cypress