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

swr-devtools

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swr-devtools - npm Package Compare versions

Comparing version 0.4.0 to 0.5.0

19

cjs/swr-cache.d.ts
import { Cache } from "swr";
export declare type SWRCacheData = {
id: number;
export declare type DevToolsCacheData = {
key: string;
data: any;
isValidating: boolean;
error: string;
data?: unknown;
isValidating?: boolean;
isLoading?: boolean;
error?: unknown;
timestamp: Date;
timestampString: string;
isInfinite?: boolean;
infiniteKey?: string;
};
export declare const injectSWRCache: (cache: Cache, watcher: (key: string, value: any) => void) => void;
export declare const isMetaCache: (key: string) => boolean;
export declare const isInfiniteCache: (key: string) => boolean;
export declare const getInfiniteCacheKey: (key: string) => string;
export declare const convertToDevToolsCacheData: (key: string, value: any) => {
key: string;
value: Partial<DevToolsCacheData>;
};
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getInfiniteCacheKey = exports.isInfiniteCache = exports.isMetaCache = exports.injectSWRCache = void 0;
exports.convertToDevToolsCacheData = exports.injectSWRCache = void 0;
var injectSWRCache = function (cache, watcher) {

@@ -8,3 +19,5 @@ // intercept operations modifying the cache store

cache.set = function (key, value) {
watcher(key, value);
if (!isMetaCache(key)) {
watcher(key, value);
}
return originalSet.call(cache, key, value);

@@ -14,3 +27,5 @@ };

cache.delete = function (key) {
watcher(key, undefined);
if (!isMetaCache(key)) {
watcher(key, undefined);
}
return originalDelete.call(cache, key);

@@ -21,15 +36,72 @@ };

var isMetaCache = function (key) {
return /^\$(?:req|err|ctx|len)\$/.test(key);
return /^\$(?:ctx|len)\$/.test(key);
};
exports.isMetaCache = isMetaCache;
var isErrorCache = function (key) {
return /^\$err\$/.test(key);
};
var isInfiniteCache = function (key) {
return /^\$inf\$/.test(key);
return /\$inf\$/.test(key);
};
exports.isInfiniteCache = isInfiniteCache;
var getInfiniteCacheKey = function (key) {
var isIsValidatingCache = function (key) {
return /^\$req\$/.test(key);
};
var filterMetaCacheKey = function (key) {
var _a, _b;
var match = key.match(/^\$inf\$(?<cacheKey>.*)?/);
var match = key.match(/^(?:\$(?:req|swr|err)\$)?(?:\$inf\$)(?<cacheKey>.*)?/);
return (_b = (_a = match === null || match === void 0 ? void 0 : match.groups) === null || _a === void 0 ? void 0 : _a.cacheKey) !== null && _b !== void 0 ? _b : key;
};
exports.getInfiniteCacheKey = getInfiniteCacheKey;
var isV2CacheData = function (data) {
return "isValidating" in data && "isLoading" in data;
};
var isV1MetaCache = function (key) {
return /^\$swr\$/.test(key);
};
// refs. https://github.com/koba04/swr-devtools/issues/48
var convertToDevToolsCacheData = function (key, value) {
var isInfinite = isInfiniteCache(key);
var infiniteKey = isInfinite ? filterMetaCacheKey(key) : undefined;
if (value !== undefined &&
typeof value === "object" &&
isV2CacheData(value)) {
// SWR v2
return {
key: key,
value: __assign(__assign({}, value), { isInfinite: isInfinite, infiniteKey: infiniteKey }),
};
}
else if (value !== undefined && isV1MetaCache(key)) {
// SWR ^1.3.0 ($swr$ cache key)
return {
key: key.replace("$swr$", ""),
value: __assign(__assign({}, value), { isInfinite: isInfinite, infiniteKey: infiniteKey }),
};
}
else if (isErrorCache(key)) {
// SWR <1.3.0
return {
key: key.replace("$err$", ""),
value: {
error: value,
isInfinite: isInfinite,
infiniteKey: infiniteKey,
},
};
}
else if (isIsValidatingCache(key)) {
// SWR <1.3.0
return {
key: key.replace("$req$", ""),
value: {
isValidating: value,
isInfinite: isInfinite,
infiniteKey: infiniteKey,
},
};
}
return {
key: key,
value: { data: value, isInfinite: isInfinite },
};
};
exports.convertToDevToolsCacheData = convertToDevToolsCacheData;
//# sourceMappingURL=swr-cache.js.map
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {

@@ -27,7 +38,8 @@ if (k2 === undefined) k2 = k;

var injected = new WeakSet();
// TOOD: we have to support more types
var convertToSerializableObject = function (value) {
return value instanceof Error ? { message: value.message } : value;
};
var inject = function (cache) {
return (0, swr_cache_1.injectSWRCache)(cache, function (key, value) {
if ((0, swr_cache_1.isMetaCache)(key)) {
return;
}
window.postMessage({

@@ -37,3 +49,6 @@ type: "updated_swr_cache",

key: key,
value: value,
value: Object.keys(value).reduce(function (acc, cacheKey) {
var _a;
return (__assign(__assign({}, acc), (_a = {}, _a[cacheKey] = convertToSerializableObject(value[cacheKey]), _a)));
}, {}),
},

@@ -40,0 +55,0 @@ }, "*");

import { Cache } from "swr";
export declare type SWRCacheData = {
id: number;
export declare type DevToolsCacheData = {
key: string;
data: any;
isValidating: boolean;
error: string;
data?: unknown;
isValidating?: boolean;
isLoading?: boolean;
error?: unknown;
timestamp: Date;
timestampString: string;
isInfinite?: boolean;
infiniteKey?: string;
};
export declare const injectSWRCache: (cache: Cache, watcher: (key: string, value: any) => void) => void;
export declare const isMetaCache: (key: string) => boolean;
export declare const isInfiniteCache: (key: string) => boolean;
export declare const getInfiniteCacheKey: (key: string) => string;
export declare const convertToDevToolsCacheData: (key: string, value: any) => {
key: string;
value: Partial<DevToolsCacheData>;
};

@@ -0,1 +1,12 @@

var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
export var injectSWRCache = function (cache, watcher) {

@@ -5,3 +16,5 @@ // intercept operations modifying the cache store

cache.set = function (key, value) {
watcher(key, value);
if (!isMetaCache(key)) {
watcher(key, value);
}
return originalSet.call(cache, key, value);

@@ -11,17 +24,78 @@ };

cache.delete = function (key) {
watcher(key, undefined);
if (!isMetaCache(key)) {
watcher(key, undefined);
}
return originalDelete.call(cache, key);
};
};
export var isMetaCache = function (key) {
return /^\$(?:req|err|ctx|len)\$/.test(key);
var isMetaCache = function (key) {
return /^\$(?:ctx|len)\$/.test(key);
};
export var isInfiniteCache = function (key) {
return /^\$inf\$/.test(key);
var isErrorCache = function (key) {
return /^\$err\$/.test(key);
};
export var getInfiniteCacheKey = function (key) {
var isInfiniteCache = function (key) {
return /\$inf\$/.test(key);
};
var isIsValidatingCache = function (key) {
return /^\$req\$/.test(key);
};
var filterMetaCacheKey = function (key) {
var _a, _b;
var match = key.match(/^\$inf\$(?<cacheKey>.*)?/);
var match = key.match(/^(?:\$(?:req|swr|err)\$)?(?:\$inf\$)(?<cacheKey>.*)?/);
return (_b = (_a = match === null || match === void 0 ? void 0 : match.groups) === null || _a === void 0 ? void 0 : _a.cacheKey) !== null && _b !== void 0 ? _b : key;
};
var isV2CacheData = function (data) {
return "isValidating" in data && "isLoading" in data;
};
var isV1MetaCache = function (key) {
return /^\$swr\$/.test(key);
};
// refs. https://github.com/koba04/swr-devtools/issues/48
export var convertToDevToolsCacheData = function (key, value) {
var isInfinite = isInfiniteCache(key);
var infiniteKey = isInfinite ? filterMetaCacheKey(key) : undefined;
if (value !== undefined &&
typeof value === "object" &&
isV2CacheData(value)) {
// SWR v2
return {
key: key,
value: __assign(__assign({}, value), { isInfinite: isInfinite, infiniteKey: infiniteKey }),
};
}
else if (value !== undefined && isV1MetaCache(key)) {
// SWR ^1.3.0 ($swr$ cache key)
return {
key: key.replace("$swr$", ""),
value: __assign(__assign({}, value), { isInfinite: isInfinite, infiniteKey: infiniteKey }),
};
}
else if (isErrorCache(key)) {
// SWR <1.3.0
return {
key: key.replace("$err$", ""),
value: {
error: value,
isInfinite: isInfinite,
infiniteKey: infiniteKey,
},
};
}
else if (isIsValidatingCache(key)) {
// SWR <1.3.0
return {
key: key.replace("$req$", ""),
value: {
isValidating: value,
isInfinite: isInfinite,
infiniteKey: infiniteKey,
},
};
}
return {
key: key,
value: { data: value, isInfinite: isInfinite },
};
};
//# sourceMappingURL=swr-cache.js.map

@@ -0,10 +1,22 @@

var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import React, { useLayoutEffect } from "react";
import { useSWRConfig, SWRConfig } from "swr";
import { injectSWRCache, isMetaCache } from "./swr-cache";
import { injectSWRCache } from "./swr-cache";
var injected = new WeakSet();
// TOOD: we have to support more types
var convertToSerializableObject = function (value) {
return value instanceof Error ? { message: value.message } : value;
};
var inject = function (cache) {
return injectSWRCache(cache, function (key, value) {
if (isMetaCache(key)) {
return;
}
window.postMessage({

@@ -14,3 +26,6 @@ type: "updated_swr_cache",

key: key,
value: value,
value: Object.keys(value).reduce(function (acc, cacheKey) {
var _a;
return (__assign(__assign({}, acc), (_a = {}, _a[cacheKey] = convertToSerializableObject(value[cacheKey]), _a)));
}, {}),
},

@@ -17,0 +32,0 @@ }, "*");

{
"name": "swr-devtools",
"version": "0.4.0",
"version": "0.5.0",
"description": "A React component for SWR DevTools",

@@ -48,4 +48,4 @@ "main": "cjs/index.js",

"peerDependencies": {
"react": "^17.0.2",
"swr": "^1.0.0"
"react": ">=17.0.2",
"swr": ">=1.0.0"
},

@@ -55,6 +55,5 @@ "devDependencies": {

"react": "^17.0.2",
"swr": "^1.0.0",
"swr": "^2.0.0-beta.2",
"typescript": "^4.1.3"
},
"gitHead": "8f010ec2a531c9fd34a55f269c4e3e767cd1620c"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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