From 265fe3339ad3bdd920750123098a68a20a7fee69 Mon Sep 17 00:00:00 2001 From: Xavier Sosnovsky Date: Sat, 20 Oct 2018 09:53:11 +0200 Subject: [PATCH 1/2] chore(package.json): Change version & description --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b44e1b4..23e00b8 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,8 @@ "engines": { "node": ">=6" }, - "description": "The SDMX RESTful API for JavaScript", - "version": "2.8.0", + "description": "SDMX REST API client for JavaScript", + "version": "2.10.0", "main": "./lib/index.js", "scripts": { "prebuild": "rm -rf lib && mkdir lib", From 0878a69e065a263bf4ee1235fcd2668cd6dd7835 Mon Sep 17 00:00:00 2001 From: Xavier Sosnovsky Date: Sat, 20 Oct 2018 11:37:54 +0200 Subject: [PATCH 2/2] feat(api): Add option to return full response A new method has been added (request2) that will return the full response rather than only its body. fix #85 --- src/index.coffee | 27 ++++++++++++++++++--------- test/index.test.coffee | 15 +++++++++++++-- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/index.coffee b/src/index.coffee index 0c19ed6..02c85d9 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -265,22 +265,30 @@ getUrl = (query, service) -> # @see #getService # request = (params...) -> + request2(params...).then((response) -> + checkStatus params[0], response + response.text() + ) + +# +# Executes the supplied query against the supplied service and returns a +# Promise. +# +# At the difference with the request() function, request2() will +# include the response headers and additional information such as the status. +# +# @see #request for additional information about the required parameters. +# +request2 = (params...) -> q = params[0] s = if typeof q is 'string' then guessService q else getService params[1] u = if typeof q is 'string' then q else getUrl q, s o = if typeof q is 'string' then params[1] else params[2] - isDataQuery = false - if typeof q is 'string' and q.indexOf('/data/') > -1 - isDataQuery = true - else if q.flow - isDataQuery = true + isDataQuery = if u.indexOf('/data/') > -1 then true else false requestOptions = addHeaders o, s, isDataQuery fetch(u, requestOptions) - .then((response) -> - checkStatus q, response - response.text()) - .then((body) -> body) + .then((response) -> response) module.exports = getService: getService @@ -290,6 +298,7 @@ module.exports = getAvailabilityQuery: getAvailabilityQuery getUrl: getUrl request: request + request2: request2 data: DataFormat: DataFormat DataDetail: DataDetail diff --git a/test/index.test.coffee b/test/index.test.coffee index 6ea4d9b..df12375 100644 --- a/test/index.test.coffee +++ b/test/index.test.coffee @@ -192,7 +192,7 @@ describe 'API', -> test = -> sdmxrest.getUrl query should.Throw(test, Error, 'Service is a mandatory parameter') - describe 'when using execute()', -> + describe 'when using request()', -> it 'offers to execute a request from a query and service objects', -> query = nock('http://sdw-wsrest.ecb.europa.eu') @@ -312,7 +312,6 @@ describe 'API', -> sdmxrest.request {flow: 'EXR', key: 'A.CHF.NOK.SP00.A'}, 'ECB' response.should.eventually.equal 'OK' - it 'allows disabling content compression', -> query = nock('http://sdw-wsrest.ecb.europa.eu') .matchHeader('accept-encoding', (h) -> h is undefined) @@ -323,3 +322,15 @@ describe 'API', -> response = sdmxrest.request {flow: 'EXR', key: 'A.CHF.NOK.SP00.A'}, 'ECB', opts response.should.eventually.equal 'OK' + + describe 'when using request2()', -> + it 'offers a way to retrieve response headers', -> + query = nock('http://sdw-wsrest.ecb.europa.eu') + .get((uri) -> uri.indexOf('EXR') > -1) + .reply 200, 'OK', {'X-My-Headers': 'My Header value'} + response = + sdmxrest.request2 {flow: 'EXR', key: 'A.CHF.EUR.SP00.A'}, 'ECB' + response.should.eventually.have.property('status').that.equals 200 + response.should.eventually.have.property('headers').that.is.an 'object' + response.should.eventually.have.property('headers').that.have.property 'X-My-Headers' + response.should.eventually.respondTo 'text'