From 2552d03c8c309289f32087ff1485a3bd22979cb9 Mon Sep 17 00:00:00 2001 From: Pieter Berkel Date: Wed, 15 Nov 2023 09:24:56 +0100 Subject: [PATCH] new domain --- src/request.js | 179 +++++++++++++++++++++++++------------------------ 1 file changed, 93 insertions(+), 86 deletions(-) diff --git a/src/request.js b/src/request.js index bb3a3b5..bf24726 100644 --- a/src/request.js +++ b/src/request.js @@ -1,100 +1,107 @@ const https = require("https"); class JSONRequest { - constructor(options) { - this._options = options; - } + constructor(options) { + this._options = options; + } - _requestOptions(method, path, headers, encoding) { - if(!headers) { - headers = { - "Content-Type": "application/json" - }; - } + _requestOptions(method, path, headers, encoding) { + if (!headers) { + headers = { + "Content-Type": "application/json", + }; + } - if(!encoding) { - encoding = "utf8"; - } + if (!encoding) { + encoding = "utf8"; + } - return { - method, headers, encoding, - hostname: "api.postcode.nl", - path: "/rest" + path, - auth: this._options.key+":"+this._options.secret - } - } + return { + method, + headers, + encoding, + hostname: "api.postcode.eu", + path: "/rest" + path, + auth: this._options.key + ":" + this._options.secret, + }; + } - _parseJsonResponse(response) { - return new Promise((resolve, reject) => { - const strings = []; + _parseJsonResponse(response) { + return new Promise((resolve, reject) => { + const strings = []; - response.on("data", (chunk) => { - strings.push(chunk); - }); + response.on("data", (chunk) => { + strings.push(chunk); + }); - response.on("end", () => { - try { - const string = strings.join(""); - if(response.statusCode === 200) { - resolve(JSON.parse(string)); - }else if(response.statusCode === 401) { - reject(Object.assign(new Error("Invalid credentials for call"), { - statusCode: response.statusCode, - headers: response.headers - })); - }else{ - let json; - let error = "Request returned HTTP code "+response.statusCode; - try { - json = JSON.parse(string); - if(json && json.exception) { - error = json.exception; - } - } catch (err) { - reject(err) - } - reject(Object.assign(new Error(error), { - string, json, - statusCode: response.statusCode, - headers: response.headers - })); - } - } catch (err) { - reject(err); - } - }); + response.on("end", () => { + try { + const string = strings.join(""); + if (response.statusCode === 200) { + resolve(JSON.parse(string)); + } else if (response.statusCode === 401) { + reject( + Object.assign(new Error("Invalid credentials for call"), { + statusCode: response.statusCode, + headers: response.headers, + }) + ); + } else { + let json; + let error = "Request returned HTTP code " + response.statusCode; + try { + json = JSON.parse(string); + if (json && json.exception) { + error = json.exception; + } + } catch (err) { + reject(err); + } + reject( + Object.assign(new Error(error), { + string, + json, + statusCode: response.statusCode, + headers: response.headers, + }) + ); + } + } catch (err) { + reject(err); + } + }); - response.on("error", reject); - }); - } + response.on("error", reject); + }); + } - get(path) { - return new Promise((resolve, reject) => { - const opts = this._requestOptions("GET", path); - const request = https.request(opts, (res) => { - resolve(this._parseJsonResponse(res)); - }); - request.on("error", reject); - request.end(); - }); - } + get(path) { + return new Promise((resolve, reject) => { + const opts = this._requestOptions("GET", path); + const request = https.request(opts, (res) => { + resolve(this._parseJsonResponse(res)); + }); + request.on("error", reject); + request.end(); + }); + } - post(path, data) { - return new Promise((resolve, reject) => { - const postData = JSON.stringify(data); - const opts = this._requestOptions("POST", path, { - "Content-Type": "application/json", - "Content-Length": postData.length - }); + post(path, data) { + return new Promise((resolve, reject) => { + const postData = JSON.stringify(data); + const opts = this._requestOptions("POST", path, { + "Content-Type": "application/json", + "Content-Length": postData.length, + }); - const request = https.request(opts, (res) => { - resolve(this._parseJsonResponse(res)); - }); - request.write(postData); - request.on("error", reject); - request.end(); - }); - } -}; + const request = https.request(opts, (res) => { + resolve(this._parseJsonResponse(res)); + }); + request.write(postData); + request.on("error", reject); + request.end(); + }); + } +} -module.exports = JSONRequest; \ No newline at end of file +module.exports = JSONRequest;