Skip to content

Commit

Permalink
Refactor: No more production dependencies (#8)
Browse files Browse the repository at this point in the history
* Refactor: Replaced httpreq dep by native fetch
* Refactor: Prefix API error message
* Refactor: Increase default timeout to 15 sec
* Fix: urgentpolitiebericht no longer taking params
* Package: Minimum node v18
  • Loading branch information
fvdm authored Sep 15, 2023
1 parent 27fbcca commit b7faa41
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 43 deletions.
32 changes: 14 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
* License: Unlicense (public domain, see LICENSE file)
*/

const { doRequest } = require ('httpreq');

module.exports = class PolitieAPI {

/**
* @param {number} [timeout=10000] Request time out in ms
*/

constructor ({
timeout = 10000,
timeout = 15000,
} = {}) {
this._config = {
timeout,
Expand All @@ -42,28 +40,29 @@ module.exports = class PolitieAPI {
empty,
}) {
const options = {
url: 'https://api.politie.nl' + path,
method: 'GET',
timeout: this._config.timeout,
parameters,
signal: AbortSignal.timeout (parseInt (this._config.timeout, 10)),
headers: {
'Accept': 'application/json',
'User-Agent': 'nodejs-politieapi',
},
};

const res = await doRequest (options);
const params = new URLSearchParams (parameters);
const url = 'https://api.politie.nl' + path + '?' + params;
const res = await fetch (url, options);

// Success, but empty
if (res.statusCode === 204) {
if (res.status === 204) {
return parameters.uid ? {} : empty;
}

// API error in HTML
const htmlError = res.body.match (/<title>([^<]+) \| [^<]+<\/title>/);
const body = await res.text();
const htmlError = body.match (/<title>([^<]+) \| [^<]+<\/title>/);

if (res.statusCode === 400 && htmlError) {
const error = new Error (htmlError[1]);
if (res.status === 400 && htmlError) {
const error = new Error ('API: ' + htmlError[1]);

error.code = -1;
error.type = '';
Expand All @@ -73,12 +72,12 @@ module.exports = class PolitieAPI {
}

// Parse response
const data = JSON.parse (res.body);
const data = JSON.parse (body);

/*
// API error in JSON
if (res.statusCode === 400) {
const error = new Error (data.message);
if (res.status === 400) {
const error = new Error ('API: ' + data.message);
error.code = data.code;
error.type = data.type;
Expand Down Expand Up @@ -190,15 +189,12 @@ module.exports = class PolitieAPI {
/**
* Get the urgent news message
*
* @param {object} [parameters] Method parameters
*
* @return {Promise<object>}
*/

async urgentpolitiebericht (parameters = {}) {
async urgentpolitiebericht () {
return this._talk ({
path: '/v4/urgentpolitiebericht',
parameters,
key: 'opsporingsberichten',
empty: [{}],
})
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
},
"main": "index.js",
"files": [],
"dependencies": {
"httpreq": "^0.5.1"
},
"dependencies": {},
"devDependencies": {
"dotest": "^2"
},
"engines": {
"node": ">=12"
"node": ">=18"
},
"keywords": [
"api",
Expand Down
22 changes: 1 addition & 21 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,6 @@ dotest.add ('urgentpolitiebericht', async test => {
});


dotest.add ('urgentpolitiebericht - language', async test => {
try {
const data = await app.urgentpolitiebericht ({
language: 'uk',
});

test()
.isObject ('fail', 'data', data)
.isNotEmpty ('warn', 'data', data)
.done()
;
}
catch (err) {
test (err).done();
}
});


dotest.add ('Invalid UID', async test => {
try {
const data = await app.nieuws ({
Expand Down Expand Up @@ -219,7 +201,7 @@ dotest.add ('Request timeout', async test => {
test()
.isUndefined ('fail', 'data', data)
.isError ('fail', 'error', error)
.isExactly ('fail', 'error.code', error && error.code, 'TIMEOUT')
.isExactly ('fail', 'error.name', error && error.name, 'TimeoutError')
.done()
;
});
Expand Down Expand Up @@ -275,11 +257,9 @@ dotest.add ('API error - HTML', async test => {
.isUndefined ('fail', 'data', data)
.isError ('fail', 'error', error)
.isNotEmpty ('fail', 'error.message', error && error.message)
.isNumber ('warn', 'error.code', error && error.code)
.isExactly ('fail', 'error.code', error && error.code, -1)
.isString ('warn', 'error.type', error && error.type)
.isArray ('warn', 'error.invalidFields', error && error.invalidFields)
.isNotEmpty ('warn', 'error.invalidFields', error && error.invalidFields)
.done()
;
});
Expand Down

0 comments on commit b7faa41

Please sign in to comment.