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

@planning-inspectorate/address-lookup

v1.0.7

Published

Search Address Service by postcode based on OS places

Downloads

169

Readme

address-lookup

A service to retrieve UK addresses by postcode.

The service consumes the OS Apis (https://apidocs.os.uk/), specifically the DPA dataset (https://apidocs.os.uk/docs/os-places-dpa-output).

Installation

npm i @planning-inspectorate/address-lookup

And add the API key in .env of your project:

OS_PLACES_API_KEY = {insert key}

You can then use the service importing

import {findAddressListByPostcode} from "@planning-inspectorate/address-lookup";
    
const {errors, addressList} = await findAddressListByPostcode('EC2M7PD', {maxResults: 10});

Params

The function needs a postcode parameter with the postcode and an optional options parameter to refine the query.

This is the structure:

{
 postcode: string, 
 options: {
    maxResults: integer, 
 }
}

postcode

postcode should be a string.

It does not get validated inside the function: in case it's not valid the API will just return an empty list of addresses.

A previous validation of it could be useful to prevent useless requests.

  • ⚠️ The OS Api does not require specifically postcodes. The string could be a street or a geographical indication as well. However that's not the purpose of this function

options

options is optional and contains two parameters.

maxResults

Should be an integer.

Set the maximum number of results (could possibly be less if the API can't find enough) returned by the function.

Default is 100.

Return value:

Success

If the API key is defined and the postcode is valid, the function will return an object containing a property named addressList

const {addressList} = await findAddressListByPostcode('EC2M7PD');

addressList is an array of objects with this structure:

{
    apiReference: string, //unique reference
    addressLine1: string, // street number and street name
    addressLine2: string, //extra info like flat number or organisation name, most likely empty
    displayAddress: string, // string to be displayed in the dropdown: extra info, number and street, town, postcode
    postcode: string,
    town: string
}
  • ⚠️ findAddressListByPostcode is an async function. Will return a Promise if used without the await
  • ⚠️ addressList can be empty if the API can't find any match with the given postcode. Always check for errors

Fail

If the function does not return an addressList then an error has occurred. In that case it will return:

const {errors} = await findAddressListByPostcode('EC2M7PD');

errors is an object and it's meant to be integrated with the GOVUK design system mechanism to show error messages (hence its structure with the msg property).
This is its structure:

{
    apiKey?: {msg: string}
    postcode?: {msg: string}
}

apiKey

The apiKey property will be defined when the function cannot find an API key.

The content of the message will be:

An error occurred, please try again later

Cannot be more specific because this is meant to be shown to users. However an error log will explaing what's happening.

When the api key error message is returned, addressList is also returned as an empty array

postcode

The postcode property will be defined when the API cannot find any address or for any not-200 response of the OS API.

The content of the message will be:

Enter a valid postcode

When the postcode error message is returned, addressList is also returned as an empty array

Dev

To execute Jest tests for the function and its utilities, run

npm run test

To verify the actual response of the function run

npm run dev INSERT_API_KEY_HERE

It will return some address for a specific postcode hardcoded in src/dev.js. It can be changed without affecting anything else.