@cuvva/postcodesio-client

中文文档
0.3.6发布于 2021/07/08MITCommonJS近 30 天下载 2455

Abstracts access to the Postcodes.io API

原始使用文档
文档目录17 节
  1. Installation
  2. Usage
  3. Promises
  4. Methods
  5. Lookup
  6. Near Coordinate
  7. Near Postcode
  8. Reverse Geocode
  9. Validate
  10. Random
  11. Types
  12. Postcode
  13. Outcode
  14. Testing
  15. Support
  16. Authors
  17. License

postcodes.io client

Abstracts access to the postcodes.io API.

NPM Version Build Status Coverage Status

var PostcodesIO = require('postcodesio-client');
var postcodes = new PostcodesIO();

postcodes.lookup('EC1V 9LB').then(postcode => {
    console.log(postcode);

    // {
    //   "postcode": "EC1V 9LB",
    //   "admin_district": "Islington",
    //   "longitude": -0.091247681768113,
    //   "latitude": 51.5278436902703,
    //   "region": "London",
    //   ...
    // }
});

Installation

$ npm install postcodesio-client

Usage

Create an instance of the client, providing options if you wish:

var PostcodesIO = require('postcodesio-client');

var postcodes = new PostcodesIO('https://api.postcodes.io', {
    headers: { 'User-Agent': 'MyAwesomeApp/1.0.0' } // default {} - extra headers
});

Make requests using Promises:

postcodes
    .lookup('EC1V 9LB')
    .then(function (postcode) {
        console.log(postcode);
    }, function (error) {
        console.log('oh no, it failed ;_;');
    });

Not found (404) responses from the API are not considered errors. Instead, the output will be null.

postcodes.lookup('F4K3AA').then(postcode => {
    console.log(postcode);
    // null
});

Only Promises (Promises/A+) are supported - callbacks are not. The following two sections specify exactly how you can apply Promises to the reference below.

Promises

postcodes
    .method(parameters...)
    .then(function (outputs...) {

    }, function (error) {

    });
  • method - the method you want to call
  • parameters - any arguments specific to that method (see Methods)
  • error - instance of Error
  • outputs - any outputs for that method (see Methods)

Both parameters and outputs can mean zero, one, or many arguments. These are specified for each method.

Methods

Lookup

Get lots of data for a postcode or outcode.

postcodes.lookup(code)

Parameters:

  • code - string - the postcode or outcode to retrieve

Outputs:

This may also be called explicitly as postcodes.lookupPostcode(postcode) or postcodes.lookupOutcode(outcode).

Near Coordinate

Find postcodes closest to a coordinate.

postcodes.near(latitude, longitude)

This may also be called explicitly as postcodes.nearCoordinate(latitude, longitude, options).

Parameters:

  • latitude - number - the latitude of the coordinate
  • longitude - number - the longitude of the coordinate
  • options - object - optional, only available with an explicit call

Outputs:

  • postcodes - array of Postcode - the nearby postcodes

Near Postcode

Find the postcodes closest to another postcode.

Warning: results may include the original postcode.

postcodes.near(postcode)

This may also be called explicitly as postcodes.nearPostcode(postcode).

Parameters:

  • postcode - string - the postcode to search around

Outputs:

  • postcodes - array of Postcode - the nearby postcodes

Reverse Geocode

Find the single closest postcode to a coordinate.

postcodes.reverseGeocode(latitude, longitude)

Parameters:

  • latitude - number - the latitude of the coordinate
  • longitude - number - the longitude of the coordinate

Outputs:

  • postcode - Postcode - the nearby postcode

Validate

Validates that the postcode exists. (Means it is in the official Royal Mail Postcode Address File).

postcodes.validate(postcode)

Parameters:

  • postcode - string - the postcode to retrieve

Outputs:

  • exists - boolean - indicates existence of postcode

Random

Retrieve a random postcode. Not really sure why you'd want to do this, but here it is...

postcodes.random()

Outputs:

  • postcode - Postcode - a random postcode

Types

Postcode

Example:

{
    "postcode": "EC1V 9LB",
    "quality": 1,
    "eastings": 532506,
    "northings": 182719,
    "country": "England",
    "nhs_ha": "London",
    "admin_county": null,
    "admin_district": "Islington",
    "admin_ward": "Bunhill",
    "longitude": -0.091247681768113,
    "latitude": 51.5278436902703,
    "parliamentary_constituency": "Islington South and Finsbury",
    "european_electoral_region": "London",
    "primary_care_trust": "Islington",
    "region": "London",
    "parish": null,
    "lsoa": "Islington 023A",
    "msoa": "Islington 023",
    "nuts": null,
    "incode": "9LB",
    "outcode": "EC1V",
    "ccg": "NHS Islington"
}

Outcode

Example:

{
    "outcode": "EC1V",
    "longitude": -0.0981811622126924,
    "latitude": 51.5266761246198,
    "northings": 182576,
    "eastings": 532028,
    "admin_district": [
        "Hackney",
        "Islington"
    ],
    "parish": [
        "Hackney, unparished area",
        "Islington, unparished area"
    ],
    "admin_county": [],
    "admin_ward": [
        "Bunhill",
        "Hoxton West",
        "Clerkenwell",
        "Hoxton East & Shoreditch",
        "St Peter's"
    ],
    "country": [
        "England"
    ]
}

Testing

Install the development dependencies first:

$ npm install

Then run the tests:

$ npm test

Support

Please open an issue on this repository.

Authors

License

MIT licensed - see LICENSE file

版本与发布记录

@cuvva/postcodesio-client 共发布 2 个版本,首次发布于 2021/07/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.6latest2021/07/085 年前
0.3.52021/07/065 年前

关于 @cuvva/postcodesio-client 的常见问题

@cuvva/postcodesio-client 是什么?

Abstracts access to the Postcodes.io API

如何安装 @cuvva/postcodesio-client?

在项目目录运行 npm install @cuvva/postcodesio-client,或使用 pnpm add / yarn add / bun add。

@cuvva/postcodesio-client 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cuvva__postcodesio-client 是否存在。

@cuvva/postcodesio-client 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cuvva/postcodesio-client 可以商用吗?

@cuvva/postcodesio-client 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cuvva/postcodesio-client 的下载量有多少?

根据 npm 官方下载统计,@cuvva/postcodesio-client 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,455 次,日均约 82 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cuvva/postcodesio-client 还在维护吗?

最新版本 0.3.6 发布于 2021/07/08(5 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/07/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。