clearpostcode 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +189 -38
- package/dist/cjs/addresses.d.ts +28 -0
- package/dist/cjs/addresses.js +64 -0
- package/dist/cjs/browser.d.ts +39 -0
- package/dist/cjs/browser.js +54 -0
- package/dist/cjs/client.d.ts +146 -0
- package/dist/cjs/client.js +188 -0
- package/dist/cjs/errors.d.ts +73 -0
- package/dist/cjs/errors.js +111 -0
- package/dist/cjs/generated.d.ts +546 -0
- package/dist/cjs/generated.js +63 -0
- package/dist/cjs/http.d.ts +62 -0
- package/dist/cjs/http.js +173 -0
- package/dist/cjs/index.d.ts +24 -0
- package/dist/cjs/index.js +54 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/page.d.ts +20 -0
- package/dist/cjs/page.js +39 -0
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.js +24 -0
- package/dist/esm/addresses.d.ts +28 -0
- package/dist/esm/addresses.js +47 -0
- package/dist/esm/browser.d.ts +39 -0
- package/dist/esm/browser.js +33 -0
- package/dist/esm/client.d.ts +146 -0
- package/dist/esm/client.js +169 -0
- package/dist/esm/errors.d.ts +73 -0
- package/dist/esm/errors.js +92 -0
- package/dist/esm/generated.d.ts +546 -0
- package/dist/esm/generated.js +42 -0
- package/dist/esm/http.d.ts +62 -0
- package/dist/esm/http.js +161 -0
- package/dist/esm/index.d.ts +24 -0
- package/dist/esm/index.js +37 -0
- package/dist/esm/page.d.ts +20 -0
- package/dist/esm/page.js +17 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.js +2 -0
- package/package.json +43 -13
- package/index.d.ts +0 -121
- package/index.js +0 -88
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { BASE_URL } from "./generated.js";
|
|
2
|
+
import { Transport } from "./http.js";
|
|
3
|
+
import { pageOf, every } from "./page.js";
|
|
4
|
+
import { Addresses } from "./addresses.js";
|
|
5
|
+
import { ClearPostcodeError } from "./errors.js";
|
|
6
|
+
export const BULK_POSTCODES = 100;
|
|
7
|
+
export const BULK_POINTS = 20;
|
|
8
|
+
/** Did an item find anything: a bulk lookup's record, a bulk reverse point's postcodes. */
|
|
9
|
+
const found = (item) => (Array.isArray(item.result) ? item.result.length > 0 : item.result !== null);
|
|
10
|
+
/** Send `items` in chunks of `size`, one after another, and join the answers. A chunk that fails throws its error
|
|
11
|
+
* with `partial` holding the chunks already answered (and billed). */
|
|
12
|
+
async function chunked(items, size, send) {
|
|
13
|
+
const data = [];
|
|
14
|
+
const requestIds = [];
|
|
15
|
+
let meta = null;
|
|
16
|
+
let rateLimit = null;
|
|
17
|
+
for (let i = 0; i < items.length; i += size) {
|
|
18
|
+
let r;
|
|
19
|
+
try {
|
|
20
|
+
r = await send(items.slice(i, i + size));
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
if (e instanceof ClearPostcodeError)
|
|
24
|
+
e.partial = { data, meta, requestIds };
|
|
25
|
+
throw e;
|
|
26
|
+
}
|
|
27
|
+
data.push(...r.data);
|
|
28
|
+
requestIds.push(r.requestId);
|
|
29
|
+
rateLimit = r.rateLimit;
|
|
30
|
+
meta = { ...r.meta, sent: (meta?.sent ?? 0) + (r.meta.sent ?? r.data.length), found: (meta?.found ?? 0) + (r.meta.found ?? r.data.filter(found).length) };
|
|
31
|
+
}
|
|
32
|
+
return { data, meta, requestIds, rateLimit };
|
|
33
|
+
}
|
|
34
|
+
/** `fields` as the API takes it: comma-separated, or left out (an empty list is the full record). */
|
|
35
|
+
const fieldsOf = (fields) => (fields?.length ? fields.join(",") : undefined);
|
|
36
|
+
/** Postcodes: lookup, validation, nearest, reverse geocoding, bulk, autocomplete and distance. */
|
|
37
|
+
export class Postcodes {
|
|
38
|
+
#t;
|
|
39
|
+
constructor(transport) {
|
|
40
|
+
this.#t = transport;
|
|
41
|
+
}
|
|
42
|
+
lookup(postcode, params = {}, options = {}) {
|
|
43
|
+
return this.#t.request("getPostcode", { path: { postcode }, query: { fields: fieldsOf(params.fields) } }, options);
|
|
44
|
+
}
|
|
45
|
+
/** Is the format valid, is it in the current edition, is it live? Never throws for a wrong postcode. */
|
|
46
|
+
validate(postcode, options = {}) {
|
|
47
|
+
return this.#t.request("validatePostcode", { path: { postcode } }, options);
|
|
48
|
+
}
|
|
49
|
+
/** Live postcodes nearest this one, nearest first. */
|
|
50
|
+
nearest(postcode, params = {}, options = {}) {
|
|
51
|
+
return this.#t.request("nearestToPostcode", { path: { postcode }, query: { radius: params.radius, limit: params.limit } }, options);
|
|
52
|
+
}
|
|
53
|
+
/** Live postcodes nearest a point (reverse geocoding), out to 20 km. */
|
|
54
|
+
reverse(point, options = {}) {
|
|
55
|
+
return this.#t.request("reverseGeocode", { query: { lat: point.latitude, lon: point.longitude, radius: point.radius, limit: point.limit } }, options);
|
|
56
|
+
}
|
|
57
|
+
bulk(postcodes, params = {}, options = {}) {
|
|
58
|
+
return chunked(postcodes, BULK_POSTCODES, (chunk) => this.#t.request("bulkLookup", { query: { fields: fieldsOf(params.fields) }, body: { postcodes: chunk } }, options));
|
|
59
|
+
}
|
|
60
|
+
/** Nearest postcodes for any number of points, sent 20 a call. Needs a secret key. */
|
|
61
|
+
bulkReverse(points, options = {}) {
|
|
62
|
+
return chunked(points, BULK_POINTS, (chunk) => this.#t.request("bulkReverse", { body: { points: chunk.map((p) => ({ lat: p.latitude, lon: p.longitude, radius: p.radius, limit: p.limit })) } }, options));
|
|
63
|
+
}
|
|
64
|
+
/** Postcodes (or districts, for 1 to 4 characters) that start with what has been typed. Free. */
|
|
65
|
+
autocomplete(q, params = {}, options = {}) {
|
|
66
|
+
return this.#t.request("autocomplete", { query: { q, limit: params.limit } }, options);
|
|
67
|
+
}
|
|
68
|
+
/** Straight-line distance between two postcodes, in kilometres and miles, with the bearing. */
|
|
69
|
+
distance(from, to, options = {}) {
|
|
70
|
+
return this.#t.request("distance", { query: { from, to } }, options);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/** Postcode districts (outward codes such as SW1A). */
|
|
74
|
+
export class Outcodes {
|
|
75
|
+
#t;
|
|
76
|
+
constructor(transport) {
|
|
77
|
+
this.#t = transport;
|
|
78
|
+
}
|
|
79
|
+
/** One district: counts, sectors, centre, councils, wards, constituencies and places. */
|
|
80
|
+
get(outcode, options = {}) {
|
|
81
|
+
return this.#t.request("getOutcode", { path: { outcode } }, options);
|
|
82
|
+
}
|
|
83
|
+
/** One page of the district's live postcodes (1 lookup a page). Needs a secret key. */
|
|
84
|
+
postcodes(outcode, params = {}, options = {}) {
|
|
85
|
+
return pageOf(this.#t, "outcodePostcodes", { path: { outcode }, query: { limit: params.limit } }, "cursor", params.cursor ?? null, options);
|
|
86
|
+
}
|
|
87
|
+
/** Every live postcode in the district, page after page. Needs a secret key. */
|
|
88
|
+
allPostcodes(outcode, params = {}, options = {}) {
|
|
89
|
+
return every(() => this.postcodes(outcode, params, options));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** Postcode sectors (such as SW1A 1). */
|
|
93
|
+
export class Sectors {
|
|
94
|
+
#t;
|
|
95
|
+
constructor(transport) {
|
|
96
|
+
this.#t = transport;
|
|
97
|
+
}
|
|
98
|
+
/** One sector: its live postcodes with coordinates, centre, councils and wards. */
|
|
99
|
+
get(sector, options = {}) {
|
|
100
|
+
return this.#t.request("getSector", { path: { sector } }, options);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** Postcode areas (the letters, such as SW). */
|
|
104
|
+
export class Areas {
|
|
105
|
+
#t;
|
|
106
|
+
constructor(transport) {
|
|
107
|
+
this.#t = transport;
|
|
108
|
+
}
|
|
109
|
+
/** One area: its name, counts and every district in it. */
|
|
110
|
+
get(area, options = {}) {
|
|
111
|
+
return this.#t.request("getArea", { path: { area } }, options);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** Places by name (OS Open Names). */
|
|
115
|
+
export class Places {
|
|
116
|
+
#t;
|
|
117
|
+
constructor(transport) {
|
|
118
|
+
this.#t = transport;
|
|
119
|
+
}
|
|
120
|
+
/** Cities, towns, villages and neighbourhoods whose name starts with `q`; accents optional. */
|
|
121
|
+
search(q, params = {}, options = {}) {
|
|
122
|
+
return this.#t.request("places", { query: { q, limit: params.limit } }, options);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** Your account. */
|
|
126
|
+
export class Account {
|
|
127
|
+
#t;
|
|
128
|
+
constructor(transport) {
|
|
129
|
+
this.#t = transport;
|
|
130
|
+
}
|
|
131
|
+
/** Your plan, this period's allowance, lookups used, credits and when it resets. Free. Needs a secret key. */
|
|
132
|
+
usage(options = {}) {
|
|
133
|
+
return this.#t.request("usage", {}, options);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/** Defaults every client shares. */
|
|
137
|
+
export function transportConfig(options, key, extra) {
|
|
138
|
+
if (!options.fetch && typeof globalThis.fetch !== "function")
|
|
139
|
+
throw new TypeError("No fetch here: pass one as the fetch option.");
|
|
140
|
+
return {
|
|
141
|
+
key,
|
|
142
|
+
baseUrl: (options.baseUrl ?? BASE_URL).replace(/\/+$/, ""),
|
|
143
|
+
timeout: options.timeout ?? 10_000,
|
|
144
|
+
maxRetries: options.maxRetries ?? 2,
|
|
145
|
+
maxRetryDelay: options.maxRetryDelay ?? 60_000,
|
|
146
|
+
fetch: options.fetch ?? globalThis.fetch.bind(globalThis),
|
|
147
|
+
...extra,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/** The resources every client carries. */
|
|
151
|
+
export class Client {
|
|
152
|
+
postcodes;
|
|
153
|
+
outcodes;
|
|
154
|
+
sectors;
|
|
155
|
+
areas;
|
|
156
|
+
places;
|
|
157
|
+
account;
|
|
158
|
+
addresses;
|
|
159
|
+
constructor(config) {
|
|
160
|
+
const t = new Transport(config);
|
|
161
|
+
this.postcodes = new Postcodes(t);
|
|
162
|
+
this.outcodes = new Outcodes(t);
|
|
163
|
+
this.sectors = new Sectors(t);
|
|
164
|
+
this.areas = new Areas(t);
|
|
165
|
+
this.places = new Places(t);
|
|
166
|
+
this.account = new Account(t);
|
|
167
|
+
this.addresses = new Addresses(t);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { Meta, Problem, ProblemCode } from "./generated.js";
|
|
2
|
+
/** What a bulk call had answered (and billed) before the chunk that failed. */
|
|
3
|
+
export interface BulkPartial {
|
|
4
|
+
readonly data: unknown[];
|
|
5
|
+
readonly meta: Meta | null;
|
|
6
|
+
readonly requestIds: (string | null)[];
|
|
7
|
+
}
|
|
8
|
+
/** Every error the client throws. */
|
|
9
|
+
export declare class ClearPostcodeError extends Error {
|
|
10
|
+
name: string;
|
|
11
|
+
/** Set when a bulk call fails part-way: the chunks already answered, so nothing billed is lost. */
|
|
12
|
+
partial: BulkPartial | null;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The API answered with an error: an RFC 9457 problem. `code` is the problem type ("postcode_not_found"), `requestId`
|
|
16
|
+
* the id to quote to support, `retryAfter` the seconds the API asked you to wait (429 and 503), when it said.
|
|
17
|
+
*/
|
|
18
|
+
export declare class APIError extends ClearPostcodeError {
|
|
19
|
+
name: string;
|
|
20
|
+
readonly status: number;
|
|
21
|
+
/** The problem type. New codes may appear, so match the ones you handle and let the rest through. */
|
|
22
|
+
readonly code: ProblemCode | (string & {}) | null;
|
|
23
|
+
readonly title: string | null;
|
|
24
|
+
readonly detail: string | null;
|
|
25
|
+
readonly requestId: string | null;
|
|
26
|
+
readonly retryAfter: number | null;
|
|
27
|
+
/** The problem body as the API sent it (extra members such as `parameters`, `resets` or `upgrade` included). */
|
|
28
|
+
readonly problem: Partial<Problem>;
|
|
29
|
+
readonly headers: Headers;
|
|
30
|
+
constructor(status: number, problem: Partial<Problem> | null, headers: Headers, retryAfter: number | null);
|
|
31
|
+
}
|
|
32
|
+
/** 400 or 413: a parameter, the postcode format or the body (invalid_postcode, unknown_parameter, bad_request…). */
|
|
33
|
+
export declare class BadRequestError extends APIError {
|
|
34
|
+
name: string;
|
|
35
|
+
}
|
|
36
|
+
/** 401: the key is missing where one is needed, malformed or not active (invalid_key, key_required). */
|
|
37
|
+
export declare class AuthenticationError extends APIError {
|
|
38
|
+
name: string;
|
|
39
|
+
}
|
|
40
|
+
/** 402: the period's lookups, or the address credits, are used up (quota_exceeded, address_credits_exhausted). */
|
|
41
|
+
export declare class QuotaExceededError extends APIError {
|
|
42
|
+
name: string;
|
|
43
|
+
}
|
|
44
|
+
/** 403: the key may not make this call (origin_not_allowed, publishable_key_not_allowed, address_lookup_not_enabled…). */
|
|
45
|
+
export declare class PermissionDeniedError extends APIError {
|
|
46
|
+
name: string;
|
|
47
|
+
}
|
|
48
|
+
/** 404: no such postcode, district or address (postcode_not_found, not_covered, not_found). */
|
|
49
|
+
export declare class NotFoundError extends APIError {
|
|
50
|
+
name: string;
|
|
51
|
+
}
|
|
52
|
+
/** 422: a real postcode with no coordinates (no_position: the Channel Islands and the Isle of Man). */
|
|
53
|
+
export declare class UnprocessableEntityError extends APIError {
|
|
54
|
+
name: string;
|
|
55
|
+
}
|
|
56
|
+
/** 429: too many requests, a day's keyless allowance, or the key's daily cap (rate_limited, daily_cap_reached). */
|
|
57
|
+
export declare class RateLimitError extends APIError {
|
|
58
|
+
name: string;
|
|
59
|
+
}
|
|
60
|
+
/** 5xx: a moment's trouble on the API's side (server_error, service_unavailable). Retried for you. */
|
|
61
|
+
export declare class InternalServerError extends APIError {
|
|
62
|
+
name: string;
|
|
63
|
+
}
|
|
64
|
+
/** The API could not be reached (DNS, TLS, a dropped connection). Retried for you. */
|
|
65
|
+
export declare class APIConnectionError extends ClearPostcodeError {
|
|
66
|
+
name: string;
|
|
67
|
+
}
|
|
68
|
+
/** No answer within the timeout. Retried for you. */
|
|
69
|
+
export declare class APITimeoutError extends APIConnectionError {
|
|
70
|
+
name: string;
|
|
71
|
+
}
|
|
72
|
+
/** The error class for an HTTP status. */
|
|
73
|
+
export declare function errorFor(status: number, problem: Partial<Problem> | null, headers: Headers, retryAfter: number | null): APIError;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** Every error the client throws. */
|
|
2
|
+
export class ClearPostcodeError extends Error {
|
|
3
|
+
name = "ClearPostcodeError";
|
|
4
|
+
/** Set when a bulk call fails part-way: the chunks already answered, so nothing billed is lost. */
|
|
5
|
+
partial = null;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The API answered with an error: an RFC 9457 problem. `code` is the problem type ("postcode_not_found"), `requestId`
|
|
9
|
+
* the id to quote to support, `retryAfter` the seconds the API asked you to wait (429 and 503), when it said.
|
|
10
|
+
*/
|
|
11
|
+
export class APIError extends ClearPostcodeError {
|
|
12
|
+
name = "APIError";
|
|
13
|
+
status;
|
|
14
|
+
/** The problem type. New codes may appear, so match the ones you handle and let the rest through. */
|
|
15
|
+
code;
|
|
16
|
+
title;
|
|
17
|
+
detail;
|
|
18
|
+
requestId;
|
|
19
|
+
retryAfter;
|
|
20
|
+
/** The problem body as the API sent it (extra members such as `parameters`, `resets` or `upgrade` included). */
|
|
21
|
+
problem;
|
|
22
|
+
headers;
|
|
23
|
+
constructor(status, problem, headers, retryAfter) {
|
|
24
|
+
const p = problem ?? {};
|
|
25
|
+
const code = typeof p.type === "string" && p.type.includes("#") ? p.type.slice(p.type.lastIndexOf("#") + 1) : null;
|
|
26
|
+
const requestId = p.request_id ?? headers.get("x-request-id");
|
|
27
|
+
super(`${status}${code ? ` ${code}` : ""}: ${p.detail ?? p.title ?? "the API answered with an error"}${requestId ? ` (request ${requestId})` : ""}`);
|
|
28
|
+
this.status = status;
|
|
29
|
+
this.code = code;
|
|
30
|
+
this.title = p.title ?? null;
|
|
31
|
+
this.detail = p.detail ?? null;
|
|
32
|
+
this.requestId = requestId;
|
|
33
|
+
this.retryAfter = retryAfter;
|
|
34
|
+
this.problem = p;
|
|
35
|
+
this.headers = headers;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** 400 or 413: a parameter, the postcode format or the body (invalid_postcode, unknown_parameter, bad_request…). */
|
|
39
|
+
export class BadRequestError extends APIError {
|
|
40
|
+
name = "BadRequestError";
|
|
41
|
+
}
|
|
42
|
+
/** 401: the key is missing where one is needed, malformed or not active (invalid_key, key_required). */
|
|
43
|
+
export class AuthenticationError extends APIError {
|
|
44
|
+
name = "AuthenticationError";
|
|
45
|
+
}
|
|
46
|
+
/** 402: the period's lookups, or the address credits, are used up (quota_exceeded, address_credits_exhausted). */
|
|
47
|
+
export class QuotaExceededError extends APIError {
|
|
48
|
+
name = "QuotaExceededError";
|
|
49
|
+
}
|
|
50
|
+
/** 403: the key may not make this call (origin_not_allowed, publishable_key_not_allowed, address_lookup_not_enabled…). */
|
|
51
|
+
export class PermissionDeniedError extends APIError {
|
|
52
|
+
name = "PermissionDeniedError";
|
|
53
|
+
}
|
|
54
|
+
/** 404: no such postcode, district or address (postcode_not_found, not_covered, not_found). */
|
|
55
|
+
export class NotFoundError extends APIError {
|
|
56
|
+
name = "NotFoundError";
|
|
57
|
+
}
|
|
58
|
+
/** 422: a real postcode with no coordinates (no_position: the Channel Islands and the Isle of Man). */
|
|
59
|
+
export class UnprocessableEntityError extends APIError {
|
|
60
|
+
name = "UnprocessableEntityError";
|
|
61
|
+
}
|
|
62
|
+
/** 429: too many requests, a day's keyless allowance, or the key's daily cap (rate_limited, daily_cap_reached). */
|
|
63
|
+
export class RateLimitError extends APIError {
|
|
64
|
+
name = "RateLimitError";
|
|
65
|
+
}
|
|
66
|
+
/** 5xx: a moment's trouble on the API's side (server_error, service_unavailable). Retried for you. */
|
|
67
|
+
export class InternalServerError extends APIError {
|
|
68
|
+
name = "InternalServerError";
|
|
69
|
+
}
|
|
70
|
+
/** The API could not be reached (DNS, TLS, a dropped connection). Retried for you. */
|
|
71
|
+
export class APIConnectionError extends ClearPostcodeError {
|
|
72
|
+
name = "APIConnectionError";
|
|
73
|
+
}
|
|
74
|
+
/** No answer within the timeout. Retried for you. */
|
|
75
|
+
export class APITimeoutError extends APIConnectionError {
|
|
76
|
+
name = "APITimeoutError";
|
|
77
|
+
}
|
|
78
|
+
const BY_STATUS = {
|
|
79
|
+
400: BadRequestError,
|
|
80
|
+
401: AuthenticationError,
|
|
81
|
+
402: QuotaExceededError,
|
|
82
|
+
403: PermissionDeniedError,
|
|
83
|
+
404: NotFoundError,
|
|
84
|
+
413: BadRequestError,
|
|
85
|
+
422: UnprocessableEntityError,
|
|
86
|
+
429: RateLimitError,
|
|
87
|
+
};
|
|
88
|
+
/** The error class for an HTTP status. */
|
|
89
|
+
export function errorFor(status, problem, headers, retryAfter) {
|
|
90
|
+
const E = BY_STATUS[status] ?? (status >= 500 ? InternalServerError : APIError);
|
|
91
|
+
return new E(status, problem, headers, retryAfter);
|
|
92
|
+
}
|