File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,9 @@ const transformRelationToIri = (payload) => {
16
16
return payload ;
17
17
} ;
18
18
19
+ const makeParamArray = ( key , arr ) =>
20
+ arr . map ( val => `${ key } []=${ val } ` ) . join ( '&' ) ;
21
+
19
22
export default function ( id , options = { } ) {
20
23
if ( 'undefined' === typeof options . headers ) options . headers = new Headers ( ) ;
21
24
@@ -33,6 +36,18 @@ export default function(id, options = {}) {
33
36
if ( isObject ( payload ) && payload [ '@id' ] )
34
37
options . body = JSON . stringify ( transformRelationToIri ( payload ) ) ;
35
38
39
+ if ( options . params ) {
40
+ const params = normalize ( options . params ) ;
41
+ let queryString = Object . keys ( params )
42
+ . map ( key =>
43
+ Array . isArray ( params [ key ] )
44
+ ? makeParamArray ( key , params [ key ] )
45
+ : `${ key } =${ params [ key ] } `
46
+ )
47
+ . join ( '&' ) ;
48
+ id = `${ id } ?${ queryString } ` ;
49
+ }
50
+
36
51
return global . fetch ( new URL ( id , ENTRYPOINT ) , options ) . then ( response => {
37
52
if ( response . ok ) return response ;
38
53
Original file line number Diff line number Diff line change
1
+ import get from 'lodash/get' ;
2
+ import has from 'lodash/has' ;
3
+ import mapValues from 'lodash/mapValues' ;
4
+
5
+ export function normalize ( data ) {
6
+ if ( has ( data , 'hydra:member' ) ) {
7
+ // Normalize items in collections
8
+ data [ 'hydra:member' ] = data [ 'hydra:member' ] . map ( item => normalize ( item ) ) ;
9
+
10
+ return data ;
11
+ }
12
+
13
+ // Flatten nested documents
14
+ return mapValues ( data , value =>
15
+ Array . isArray ( value )
16
+ ? value . map ( v => get ( v , '@id' , v ) )
17
+ : get ( value , '@id' , value )
18
+ ) ;
19
+ }
You can’t perform that action at this time.
0 commit comments