Skip to content

Commit

Permalink
Remove the query option for fetch()
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 30, 2022
1 parent e2c9751 commit 495fbba
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
6 changes: 0 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import Conf from 'conf';
import {Options} from 'got';

export interface FetchOptions extends Partial<Options> {
// Deprecated, but left for backwards-compatibility.
/**
URL search parameters.
*/
readonly query?: string | Record<string, string | number | boolean | undefined> | URLSearchParams | undefined;

/**
Number of milliseconds this request should be cached.
*/
Expand Down
19 changes: 10 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,26 @@ alfy.fetch = async (url, options) => {
...options,
};

const {transform, maxAge} = options;
delete options.transform;
delete options.maxAge;

// Deprecated, but left for backwards-compatibility.
// TODO: Remove this in 2024.
if (options.query) {
options.searchParams = options.query;
delete options.query;
throw new Error('The `query` option was renamed to `searchParams`.');
}

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

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

const rawKey = url + JSON.stringify(options);

// This must be below the cache key generation.
const {transform, maxAge} = options;
delete options.transform;
delete options.maxAge;

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

Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ You need [Node.js 14+](https://nodejs.org) and [Alfred 4](https://www.alfredapp.

## Install

```
$ npm install alfy
```sh
npm install alfy
```

## Usage
Expand Down Expand Up @@ -121,8 +121,8 @@ You can remove [these](https://github.com/samverschueren/alfred-link#infoplist)

After publishing your workflow to npm, your users can easily install or update the workflow.

```
$ npm install --global alfred-unicorn
```sh
npm install --global alfred-unicorn
```

> Tip: instead of manually updating every workflow yourself, use the [alfred-updater](https://github.com/SamVerschueren/alfred-updater) workflow to do that for you.
Expand Down Expand Up @@ -351,7 +351,7 @@ URL to fetch.

Type: `object`

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

###### json

Expand Down
4 changes: 2 additions & 2 deletions test/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ test('cache', async t => {
test('cache key', async t => {
const alfy = createAlfy();

t.deepEqual(await alfy.fetch(`${URL}/cache-key`, {query: {unicorn: 'rainbow'}, maxAge: 5000}), {unicorn: 'rainbow'});
t.truthy(alfy.cache.store['https://foo.bar/cache-key{"query":{"unicorn":"rainbow"},"maxAge":5000}']);
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}']);
});

test('invalid version', async t => {
Expand Down

0 comments on commit 495fbba

Please sign in to comment.