Skip to content

Commit

Permalink
Add resolveBodyOnly option to alfy.fetch (#163)
Browse files Browse the repository at this point in the history
Co-authored-by: Grant Sander <gksander93@gmail.com>
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
3 people committed Dec 12, 2022
1 parent a47cc3e commit 522fa7b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ alfy.cache = new CacheConf({

alfy.fetch = async (url, options) => {
options = {
resolveBodyOnly: true,
...options,
};

Expand Down Expand Up @@ -150,8 +151,6 @@ alfy.fetch = async (url, options) => {
options.responseType = 'json';
}

options.resolveBodyOnly = true;

let response;
try {
response = await got(url, options);
Expand Down
44 changes: 43 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,39 @@ Type: `number`

Number of milliseconds this request should be cached.

###### resolveBodyOnly

Type: `boolean`\
Default: `true`

Whether to resolve with only body or a full response.

```js
import alfy from 'alfy';

await alfy.fetch('https://api.foo.com');
//=> {foo: 'bar'}

await alfy.fetch('https://api.foo.com', {
resolveBodyOnly: false
});
/*
{
body: {
foo: 'bar'
},
headers: {
'content-type': 'application/json'
}
}
*/
```

###### transform

Type: `Function`

Transform the response before it gets cached.
Transform the response body before it gets cached.

```js
import alfy from 'alfy';
Expand All @@ -383,6 +411,20 @@ await alfy.fetch('https://api.foo.com', {
})
```

Transform the response.

```js
import alfy from 'alfy';

await alfy.fetch('https://api.foo.com', {
resolveBodyOnly: false,
transform: response => {
response.body.foo = 'bar';
return response;
}
})
```

You can also return a Promise.

```js
Expand Down
34 changes: 32 additions & 2 deletions test/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ test.before(() => {
nock(URL).get('/cache-version').once().reply(200, {foo: 'bar'});
nock(URL).get('/cache-version').twice().reply(200, {unicorn: 'rainbow'});
nock(URL).get('/string-response').once().reply(200, 'unicorn is rainbow');
nock(URL).get('/transform-full-response').once().reply(200, {foo: 'bar'}, {'accept-encoding': 'gzip'});
nock(URL).get('/resolve-full-response').once().reply(200, {foo: 'bar'}, {'accept-encoding': 'gzip'});
});

test('no cache', async t => {
Expand Down Expand Up @@ -42,6 +44,27 @@ test('transform', async t => {
});
});

test('transform full response', async t => {
const alfy = createAlfy();
const result = await alfy.fetch(`${URL}/transform-full-response`, {
resolveBodyOnly: false,
transform(response) {
response.body.unicorn = 'rainbow';
response.headers['accept-encoding'] = 'br';
return response;
},
});

t.deepEqual(result.headers, {
'content-type': 'application/json',
'accept-encoding': 'br',
});
t.deepEqual(result.body, {
foo: 'bar',
unicorn: 'rainbow',
});
});

test('cache', async t => {
const alfy = createAlfy();

Expand All @@ -57,7 +80,7 @@ test('cache key', async t => {
const alfy = createAlfy();

t.deepEqual(await alfy.fetch(`${URL}/cache-key`, {searchParams: {unicorn: 'rainbow'}, maxAge: 5000}), {unicorn: 'rainbow'});
t.truthy(alfy.cache.store['https://foo.bar/cache-key{"searchParams":{"unicorn":"rainbow"},"maxAge":5000}']);
t.truthy(alfy.cache.store['https://foo.bar/cache-key{"resolveBodyOnly":true,"searchParams":{"unicorn":"rainbow"},"maxAge":5000}']);
});

test('invalid version', async t => {
Expand All @@ -66,7 +89,7 @@ test('invalid version', async t => {
const alfy = createAlfy({cache, version: '1.0.0'});
t.deepEqual(await alfy.fetch(`${URL}/cache-version`, {maxAge: 5000}), {foo: 'bar'});
t.deepEqual(await alfy.fetch(`${URL}/cache-version`, {maxAge: 5000}), {foo: 'bar'});
t.deepEqual(alfy.cache.store['https://foo.bar/cache-version{"maxAge":5000}'].data, {foo: 'bar'});
t.deepEqual(alfy.cache.store['https://foo.bar/cache-version{"resolveBodyOnly":true,"maxAge":5000}'].data, {foo: 'bar'});

const alfy2 = createAlfy({cache, version: '1.0.0'});
t.deepEqual(await alfy2.fetch(`${URL}/cache-version`, {maxAge: 5000}), {foo: 'bar'});
Expand All @@ -80,3 +103,10 @@ test('non-JSON response', async t => {
const alfy = createAlfy();
t.is(await alfy.fetch(`${URL}/string-response`, {json: false}), 'unicorn is rainbow');
});

test('resolve full response', async t => {
const alfy = createAlfy();
const result = await alfy.fetch(`${URL}/resolve-full-response`, {resolveBodyOnly: false});
t.deepEqual(result.headers, {'content-type': 'application/json', 'accept-encoding': 'gzip'});
t.deepEqual(result.body, {foo: 'bar'});
});

0 comments on commit 522fa7b

Please sign in to comment.