10000 Merge pull request #30 from manosim/callbacks · core-api/javascript-client@e5fa6a2 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit e5fa6a2

Browse files
authored
Merge pull request #30 from manosim/callbacks
Request & Response Callbacks
2 parents b83a99e + 58b0c01 commit e5fa6a2

File tree

5 files changed

+70
-19
lines changed

5 files changed

+70
-19
lines changed

lib/client.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const document = require('./document')
22
const codecs = require('./codecs')
33
const errors = require('./errors')
4-
const transportsModule = require('./transports')
4+
const transports = require('./transports')
55
const utils = require('./utils')
66

77
function lookupLink (node, keys) {
@@ -22,9 +22,15 @@ function lookupLink (node, keys) {
2222
}
2323

2424
class Client {
25-
constructor (decoders = null, transports = null, csrf = null) {
26-
this.decoders = decoders || [new codecs.CoreJSONCodec(), new codecs.JSONCodec(), new codecs.TextCodec()]
27-
this.transports = transports || [new transportsModule.HTTPTransport({csrf: csrf})]
25+
constructor (options = {}) {
26+
const transportOptions = {
27+
csrf: options.csrf,
28+
requestCallback: options.requestCallback,
29+
responseCallback: options.responseCallback
30+
}
31+
32+
this.decoders = options.decoders || [new codecs.CoreJSONCodec(), new codecs.JSONCodec(), new codecs.TextCodec()]
33+
this.transports = options.transports || [new transports.HTTPTransport(transportOptions)]
2834
}
2935

3036
action (document, keys, params = {}) {

lib/transports/http.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class HTTPTransport {
1919
this.csrf = options.csrf
2020
this.fetch = options.fetch || fetch
2121
this.FormData = options.FormData || window.FormData
22+
this.requestCallback = options.requestCallback
23+
this.responseCallback = options.responseCallback
2224
}
2325

2426
buildRequest (link, decoders, params = {}) {
@@ -113,19 +115,29 @@ class HTTPTransport {
113115
}
114116

115117
action (link, decoders, params = {}) {
118+
const responseCallback = this.responseCallback
116119
const request = this.buildRequest(link, decoders, params)
117120

121+
if (this.requestCallback) {
122+
this.requestCallback(request)
123+
}
124+
118125
return this.fetch(request.url, request.options)
119126
.then(function (response) {
120-
return parseResponse(response, decoders).then(function (data) {
121-
if (response.ok) {
122-
return data
123-
} else {
124-
const title = response.status + ' ' + response.statusText
125-
const error = new errors.ErrorMessage(title, data)
126-
return Promise.reject(error)
127-
}
128-
})
127+
if (responseCallback) {
128+
responseCallback(response)
129+
}
130+
131+
return parseResponse(response, decoders)
132+
.then(function (data) {
133+
if (response.ok) {
134+
return data
135+
} else {
136+
const title = response.status + ' ' + response.statusText
137+
const error = new errors.ErrorMessage(title, data)
138+
return Promise.reject(error)
139+
}
140+
})
129141
})
130142
}
131143
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coreapi",
3-
"version": "0.0.18",
3+
"version": "0.0.19",
44
"description": "Javascript client library for Core API",
55
"main": "lib/index.js",
66
"scripts": {
@@ -29,7 +29,8 @@
2929
"describe",
3030
"expect",
3131
"it",
32-
"xit"
32+
"xit",
33+
"jest"
3334
]
3435
},
3536
"jest": {

tests/client.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('Test the Client', function () {
99
const transport = new transportsModule.HTTPTransport({
1010
fetch: mockedFetch
1111
})
12-
const client = new coreapi.Client(null, [transport])
12+< 47CA /span>
const client = new coreapi.Client({transports: [transport]})
1313
const url = 'http://example.com'
1414

1515
client.get(url)
@@ -27,7 +27,7 @@ describe('Test the Client', function () {
2727
const transport = new transportsModule.HTTPTransport({
2828
fetch: mockedFetch
2929
})
30-
const client = new coreapi.Client(null, [transport])
30+
const client = new coreapi.Client({transports: [transport]})
3131
const url = 'http://example.com'
3232

3333
client.get(url)
@@ -45,7 +45,7 @@ describe('Test the Client', function () {
4545
const transport = new transportsModule.HTTPTransport({
4646
fetch: mockedFetch
4747
})
48-
const client = new coreapi.Client(null, [transport])
48+
const client = new coreapi.Client({transports: [transport]})
4949
const document = new coreapi.Document('', '', '', {nested: {link: new coreapi.Link('http://example.com', 'get')}})
5050

5151
client.action(document, ['nested', 'link'])
@@ -63,7 +63,7 @@ describe('Test the Client', function () {
6363
const transport = new transportsModule.HTTPTransport({
6464
fetch: mockedFetch
6565
})
66-
const client = new coreapi.Client(null, [transport])
66+
const client = new coreapi.Client({transports: [transport]})
6767
const document = new coreapi.Document('', '', '', {nested: {link: new coreapi.Link('http://example.com', 'get')}})
6868

6969
client.action(document, ['nested', 'link'])

tests/transports/http.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,36 @@ describe('Test the HTTPTransport', function () {
242242
const callTransport = () => transport.action(link, decoders, params)
243243
expect(callTransport).toThrowError(new errors.ParameterError('Unknown parameter: "hello"'))
244244
})
245+
246+
it('should call the requestCallback if any', function () {
247+
const requestCallback = jest.fn()
248+
249+
const url = 'http://www.example.com/'
250+
const link = new document.Link(url, 'get')
251+
const transport = new transports.HTTPTransport({
252+
fetch: testUtils.echo,
253+
requestCallback: requestCallback
254+
})
255+
256+
return transport.action(link, decoders)
257+
.then(() => {
258+
expect(requestCallback).toHaveBeenCalledTimes(1)
259+
})
260+
})
261+
262+
it('should call the responseCallback if any', function () {
263+
const responseCallback = jest.fn()
264+
265+
const url = 'http://www.example.com/'
266+
const link = new document.Link(url, 'get')
267+
const transport = new transports.HTTPTransport({
268+
fetch: testUtils.echo,
269+
responseCallback: responseCallback
270+
})
271+
272+
return transport.action(link, decoders)
273+
.then(() => {
274+
expect(responseCallback).toHaveBeenCalledTimes(1)
275+
})
276+
})
245277
})

0 commit comments

Comments
 (0)
0