resourcerer 2.0.1 → 2.0.3

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
@@ -932,7 +932,14 @@ function MyComponent(props) {
932
932
  return <Button onClick={() => invalidate(["todos"])}>Invalidate me</Button>;
933
933
  ```
934
934
 
935
- * Unlike [`refetching`](#refetching), the ResourceKeys passed to `invalidate` do not need to be from those returned by the executor function. They can any resource key.
935
+ * Unlike [`refetching`](#refetching), the ResourceKeys passed to `invalidate` do not need to be from those returned by the executor function. They can any resource key. This function is also available as a static import from the `resourcerer` package.
936
+
937
+ Rather than invalidating the cache for select keys, you can also invalidate the entire cache with the exception of some keys by passing the `{except: true}` option:
938
+
939
+ ```js
940
+ // this will remove all items from the cache except those from the "todos" resource
941
+ invalidate(["todos"], {except: true});
942
+ ```
936
943
 
937
944
  ## Tracking Request Times
938
945
 
@@ -53,45 +53,45 @@ export default class Collection<T extends Record<string, any> = object, O extend
53
53
  } & SyncOptions & CSetOptions): Promise<[InstanceType<this["Model"]>, Response]>;
54
54
  parse(response: any, options: SyncOptions & CSetOptions): T[];
55
55
  _getModelClass<T extends Record<string, any>, O extends Record<string, any>>(): {
56
- new (attributes?: T | undefined, options?: O & SetOptions & ConstructorOptions): {
56
+ new (attributes?: T_1 | undefined, options?: O_1 & SetOptions & ConstructorOptions): {
57
57
  cid: string;
58
58
  id: string | number;
59
- attributes: T;
60
- readonly urlOptions: O;
61
- collection?: Collection;
62
- lazy?: boolean;
63
- refetching?: boolean;
64
- measure?: boolean | ((config: ResourceConfigObj) => boolean);
65
- isEmptyModel?: boolean;
66
- toJSON(): T;
59
+ attributes: T_1;
60
+ readonly urlOptions: O_1;
61
+ collection?: Collection<object, object> | undefined;
62
+ lazy?: boolean | undefined;
63
+ refetching?: boolean | undefined;
64
+ measure?: boolean | ((config: ResourceConfigObj) => boolean) | undefined;
65
+ isEmptyModel?: boolean | undefined;
66
+ toJSON(): T_1;
67
67
  sync(model: Model<Record<string, any>, Record<string, any>> | Collection<object, object>, options?: SyncOptions | undefined): Promise<[any, Response]>;
68
- get<K extends keyof T>(attr: K): T[K];
69
- pick<K extends keyof T>(...attrs: K[]): Pick<T, K>;
70
- has(attr: keyof T): boolean;
71
- set(attrs?: Partial<T>, options?: SetOptions): any;
72
- unset(attr: keyof T, options?: SetOptions): any;
73
- clear(options?: SetOptions): any;
68
+ get<K extends keyof T_1>(attr: K): T_1[K];
69
+ pick<K_1 extends keyof T_1>(...attrs: K_1[]): Pick<T_1, K_1>;
70
+ has(attr: keyof T_1): boolean;
71
+ set(attrs?: Partial<T_1>, options?: SetOptions): any;
72
+ unset(attr: keyof T_1, options?: SetOptions | undefined): any;
73
+ clear(options?: SetOptions | undefined): any;
74
74
  fetch(options?: SyncOptions & SetOptions): Promise<readonly [any, Response]>;
75
- save(attrs: Partial<T>, options?: {
76
- wait?: boolean;
77
- patch?: boolean;
75
+ save(attrs: Partial<T_1>, options?: {
76
+ wait?: boolean | undefined;
77
+ patch?: boolean | undefined;
78
78
  } & SyncOptions & SetOptions): Promise<[any, Response]>;
79
79
  destroy(options?: {
80
- wait?: boolean;
80
+ wait?: boolean | undefined;
81
81
  } & SyncOptions & SetOptions): Promise<[any, Response]>;
82
- url(options?: O_1): string;
83
- parse(response: any, options: SyncOptions & SetOptions): T;
82
+ url(options?: O_2): string;
83
+ parse(response: any, options: SyncOptions & SetOptions): T_1;
84
84
  isNew(): boolean;
85
85
  _callbacks: {
86
86
  callback: (...args: any[]) => void;
87
- context: NonNullable<unknown>;
87
+ context: {};
88
88
  }[];
89
89
  triggerUpdate(): void;
90
90
  onUpdate(callback: (...args: any[]) => void, context: {}): void;
91
91
  offUpdate(ctx: {}): void;
92
92
  };
93
93
  idAttribute: string;
94
- dependencies: Array<string | ((attrs: Record<string, any>) => Record<string, any>)>;
94
+ dependencies: (string | ((attrs: Record<string, any>) => Record<string, any>))[];
95
95
  cacheTimeout: number;
96
96
  defaults: {};
97
97
  measure: boolean | ((config: ResourceConfigObj) => boolean);
@@ -12,6 +12,6 @@ export declare function useResources<T extends ResourceKeys, O extends Record<st
12
12
  [Key in T as `${T}Status`]: number;
13
13
  };
14
14
  export declare const withResources: (getResources: ExecutorFunction) => (Component: ComponentClass<Record<string, any>>) => (props: Record<string, any>) => React.CElement<{
15
- children: React.ReactElement;
15
+ children: React.ReactElement<any, string | React.JSXElementConstructor<any>>;
16
16
  }, ErrorBoundary>;
17
17
  export declare function getCacheKey({ resourceKey, params, path, data, }: InternalResourceConfigObj): string;
@@ -18,7 +18,10 @@ export function useResources(getResources, _props) {
18
18
  hasInitiallyLoaded: hasLoaded(getCriticalLoadingStates(initialLoadingStates, resources)),
19
19
  });
20
20
  const criticalLoadingStates = getCriticalLoadingStates(loadingStates, resources);
21
- const [models, setModels] = useState(modelAggregator(resources.filter(withoutPrefetch)));
21
+ const [models, setModels] = useState(() => {
22
+ const result = modelAggregator(resources.filter(withoutPrefetch));
23
+ return typeof result === "function" ? result({}) : result;
24
+ });
22
25
  const isMountedRef = useIsMounted();
23
26
  const componentRef = useRef({});
24
27
  const prevPropsRef = useRef();
@@ -35,6 +38,7 @@ export function useResources(getResources, _props) {
35
38
  const previousCacheKey = prevConfig && getCacheKey(prevConfig);
36
39
  return (!previousCacheKey ||
37
40
  previousCacheKey !== getCacheKey(config) ||
41
+ !models[getResourcePropertyName(name, config.resourceKey)] ||
38
42
  !hasAllDependencies(["", prevConfig]) ||
39
43
  (config.refetch &&
40
44
  (hasLoaded(loadingStates[getResourceState(name)]) ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resourcerer",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "github.com/noahgrant/resourcerer"