resourcerer 1.2.2 → 2.0.0-beta
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 +138 -164
- package/build/index.d.ts +9 -0
- package/build/index.js +9 -0
- package/build/lib/collection.d.ts +107 -0
- package/build/lib/collection.js +251 -0
- package/build/lib/config.d.ts +20 -0
- package/build/lib/config.js +23 -0
- package/build/lib/error-boundary.d.ts +12 -0
- package/build/lib/error-boundary.js +18 -0
- package/build/lib/events.d.ts +11 -0
- package/build/lib/events.js +12 -0
- package/build/lib/model-cache.d.ts +13 -0
- package/build/lib/model-cache.js +62 -0
- package/build/lib/model.d.ts +48 -0
- package/build/lib/model.js +170 -0
- package/build/lib/prefetch.d.ts +3 -0
- package/build/lib/prefetch.js +27 -0
- package/build/lib/request.d.ts +20 -0
- package/build/lib/request.js +53 -0
- package/build/lib/resourcerer.d.ts +13 -0
- package/build/lib/resourcerer.js +406 -0
- package/build/lib/sync.d.ts +18 -0
- package/build/lib/sync.js +58 -0
- package/build/lib/types.d.ts +66 -0
- package/build/lib/types.js +1 -0
- package/build/lib/utils.d.ts +14 -0
- package/build/lib/utils.js +87 -0
- package/docs/advanced_topics.md +9 -77
- package/docs/collection.md +28 -28
- package/docs/migrating_to_2.0.md +72 -0
- package/docs/model.md +21 -21
- package/docs/typescript.md +68 -20
- package/package.json +27 -30
- package/resourcerer.d.ts +50 -0
- package/test/collection.test.js +305 -316
- package/test/config.test.js +21 -75
- package/test/events.test.js +42 -41
- package/test/model-cache.test.js +99 -77
- package/test/model.test.js +204 -209
- package/test/prefetch.test.js +35 -61
- package/test/request.test.js +104 -109
- package/test/sync.test.js +132 -107
- package/test/utils.test.js +173 -159
- package/index.d.ts +0 -269
- package/index.js +0 -14
- package/lib/collection.js +0 -584
- package/lib/config.js +0 -131
- package/lib/constants.js +0 -6
- package/lib/error-boundary.js +0 -47
- package/lib/events.js +0 -19
- package/lib/model-cache.js +0 -131
- package/lib/model.js +0 -372
- package/lib/prefetch.js +0 -59
- package/lib/request.js +0 -126
- package/lib/resourcerer.js +0 -963
- package/lib/sync.js +0 -94
- package/lib/utils.js +0 -220
- package/test/model-mocks.js +0 -110
- package/test/setup.js +0 -63
package/docs/typescript.md
CHANGED
|
@@ -1,22 +1,17 @@
|
|
|
1
1
|
# Using resourcerer with TypeScript
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Resourcerer is ⚡⚡⚡ with TypeScript, but requires one extra step in your config file:
|
|
3
|
+
1. Resourcerer is ⚡⚡⚡ with TypeScript, but requires one extra step in your config file:
|
|
6
4
|
|
|
7
5
|
```ts
|
|
8
6
|
// js/core/resourcerer-config.ts
|
|
9
|
-
import {
|
|
7
|
+
import {register} from 'resourcerer';
|
|
10
8
|
import TodosCollection from 'js/models/todos-collection';
|
|
11
9
|
import UserTodosCollection from 'js/models/users-todos-collection';
|
|
12
10
|
|
|
13
11
|
/**
|
|
14
|
-
* Because the ModelMap is populated in user-land, we have to annotate its types
|
|
15
|
-
* Add your model types to this interface declaration, which will be merged with
|
|
16
|
-
* in the library.
|
|
17
|
-
*
|
|
18
|
-
* I wish we didn't have to do this duplication, and maybe there's a way to do it without, but
|
|
19
|
-
* I am not good enough at TypeScript to figure it out myself. If you can, please submit a PR!
|
|
12
|
+
* Because we `register` the ModelMap is populated in user-land, we have to annotate its types
|
|
13
|
+
* here, as well. Add your model types to this interface declaration, which will be merged with
|
|
14
|
+
* the interface in the library.
|
|
20
15
|
*/
|
|
21
16
|
declare module 'resourcerer' {
|
|
22
17
|
export interface ModelMap {
|
|
@@ -26,19 +21,16 @@ declare module 'resourcerer' {
|
|
|
26
21
|
}
|
|
27
22
|
}
|
|
28
23
|
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
// now register your models as normal
|
|
25
|
+
register({
|
|
31
26
|
todos: TodosCollection,
|
|
32
27
|
userTodos: UserTodosCollection,
|
|
33
28
|
// ...etc
|
|
34
29
|
});
|
|
35
30
|
```
|
|
36
31
|
|
|
37
|
-
Note that you no longer need to add a resource key like `USER_TODOS` in the ModelMap, which would normally get
|
|
38
|
-
auto-magically turned into the camelCase `userTodos`. Since TypeScript will enforce correctness, you can just
|
|
39
|
-
use the string literal `userTodos` instead.
|
|
40
32
|
|
|
41
|
-
When defining your models, you can add schemas as generic types:
|
|
33
|
+
2. When defining your models, you can add schemas as generic types:
|
|
42
34
|
|
|
43
35
|
```ts
|
|
44
36
|
// js/models/todos-collection.ts
|
|
@@ -59,11 +51,9 @@ export default TodosCollection extends Collection<Todo> {
|
|
|
59
51
|
and now you can use it in your components!
|
|
60
52
|
|
|
61
53
|
```tsx
|
|
62
|
-
import {
|
|
54
|
+
import {useResources} from 'resourcerer';
|
|
63
55
|
|
|
64
|
-
|
|
65
|
-
// string literal! TypeScript will enforce correctness
|
|
66
|
-
const getResources: ExecutorFunction = (resourceKeys, props) => ({todos: {}});
|
|
56
|
+
const getResources = (props) => ({todos: {}});
|
|
67
57
|
|
|
68
58
|
function MyComponent(props) {
|
|
69
59
|
const {
|
|
@@ -92,3 +82,61 @@ function MyComponent(props) {
|
|
|
92
82
|
);
|
|
93
83
|
}
|
|
94
84
|
```
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
3. You get the best built-in type hints when you inline the `getResources` function:
|
|
88
|
+
|
|
89
|
+
```tsx
|
|
90
|
+
function MyComponent() {
|
|
91
|
+
const {
|
|
92
|
+
isLoading,
|
|
93
|
+
hasErrored,
|
|
94
|
+
hasLoaded,
|
|
95
|
+
// ERROR: property tdoosCollection does not exist on...
|
|
96
|
+
tdoosCollection
|
|
97
|
+
// ERROR Property ogrdI does not exist on type...
|
|
98
|
+
} = useResources(({orgId}) => ({todos: {params: {orgId}}}), {ogrdI: "oops"});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
But inlining executor functions leaves you susceptible to [the subtle bug where you are always reading from current props](https://github.com/noahgrant/resourcerer/tree/typescript?tab=readme-ov-file#differences-between-useresources-and-withresources). Executor functions can also get pretty involved, so it's nice to extract it. You still get good type hints, but you'll need to type out your props:
|
|
102
|
+
|
|
103
|
+
```tsx
|
|
104
|
+
// type out these props
|
|
105
|
+
const getResources = (props: {orgId: string}) => ({todos: {params: {orgId}}});
|
|
106
|
+
|
|
107
|
+
function MyComponent() {
|
|
108
|
+
const {
|
|
109
|
+
isLoading,
|
|
110
|
+
hasErrored,
|
|
111
|
+
hasLoaded,
|
|
112
|
+
// ERROR: property tdoosCollection does not exist on...
|
|
113
|
+
tdoosCollection
|
|
114
|
+
// ERROR no overload matches this call...
|
|
115
|
+
// since ogrdI is being passed when we expect orgId
|
|
116
|
+
} = useResources(getResources, {ogrdI: "oops"});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Using this method, the only drawback is that you don't get type hints for the ResourceKeys like `todos`. If you want to add those, you'll have to import the `ExecutorFunction` type and pass the list of ResourceKeys:
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
import {type ExecutorFunction, useResources} from 'resourcerer';
|
|
123
|
+
|
|
124
|
+
// "todos" and "todoItem" will come up as type hints, both as the type parameters and the Resource Config Object keys
|
|
125
|
+
const getResources: ExecutorFunction<"todos" | "todoItem"> = (props: {orgId: string}) => ({
|
|
126
|
+
todos: {params: {orgId}},
|
|
127
|
+
todoItem: {}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
function MyComponent() {
|
|
131
|
+
const {
|
|
132
|
+
isLoading,
|
|
133
|
+
hasErrored,
|
|
134
|
+
hasLoaded,
|
|
135
|
+
// ERROR: property tdoosCollection does not exist on...
|
|
136
|
+
tdoosCollection
|
|
137
|
+
// ERROR no overload matches this call...
|
|
138
|
+
// since ogrdI is being passed when we expect orgId
|
|
139
|
+
} = useResources(getResources, {ogrdI: "oops"});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Up to you how you want your types!
|
package/package.json
CHANGED
|
@@ -1,43 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "resourcerer",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-beta",
|
|
4
4
|
"repository": "github.com/noahgrant/resourcerer",
|
|
5
5
|
"description": "Declarative data-fetching and caching framework for React",
|
|
6
6
|
"keywords": ["declarative data-fetching framework", "react"],
|
|
7
|
-
"files": ["
|
|
8
|
-
"main": "index.js",
|
|
7
|
+
"files": ["build/**/*.js", "*.d.ts", "build/**/*.d.ts", "test/*.js", "*.md", "docs/*.md"],
|
|
8
|
+
"main": "build/index.js",
|
|
9
|
+
"types": "resourcerer.d.ts",
|
|
9
10
|
"type": "module",
|
|
10
11
|
"scripts": {
|
|
11
|
-
"checks": "npm test && npm run lint",
|
|
12
|
-
"lint": "eslint lib/*.
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
12
|
+
"checks": "npm typecheck && npm test && npm run lint",
|
|
13
|
+
"lint": "eslint lib/*.ts* test/*.js*",
|
|
14
|
+
"typecheck": "tsc --noEmit",
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"prepublish": "npm run build",
|
|
17
|
+
"test": "vitest run",
|
|
18
|
+
"test:watch": "vitest",
|
|
19
|
+
"test:coverage": "vitest run --coverage",
|
|
20
|
+
"test:debug": "vitest --inspect-brk --pool threads --poolOptions.threads.singleThread run"
|
|
16
21
|
},
|
|
17
|
-
"
|
|
18
|
-
"collectCoverageFrom": ["lib/*.{js,jsx}", "!lib/constants.js"],
|
|
19
|
-
"moduleDirectories": ["node_modules"],
|
|
20
|
-
"setupFilesAfterEnv": ["./test/setup"],
|
|
21
|
-
"testEnvironment": "jsdom"
|
|
22
|
-
},
|
|
23
|
-
"exports": "./index.js",
|
|
22
|
+
"exports": "./build/index.js",
|
|
24
23
|
"devDependencies": {
|
|
25
|
-
"@
|
|
26
|
-
"@
|
|
27
|
-
"@
|
|
28
|
-
"@
|
|
29
|
-
"@
|
|
30
|
-
"@
|
|
31
|
-
"@
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"eslint": "^7.32.0",
|
|
24
|
+
"@eslint/js": "^9.6.0",
|
|
25
|
+
"@stylistic/eslint-plugin": "^2.1.0",
|
|
26
|
+
"@types/eslint__js": "^8.42.3",
|
|
27
|
+
"@types/react": "^18.3.3",
|
|
28
|
+
"@types/react-dom": "^18.3.0",
|
|
29
|
+
"@vitejs/plugin-react": "^4.3.1",
|
|
30
|
+
"@vitest/coverage-v8": "^1.6.0",
|
|
31
|
+
"eslint": "^8.57.0",
|
|
32
|
+
"eslint-config-prettier": "^9.1.0",
|
|
35
33
|
"eslint-plugin-jest": "^25.2.2",
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"whatwg-fetch": "^3.6.2"
|
|
34
|
+
"prettier": "^3.2.5",
|
|
35
|
+
"typescript": "^5.4.5",
|
|
36
|
+
"typescript-eslint": "^7.14.1",
|
|
37
|
+
"vitest": "^1.6.0"
|
|
41
38
|
},
|
|
42
39
|
"peerDependencies": {
|
|
43
40
|
"react": "^16.14.0 || ^17.0.0 || ^18.0.0",
|
package/resourcerer.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import Collection from "./build/lib/collection.js";
|
|
2
|
+
|
|
3
|
+
export * from "./build/index.js";
|
|
4
|
+
|
|
5
|
+
declare module "resourcerer" {
|
|
6
|
+
type WithModelSuffix<K extends ResourceKeys, C> =
|
|
7
|
+
C extends Collection<any, any> ? `${K}Collection` : `${K}Model`;
|
|
8
|
+
export type LoadingStates = "error" | "loading" | "loaded" | "pending";
|
|
9
|
+
export type ResourceConfigObj<K extends ResourceKeys> = {
|
|
10
|
+
data?: { [key: string]: any };
|
|
11
|
+
dependsOn?: boolean;
|
|
12
|
+
force?: boolean;
|
|
13
|
+
lazy?: boolean;
|
|
14
|
+
noncritical?: boolean;
|
|
15
|
+
options?: { [key: string]: any };
|
|
16
|
+
path?: { [key: string]: any };
|
|
17
|
+
params?: { [key: string]: any };
|
|
18
|
+
prefetches?: { [key: string]: any }[];
|
|
19
|
+
provides?: (
|
|
20
|
+
model: InstanceType<ModelMap[K]>,
|
|
21
|
+
props: Record<string, any>
|
|
22
|
+
) => { [key: string]: any };
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export interface ModelMap {}
|
|
26
|
+
export type ResourceKeys = Extract<keyof ModelMap, string>;
|
|
27
|
+
|
|
28
|
+
export type ExecutorFunction<T extends ResourceKeys, O = any> = (props: O) => {
|
|
29
|
+
[Key in T]?: ResourceConfigObj<Key>;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type UseResourcesResponse = {
|
|
33
|
+
isLoading: boolean;
|
|
34
|
+
hasErrored: boolean;
|
|
35
|
+
hasLoaded: boolean;
|
|
36
|
+
hasInitiallyLoaded: boolean;
|
|
37
|
+
refetch: (keys: ResourceKeys | ResourceKeys[]) => void;
|
|
38
|
+
invalidate: (keys: ResourceKeys | ResourceKeys[]) => void;
|
|
39
|
+
setResourceState(newState: { [key: string]: any }): void;
|
|
40
|
+
[key: `${string}LoadingState`]: LoadingStates;
|
|
41
|
+
[key: `${string}Status`]: number;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export function useResources<T extends ResourceKeys, O extends Record<string, any>>(
|
|
45
|
+
getResources: ExecutorFunction<T, O>,
|
|
46
|
+
_props: O
|
|
47
|
+
): UseResourcesResponse & {
|
|
48
|
+
[Key in T as WithModelSuffix<Key, InstanceType<ModelMap[Key]>>]: InstanceType<ModelMap[Key]>;
|
|
49
|
+
};
|
|
50
|
+
}
|