File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -137,9 +137,18 @@ alfy.fetch = async (url, options) => {
137
137
return cachedResponse ;
138
138
}
139
139
140
+ if ( 'json' in options && options . json === false ) {
141
+ delete options . json ;
142
+ options . responseType = 'text' ;
143
+ } else {
144
+ options . responseType = 'json' ;
145
+ }
146
+
147
+ options . resolveBodyOnly = true ;
148
+
140
149
let response ;
141
150
try {
142
- response = await got ( url , options ) . json ( ) ;
151
+ response = await got ( url , options ) ;
143
152
} catch ( error ) {
144
153
if ( cachedResponse ) {
145
154
return cachedResponse ;
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ test.before(() => {
13
13
nock ( URL ) . get ( '/cache-key?unicorn=rainbow' ) . reply ( 200 , { unicorn : 'rainbow' } ) ;
14
14
nock ( URL ) . get ( '/cache-version' ) . once ( ) . reply ( 200 , { foo : 'bar' } ) ;
15
15
nock ( URL ) . get ( '/cache-version' ) . twice ( ) . reply ( 200 , { unicorn : 'rainbow' } ) ;
16
+ nock ( URL ) . get ( '/string-response' ) . once ( ) . reply ( 200 , 'unicorn is rainbow' ) ;
16
17
} ) ;
17
18
18
19
test ( 'no cache' , async t => {
@@ -74,3 +75,8 @@ test('invalid version', async t => {
74
75
// t.deepEqual(await alfy3.fetch(`${URL}/cache-version`, {maxAge: 5000}), {unicorn: 'rainbow'});
75
76
// t.deepEqual(alfy.cache.store['https://foo.bar/cache-version{"maxAge":5000}'].data, {unicorn: 'rainbow'});
76
77
} ) ;
78
+
79
+ test ( 'non-JSON response' , async t => {
80
+ const alfy = createAlfy ( ) ;
81
+ t . is ( await alfy . fetch ( `${ URL } /string-response` , { json : false } ) , 'unicorn is rainbow' ) ;
82
+ } ) ;
You can’t perform that action at this time.
0 commit comments