PostcodeClient
Calls NIPOST’s postcode gateway. A client retries, waits, shares identical calls and limits
its parallel requests as spec/client.md says. It sends only the requests that its caller
makes, and it never logs.
Example
Section titled “Example”import { PostcodeClient } from '@gatepost/client';
const client = new PostcodeClient({ apiKey: process.env['NIPOST_API_KEY'] });const result = await client.lookup('fc 01 z99 zz 01');result.postcode.canonical; // 'FC-01-Z99-ZZ-01'Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new PostcodeClient(
options?):PostcodeClient
Makes a client. Two clients share nothing, so each can have its own key and settings.
Parameters
Section titled “Parameters”options?
Section titled “options?”ClientOptions = {}
The key, the gateway’s address, the transport, the timeout, the retries and the cache time. Each one is optional.
Returns
Section titled “Returns”PostcodeClient
Throws
Section titled “Throws”RangeError for a timeoutMs of 0 or less, a maxRetries that is not a whole
number of 0 or more, a cacheTtlMs that is negative or not finite, or an empty apiKey.
Throws
Section titled “Throws”TypeError for a secret key in a web page.
Methods
Section titled “Methods”autocomplete()
Section titled “autocomplete()”autocomplete(
q,options?):Promise<AutocompleteResult>
Asks the gateway for the values of the segment that a user is typing. Empty text sends no request, because the gateway does not answer it. A timeout gets no retry, because the next keystroke replaces the call.
Parameters
Section titled “Parameters”string
The text that the user typed, in any spacing and letter case.
options?
Section titled “options?”Readonly<{ signal?: AbortSignal; }> = {}
A signal that cancels the call.
Returns
Section titled “Returns”Promise<AutocompleteResult>
The active segment and the gateway’s values for it.
Throws
Section titled “Throws”PostcodeError invalid_input for text that is empty, too long or not letters and
digits, and the code that the status map gives for a failed response.
Example
Section titled “Example”import { PostcodeClient } from '@gatepost/client';
const client = new PostcodeClient({ apiKey: process.env['NIPOST_API_KEY'] });const result = await client.autocomplete('fc 01 z');result.segment; // 'district'clearCache()
Section titled “clearCache()”clearCache():
void
Removes every kept result, so that the next call of each kind sends a request.
Returns
Section titled “Returns”void
Example
Section titled “Example”import { PostcodeClient } from '@gatepost/client';
const apiKey = process.env['NIPOST_API_KEY'];const client = new PostcodeClient({ apiKey, cacheTtlMs: 60_000 });await client.lookup('FC-01-Z99-ZZ-01');client.clearCache();lookup()
Section titled “lookup()”lookup(
code,options?):Promise<LookupResult>
Asks the gateway about one postcode. The core parses the code first, and a code that does
not parse as a whole postcode sends no request. A postcode that the gateway does not know
gives a result with valid false, not an error.
Parameters
Section titled “Parameters”string | Postcode
The postcode as text in any form, or as a postcode that parse returned.
options?
Section titled “options?”Readonly<{ level?: LookupLevel; signal?: AbortSignal; }> = {}
The lookup level, which is 1 by default, and a signal that cancels the
call.
Returns
Section titled “Returns”Promise<LookupResult>
The facts about the postcode, up to the level that the key holds.
Throws
Section titled “Throws”PostcodeError invalid_input for a code that does not parse, and the code that the
status map gives for a failed response.
Example
Section titled “Example”import { PostcodeClient } from '@gatepost/client';
const client = new PostcodeClient({ apiKey: process.env['NIPOST_API_KEY'] });const result = await client.lookup('FC-01-Z99-ZZ-01', { level: 2 });result.levelRequested; // 2if (result.levelReceived >= 2) { result.administrativeAddress?.stateName;}reverse()
Section titled “reverse()”reverse(
lat,lng,options?):Promise<ReverseResult>
Asks the gateway for the unit nearest to a coordinate. A point with no postcode in range
gives a result with found false, not an error.
Parameters
Section titled “Parameters”number
The latitude in degrees.
number
The longitude in degrees.
options?
Section titled “options?”Readonly<{ maxDistanceM?: number; signal?: AbortSignal; }> = {}
The radius maxDistanceM in metres, which the gateway sets to 25 when it
is missing, and a signal that cancels the call.
Returns
Section titled “Returns”Promise<ReverseResult>
What the gateway found.
Throws
Section titled “Throws”PostcodeError with the code that the status map gives for a failed response.
Example
Section titled “Example”import { PostcodeClient } from '@gatepost/client';
const client = new PostcodeClient({ apiKey: process.env['NIPOST_API_KEY'] });const result = await client.reverse(9, 7, { maxDistanceM: 250 });if (result.unit !== null) { result.unit.postcode.canonical;}Unofficial. Not made or endorsed by NIPOST.