Skip to content

Commit

Permalink
wip: v2
Browse files Browse the repository at this point in the history
  • Loading branch information
joostdebruijn committed Dec 29, 2023
1 parent 41c5c4f commit 99adfb9
Show file tree
Hide file tree
Showing 25 changed files with 3,952 additions and 2,034 deletions.
29 changes: 0 additions & 29 deletions .eslintrc

This file was deleted.

25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test

on:
push:
pull_request:

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18, 20, 21]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run lint
- run: npm run test
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

50 changes: 25 additions & 25 deletions lib/getAddresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,66 +37,66 @@
* });
*/

'use strict';
const helpers = require('./helpers.js');
const api = require('./requestApi.js');
'use strict'
const helpers = require('./helpers.js')
const api = require('./requestApi.js')

function getAddresses(options, query, callback) {
function getAddresses (options, query, callback) {
// As query is optional and there are only two arguments, the second argument must be the callback
if (arguments.length === 2) {
callback = arguments[1];
query = {};
callback = arguments[1]
query = {}
}

// Preparing options for request
options.url = 'https://postcode-api.apiwise.nl/v2/addresses';
options.qs = {};
options.url = 'https://postcode-api.apiwise.nl/v2/addresses'
options.qs = {}
options.headers = {
'X-Api-Key' : options.apiKey
};
'X-Api-Key': options.apiKey
}

// Optional postcode and number quering, only if postcode is given
if (query.postcode) {
// Check if it is in the right format
if (helpers.validatePostcodeP6(query.postcode)) {
options.qs.postcode = query.postcode;
options.qs.postcode = query.postcode
// Check if there is a number given in the right format
if (query.number) {
// Check if it is in the right format
if (typeof(query.number) == 'number') {
options.qs.number = query.number;
if (typeof (query.number) === 'number') {
options.qs.number = query.number
} else {
// If the numberis not in the right format, throw an error
return callback(new Error('The key number must be an integer'), null);
return callback(new Error('The key number must be an integer'), null)
}
}
} else {
// If the postcode is not in P6-format, throw an error
return callback(new Error('The key postcode must be in P6-formatted for this API-call'), null);
return callback(new Error('The key postcode must be in P6-formatted for this API-call'), null)
}
}

// Optional distance features
if (typeof(query.latitude) === 'number' && typeof(query.longitude) === 'number' && query.sort === 'distance') {
if (typeof (query.latitude) === 'number' && typeof (query.longitude) === 'number' && query.sort === 'distance') {
// If everything is allright, fill the query parameters
options.qs.coords = {};
options.qs.coords.latitude = query.latitude;
options.qs.coords.longitude = query.longitude;
options.qs.sort = 'distance';
options.qs.coords = {}
options.qs.coords.latitude = query.latitude
options.qs.coords.longitude = query.longitude
options.qs.sort = 'distance'
} else if (query.sort && query.sort !== 'distance') {
// When the query parameter is filled, but not with a valid value, throw an error.
return callback(new Error('Query parameter \'sort\' did not have a valid value'), null);
return callback(new Error('Query parameter \'sort\' did not have a valid value'), null)
} else if (query.latitude || query.longitude) {
// When one of the two is filled, or filled with an incorrect value, throw an error.
return callback(new Error('Both latitude and longitude must be provided as a number, also sort is required'), null);
return callback(new Error('Both latitude and longitude must be provided as a number, also sort is required'), null)
}

// Executing API-request
if (options.followNext) {
return helpers.followNext(options, callback);
return helpers.followNext(options, callback)
} else {
return api.get(options, callback);
return api.get(options, callback)
}
}

module.exports = getAddresses;
module.exports = getAddresses
26 changes: 13 additions & 13 deletions lib/getAddressesByPostcodeAndNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,30 @@
* }
* });
*/

'use strict';
const helpers = require('./helpers.js');
const api = require('./requestApi.js');

function getAddressesByPostcodeAndNumber(options, query, callback) {
'use strict'
const helpers = require('./helpers.js')
const api = require('./requestApi.js')

function getAddressesByPostcodeAndNumber (options, query, callback) {
// Check if postcode is correctly formatted for this API-call
if (!query.postcode || !helpers.validatePostcodeP6(query.postcode)) {
return callback(new Error('The key postcode must be in P6-formatted for this API-call'), null);
return callback(new Error('The key postcode must be in P6-formatted for this API-call'), null)
}

// Validate if number is in the query and if it is really a number (as required by external API)
if (!query.number || typeof(query.number) !== 'number') {
return callback(new Error('The key number is required in the query and must be an integer'), null);
if (!query.number || typeof (query.number) !== 'number') {
return callback(new Error('The key number is required in the query and must be an integer'), null)
}

// Preparing options for request
options.url = 'https://postcode-api.apiwise.nl/v2/addresses/?postcode=' + query.postcode + '&number=' + query.number;
options.url = 'https://postcode-api.apiwise.nl/v2/addresses/?postcode=' + query.postcode + '&number=' + query.number
options.headers = {
'X-Api-Key' : options.apiKey
};
'X-Api-Key': options.apiKey
}

// Executing API-request
return api.get(options, callback);
return api.get(options, callback)
}

module.exports = getAddressesByPostcodeAndNumber;
module.exports = getAddressesByPostcodeAndNumber
46 changes: 23 additions & 23 deletions lib/getPostcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,56 +34,56 @@
* });
*/

