Skip to content

Commit 9258ecf

Browse files
authored
feat(Core Interface): Update to Version 2
Version 2 of core-interface includes breaking changes to improve load and running time. This is achieved by reorganising the library to be better tree-shakeable. Helper methods (like lookupPostcode) and resources (like addresses, udprn) are not included with the client. Now they are exported by the library and require the client to be injected BREAKING CHANGE: - Package now exports a `defaults` object - Client.defaults has been removed - All client config is now stored in `client.config` - All resources have been removed from the client. Instead retrieve these from the library and inject the client. E.g. `client.postcodes.retrieve` becomes `postcodes.retrieve(client, ...)` - Helper methods (like lookupPostcode, ping) have been removed from the client. Instead retrieve these from teh library and inject the client. E.g. `client.lookupPostcode` becomes `lookupPostcode(client, ...)`
1 parent e480db8 commit 9258ecf

File tree

8 files changed

+61
-100
lines changed

8 files changed

+61
-100
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,20 @@ const client = new Client({ api_key: "iddqd" });
6868
#### Use
6969

7070
```javascript
71-
const addresses = await client.lookupPostcode({ postcode: "SW1A2AA" });
71+
import { lookupPostcode } from "@ideal-postcodes/core-node";
72+
73+
const addresses = await lookupPostcode({ client, postcode: "SW1A2AA" });
7274
```
7375

7476
#### Catch Errors
7577

7678
```javascript
77-
const { IdpcRequestFailedError } = Client.errors;
79+
import { lookupAddress, errors } from "@ideal-postcodes/core-node";
7880

7981
try {
80-
await client.lookupAddress({ query: "10 downing street" });
82+
await lookupAddress({ client, query: "10 downing street" });
8183
} catch (error) {
82-
if (error instanceof IdpcRequestFailedError) {
84+
if (error instanceof errors.IdpcRequestFailedError) {
8385
// IdpcRequestFailedError indicates a 402 response code
8486
// Possibly the key balance has been depleted
8587
}
@@ -138,9 +140,10 @@ For a complete list of client methods, including low level resource methods, ple
138140
Return addresses associated with a given `postcode`
139141

140142
```javascript
143+
import { lookupPostcode } from "@ideal-postcodes/core-node";
141144
const postcode = "id11qd";
142145

143-
const addresses = await client.lookupPostcode({ postcode });
146+
const addresses = await lookupPostcode({ client, postcode });
144147
```
145148

146149
[Method options](https://core-interface.ideal-postcodes.dev/interfaces/lookuppostcodeoptions.html)
@@ -150,9 +153,10 @@ const addresses = await client.lookupPostcode({ postcode });
150153
Return addresses associated with a given `query`
151154

152155
```javascript
156+
import { lookupAddress } from "@ideal-postcodes/core-node";
153157
const query = "10 downing street sw1a";
154158

