Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

swr

Package Overview
Dependencies
Maintainers
7
Versions
164
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swr - npm Package Compare versions

Comparing version 2.1.5 to 2.2.0-beta.0

6

_internal/dist/index.d.ts

@@ -314,3 +314,3 @@ import react, { FC, PropsWithChildren, MutableRefObject } from 'react';

declare const mergeConfigs: (a: Partial<FullConfiguration>, b?: Partial<FullConfiguration>) => Partial<FullConfiguration<any, any, BareFetcher<unknown>>>;
declare const mergeConfigs: (a: Partial<FullConfiguration>, b?: Partial<FullConfiguration>) => Partial<FullConfiguration>;

@@ -321,3 +321,3 @@ type KeyFilter = (key?: Arguments) => boolean;

declare const normalize: <KeyType_1 = Key, Data = any>(args: [KeyType_1] | [KeyType_1, ((arg: string) => FetcherResponse<Data>) | ((arg: [any, ...unknown[]]) => FetcherResponse<Data>) | ((arg: readonly [any, ...unknown[]]) => FetcherResponse<Data>) | ((arg: Record<any, any>) => FetcherResponse<Data>) | ((arg: string | [any, ...unknown[]] | readonly [any, ...unknown[]] | Record<any, any>) => FetcherResponse<Data>) | null] | [KeyType_1, Partial<PublicConfiguration<any, any, BareFetcher<any>>> | undefined] | [KeyType_1, ((arg: string) => FetcherResponse<Data>) | ((arg: [any, ...unknown[]]) => FetcherResponse<Data>) | ((arg: readonly [any, ...unknown[]]) => FetcherResponse<Data>) | ((arg: Record<any, any>) => FetcherResponse<Data>) | ((arg: string | [any, ...unknown[]] | readonly [any, ...unknown[]] | Record<any, any>) => FetcherResponse<Data>) | null, Partial<PublicConfiguration<any, any, BareFetcher<any>>> | undefined]) => [KeyType_1, ((arg: string) => FetcherResponse<Data>) | ((arg: [any, ...unknown[]]) => FetcherResponse<Data>) | ((arg: readonly [any, ...unknown[]]) => FetcherResponse<Data>) | ((arg: Record<any, any>) => FetcherResponse<Data>) | ((arg: string | [any, ...unknown[]] | readonly [any, ...unknown[]] | Record<any, any>) => FetcherResponse<Data>) | null, Partial<Partial<PublicConfiguration<Data, any, BareFetcher<any>>>>];
declare const normalize: <KeyType_1 = Key, Data = any>(args: [KeyType_1] | [KeyType_1, Fetcher<Data> | null] | [KeyType_1, Partial<PublicConfiguration<any, any, BareFetcher<any>>> | undefined] | [KeyType_1, Fetcher<Data> | null, Partial<PublicConfiguration<any, any, BareFetcher<any>>> | undefined]) => [KeyType_1, Fetcher<Data> | null, Partial<Partial<PublicConfiguration<Data, any, BareFetcher<any>>>>];

@@ -349,4 +349,4 @@ declare const withArgs: <SWRType>(hook: any) => SWRType;

type PreloadFetcher<Data = unknown, SWRKey extends Key = Key> = SWRKey extends () => infer Arg ? (arg: Arg) => FetcherResponse<Data> : SWRKey extends infer Arg ? (arg: Arg) => FetcherResponse<Data> : never;
declare const preload: <Data = any, SWRKey extends Key = Key, Fetcher extends BareFetcher<unknown> = PreloadFetcher<Data, SWRKey>>(key_: SWRKey, fetcher: Fetcher) => ReturnType<Fetcher>;
declare const preload: <Data = any, SWRKey extends Key = Key, Fetcher extends BareFetcher = PreloadFetcher<Data, SWRKey>>(key_: SWRKey, fetcher: Fetcher) => ReturnType<Fetcher>;
export { Arguments, BareFetcher, BlockingData, Broadcaster, Cache, Fetcher, FetcherResponse, FullConfiguration, GlobalState, IS_REACT_LEGACY, IS_SERVER, InternalConfiguration, Key, KeyLoader, KeyedMutator, Middleware, Mutator, MutatorCallback, MutatorConfig, MutatorFn, MutatorOptions, MutatorWrapper, OBJECT, ProviderConfiguration, PublicConfiguration, RevalidateCallback, RevalidateEvent, Revalidator, RevalidatorOptions, SWRConfig, SWRConfiguration, SWRGlobalState, SWRHook, SWRResponse, ScopedMutator, State, StateDependencies, StrictTupleKey, UNDEFINED, cache, compare, createCacheHelper, defaultConfig, defaultConfigOptions, getTimestamp, hasRequestAnimationFrame, initCache, internalMutate, isDocumentDefined, isFunction, isUndefined, isWindowDefined, mergeConfigs, mergeObjects, mutate, noop, normalize, preload, preset, rAF, constants as revalidateEvents, serialize, slowConnection, stableHash, subscribeCallback, useIsomorphicLayoutEffect, useSWRConfig, useStateWithDeps, withArgs, withMiddleware };