'use strict';
const helpers = require('./helpers.js');
const api = require('./requestApi.js');
'use strict'
const helpers = require('./helpers.js')
const api = require('./requestApi.js')

function getPostcodes(options, query, callback) {
function getPostcodes (options, query, callback) {
// As query is optional and there are only two arguments, the second argument must be the callback
if (arguments.length === 2) {
callback = arguments[1];
query = {};
callback = arguments[1]
query = {}
}

// Preparing options for request
options.url = 'https://postcode-api.apiwise.nl/v2/postcodes';
options.qs = {};
options.url = 'https://postcode-api.apiwise.nl/v2/postcodes'
options.qs = {}
options.headers = {
'X-Api-Key' : options.apiKey
};
'X-Api-Key': options.apiKey
}

// Check if postcodeArea has a value
if (query.postcodeArea) {
// Check if postcode is correctly formatted for this API-call
if (typeof(query.postcodeArea) == 'string' && helpers.validatePostcodeP4(query.postcodeArea)) {
options.qs.postcodeArea = query.postcodeArea;
if (typeof (query.postcodeArea) === 'string' && helpers.validatePostcodeP4(query.postcodeArea)) {
options.qs.postcodeArea = query.postcodeArea
} else {
// Throw an error if the postcodeArea is not in the right format
return callback(new Error('A postcode formatted in P4 is a required for this API'), null);
return callback(new Error('A postcode formatted in P4 is a required for this API'), null)
}
}

// Optional distance features
if (typeof(query.latitude) === 'number' && typeof(query.longitude) === 'number' && query.sort === 'distance') {
if (typeof (query.latitude) === 'number' && typeof (query.longitude) === 'number' && query.sort === 'distance') {
// If everything is allright, fill the query parameters
options.qs.coords = {};
options.qs.coords.latitude = query.latitude;
options.qs.coords.longitude = query.longitude;
options.qs.sort = 'distance';
options.qs.coords = {}
options.qs.coords.latitude = query.latitude
options.qs.coords.longitude = query.longitude
options.qs.sort = 'distance'
} else if (query.sort && query.sort !== 'distance') {
// When the query parameter is filled, but not with a valid value, throw an error.
return callback(new Error('Query parameter \'sort\' did not have a valid value'), null);
return callback(new Error('Query parameter \'sort\' did not have a valid value'), null)
} else if (query.latitude || query.longitude) {
// When one of the two is filled, or filled with an incorrect value, throw an error.
return callback(new Error('Both latitude and longitude must be provided as a number, also sort is required'), null);
return callback(new Error('Both latitude and longitude must be provided as a number, also sort is required'), null)
}

// Executing API-request
if (options.followNext) {
return helpers.followNext(options, callback);
return helpers.followNext(options, callback)
} else {
return api.get(options, callback);
return api.get(options, callback)
}
}

module.exports = getPostcodes;
module.exports = getPostcodes
20 changes: 10 additions & 10 deletions lib/getSingleAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@
* });
*/

'use strict';
const api = require('./requestApi.js');
'use strict'
const api = require('./requestApi.js')

function getSingleAddress(options, id, callback) {
function getSingleAddress (options, id, callback) {
// Check if postcode is correctly formatted for this API-call
if (!id || typeof(id) !== 'string') {
return callback(new Error('The BAG identifier is a required parameter and must be formatted as a string'), null);
if (!id || typeof (id) !== 'string') {
return callback(new Error('The BAG identifier is a required parameter and must be formatted as a string'), null)
}

// Preparing options for request
options.url = 'https://postcode-api.apiwise.nl/v2/addresses/' + id;
options.url = 'https://postcode-api.apiwise.nl/v2/addresses/' + id
options.headers = {
'X-Api-Key' : options.apiKey
};
'X-Api-Key': options.apiKey
}

// Executing API-request
return api.get(options, callback);
return api.get(options, callback)
}

module.exports = getSingleAddress;
module.exports = getSingleAddress
22 changes: 11 additions & 11 deletions lib/getSinglePostcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@
* });
*/

'use strict';
const helpers = require('./helpers.js');
const api = require('./requestApi.js');
'use strict'
const helpers = require('./helpers.js')
const api = require('./requestApi.js')

function getSinglePostcode(options, postcode, callback) {
function getSinglePostcode (options, postcode, callback) {
// Check if postcode is correctly formatted for this API-call
if (!postcode || typeof(postcode) !== 'string' || !helpers.validatePostcodeP6(postcode)) {
return callback(new Error('The postcode is required and must be in P6-formatted for this API-call'), null);
if (!postcode || typeof (postcode) !== 'string' || !helpers.validatePostcodeP6(postcode)) {
return callback(new Error('The postcode is required and must be in P6-formatted for this API-call'), null)
}

options.url = 'https://postcode-api.apiwise.nl/v2/postcodes/' + postcode;
options.url = 'https://postcode-api.apiwise.nl/v2/postcodes/' + postcode
options.headers = {
'X-Api-Key' : options.apiKey
};
'X-Api-Key': options.apiKey
}

// Executing API-request
return api.get(options, callback);
return api.get(options, callback)
}

module.exports = getSinglePostcode;
module.exports = getSinglePostcode
Loading

0 comments on commit 99adfb9

Please sign in to comment.