8000 Request & Response Callbacks · core-api/javascript-client@23666ed · 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 23666ed

Browse files
committed
Request & Response Callbacks
1 parent b83a99e commit 23666ed

File tree

4 files changed

+68
-17
lines changed

4 files changed

+68
-17
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: 22 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,30 @@ 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+
128+
if (responseCallback) {
129+
responseCallback(response)
130+
}
131+
132+
return parseResponse(response, decoders)
133+
.then(function (data) {
134+
if (response.ok) {
135+
return data
136+
} else {
137+
const title = response.status + ' ' + response.statusText
138+
const error = new errors.ErrorMessage(title, data)
139+
return Promise.reject(error)
140+
}
141+
})
129142
})
130143
}
131144
}

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+
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 F438 ', '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