Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sosna committed Oct 21, 2018
2 parents 9110478 + a5f24e2 commit 27c8434
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 18 additions & 9 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -290,6 +298,7 @@ module.exports =
getAvailabilityQuery: getAvailabilityQuery
getUrl: getUrl
request: request
request2: request2
data:
DataFormat: DataFormat
DataDetail: DataDetail
Expand Down
15 changes: 13 additions & 2 deletions test/index.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -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'

0 comments on commit 27c8434

Please sign in to comment.