Skip to content

Commit 342a0c8

Browse files
committed
Release v1.0.0
- Removed traces of the iDEAL API - Added keywords - Added the signal API - Moved the JSONRequest class to a separate directory - Added some tests for both signal and address
1 parent 7f98a1b commit 342a0c8

File tree

5 files changed

+181
-99
lines changed

5 files changed

+181
-99
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ API wrapper for the Postcode.nl API.
44
Features:
55
- gathering addresses using postcode, house number and house number addition
66
- verifying address data
7-
- processing iDEAL payments
87

98
# Example
109

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-postcode",
3-
"version": "0.0.3",
3+
"version": "1.0.0",
44
"description": "API wrapper for the Postcode.nl APIs",
55
"main": "lib/postcode.js",
66
"scripts": {
@@ -17,6 +17,11 @@
1717
"bugs": {
1818
"url": "https://github.com/Cyberuben/node-postcode/issues"
1919
},
20+
"keywords": [
21+
"postcode",
22+
"postcode.nl",
23+
"address"
24+
],
2025
"homepage": "https://github.com/Cyberuben/node-postcode#readme",
2126
"devDependencies": {
2227
"babel-cli": "^6.10.1",

src/postcode.js

Lines changed: 23 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,10 @@
1-
var https = require("https");
2-
3-
class JSONRequest {
4-
constructor(options) {
5-
this._options = options;
6-
}
7-
8-
_requestOptions(method, path, headers, encoding) {
9-
if(!headers) {
10-
headers = {
11-
"Content-Type": "application/json"
12-
};
13-
}
14-
15-
if(!encoding) {
16-
encoding = "utf8";
17-
}
18-
19-
return {
20-
method, headers, encoding,
21-
protocol: "https:",
22-
hostname: "api.postcode.nl",
23-
path: "/rest" + path,
24-
auth: this._options.key+":"+this._options.secret
25-
}
26-
}
27-
28-
_parseJsonResponse(response) {
29-
return new Promise((resolve, reject) => {
30-
const strings = [];
31-
32-
response.on("data", (chunk) => {
33-
strings.push(chunk);
34-
});
35-
36-
response.on("end", () => {
37-
try {
38-
const string = strings.join("");
39-
if(response.statusCode === 200) {
40-
resolve(JSON.parse(string));
41-
}else if(response.statusCode === 401) {
42-
reject(Object.assign(new Error("Invalid credentials for call"), {
43-
statusCode: response.statusCode,
44-
headers: response.headers
45-
}));
46-
}else{
47-
var json;
48-
var error = "Request returned HTTP code "+response.statusCode;
49-
try {
50-
json = JSON.parse(string);
51-
if(json && json.exception) {
52-
error = json.exception;
53-
}
54-
} catch (err) {
55-
56-
}
57-
reject(Object.assign(new Error(error), {
58-
string, json,
59-
statusCode: response.statusCode,
60-
headers: response.headers
61-
}));
62-
}
63-
} catch (err) {
64-
reject(err);
65-
}
66-
});
67-
68-
response.on("error", reject);
69-
});
70-
}
71-
72-
get(path) {
73-
return new Promise((resolve, reject) => {
74-
const opts = this._requestOptions("GET", path);
75-
const request = https.request(opts, res => {
76-
resolve(this._parseJsonResponse(res));
77-
});
78-
request.on("error", reject);
79-
request.end();
80-
});
81-
}
82-
};
1+
const JSONRequest = require("./request");
832

843
class PostcodeClient {
854
constructor (options) {
86-
if(!options.hasOwnProperty("key")) throw new TypeError("'options.key' has to be set");
87-
if(!options.hasOwnProperty("secret")) throw new TypeError("'options.secret' has to be set");
5+
if(!options) throw new TypeError("'options' has to be set");
6+
if(!options.key) throw new TypeError("'options.key' has to be set");
7+
if(!options.secret) throw new TypeError("'options.secret' has to be set");
888

899
this._options = options;
9010

@@ -138,15 +58,26 @@ class PostcodeClient {
13858
});
13959
});
14060
}
141-
};
142-
143-
class iDEAL {
14461

145-
};
146-
147-
class PostcodeiDEALClient {
62+
signal(options) {
63+
if(!options || typeof options != "object") throw new TypeError("'options' is required and should be an object");
64+
if(Object.keys(options).length == 0) throw new TypeError("'options' must have at least one option");
14865

66+
return new Promise((resolve, reject) => {
67+
this._r.post("/signal/check", options)
68+
.then((res) => {
69+
resolve(res);
70+
})
71+
.catch((err) => {
72+
if(err.statusCode === 400) {
73+
err.code = err.json.exceptionId;
74+
reject(err);
75+
}else{
76+
reject(error);
77+
}
78+
});
79+
});
80+
}
14981
};
15082

151-
module.exports.Postcode = PostcodeClient;
152-
module.exports.iDEAL = PostcodeiDEALClient;
83+
module.exports = PostcodeClient;