@@ -1,7 +0,7 @@

import * as react from 'react';
import * as swr__internal from 'swr/_internal';
import { FullConfiguration, Key, SWRHook } from 'swr/_internal';
export { Arguments, BareFetcher, Cache, Fetcher, Key, KeyLoader, KeyedMutator, Middleware, MutatorCallback, MutatorOptions, Revalidator, RevalidatorOptions, SWRConfiguration, SWRHook, SWRResponse, State, mutate, preload, useSWRConfig } from 'swr/_internal';
import ReactExports from 'react';
declare const SWRConfig: react.FC<react.PropsWithChildren<{
declare const SWRConfig: ReactExports.FC<ReactExports.PropsWithChildren<{
value?: (Partial<swr__internal.PublicConfiguration<any, any, swr__internal.BareFetcher<any>>> & Partial<swr__internal.ProviderConfiguration> & {

@@ -8,0 +8,0 @@ provider?: ((cache: Readonly<swr__internal.Cache<any>>) => swr__internal.Cache<any>) | undefined;

@@ -1,2 +0,2 @@

import { useRef, useMemo, useCallback, useDebugValue } from 'react';
import ReactExports, { useRef, useMemo, useCallback, useDebugValue } from 'react';
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';

@@ -6,2 +6,22 @@ import { OBJECT, SWRConfig as SWRConfig$1, defaultConfig, withArgs, SWRGlobalState, serialize, createCacheHelper, isUndefined, getTimestamp, UNDEFINED, isFunction, revalidateEvents, internalMutate, useIsomorphicLayoutEffect, subscribeCallback, IS_SERVER, rAF, IS_REACT_LEGACY, mergeObjects } from 'swr/_internal';

/// <reference types="react/experimental" />
const use = ReactExports.use || ((promise)=>{
if (promise.status === 'pending') {
throw promise;
} else if (promise.status === 'fulfilled') {
return promise.value;
} else if (promise.status === 'rejected') {
throw promise.reason;
} else {
promise.status = 'pending';
promise.then((v)=>{
promise.status = 'fulfilled';
promise.value = v;
}, (e)=>{
promise.status = 'rejected';
promise.reason = e;
});
throw promise;
}
});
const WITH_DEDUPE = {

@@ -466,3 +486,7 @@ dedupe: true

unmountedRef.current = false;
throw isUndefined(error) ? revalidate(WITH_DEDUPE) : error;
if (isUndefined(error)) {
use(revalidate(WITH_DEDUPE));
} else {
throw error;
}
}

@@ -469,0 +493,0 @@ return {

Object.defineProperty(exports, '__esModule', { value: true });
var react = require('react');
var ReactExports = require('react');
var index_js = require('use-sync-external-store/shim/index.js');
var _internal = require('swr/_internal');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var ReactExports__default = /*#__PURE__*/_interopDefault(ReactExports);
/// <reference types="react/experimental" />
const use = ReactExports__default.default.use || ((promise)=>{
if (promise.status === 'pending') {
throw promise;
} else if (promise.status === 'fulfilled') {
return promise.value;
} else if (promise.status === 'rejected') {
throw promise.reason;
} else {
promise.status = 'pending';
promise.then((v)=>{
promise.status = 'fulfilled';
promise.value = v;
}, (e)=>{
promise.status = 'rejected';
promise.reason = e;
});
throw promise;
}
});
const WITH_DEDUPE = {

@@ -19,14 +43,14 @@ dedupe: true

// If it's the initial render of this hook.
const initialMountedRef = react.useRef(false);
const initialMountedRef = ReactExports.useRef(false);
// If the hook is unmounted already. This will be used to prevent some effects
// to be called after unmounting.
const unmountedRef = react.useRef(false);
const unmountedRef = ReactExports.useRef(false);
// Refs to keep the key and config.
const keyRef = react.useRef(key);
const fetcherRef = react.useRef(fetcher);
const configRef = react.useRef(config);
const keyRef = ReactExports.useRef(key);
const fetcherRef = ReactExports.useRef(fetcher);
const configRef = ReactExports.useRef(config);
const getConfig = ()=>configRef.current;
const isActive = ()=>getConfig().isVisible() && getConfig().isOnline();
const [getCache, setCache, subscribeCache, getInitialCache] = _internal.createCacheHelper(cache, key);
const stateDependencies = react.useRef({}).current;
const stateDependencies = ReactExports.useRef({}).current;
const fallback = _internal.isUndefined(fallbackData) ? config.fallback[key] : fallbackData;

@@ -53,3 +77,3 @@ const isEqual = (prev, current)=>{

};
const getSnapshot = react.useMemo(()=>{
const getSnapshot = ReactExports.useMemo(()=>{
const shouldStartRequest = (()=>{

@@ -121,3 +145,3 @@ if (!key) return false;

// Get the current state that SWR should return.
const cached = index_js.useSyncExternalStore(react.useCallback((callback)=>subscribeCache(key, (current, prev)=>{
const cached = index_js.useSyncExternalStore(ReactExports.useCallback((callback)=>subscribeCache(key, (current, prev)=>{
if (!isEqual(prev, current)) callback();

@@ -135,3 +159,3 @@ }), // eslint-disable-next-line react-hooks/exhaustive-deps

// Use a ref to store previously returned data. Use the initial data as its initial value.
const laggyDataRef = react.useRef(data);
const laggyDataRef = ReactExports.useRef(data);
const returnedData = keepPreviousData ? _internal.isUndefined(cachedData) ? laggyDataRef.current : cachedData : data;

@@ -163,3 +187,3 @@ // - Suspense mode and there's stale data for the initial render.

// `fetcher`, to correctly handle the many edge cases.
const revalidate = react.useCallback(async (revalidateOpts)=>{
const revalidate = ReactExports.useCallback(async (revalidateOpts)=>{
const currentFetcher = fetcherRef.current;

@@ -349,3 +373,3 @@ if (!key || !currentFetcher || unmountedRef.current || getConfig().isPaused()) {

// eslint-disable-next-line react-hooks/exhaustive-deps
const boundMutate = react.useCallback(// Use callback to make sure `keyRef.current` returns latest result every time
const boundMutate = ReactExports.useCallback(// Use callback to make sure `keyRef.current` returns latest result every time
(...args)=>{

@@ -456,3 +480,3 @@ return _internal.internalMutate(cache, keyRef.current, ...args);

// Display debug info in React DevTools.
react.useDebugValue(returnedData);
ReactExports.useDebugValue(returnedData);
// In Suspense mode, we can't return the empty `data` state.

@@ -473,3 +497,7 @@ // If there is an `error`, the `error` needs to be thrown to the error boundary.

unmountedRef.current = false;
throw _internal.isUndefined(error) ? revalidate(WITH_DEDUPE) : error;
if (_internal.isUndefined(error)) {
use(revalidate(WITH_DEDUPE));
} else {
throw error;
}
}

@@ -476,0 +504,0 @@ return {

@@ -7,4 +7,4 @@ import { Key, SWRResponse } from 'swr';

}>;
type MutationFetcher<Data = unknown, ExtraArg = unknown, SWRKey extends Key = Key> = SWRKey extends () => infer Arg | null | undefined | false ? (key: Arg, options: FetcherOptions<ExtraArg>) => FetcherResponse<Data> : SWRKey extends null | undefined | false ? never : SWRKey extends infer Arg ? (key: Arg, options: FetcherOptions<ExtraArg>) => FetcherResponse<Data> : never;
type SWRMutationConfiguration<Data, Error, ExtraArg = any, SWRMutationKey extends Key = Key, SWRData = any> = {
type MutationFetcher<Data = unknown, SWRKey extends Key = Key, ExtraArg = unknown> = SWRKey extends () => infer Arg | null | undefined | false ? (key: Arg, options: FetcherOptions<ExtraArg>) => FetcherResponse<Data> : SWRKey extends null | undefined | false ? never : SWRKey extends infer Arg ? (key: Arg, options: FetcherOptions<ExtraArg>) => FetcherResponse<Data> : never;
type SWRMutationConfiguration<Data, Error, SWRMutationKey extends Key = Key, ExtraArg = any, SWRData = any> = {
revalidate?: boolean;

@@ -14,8 +14,26 @@ populateCache?: boolean | ((result: Data, currentData: SWRData | undefined) => SWRData);

rollbackOnError?: boolean | ((error: unknown) => boolean);
throwOnError?: boolean;
fetcher?: MutationFetcher<Data, ExtraArg, SWRMutationKey>;
onSuccess?: (data: Data, key: string, config: Readonly<SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey, SWRData>>) => void;
onError?: (err: Error, key: string, config: Readonly<SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey, SWRData>>) => void;
fetcher?: MutationFetcher<Data, SWRMutationKey, ExtraArg>;
onSuccess?: (data: Data, key: string, config: Readonly<SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg, SWRData>>) => void;
onError?: (err: Error, key: string, config: Readonly<SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg, SWRData>>) => void;
};
interface SWRMutationResponse<Data = any, Error = any, ExtraArg = never, SWRMutationKey extends Key = Key> extends Pick<SWRResponse<Data, Error>, 'data' | 'error'> {
type RemoveUndefined<T> = T extends undefined ? never : T;
interface TriggerWithArgs<Data = any, Error = any, SWRMutationKey extends Key = Key, ExtraArg = never> {
<SWRData = Data>(extraArgument: ExtraArg, options?: SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg, SWRData>): Promise<Data>;
<SWRData = Data>(extraArgument: ExtraArg, options?: SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg, SWRData> & {
throwOnError: true;
}): Promise<RemoveUndefined<Data>>;
<SWRData = Data>(extraArgument: ExtraArg, options?: SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg, SWRData> & {
throwOnError: false;
}): Promise<Data | undefined>;
}
interface TriggerWithoutArgs<Data = any, Error = any, SWRMutationKey extends Key = Key, ExtraArg = never> {
<SWRData = Data>(extraArgument?: null, options?: SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg, SWRData>): Promise<Data>;
<SWRData = Data>(extraArgument?: null, options?: SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg, SWRData> & {
throwOnError: true;
}): Promise<RemoveUndefined<Data>>;
<SWRData = Data>(extraArgument?: null, options?: SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg, SWRData> & {
throwOnError: false;
}): Promise<Data>;
}
interface SWRMutationResponse<Data = any, Error = any, SWRMutationKey extends Key = Key, ExtraArg = never> extends Pick<SWRResponse<Data, Error>, 'data' | 'error'> {
/**

@@ -29,3 +47,3 @@ * Indicates if the mutation is in progress.

*/
trigger: [ExtraArg] extends [never] ? <SWRData = Data>(extraArgument?: null, options?: SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey, SWRData>) => Promise<Data | undefined> : <SWRData = Data>(extraArgument: ExtraArg, options?: SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey, SWRData>) => Promise<Data | undefined>;
trigger: [ExtraArg] extends [never] ? TriggerWithoutArgs<Data, Error, ExtraArg, SWRMutationKey> : TriggerWithArgs<Data, Error, SWRMutationKey, ExtraArg>;
/**

@@ -36,25 +54,77 @@ * Function to reset the mutation state (`data`, `error`, and `isMutating`).

}
type SWRMutationHook = <Data = any, Error = any, SWRMutationKey extends Key = Key, ExtraArg = never>(
/**
* The key of the resource that will be mutated. It should be the same key
* used in the `useSWR` hook so SWR can handle revalidation and race
* conditions for that resource.
*/
key: SWRMutationKey,
/**
* The function to trigger the mutation that accepts the key, extra argument
* and options. For example:
*
* ```jsx
* (api, data) => fetch(api, {
* method: 'POST',
* body: JSON.stringify(data)
* })
* ```
*/
fetcher: MutationFetcher<Data, ExtraArg, SWRMutationKey>,
/**
* Extra options for the mutation hook.
*/
options?: SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey>) => SWRMutationResponse<Data, Error, ExtraArg, SWRMutationKey>;
interface SWRMutationHook {
<Data = any, Error = any, SWRMutationKey extends Key = Key, ExtraArg = never>(
/**
* The key of the resource that will be mutated. It should be the same key
* used in the `useSWR` hook so SWR can handle revalidation and race
* conditions for that resource.
*/
key: SWRMutationKey,
/**
* The function to trigger the mutation that accepts the key, extra argument
* and options. For example:
*
* ```jsx
* (api, data) => fetch(api, {
* method: 'POST',
* body: JSON.stringify(data)
* })
* ```
*/
fetcher: MutationFetcher<Data, SWRMutationKey, ExtraArg>,
/**
* Extra options for the mutation hook.
*/
options?: SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg>): SWRMutationResponse<Data, Error, SWRMutationKey, ExtraArg>;
<Data = any, Error = any, SWRMutationKey extends Key = Key, ExtraArg = never>(
/**
* The key of the resource that will be mutated. It should be the same key
* used in the `useSWR` hook so SWR can handle revalidation and race
* conditions for that resource.
*/
key: SWRMutationKey,
/**
* The function to trigger the mutation that accepts the key, extra argument
* and options. For example:
*
* ```jsx
* (api, data) => fetch(api, {
* method: 'POST',
* body: JSON.stringify(data)
* })
* ```
*/
fetcher: MutationFetcher<Data, SWRMutationKey, ExtraArg>,
/**
* Extra options for the mutation hook.
*/
options?: SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg> & {
throwOnError: false;
}): SWRMutationResponse<Data | undefined, Error, SWRMutationKey, ExtraArg>;
<Data = any, Error = any, SWRMutationKey extends Key = Key, ExtraArg = never>(
/**
* The key of the resource that will be mutated. It should be the same key
* used in the `useSWR` hook so SWR can handle revalidation and race
* conditions for that resource.
*/
key: SWRMutationKey,
/**
* The function to trigger the mutation that accepts the key, extra argument
* and options. For example:
*
* ```jsx
* (api, data) => fetch(api, {
* method: 'POST',
* body: JSON.stringify(data)
* })
* ```
*/
fetcher: MutationFetcher<Data, SWRMutationKey, ExtraArg>,
/**
* Extra options for the mutation hook.
*/
options?: SWRMutationConfiguration<Data, Error, SWRMutationKey, ExtraArg> & {
throwOnError: true;
}): SWRMutationResponse<Data, Error, SWRMutationKey, ExtraArg>;
}

@@ -61,0 +131,0 @@ /**

{
"name": "swr",
"version": "2.1.5",
"version": "2.2.0-beta.0",
"description": "React Hooks library for remote data fetching",

@@ -13,3 +13,3 @@ "keywords": [

],
"packageManager": "pnpm@7.26.1",
"packageManager": "pnpm@8.4.0",
"main": "./core/dist/index.js",

@@ -76,3 +76,3 @@ "module": "./core/dist/index.esm.js",

"prepare": "husky install",
"csb:install": "pnpm install",
"csb:install": "corepack enable && corepack pnpm i",
"csb:build": "pnpm build",

@@ -93,2 +93,3 @@ "clean": "pnpm -r run clean && rimraf playwright-report test-result",

"test": "jest",
"test:build": "jest --config jest.config.build.js",
"test:e2e": "playwright test",

@@ -104,32 +105,32 @@ "run-all-checks": "pnpm types:check && pnpm lint && pnpm test && pnpm test-typing"

"devDependencies": {
"@playwright/test": "^1.30.0",
"@swc/core": "^1.3.29",
"@swc/jest": "0.2.24",
"@playwright/test": "^1.33.0",
"@swc/core": "^1.3.56",
"@swc/jest": "0.2.26",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@type-challenges/utils": "0.1.1",
"@types/jest": "^29.4.0",
"@types/node": "^18.11.18",
"@types/react": "^18.0.38",
"@types/jest": "^29.5.1",
"@types/node": "^18.16.3",
"@types/react": "^18.2.5",
"@types/use-sync-external-store": "^0.0.3",
"@typescript-eslint/eslint-plugin": "5.49.0",
"@typescript-eslint/parser": "5.49.0",
"@typescript-eslint/eslint-plugin": "5.59.2",
"@typescript-eslint/parser": "5.59.2",
"bunchee": "3.1.1",
"eslint": "8.32.0",
"eslint-config-prettier": "8.6.0",
"eslint": "8.39.0",
"eslint-config-prettier": "8.8.0",
"eslint-plugin-jest-dom": "4.0.3",
"eslint-plugin-react": "7.32.1",
"eslint-plugin-react": "7.32.2",
"eslint-plugin-react-hooks": "4.6.0",
"eslint-plugin-testing-library": "5.10.0",
"eslint-plugin-testing-library": "5.10.3",
"husky": "8.0.3",
"jest": "29.4.1",
"jest-environment-jsdom": "29.4.1",
"lint-staged": "13.1.0",
"next": "^13.1.6",
"prettier": "2.8.3",
"jest": "29.5.0",
"jest-environment-jsdom": "29.5.0",
"lint-staged": "13.2.2",
"next": "^13.3.4",
"prettier": "2.8.8",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "4.1.2",
"rimraf": "5.0.0",
"swr": "workspace:*",
"typescript": "4.9.4"
"typescript": "5.0.4"
},

@@ -136,0 +137,0 @@ "peerDependencies": {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc