-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
137 lines (118 loc) · 3.7 KB
/
app.js
File metadata and controls
137 lines (118 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Name: overheid.io
Description: Access overheid.io endpoints like OpenKvK, RDW and BAG with node.js
Author: Franklin van de Meent (https://frankl.in)
Source code: https://github.com/fvdm/nodejs-overheid.io
Feedback: https://github.com/fvdm/nodejs-overheid.io/issues
License: Unlicense (Public Domain) - see LICENSE file
(https://github.com/fvdm/nodejs-overheid.io/raw/master/LICENSE)
*/
var http = require ('httpreq');
var defaults = {
timeout: 5000
};
/**
* Call back an error
*
* @callback callback
* @param msg {string} - Error.message
* @param err {mixed} - Error.error
* @param res {object, null} - Client response
* @param callback {function} - `function (err) {}`
* @return {void}
*/
function doError (msg, err, res, callback) {
var error = new Error (msg);
error.error = err;
error.code = res && res.statusCode;
error.body = res && res.body;
callback (error);
}
/**
* Translate nested params to string
*
* @param obj {mixed} - Object to process
* @return {string} - Querystring
*/
function fixParams (obj) {
var key;
var nw = [];
var k;
var i;
if (obj) {
for (key in obj) {
if (obj[key] instanceof Array) {
for (i = 0; i < obj[key].length; i++) {
nw.push (key + '[]=' + encodeURIComponent (obj[key][i]));
}
} else if (obj[key] instanceof Object) {
for (k in obj[key]) {
nw.push (key + '[' + k + ']=' + encodeURIComponent (obj[key][k]));
}
} else {
nw.push (key + '=' + encodeURIComponent (obj[key]));
}
}
}
return nw.length ? '?' + nw.join ('&') : '';
}
/**
* Setup
*
* @param config {object}
* @param config.apikey {string} - API key
* @param config.dataset {string} - API dataset
* @param [config.timeout = 5000] {number} - Request timeout in ms
* @return {function}
*/
module.exports = function (config) {
/**
* Send API request
*
* @param request {object}
* @param request.dataset {string} - Name of dataset, i.e. `kvk`
* @param [request.timeout = 5000] {number} - Request timeout in ms
* @param request.path {string} - Request path after `dataset` part
* @param request.params {object} - Data parameters to send
* @param request.callback {function} - `function (err, data) {}`
* @return {void}
*/
return function (request) {
var options = {
url: 'https://overheid.io/api/' + (request.dataset || config.dataset),
method: 'GET',
headers: {
'ovio-api-key': config.apikey || '',
'User-Agent': 'overheid.io (https://github.com/fvdm/nodejs-overheid.io)'
},
timeout: request.timeout || config.timeout || defaults.timeout
};
options.url += request.path ? '/' + request.path : '';
options.url += fixParams (request.params);
http.doRequest (options, function (err, res) {
var data = res && res.body || '';
if (err) {
doError ('request failed', err, res, request.callback);
return;
}
try {
data = JSON.parse (data);
if (data.error) {
doError ('API error', data.error, res, request.callback);
} else if (data.original && data.original.error) {
if (data.original.error.match (/ niet gevonden\.$/)) {
doError ('no result', null, res, request.callback);
} else {
doError ('API error', data.original.error, res, request.callback);
}
} else if (Object.keys (data).length === 1 && Object.keys (data.headers).length === 0) {
doError ('no result', null, res, request.callback);
} else {
request.callback (null, data);
}
} catch (e) {
doError ('invalid response', e, res, request.callback);
}
});
};
};