@@ -4,13 +4,16 @@ import { Api } from './Api'
4
4
import { ApiError , NotFoundApiError } from '../errors/ApiError'
5
5
import { ModuleBuilder } from '../module/ModuleBuilder'
6
6
import { ResourceProxy } from './ResourceProxy'
7
- import { deref , hasOwn } from '../shared/utils'
7
+ import { hasOwn } from '../shared/utils'
8
8
import { Performance } from '../shared/Performance'
9
9
10
10
export class ResourcefulApi extends Api {
11
11
constructor ( ) {
12
12
super ( )
13
13
14
+ /** @var {Function[]} */
15
+ this . requestCallbacks = [ ]
16
+
14
17
/** @var {Function[]} */
15
18
this . responseCallbacks = [ ]
16
19
}
@@ -20,6 +23,23 @@ export class ResourcefulApi extends Api {
20
23
*
21
24
* @param {Function[] } callbacks
22
25
*/
26
+ setRequestCallbacks ( callbacks ) {
27
+ this . requestCallbacks = callbacks
28
+ }
29
+
30
+ addRequestCallback ( cb ) {
31
+ this . requestCallbacks . push ( cb )
32
+ }
33
+
34
+ resetRequestCallbacks ( ) {
35
+ this . requestCallbacks = [ ]
36
+ }
37
+
38
+ /**
39
+ * Functions to be called with the parsed response data.
40
+ *
41
+ * @param {Function[] } callbacks
42
+ */
23
43
setResponseCallbacks ( callbacks ) {
24
44
this . responseCallbacks = callbacks
25
45
}
@@ -45,8 +65,8 @@ export class ResourcefulApi extends Api {
45
65
* @param {Object } data
46
66
*/
47
67
async _doRequest ( method , url , params , data ) {
48
- if ( data ) {
49
- data = this . preprocessData ( data )
68
+ for ( const cb of this . requestCallbacks ) {
69
+ data = cb ( data , params , url , method )
50
70
}
51
71
52
72
return super . _doRequest ( method , url , params , data )
@@ -119,67 +139,6 @@ export class ResourcefulApi extends Api {
119
139
}
120
140
}
121
141
122
- /**
123
- * convert ResourceTypes to uppercase
124
- * to follow the json:api spects even if the incoming data is not correct
125
- *
126
- * this is just a safety net
127
- *
128
- * @param data
129
- *
130
- * @return {* }
131
- */
132
- preprocessData ( data ) {
133
- data = deref ( data )
134
- data . data . type = data . data . type . charAt ( 0 ) . toUpperCase ( ) + data . data . type . slice ( 1 )
135
-
136
- if ( data . data . relationships ) {
137
- const relationships = { }
138
- const casingWarning = ( type ) => {
139
- console . warn ( `The Resource with type '${ type } ' is sent in lower camel case. Please send as upper camel case.` )
140
- }
141
-
142
- for ( const [ name , relationship ] of Object . entries ( data . data . relationships ) ) {
143
- if ( Array . isArray ( relationship . data ) ) {
144
- relationships [ name ] = {
145
- data : relationship . data . map ( itemData => {
146
- const startChar = itemData . type . charAt ( 0 )
147
-
148
- if ( startChar === startChar . toLocaleLowerCase ( ) ) {
149
- casingWarning ( itemData . type )
150
- }
151
-
152
- return {
153
- id : itemData . id ,
154
- type : startChar . toUpperCase ( ) + itemData . type . slice ( 1 )
155
- }
156
- } )
157
- }
158
- } else if ( relationship . data !== null ) {
159
- const startChar = relationship . data . type . charAt ( 0 )
160
-
161
- if ( startChar === startChar . toLocaleLowerCase ( ) ) {
162
- casingWarning ( relationship . data . type )
163
- }
164
-
165
- relationships [ name ] = {
166
- data : {
167
- id : relationship . data . id ,
168
- type : startChar . charAt ( 0 ) . toUpperCase ( ) + relationship . data . type . slice ( 1 )
169
- }
170
- }
171
- } else if ( relationship . data === null ) {
172
- relationships [ name ] = {
173
- data : null
174
- }
175
- }
176
- }
177
- data . data . relationships = relationships
178
- }
179
-
180
- return data
181
- }
182
-
183
142
/**
184
143
* Prepare the routable requests
185
144
*
0 commit comments