resourcerer 2.0.0-beta-2 → 2.0.0
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.
|
@@ -64,7 +64,7 @@ export default class Collection<T extends Record<string, any> = object, O extend
|
|
|
64
64
|
measure?: boolean | ((config: ResourceConfigObj) => boolean) | undefined;
|
|
65
65
|
isEmptyModel?: boolean | undefined;
|
|
66
66
|
toJSON(): T_1;
|
|
67
|
-
sync(model:
|
|
67
|
+
sync(model: Model<Record<string, any>, Record<string, any>> | Collection<object, object>, options?: SyncOptions | undefined): Promise<[any, Response]>;
|
|
68
68
|
get<K extends keyof T_1>(attr: K): T_1[K];
|
|
69
69
|
pick<K_1 extends keyof T_1>(...attrs: K_1[]): Pick<T_1, K_1>;
|
|
70
70
|
has(attr: keyof T_1): boolean;
|
|
@@ -2,7 +2,7 @@ import Collection from "./collection.js";
|
|
|
2
2
|
import Model from "./model.js";
|
|
3
3
|
type Component = NonNullable<unknown>;
|
|
4
4
|
declare const _default: {
|
|
5
|
-
get(cacheKey: string):
|
|
5
|
+
get(cacheKey: string): Model<Record<string, any>, Record<string, any>> | Collection<object, object> | undefined;
|
|
6
6
|
put(cacheKey: string, model: Model | Collection, component?: Component): void;
|
|
7
7
|
register(cacheKey: string, component?: Component): void;
|
|
8
8
|
unregister(component: Component, ...cacheKeys: string[]): void;
|
package/build/lib/request.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ interface RequestOptions extends Pick<SyncOptions, "params"> {
|
|
|
10
10
|
force?: boolean;
|
|
11
11
|
lazy?: boolean;
|
|
12
12
|
}
|
|
13
|
-
export declare const getFromCache: (key: string) =>
|
|
13
|
+
export declare const getFromCache: (key: string) => Model<Record<string, any>, Record<string, any>> | Collection<object, object> | undefined;
|
|
14
14
|
export declare const existsInCache: (key: string) => boolean;
|
|
15
15
|
declare const _default: (key: string, Model: {
|
|
16
16
|
new (data: Record<string, any>, options: RequestOptions["options"]): Model;
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { ModelMap } from "./config.js";
|
|
2
2
|
import React, { type ComponentClass } from "react";
|
|
3
3
|
import ErrorBoundary from "./error-boundary.js";
|
|
4
|
-
import type { ExecutorFunction, ResourceConfigObj, InternalResourceConfigObj, ResourceKeys, UseResourcesResponse, WithModelSuffix } from "./types.js";
|
|
4
|
+
import type { ExecutorFunction, LoadingStates, ResourceConfigObj, InternalResourceConfigObj, ResourceKeys, UseResourcesResponse, WithModelSuffix } from "./types.js";
|
|
5
5
|
export declare function useResources<T extends ResourceKeys, O extends Record<string, any>>(getResources: (props: O) => {
|
|
6
6
|
[Key in T]?: ResourceConfigObj;
|
|
7
7
|
}, _props: O): UseResourcesResponse & {
|
|
8
8
|
[Key in T as WithModelSuffix<Key, InstanceType<(typeof ModelMap)[Key]>>]: InstanceType<(typeof ModelMap)[Key]>;
|
|
9
|
+
} & {
|
|
10
|
+
[Key in T as `${T}LoadingState`]: LoadingStates;
|
|
11
|
+
} & {
|
|
12
|
+
[Key in T as `${T}Status`]: number;
|
|
9
13
|
};
|
|
10
14
|
export declare const withResources: (getResources: ExecutorFunction) => (Component: ComponentClass<Record<string, any>>) => (props: Record<string, any>) => React.CElement<{
|
|
11
15
|
children: React.ReactElement<any, string | React.JSXElementConstructor<any>>;
|
package/build/lib/types.d.ts
CHANGED
|
@@ -59,8 +59,4 @@ export type UseResourcesResponse = {
|
|
|
59
59
|
setResourceState(newState: {
|
|
60
60
|
[key: string]: any;
|
|
61
61
|
}): void;
|
|
62
|
-
[key: `${string}LoadingState`]: LoadingStates;
|
|
63
|
-
[key: `${string}Collection`]: Collection;
|
|
64
|
-
[key: `${string}Model`]: Model;
|
|
65
|
-
[key: `${string}Status`]: number;
|
|
66
62
|
};
|
package/package.json
CHANGED
|
@@ -1,15 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "resourcerer",
|
|
3
|
-
"version": "2.0.0
|
|
4
|
-
"repository":
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "github.com/noahgrant/resourcerer"
|
|
7
|
+
},
|
|
5
8
|
"description": "Declarative data-fetching and caching framework for React",
|
|
6
|
-
"keywords": [
|
|
7
|
-
|
|
9
|
+
"keywords": [
|
|
10
|
+
"declarative data-fetching framework",
|
|
11
|
+
"react"
|
|
12
|
+
],
|
|
13
|
+
"files": [
|
|
14
|
+
"build/**/*.js",
|
|
15
|
+
"*.d.ts",
|
|
16
|
+
"build/**/*.d.ts",
|
|
17
|
+
"test/*.js",
|
|
18
|
+
"*.md",
|
|
19
|
+
"docs/*.md"
|
|
20
|
+
],
|
|
8
21
|
"main": "build/index.js",
|
|
9
22
|
"types": "resourcerer.d.ts",
|
|
10
23
|
"type": "module",
|
|
11
24
|
"scripts": {
|
|
12
|
-
"checks": "npm typecheck && npm test && npm run lint",
|
|
25
|
+
"checks": "npm run typecheck && npm test && npm run lint",
|
|
13
26
|
"lint": "eslint lib/*.ts* test/*.js*",
|
|
14
27
|
"typecheck": "tsc --noEmit",
|
|
15
28
|
"build": "tsc",
|
|
@@ -37,7 +50,7 @@
|
|
|
37
50
|
"vitest": "^1.6.0"
|
|
38
51
|
},
|
|
39
52
|
"peerDependencies": {
|
|
40
|
-
"react": "^16.14.0 || ^17.0.0 || ^18.0.0",
|
|
41
|
-
"react-dom": "^16.14.0 || ^17.0.0 || ^18.0.0"
|
|
53
|
+
"react": "^16.14.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
|
|
54
|
+
"react-dom": "^16.14.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
|
|
42
55
|
}
|
|
43
56
|
}
|
package/resourcerer.d.ts
CHANGED
|
@@ -45,8 +45,6 @@ declare module "resourcerer" {
|
|
|
45
45
|
refetch: (keys: ResourceKeys | ResourceKeys[]) => void;
|
|
46
46
|
invalidate: (keys: ResourceKeys | ResourceKeys[]) => void;
|
|
47
47
|
setResourceState(newState: { [key: string]: any }): void;
|
|
48
|
-
[key: `${string}LoadingState`]: LoadingStates;
|
|
49
|
-
[key: `${string}Status`]: number;
|
|
50
48
|
};
|
|
51
49
|
|
|
52
50
|
export function useResources<T extends ResourceKeys, O extends Record<string, any>>(
|
|
@@ -54,5 +52,9 @@ declare module "resourcerer" {
|
|
|
54
52
|
_props: O
|
|
55
53
|
): UseResourcesResponse & {
|
|
56
54
|
[Key in T as WithModelSuffix<Key, InstanceType<ModelMap[Key]>>]: InstanceType<ModelMap[Key]>;
|
|
55
|
+
} & {
|
|
56
|
+
[Key in T as `${T}LoadingState`]: LoadingStates;
|
|
57
|
+
} & {
|
|
58
|
+
[Key in T as `${T}Status`]: number;
|
|
57
59
|
};
|
|
58
60
|
}
|
package/test/model-cache.test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Collection from "../lib/collection";
|
|
2
2
|
import Model from "../lib/model";
|
|
3
|
-
import ModelCache from "../lib/model-cache";
|
|
3
|
+
import ModelCache, { invalidate } from "../lib/model-cache";
|
|
4
4
|
import { vi } from "vitest";
|
|
5
5
|
|
|
6
6
|
const CACHE_WAIT = 150000,
|
|
@@ -205,4 +205,62 @@ describe("ModelCache", () => {
|
|
|
205
205
|
expect(ModelCache.get("users~key=value")).toBeDefined();
|
|
206
206
|
expect(ModelCache.get("decisions")).toBeDefined();
|
|
207
207
|
});
|
|
208
|
+
|
|
209
|
+
it("when calling 'removeAllExcept' removes all models except those with a specific key", () => {
|
|
210
|
+
const cacheKeys = [
|
|
211
|
+
"user~userId=zorah",
|
|
212
|
+
"user~source=hbase_userId=noah",
|
|
213
|
+
"user",
|
|
214
|
+
"users",
|
|
215
|
+
"users~key=value",
|
|
216
|
+
"aliens",
|
|
217
|
+
"decisions",
|
|
218
|
+
];
|
|
219
|
+
|
|
220
|
+
cacheKeys.forEach((key) => ModelCache.put(key, {}, {}));
|
|
221
|
+
|
|
222
|
+
ModelCache.removeAllExcept(["users", "aliens"]);
|
|
223
|
+
expect(ModelCache.get("user")).not.toBeDefined();
|
|
224
|
+
expect(ModelCache.get("user~userId=zorah")).not.toBeDefined();
|
|
225
|
+
expect(ModelCache.get("user~source=hbase_userId=noah")).not.toBeDefined();
|
|
226
|
+
expect(ModelCache.get("users")).toBeDefined();
|
|
227
|
+
expect(ModelCache.get("users~key=value")).toBeDefined();
|
|
228
|
+
expect(ModelCache.get("aliens")).toBeDefined();
|
|
229
|
+
expect(ModelCache.get("decisions")).not.toBeDefined();
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe("invalidate", () => {
|
|
234
|
+
it("removes models from the cache immediately", () => {
|
|
235
|
+
ModelCache.put("foo", {}, {});
|
|
236
|
+
ModelCache.put("bar", {}, {});
|
|
237
|
+
ModelCache.put("baz", {}, {});
|
|
238
|
+
invalidate("foo");
|
|
239
|
+
expect(ModelCache.get("foo")).not.toBeDefined();
|
|
240
|
+
expect(ModelCache.get("bar")).toBeDefined();
|
|
241
|
+
expect(ModelCache.get("baz")).toBeDefined();
|
|
242
|
+
|
|
243
|
+
// safe
|
|
244
|
+
invalidate();
|
|
245
|
+
expect(ModelCache.get("bar")).toBeDefined();
|
|
246
|
+
expect(ModelCache.get("baz")).toBeDefined();
|
|
247
|
+
|
|
248
|
+
invalidate(["bar", "baz"]);
|
|
249
|
+
expect(ModelCache.get("bar")).not.toBeDefined();
|
|
250
|
+
expect(ModelCache.get("baz")).not.toBeDefined();
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("removes models except those specified when the 'except' option is true", () => {
|
|
254
|
+
ModelCache.put("foo", {}, {});
|
|
255
|
+
ModelCache.put("bar", {}, {});
|
|
256
|
+
ModelCache.put("baz", {}, {});
|
|
257
|
+
invalidate(["foo", "bar"], { except: true });
|
|
258
|
+
expect(ModelCache.get("foo")).toBeDefined();
|
|
259
|
+
expect(ModelCache.get("bar")).toBeDefined();
|
|
260
|
+
expect(ModelCache.get("baz")).not.toBeDefined();
|
|
261
|
+
|
|
262
|
+
invalidate([], { except: true });
|
|
263
|
+
expect(ModelCache.get("foo")).not.toBeDefined();
|
|
264
|
+
expect(ModelCache.get("bar")).not.toBeDefined();
|
|
265
|
+
});
|
|
208
266
|
});
|