-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exposing new methods getAddressById and getPostcodeArea
Some improvements to documentation and examples
- Loading branch information
1 parent
63b8e74
commit 1e51774
Showing
7 changed files
with
129 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Disable checking index, because it is only a export wrapper | ||
index.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* To search for an address based on the BAG identifier of the object. Instance of GET /addresses. | ||
* @name getAddressById | ||
* @method | ||
* @static | ||
* @since 1.1.0 | ||
* @param {Object} options - Options for performing the API call and influencing the callback. | ||
* @param {String} options.apiKey - The API-token to access the API. | ||
* @param {Boolean} [options.returnRateLimit] - If set to true, the callback will be invoked with an extra result containing an object with the limit and the remaining amount of API calls. | ||
* @param {String} id - The BAG identifier of the object. | ||
* @param {Function} callback - A callback which is called when the API-request is finished. | ||
* @param {Error} callback.error - May contain an instance of Error, if no error occured null is responded. | ||
* @param {Object} callback.result - Contains the response as a json in exactly the same format as provided by the API. If no results are found, null is responded. | ||
* @param {Object} [callback.rateLimit] - If requested in options, an object with information about the API limits is returned. Otherwise, it will return undefined. | ||
* @example | ||
* const postcodeApi = require('postcode-nl'); | ||
* | ||
* let options = { | ||
* apiKey : 'abcdefghijklmnopQRSTUVWXYZ123', | ||
* returnRateLimit : true | ||
* }; | ||
* let id = '0268200000075156'; | ||
* | ||
* postcodeApi.getAddressById(options, id, (error, result, rateLimit) => { | ||
* if (!error) { | ||
* console.log(result); // Shows the output of the API directy in the console | ||
* console.log(rateLimit); // Shows the rateLimit information in the console | ||
* } | ||
* }); | ||
*/ | ||
|
||
const api = require('./doApiCall.js'); | ||
|
||
module.exports = (options, id, callback) => { | ||
|
||
// Check if postcode is correctly formatted for this API-call | ||
if (!id == null || 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.headers = { | ||
'X-Api-Key' : options.apiKey | ||
}; | ||
|
||
// Executing API-request | ||
return api(options, callback); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Query for the summarized information of a whole postcode area | ||
* @name getPostcodeArea | ||
* @method | ||
* @static | ||
* @since 1.1.0 | ||
* @param {Object} options - Options for performing the API call and influencing the callback. | ||
* @param {String} options.apiKey - The API-token to access the API. | ||
* @param {Boolean} [options.returnRateLimit] - If set to true, the callback will be invoked with an extra result containing an object with the limit and the remaining amount of API calls. | ||
* @param {String} postcodeArea - The postcode area to be queried, formatted as P4. | ||
* @param {Function} callback - A callback which is called when the API-request is finished. | ||
* @param {Error} callback.error - May contain an instance of Error, if no error occured null is responded. | ||
* @param {Object} callback.result - Contains the response as a json in exactly the same format as provided by the API. If no results are found, null is responded. | ||
* @param {Object} [callback.rateLimit] - If requested in options, an object with information about the API limits is returned. Otherwise, it will return undefined. | ||
* @todo Support for pagination | ||
* @example | ||
* const postcodeApi = require('postcode-nl'); | ||
* | ||
* let options = { | ||
* apiKey : 'abcdefghijklmnopQRSTUVWXYZ123', | ||
* returnRateLimit : true | ||
* }; | ||
* let postcodeArea = '1234'; | ||
* | ||
* postcodeApi.getPostcodeArea(options, id, (error, result, rateLimit) => { | ||
* if (!error) { | ||
* console.log(result); // Shows the output of the API directy in the console | ||
* console.log(rateLimit); // Shows the rateLimit information in the console | ||
* } | ||
* }); | ||
*/ | ||
|
||
const helpers = require('./helpers.js'); | ||
const api = require('./doApiCall.js'); | ||
|
||
module.exports = (options, postcodeArea, callback) => { | ||
|
||
// Check if postcode is correctly formatted for this API-call | ||
if (!postcodeArea == null || typeof(postcodeArea) !== 'string' || !helpers.validatePostcodeP4(postcodeArea)) { | ||
return callback(new Error('A postcode formatted in P4 is a required for this API'), null); | ||
} | ||
|
||
// Preparing options for request | ||
options.url = 'https://postcode-api.apiwise.nl/v2/postcodes/?postcodeArea=' + postcodeArea; | ||
options.headers = { | ||
'X-Api-Key' : options.apiKey | ||
}; | ||
|
||
// Executing API-request | ||
return api(options, callback); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters