Skip to content

Commit

Permalink
Fix all options not being passed to the got request library
Browse files Browse the repository at this point in the history
Fixes #151
  • Loading branch information
sindresorhus committed Mar 16, 2022
1 parent 1b3e067 commit 0fcf1dc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Conf from 'conf';
import {Options} from 'got';

export interface FetchOptions extends Options {
// Deprecated, but left for backwards-compatibility.
/**
URL search parameters.
*/
Expand Down
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,30 @@ alfy.fetch = async (url, options) => {
...options,
};

// Deprecated, but left for backwards-compatibility.
if (options.query) {
options.searchParams = options.query;
}

if (typeof url !== 'string') {
return Promise.reject(new TypeError(`Expected \`url\` to be a \`string\`, got \`${typeof url}\``));
throw new TypeError(`Expected \`url\` to be a \`string\`, got \`${typeof url}\``);
}

if (options.transform && typeof options.transform !== 'function') {
return Promise.reject(new TypeError(`Expected \`transform\` to be a \`function\`, got \`${typeof options.transform}\``));
throw new TypeError(`Expected \`transform\` to be a \`function\`, got \`${typeof options.transform}\``);
}

const rawKey = url + JSON.stringify(options);
const key = rawKey.replace(/\./g, '\\.');
const cachedResponse = alfy.cache.get(key, {ignoreMaxAge: true});

if (cachedResponse && !alfy.cache.isExpired(key)) {
return Promise.resolve(cachedResponse);
return cachedResponse;
}

let response;
try {
response = await got(url, {searchParams: options.query}).json();
response = await got(url, options).json();
} catch (error) {
if (cachedResponse) {
return cachedResponse;
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ URL to fetch.

Type: `object`

Any of the [`got` options](https://github.com/sindresorhus/got#options).
Any of the [`got` options](https://github.com/sindresorhus/got/tree/v11.8.3#api) and the below options.

###### json

Expand Down

0 comments on commit 0fcf1dc

Please sign in to comment.