155-
const addresses = await client.lookupAddress({ query });
159+
const addresses = await lookupAddress({ client, query });
156160
```
157161

158162
[Method options](https://core-interface.ideal-postcodes.dev/interfaces/lookupaddressoptions.html)
@@ -164,9 +168,9 @@ Return address for a given `udprn`
164168
Invalid UDPRN will return `null`
165169

166170
```javascript
171+
import { lookupUdprn } from "@ideal-postcodes/core-node";
167172
const udprn = 23747771;
168-
169-
const address = await client.lookupUdprn({ udprn });
173+
const address = await lookupUdprn({ client, udprn });
170174
```
171175

172176
[Method options](https://core-interface.ideal-postcodes.dev/interfaces/lookupudprnoptions.html)

lib/client.ts

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import {
22
Client as CoreInterface,
3-
TLS,
4-
API_URL,
5-
VERSION,
6-
TIMEOUT,
7-
STRICT_AUTHORISATION,
3+
Config
84
} from "@ideal-postcodes/core-interface";
95
import { Agent, GotConfig } from "./agent";
106

@@ -13,36 +9,7 @@ import { Agent, GotConfig } from "./agent";
139
*/
1410
const USER_AGENT = `IdealPostcodes ideal-postcodes/core-node`;
1511

16-
export interface Config {
17-
/**
18-
* Use TLS. Defaults to `true`
19-
*/
20-
tls?: boolean;
21-
/**
22-
* API Key. Used in API helper methods
23-
*/
24-
api_key: string;
25-
/**
26-
* Target API hostname. Defaults to `'api.ideal-postcodes.co.uk'`
27-
*/
28-
baseUrl?: string;
29-
/**
30-
* API version. Defaults to `'v1'`
31-
*/
32-
version?: string;
33-
/**
34-
* Force autocomplete authorisation via HTTP headers only. Defaults to `false`
35-
*/
36-
strictAuthorisation?: boolean;
37-
/**
38-
* Default time in ms before HTTP request timeout. Defaults to 10s (`10000`)
39-
*/
40-
timeout?: number;
41-
/**
42-
* String map specifying default headers
43-
*/
44-
header?: Record<string, string>;
45-
}
12+
export { Config };
4613

4714
export class Client extends CoreInterface {
4815
/**
@@ -57,25 +24,6 @@ export class Client extends CoreInterface {
5724
constructor(config: Config, gotConfig: GotConfig = {}) {
5825
const agent = new Agent(gotConfig);
5926
const header = { "User-Agent": USER_AGENT };
60-
const tls = config.tls === undefined ? TLS : config.tls;
61-
const baseUrl = config.baseUrl === undefined ? API_URL : config.baseUrl;
62-
const version = config.version === undefined ? VERSION : config.version;
63-
const strictAuthorisation =
64-
config.strictAuthorisation === undefined
65-
? STRICT_AUTHORISATION
66-
: config.strictAuthorisation;
67-
const timeout = config.timeout === undefined ? TIMEOUT : config.timeout;
68-
69-
const { api_key } = config;
70-
const interfaceConfig = {
71-
tls,
72-
api_key,
73-
baseUrl,
74-
version,
75-
strictAuthorisation,
76-
timeout,
77-
header: { ...header, ...config.header },
78-
};
79-
super({ agent, ...interfaceConfig });
27+
super({ agent, ...{ ...config, ...{ header }} });
8028
}
8129
}

lib/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
import { Client } from "./client";
2-
import { Agent } from "./agent";
1+
/**
2+
* @module Library Exports
3+
*/
34

4-
export { Client, Agent };
5+
/**
6+
* Export core-interface helpers and resources
7+
*
8+
*/
9+
export * from "@ideal-postcodes/core-interface";
10+
11+
/**
12+
* Export HTTP Client with HTTP agent that supports got HTTP Client
13+
*/
14+
export { Client } from "./client";
15+
16+
export { Agent } from "./agent";

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
},
8888
"license": "MIT",
8989
"dependencies": {
90-
"@ideal-postcodes/core-interface": "~1.8.0",
90+
"@ideal-postcodes/core-interface": "~2.0.1",
9191
"got": "~11.8.0"
9292
},
9393
"devDependencies": {

test/agent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as sinon from "sinon";
22
import { assert } from "chai";
33
import { Agent, toHeader } from "../lib/agent";
44
import { Response } from "got";
5-
import { Client } from "../lib/client";
5+
import { errors } from "../lib";
66

7-
const { IdealPostcodesError } = Client.errors;
7+
const { IdealPostcodesError } = errors;
88

99
describe("Agent", () => {
1010
let agent: Agent;

test/client.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ import { assert } from "chai";
22
import { Client, Config } from "../lib/client";
33
import { Agent } from "../lib/agent";
44
import {
5-
TLS,
6-
API_URL,
7-
VERSION,
8-
TIMEOUT,
9-
STRICT_AUTHORISATION,
5+
defaults,
106
Config as InterfaceConfig,
11-
} from "@ideal-postcodes/core-interface";
7+
} from "../lib";
128

139
describe("Client", () => {
1410
describe("instantiation", () => {
@@ -20,12 +16,12 @@ describe("Client", () => {
2016
});
2117

2218
it("assigns default config values", () => {
23-
assert.equal(client.api_key, api_key);
24-
assert.equal(client.tls, TLS);
25-
assert.equal(client.baseUrl, API_URL);
26-
assert.equal(client.version, VERSION);
27-
assert.equal(client.strictAuthorisation, STRICT_AUTHORISATION);
28-
assert.equal(client.timeout, TIMEOUT);
19+
assert.equal(client.config.api_key, api_key);
20+
assert.equal(client.config.tls, defaults.tls);
21+
assert.equal(client.config.baseUrl, defaults.baseUrl);
22+
assert.equal(client.config.version, defaults.version);
23+
assert.equal(client.config.strictAuthorisation, defaults.strictAuthorisation);
24+
assert.equal(client.config.timeout, defaults.timeout);
2925
});
3026

3127
it("allows default config values to be overwritten", () => {
@@ -38,26 +34,26 @@ describe("Client", () => {
3834
timeout: 2,
3935
};
4036
const customClient = new Client(options);
41-
assert.equal(customClient.api_key, options.api_key);
42-
assert.equal(customClient.tls, options.tls);
43-
assert.equal(customClient.baseUrl, options.baseUrl);
44-
assert.equal(customClient.version, options.version);
37+
assert.equal(customClient.config.api_key, options.api_key);
38+
assert.equal(customClient.config.tls, options.tls);
39+
assert.equal(customClient.config.baseUrl, options.baseUrl);
40+
assert.equal(customClient.config.version, options.version);
4541
assert.equal(
46-
customClient.strictAuthorisation,
42+
customClient.config.strictAuthorisation,
4743
options.strictAuthorisation
4844
);
49-
assert.equal(customClient.timeout, options.timeout);
50-
assert.deepEqual((customClient.agent as any).gotConfig, {});
45+
assert.equal(customClient.config.timeout, options.timeout);
46+
assert.deepEqual((customClient.config.agent as any).gotConfig, {});
5147
});
5248

5349
it("assigns user agent header", () => {
54-
assert.match(client.header["User-Agent"], /Core-Node/i);
50+
assert.match(client.config.header["User-Agent"], /Core-Node/i);
5551
});
5652

5753
it("assigns got config", () => {
5854
const retry = 2;
5955
const customClient = new Client({ api_key }, { retry });
60-
assert.deepEqual((customClient.agent as any).gotConfig, { retry });
56+
assert.deepEqual((customClient.config.agent as any).gotConfig, { retry });
6157
});
6258
});
6359

test/integration.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assert } from "chai";
22
import { Client } from "../lib/client";
3+
import { errors, ping, lookupPostcode } from "../lib";
34
import { loadHttpFixtures } from "./fixtures/index";
45

56
loadHttpFixtures(__filename);
@@ -8,12 +9,12 @@ const SUCCESS = 200;
89

910
const api_key = process.env.VALID_API_KEY || "iddqd";
1011
const client = new Client({ api_key });
11-
const { IdpcInvalidKeyError, IdpcRequestFailedError } = Client.errors;
12+
const { IdpcInvalidKeyError, IdpcRequestFailedError } = errors;
1213

1314
describe("Client integration test", () => {
1415
describe("ping", () => {
1516
it("pings /", async () => {
16-
const response = await client.ping();
17+
const response = await ping(client);
1718
assert.equal(response.httpStatus, SUCCESS);
1819
});
1920
});
@@ -25,21 +26,21 @@ describe("Client integration test", () => {
2526
describe("lookupPostcode", () => {
2627
it("retrieves postcode", async () => {
2728
const postcode = "SW1A 2AA";
28-
const addresses = await client.lookupPostcode({ postcode });
29+
const addresses = await lookupPostcode({ client, postcode });
2930
assert.isTrue(addresses.length > 0);
3031
assert.equal(addresses[0].postcode, postcode);
3132
});
3233

3334
it("returns empty array for invalid postcode", async () => {
3435
const postcode = "Definitely bogus";
35-
const addresses = await client.lookupPostcode({ postcode });
36+
const addresses = await lookupPostcode({ client, postcode });
3637
assert.deepEqual(addresses, []);
3738
});
3839

3940
it("returns an error for limit breached", async () => {
4041
const postcode = "ID1 CLIP";
4142
try {
42-
await client.lookupPostcode({ postcode });
43+
await lookupPostcode({ client, postcode });
4344
} catch (error) {
4445
assert.isTrue(error instanceof IdpcRequestFailedError);
4546
return;
@@ -50,7 +51,7 @@ describe("Client integration test", () => {
5051
it("returns an error for balance depleted", async () => {
5152
const postcode = "ID1 CHOP";
5253
try {
53-
await client.lookupPostcode({ postcode });
54+
await lookupPostcode({ client, postcode });
5455
} catch (error) {
5556
assert.isTrue(error instanceof IdpcRequestFailedError);
5657
return;
@@ -61,7 +62,7 @@ describe("Client integration test", () => {
6162
it("returns an error for invalid key", async () => {
6263
const postcode = "SW1A 2AA";
6364
try {
64-
await client.lookupPostcode({ postcode, api_key: "badKey" });
65+
await lookupPostcode({ client, postcode, api_key: "badKey" });
6566
} catch (error) {
6667
assert.isTrue(error instanceof IdpcInvalidKeyError);
6768
return;

0 commit comments

Comments
 (0)