Firstclasspostcodes API Wrapper
npm install @firstclasspostcodes/js[//]: # "NOTE: The URL for the JS library is automatically updated by semantic-release."
The Firstclasspostcodes Javascript library is compatible with both Node.JS and Browsers.
The library will work in all modern browsers and IE9+.
An identical API is provided for both the client and server side.
```
npm install @firstclasspostcodes/js
or directly in HTML with:
`html`
Note on older browsers: We recommend using a polyfill service, the following example covers all of the required language features:
`html`
Every version of the library is accompanied by an SRI hash file, the hash can be accessed directly using:
`sh`
$ curl https://js.firstclasspostcodes.com/v1.6.2.sri.txt # => "sha256-45tfd... sha384-43567ytr..."
You can then update the above
`
`js
const client = require('@firstclasspostcodes/js')('fg3rfgy3345tgfAt3r');
const postcodeData = await client.getPostcode('sw13 8gh');
`
Using ES modules looks a little different:
`js
import Firstclasspostcodes from '@firstclasspostcodes/js';
const client = Firstclasspostcodes('fg3rfgy3345tgfAt3r');
// ...
`
`js`
const client = Firstclasspostcodes('fg3r...', {
endpoint: 'https://api.firstclasspostcodes.com/data',
content: 'json',
});
| Property | Default | Description |
|:-----|:-----|:-----|
| endpoint | https://api.firstclasspostcodes.com/data | The endpoint to be used. This can be overridden to use a private endpoint, or for testing purposes. |content
| | 'json' | The content key controls the type of response being received. geo+json can be used to receives responses in GeoJSON.
The library does not currently support wildcards or regular expressions.
`js
const client = Firstclasspostcodes('....');
const handler = (requestObj) => {
console.log(requestObj);
};
client.on('request', handler);
client.once('response', (responseObj) => {
sendSignal(responseObj);
});
client.off('request', handler);
`
| Event name | Description |
|:-----|:-----|
| request | Triggered before a request is sent. The request object to be sent is passed to the event handler. |response
| | Triggered with the parsed JSON response body upon a successful reques. |error
| | Triggered with a client error when the request fails. |operation:{name}
| | Triggered by an operation with the parameter object. |
Note: {name} is replaced with the operation name of the method, as defined inside the OpenAPI specification.
Enabling debug mode:
`js
const client = Firstclasspostcodes('....');
client.debugging = true;
`
`js
const Firstclasspostcodes = require('@firstclasspostcodes/js')
const MOCK_API_URL = 'http://localhost:3000';
// The mock API key is always 111111111111 ("12x1")
const MOCK_API_KEY = '111111111111'
const client = Firstclasspostcodes(MOCK_API_KEY, {
endpoint: MOCK_API_URL,
});
`
``
$ npm ci
$ npm run lint
$ npm test
We use Cypress to ensure that our library is working correctly inside the browser, this can be executed locally using:
```
$ npm run cypress