Skip to content

Commit 0458341

Browse files
authored
useFetch, useCachedPromise: Fix crash when passing an argument of type URLSearchParams as an option (#38)
1 parent f7fd65b commit 0458341

9 files changed

Lines changed: 28 additions & 11 deletions

File tree

docs/utils-reference/getting-started.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ npm install --save @raycast/utils
1616

1717
## Changelog
1818

19+
### v1.16.3
20+
21+
- Fix an issue where `URLSearchParams` couldn't be passed as an option to `useFetch` or `useCachedPromise`, causing extensions to crash.
22+
1923
### v1.16.2
2024

2125
- Fixed the refresh token flow to log out the user instead of throwing an error.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@raycast/utils",
3-
"version": "1.16.2",
3+
"version": "1.16.3",
44
"description": "Set of utilities to streamline building Raycast extensions",
55
"author": "Raycast Technologies Ltd.",
66
"homepage": "https://developers.raycast.com/utils-reference",

src/helpers.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import objectHash from "object-hash";
2+
13
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24
export function replacer(this: any, key: string, _value: unknown) {
35
const value = this[key];
@@ -19,3 +21,15 @@ export function reviver(_key: string, value: unknown) {
1921
}
2022
return value;
2123
}
24+
25+
export function hash(object: objectHash.NotUndefined, options?: objectHash.NormalOption): string {
26+
return objectHash(object, {
27+
replacer: (value): string => {
28+
if (value instanceof URLSearchParams) {
29+
return value.toString();
30+
}
31+
return value;
32+
},
33+
...options,
34+
});
35+
}

src/useCachedPromise.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useEffect, useRef, useCallback } from "react";
2-
import hash from "object-hash";
32
import {
43
FunctionReturningPromise,
54
UseCachedPromiseReturnType,
@@ -10,8 +9,8 @@ import {
109
} from "./types";
1110
import { useCachedState } from "./useCachedState";
1211
import { usePromise, PromiseOptions } from "./usePromise";
13-
1412
import { useLatest } from "./useLatest";
13+
import { hash } from "./helpers";
1514

1615
// Symbol to differentiate an empty cache from `undefined`
1716
const emptyCache = Symbol();

src/useFetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { useCallback, useMemo, useRef } from "react";
2-
import hash from "object-hash";
32
import { useCachedPromise, CachedPromiseOptions } from "./useCachedPromise";
43
import { useLatest } from "./useLatest";
54
import { FunctionReturningPaginatedPromise, FunctionReturningPromise, UseCachedPromiseReturnType } from "./types";
65
import { fetch } from "cross-fetch";
76
import { isJSON } from "./fetch-utils";
7+
import { hash } from "./helpers";
88

99
async function defaultParsing(response: Response) {
1010
if (!response.ok) {

src/useSQL.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { copyFile, mkdir, writeFile } from "node:fs/promises";
44
import os from "node:os";
55
import childProcess from "node:child_process";
66
import path from "node:path";
7-
import hash from "object-hash";
87
import { useRef, useState, useCallback, useMemo } from "react";
98
import { usePromise, PromiseOptions } from "./usePromise";
109
import { useLatest } from "./useLatest";
1110
import { getSpawnedPromise, getSpawnedResult } from "./exec-utils";
1211
import { showFailureToast } from "./showFailureToast";
12+
import { hash } from "./helpers";
1313

1414
/**
1515
* Executes a query on a local SQL database and returns the {@link AsyncState} corresponding to the query of the command. The last value will be kept between command runs.

src/useStreamJSON.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import StreamArray from "stream-json/streamers/StreamArray";
1212
import { isJSON } from "./fetch-utils";
1313
import { Flatten, FunctionReturningPaginatedPromise, UseCachedPromiseReturnType } from "./types";
1414
import { CachedPromiseOptions, useCachedPromise } from "./useCachedPromise";
15-
import objectHash from "object-hash";
15+
import { hash } from "./helpers";
1616

1717
async function cache(url: RequestInfo, destination: string, fetchOptions?: RequestInit) {
1818
if (typeof url === "object" || url.startsWith("http://") || url.startsWith("https://")) {
@@ -164,7 +164,7 @@ type Options<T> = {
164164
/**
165165
* The hook expects to iterate through an array of data, so by default, it assumes the JSON it receives itself represents an array. However, sometimes the array of data is wrapped in an object,
166166
* i.e. `{ "success": true, "data": […] }`, or even `{ "success": true, "results": { "data": […] } }`. In those cases, you can use `dataPath` to specify where the data array can be found.
167-
*
167+
*
168168
* @remark If your JSON object has multiple arrays that you want to stream data from, you can pass a regular expression to stream through all of them.
169169
*
170170
* @example For `{ "success": true, "data": […] }`, dataPath would be `data`
@@ -423,7 +423,7 @@ export function useStreamJSON<T, U extends any[] = any[]>(
423423
transform: ((item: unknown) => T) | undefined,
424424
) =>
425425
async ({ page }) => {
426-
const fileName = objectHash(url) + ".json";
426+
const fileName = hash(url) + ".json";
427427
const folder = environment.supportPath;
428428
if (page === 0) {
429429
controllerRef.current?.abort();

tests/src/fetch-paginated.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ActionPanel, Action, Icon, Image, List, Grid } from "@raycast/api";
1+
import { ActionPanel, Action, Icon, Image, List } from "@raycast/api";
22
import { useFetch } from "@raycast/utils";
33
import { useState } from "react";
44
import { setTimeout } from "timers/promises";

0 commit comments

Comments
 (0)