src/request.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const https = require("https");
2+
3+
class JSONRequest {
4+
constructor(options) {
5+
this._options = options;
6+
}
7+
8+
_requestOptions(method, path, headers, encoding) {
9+
if(!headers) {
10+
headers = {
11+
"Content-Type": "application/json"
12+
};
13+
}
14+
15+
if(!encoding) {
16+
encoding = "utf8";
17+
}
18+
19+
return {
20+
method, headers, encoding,
21+
hostname: "api.postcode.nl",
22+
path: "/rest" + path,
23+
auth: this._options.key+":"+this._options.secret
24+
}
25+
}
26+
27+
_parseJsonResponse(response) {
28+
return new Promise((resolve, reject) => {
29+
const strings = [];
30+
31+
response.on("data", (chunk) => {
32+
strings.push(chunk);
33+
});
34+
35+
response.on("end", () => {
36+
try {
37+
const string = strings.join("");
38+
if(response.statusCode === 200) {
39+
resolve(JSON.parse(string));
40+
}else if(response.statusCode === 401) {
41+
reject(Object.assign(new Error("Invalid credentials for call"), {
42+
statusCode: response.statusCode,
43+
headers: response.headers
44+
}));
45+
}else{
46+
let json;
47+
let error = "Request returned HTTP code "+response.statusCode;
48+
try {
49+
json = JSON.parse(string);
50+
if(json && json.exception) {
51+
error = json.exception;
52+
}
53+
} catch (err) {
54+
55+
}
56+
reject(Object.assign(new Error(error), {
57+
string, json,
58+
statusCode: response.statusCode,
59+
headers: response.headers
60+
}));
61+
}
62+
} catch (err) {
63+
reject(err);
64+
}
65+
});
66+
67+
response.on("error", reject);
68+
});
69+
}
70+
71+
get(path) {
72+
return new Promise((resolve, reject) => {
73+
const opts = this._requestOptions("GET", path);
74+
const request = https.request(opts, (res) => {
75+
resolve(this._parseJsonResponse(res));
76+
});
77+
request.on("error", reject);
78+
request.end();
79+
});
80+
}
81+
82+
post(path, data) {
83+
return new Promise((resolve, reject) => {
84+
const postData = JSON.stringify(data);
85+
const opts = this._requestOptions("POST", path, {
86+
"Content-Type": "application/json",
87+
"Content-Length": postData.length
88+
});
89+
90+
const request = https.request(opts, (res) => {
91+
resolve(this._parseJsonResponse(res));
92+
});
93+
request.write(postData);
94+
request.on("error", reject);
95+
request.end();
96+
});
97+
}
98+
};
99+
100+
module.exports = JSONRequest;

test/test.js

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ const API_SECRET = process.env.API_SECRET;
77

88
chai.should();
99

10-
let postcode = require("../lib/postcode");
11-
let PostcodeClient = postcode.Postcode;
12-
let PostcodeiDEALClient = postcode.iDEAL;
10+
let PostcodeClient = require("../lib/postcode");
11+
let JSONRequest = require("../lib/request");
1312

1413
describe("Postcode.nl API wrapper", () => {
1514
describe("PostcodeClient", () => {
@@ -35,6 +34,12 @@ describe("Postcode.nl API wrapper", () => {
3534
new PostcodeClient({key: "test", secret: "test"});
3635
}).should.not.throw(TypeError);
3736
});
37+
38+
it("should have an instance of JSONRequest and options", () => {
39+
var client = new PostcodeClient({key: "test", secret: "test"});
40+
client._r.should.be.instanceof(JSONRequest);
41+
client._options.should.be.an("object");
42+
});
3843
});
3944

4045
describe("#address()", () => {
@@ -111,9 +116,51 @@ describe("Postcode.nl API wrapper", () => {
111116
});
112117
});
113118
});
114-
});
115119

116-
describe("class PostcodeiDEALClient", () => {
120+
describe("#signal()", () => {
121+
expect(API_KEY).to.be.ok;
122+
expect(API_SECRET).to.be.ok;
123+
124+
var client = new PostcodeClient({
125+
key: API_KEY,
126+
secret: API_SECRET
127+
});
128+
129+
it("should error on empty / invalid options object", () => {
130+
(() => {
131+
client.signal();
132+
}).should.throw(TypeError);
133+
134+
(() => {
135+
client.signal({});
136+
}).should.throw(TypeError);
137+
});
138+
139+
it("should return a promise on 'valid' options object", () => {
140+
client.signal({
141+
test: "test"
142+
}).should.be.instanceof(Promise);
143+
});
117144

145+
it("should be able to generate a response", () => {
146+
return client.signal({
147+
transaction: {
148+
deliveryAddress: {
149+
postcode: "1111AA",
150+
houseNumber: 1,
151+
country: "NL"
152+
}
153+
}
154+
})
155+
.then((signalDetails) => {
156+
signalDetails.should.be.an("object");
157+
signalDetails.checkId.should.be.a("string");
158+
})
159+
.catch((err) => {
160+
assert(err === undefined, "Error calling the API: "+err.message);
161+
done();
162+
});
163+
});
164+
});
118165
});
119166
});

0 commit comments

Comments
 (0)