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

postcodex-lib-em

v1.0.1

Published

JS (TS) Lib to connect to postcodex api

Downloads

65

Readme

postcodex-lib-ts

postcodex-lib-ts is a JavaScript/TypeScript library that connects to the Postcodex API to fetch postcode-related information.

Table of Contents

Installation

To use this library, install it via npm:

npm install postcodex-lib-ts

Usage

Initialization

To use the ApiService, you need to instantiate it using the singleton pattern with your API configuration (e.g., API token).

import ApiService from 'postcodex-lib-ts';

const apiService = ApiService.getInstance({
token: 'your_api_token'
});

Fetch Data (Promise)

You can fetch postcode data using a promise-based approach with the fetchData method. This function takes a postcode as input and returns a promise that resolves with the postcode data.

apiService.fetchData('EC1A 1BB')
.then(data => {
console.log('Postcode data:', data);
})
.catch(error => {
console.error('Error fetching postcode data:', error);
});

Fetch Data (Callback)

If you prefer to use callbacks, the fetchDataWithCallback method allows you to fetch data for a given API endpoint and handle the response using a callback function.

apiService.fetchDataWithCallback('EC1A-1BB', (data) => {
  if (data) {
    console.log('Postcode data (callback):', data);
  } else {
    console.error('Error fetching postcode data with callback');
  }
});

API Reference

ApiServiceConfig

|Property|Type|Description| |---|---|---| |token|string|Your Postcodex API token.|

ApiServiceResponse

An example response object returned by the API looks like this:

{
    "postcode": "EC1A 1BB",
    "postTown": "London",
    "dependentLocality": "",
    "doubleDependentLocality": "",
    "thoroughfare": "St. Martin's Le Grand",
    "dependentThoroughfare": "",
    "buildingNumber": "1",
    "buildingName": "",
    "subBuildingName": "",
    "poBox": "",
    "departmentName": "",
    "OrganisationName": "",
    "udprn": "123456789",
    "postcodeType": "S",
    "suOrganisationIdentifier": "",
    "deliveryPointSuffix": "1A",
    "addressLine1": "1 St. Martin's Le Grand",
    "addressLine2": "",
    "addressLine3": "",
    "addressLine4": "",
    "addressDescription": ""
}

Callback

The callback function signature used in fetchDataWithCallback is:

type Callback = (response: ApiServiceResponse | null) => void;
  • response: The postcode data fetched from the API. Will be null in case of an error.

Development

To work on the project locally:

  1. Clone the repository.

  2. Install dependencies:

    npm install
  3. Build the project:

    npm run build

For development builds, you can use:

npm run build:dev

To build for production:

npm run build:prod

Local tests

  • Please check the demo folder with an integration example

  • You can test the demo by opening the index.html file or use docker

    cd demo;
    docker run -p 80:80 -v $(pwd)/..:/usr/share/nginx/html nginx
  • then open http://localhost/demo

Environment Variables

Make sure to set the LOOKUP_URL environment variable in a .env file or directly in your environment. This variable is used as the base URL for API requests.

Example .env file:

LOOKUP_URL=https://api.postcodex.com

You will also need to provide your API token when instantiating the service in your application.

License

This project is licensed under the ISC License.

Integration Guide

Here’s a step-by-step guide on how to integrate postcodex-lib-ts into your project.

1. Install the Package

Install the library using npm:

npm install postcodex-lib-ts

2. Setup Environment Variables

Create a .env file at the root of your project if you haven't already, and add the LOOKUP_URL for the API:

LOOKUP_URL=https://api.postcodex.com

3. Using the Library in Your Code

Once installed, import and configure the ApiService in your project:

import ApiService from 'postcodex-lib-ts';

// Initialize the API service with your token
const apiService = ApiService.getInstance({
    token: 'your_api_token'
});

// Fetch postcode data using the promise-based method
apiService.fetchData('SW1A 1AA')
    .then(data => {
        console.log('Postcode data:', data);
    })
    .catch(error => {
        console.error('Error:', error);
    });

Alternatively, if you prefer using a callback-based approach:

apiService.fetchDataWithCallback('SW1A-1AA', (data) => {
    if (data) {
        console.log('Postcode data (callback):', data);
    } else {
        console.error('Error fetching postcode data with callback');
    }
});

4. Build and Deploy

You can build your project using:

npm run build

For production-ready builds:

npm run build:prod

This generates the bundled files that are ready to be published or deployed in a production environment.

Publish npm package

This should be done with postcodex login

npm login

After the prod build need to update the publish folder.

  • rename publish.js file to index.js
  • update package.json
  • update README instructions
npm publish

Contribution guidelines

  • Writing tests
  • Code review
  • Other guidelines

Who do I talk to?

  • Repo owner or admin
  • Other community or team contact