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
843class 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 ;
0 commit comments