Skip to content

Commit aecee81

Browse files
committed
feat: Add response data callbacks
It is now possible to register callbacks that can read and modify the actual response json (or the empty response base structure on requests not returning any response (e.g. delete)
1 parent 05f7a08 commit aecee81

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# next version
22

3+
- **BREAKING**: Success and error callbacks can no longer modify the response data,
4+
to act on the parsed response data for all requests you **MUST** use
5+
`ResourcefulApi`'s new `responseCallback`s.
36
- The `deleteAction` **can** be passed
47
a full query now, this brings it in alignment with
58
the other actions.

src/api/ResourcefulApi.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ import { deref, hasOwn } from '../shared/utils'
88
import { Performance } from '../shared/Performance'
99

1010
export class ResourcefulApi extends Api {
11+
constructor () {
12+
super()
13+
14+
/** @var {Function[]} */
15+
this.responseCallbacks = []
16+
}
17+
18+
/**
19+
* Functions to be called with the parsed request data.
20+
*
21+
* @param {Function[]} callbacks
22+
*/
23+
setResponseCallbacks (callbacks) {
24+
this.responseCallbacks = callbacks
25+
}
26+
27+
addResponseCallback (cb) {
28+
this.responseCallbacks.push(cb)
29+
}
30+
31+
resetResponseCallbacks () {
32+
this.responseCallbacks = []
33+
}
34+
1135
/**
1236
* Extends `Api::doRequest()` to handle some data preprocessing.
1337
*
@@ -27,13 +51,19 @@ export class ResourcefulApi extends Api {
2751

2852
return super._doRequest(method, url, params, data)
2953
.then(async (response) => {
54+
let parsedResponse = this._createDatalessResponse(response)
55+
3056
if (this._shouldDecodeResponseJson(response.status)) {
3157
const json = await this._decodeResponseJson(response)
3258

33-
return this._parseResponse(response.status, json)
59+
parsedResponse = this._parseResponse(response.status, json)
60+
}
61+
62+
for (const cb of this.responseCallbacks) {
63+
parsedResponse = cb(parsedResponse, response.status)
3464
}
3565

36-
return this._createDatalessResponse(response)
66+
return Object.freeze(parsedResponse)
3767
})
3868
}
3969

test/api/ResourcefulApi.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,30 @@ test('keeps null relationships after preprocessing', () => {
7474
const preprocessedData = api.preprocessData(testResource)
7575
expect(preprocessedData.data.relationships.hobbies.data).toBe(null)
7676
})
77+
78+
test('runs response callbacks', async () => {
79+
const api = new ResourcefulApi()
80+
api.setBaseUrl('http://api/')
81+
api.addResponseCallback((response, status) => {
82+
response.status_on_response = status
83+
84+
return response
85+
})
86+
87+
expect(await api.get('/book/1')).toMatchObject({
88+
data: {
89+
book: {
90+
1: {
91+
attributes: {
92+
author: expect.any(String),
93+
title: expect.any(String)
94+
}
95+
}
96+
}
97+
},
98+
links: {},
99+
meta: {},
100+
status: 200,
101+
status_on_response: 200
102+
})
103+
})

0 commit comments

Comments
 (0)