clear-react-router 1.8.5 → 1.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -584,6 +584,14 @@ await invalidate('/posts', { withBeforeLoad: true });
584
584
  await invalidate(['/posts', '/users'], { withBeforeLoad: true });
585
585
  ```
586
586
 
587
+ #### Returns
588
+
589
+ An array of objects with the following structure:
590
+ ```ts
591
+ { path: string; data: unknown; error: unknown }
592
+ ```
593
+ Each object represents a revalidated route, where `path` is the route pathname, `data` is the revalidated loader result, and `error` is the loader error, if any.
594
+
587
595
  #### Notes
588
596
 
589
597
  * **Only routes that already have cached data are revalidated.**
@@ -1,18 +1,16 @@
1
- import { type CSSProperties, ReactNode } from 'react';
1
+ import { type CSSProperties, ReactNode, ComponentPropsWithoutRef } from 'react';
2
2
  import { RouterProps } from '../types';
3
- type LinkProps = {
3
+ type States = {
4
+ isActive: boolean;
5
+ isPending: boolean;
6
+ };
7
+ type LinkProps = Omit<ComponentPropsWithoutRef<'a'>, 'href' | 'className' | 'style'> & {
4
8
  to: string;
5
9
  children: ReactNode;
6
10
  prefetch?: RouterProps['prefetch'];
7
11
  hoverPrefetchDelay?: number;
8
- style?: CSSProperties | (({ isActive }: {
9
- isActive: boolean;
10
- isPending: boolean;
11
- }) => CSSProperties);
12
- className?: string | (({ isActive }: {
13
- isActive: boolean;
14
- isPending: boolean;
15
- }) => string);
12
+ style?: CSSProperties | ((arg: States) => CSSProperties);
13
+ className?: string | ((arg: States) => string);
16
14
  activeClassName?: string;
17
15
  pendingClassName?: string;
18
16
  onClick?(): void;
@@ -1 +1 @@
1
- export declare const useInvalidate: () => (pathList?: string | string[], options?: import("../types").InvalidateOptions) => Promise<void>;
1
+ export declare const useInvalidate: () => (pathList?: string | string[], options?: import("../types").InvalidateOptions) => Promise<import("../types").InvalidateResult[]>;
package/dist/index.js CHANGED
@@ -330,27 +330,30 @@ var createInvalidate = ({ routeItemDataState, loaderStateRef, timestampMap, curr
330
330
  beforeLoadError: error
331
331
  }));
332
332
  }
333
- await revalidateCache({
333
+ const result = await revalidateCache({
334
334
  routeItem,
335
335
  pathname
336
336
  });
337
337
  if (pathname === routePathname) currentLoaderState.setState(loaderStateRef.value);
338
+ return {
339
+ path: pathname,
340
+ ...result
341
+ };
338
342
  };
339
343
  const invalidateItem = async (pathname, options) => {
340
344
  const routeItem = findRoute(pathname);
341
- if (!routeItem) return;
345
+ if (!routeItem) return [];
342
346
  const pathnameArray = [];
343
347
  for (const [key] of timestampMap) if (comparePaths(routeItem, key)) pathnameArray.push(key);
344
- await Promise.all(pathnameArray.map((pathname) => invalidatePath(routeItem, pathname, options)));
345
- if (options?.withChildren && routeItem.children?.length) {
346
- const childPathList = routeItem.children.map((el) => el.path);
347
- await Promise.all(childPathList.map((el) => invalidateItem(`${pathname}${el}`, options)));
348
- }
348
+ const currentResults = await Promise.all(pathnameArray.map((pathname) => invalidatePath(routeItem, pathname, options)));
349
+ if (!options?.withChildren || !routeItem.children?.length) return currentResults;
350
+ const childResults = await Promise.all(routeItem.children.map((child) => invalidateItem(`${pathname}${child.path}`, options)));
351
+ return [...currentResults, ...childResults.flat()];
349
352
  };
350
353
  return async (pathList, options) => {
351
354
  const routePathname = routeItemDataState.getState().location.pathname;
352
355
  const pathnameList = Array.isArray(pathList) ? pathList : pathList ? [pathList] : [routePathname];
353
- await Promise.all(pathnameList.map((pathname) => invalidateItem(pathname, options)));
356
+ return (await Promise.all(pathnameList.map((pathname) => invalidateItem(pathname, options)))).flat();
354
357
  };
355
358
  };
356
359
  //#endregion
@@ -393,10 +396,7 @@ var createRevalidateCache = (routerState) => {
393
396
  if (isCacheItemFresh({
394
397
  routeItem,
395
398
  pathname
396
- })) {
397
- loaderStateRef.set(loaderMapRef[pathname]);
398
- return;
399
- }
399
+ })) loaderStateRef.set(loaderMapRef[pathname]);
400
400
  const promise = (async () => {
401
401
  if (!routeItem?.loader) return;
402
402
  try {
@@ -415,6 +415,10 @@ var createRevalidateCache = (routerState) => {
415
415
  loaderError: null
416
416
  }));
417
417
  loaderMapRef[pathname] = loaderStateRef.value;
418
+ return {
419
+ data: result,
420
+ error: null
421
+ };
418
422
  } catch (error) {
419
423
  const retry = getRetry(routeItem);
420
424
  if (retry && retry.count > retried) {
@@ -424,11 +428,21 @@ var createRevalidateCache = (routerState) => {
424
428
  routeItem,
425
429
  pathname
426
430
  }, retried + 1);
427
- } else loaderStateRef.set((prev) => ({
428
- ...prev,
429
- data: null,
430
- loaderError: error
431
- }));
431
+ return {
432
+ data: null,
433
+ error
434
+ };
435
+ } else {
436
+ loaderStateRef.set((prev) => ({
437
+ ...prev,
438
+ data: null,
439
+ loaderError: error
440
+ }));
441
+ return {
442
+ data: null,
443
+ error
444
+ };
445
+ }
432
446
  } finally {
433
447
  loadingPromises.delete(pathname);
434
448
  }
@@ -1,2 +1,2 @@
1
- import { type InvalidateOptions, RevalidateCache, RouterState } from '../types';
2
- export declare const createInvalidate: ({ routeItemDataState, loaderStateRef, timestampMap, currentLoaderState, contextState }: RouterState, revalidateCache: RevalidateCache) => (pathList?: string | string[], options?: InvalidateOptions) => Promise<void>;
1
+ import { type InvalidateOptions, InvalidateResult, RevalidateCache, RouterState } from '../types';
2
+ export declare const createInvalidate: ({ routeItemDataState, loaderStateRef, timestampMap, currentLoaderState, contextState }: RouterState, revalidateCache: RevalidateCache) => (pathList?: string | string[], options?: InvalidateOptions) => Promise<InvalidateResult[]>;
package/dist/types.d.ts CHANGED
@@ -34,7 +34,7 @@ export type ClientRouteItem = {
34
34
  actions?: (arg: {
35
35
  context: Record<string, unknown>;
36
36
  params: Record<string, string>;
37
- invalidate: (path?: string) => Promise<void>;
37
+ invalidate: (path?: string) => Promise<InvalidateResult[]>;
38
38
  setContext: Dispatch<SetStateAction<Record<string, unknown>>>;
39
39
  }) => Record<string, (arg: FormData) => Promise<unknown> | Promise<void> | void | unknown>;
40
40
  };
@@ -110,7 +110,7 @@ export type RouterType = {
110
110
  state: Omit<RouterState, 'loaderStateRef' | 'timestampMap'>;
111
111
  runtime: {
112
112
  navigate(arg: Location): Promise<void>;
113
- invalidate(pathList?: string | string[], options?: InvalidateOptions): Promise<void>;
113
+ invalidate(pathList?: string | string[], options?: InvalidateOptions): Promise<InvalidateResult[]>;
114
114
  prefetch(pathname: string): Promise<void>;
115
115
  };
116
116
  hooks: {
@@ -128,7 +128,7 @@ export type RouterType = {
128
128
  useNavigate: () => (arg: Location | string | -1) => Promise<void>;
129
129
  useGetAction: (actionKey: string) => {
130
130
  currentAction: (arg: FormData) => Promise<unknown> | Promise<void> | void | unknown;
131
- invalidate: (pathList?: string | string[], options?: InvalidateOptions) => Promise<void>;
131
+ invalidate: (pathList?: string | string[], options?: InvalidateOptions) => Promise<InvalidateResult[]>;
132
132
  };
133
133
  useRestoreScroll: () => () => void;
134
134
  useAction: (action: string, options?: Options) => (arg: FormData) => Promise<void>;
@@ -138,9 +138,16 @@ export type InvalidateOptions = {
138
138
  withChildren?: boolean;
139
139
  withBeforeLoad?: boolean;
140
140
  };
141
- export type RevalidateCache = ({ routeItem, pathname }: RevalidateCacheArgs) => Promise<unknown> | undefined;
141
+ export type RevalidateCache = ({ routeItem, pathname, }: RevalidateCacheArgs) => Promise<{
142
+ data: unknown;
143
+ error: unknown;
144
+ }>;
142
145
  export type Options = Partial<{
143
146
  onSuccess: (args: unknown) => void;
144
147
  onError: (args: unknown) => void;
145
148
  }> | undefined;
149
+ export type InvalidateResult = {
150
+ path: string;
151
+ data: unknown;
152
+ };
146
153
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "1.8.5",
3
+ "version": "1.8.6",
4
4
  "description": "A lightweight, type-safe routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {