resourcerer 2.0.0-beta → 2.0.0-beta-1
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/build/lib/collection.d.ts +1 -1
- package/build/lib/model-cache.d.ts +1 -1
- package/build/lib/request.d.ts +1 -1
- package/build/lib/resourcerer.d.ts +2 -2
- package/docs/typescript.md +13 -11
- package/package.json +1 -1
- package/resourcerer.d.ts +11 -3
|
@@ -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: Model<Record<string, any>, Record<string, any
|
|
67
|
+
sync(model: Collection<object, object> | Model<Record<string, any>, Record<string, any>>, 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): Model<Record<string, any>, Record<string, any>> |
|
|
5
|
+
get(cacheKey: string): Collection<object, object> | Model<Record<string, any>, Record<string, any>> | 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) => Model<Record<string, any>, Record<string, any>> |
|
|
13
|
+
export declare const getFromCache: (key: string) => Collection<object, object> | Model<Record<string, any>, Record<string, any>> | 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;
|
|
@@ -2,9 +2,9 @@ import { ModelMap } from "./config.js";
|
|
|
2
2
|
import React, { type ComponentClass } from "react";
|
|
3
3
|
import ErrorBoundary from "./error-boundary.js";
|
|
4
4
|
import type { ExecutorFunction, ResourceConfigObj, InternalResourceConfigObj, ResourceKeys, UseResourcesResponse, WithModelSuffix } from "./types.js";
|
|
5
|
-
export declare function useResources<T extends ResourceKeys
|
|
5
|
+
export declare function useResources<T extends ResourceKeys, O extends Record<string, any>>(getResources: (props: O) => {
|
|
6
6
|
[Key in T]?: ResourceConfigObj;
|
|
7
|
-
}, _props:
|
|
7
|
+
}, _props: O): UseResourcesResponse & {
|
|
8
8
|
[Key in T as WithModelSuffix<Key, InstanceType<(typeof ModelMap)[Key]>>]: InstanceType<(typeof ModelMap)[Key]>;
|
|
9
9
|
};
|
|
10
10
|
export declare const withResources: (getResources: ExecutorFunction) => (Component: ComponentClass<Record<string, any>>) => (props: Record<string, any>) => React.CElement<{
|
package/docs/typescript.md
CHANGED
|
@@ -42,8 +42,8 @@ interface Todo {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
export default TodosCollection extends Collection<Todo> {
|
|
45
|
-
url() {
|
|
46
|
-
return
|
|
45
|
+
url({userId}: {userId: string}) {
|
|
46
|
+
return `/${userId}/todos`;
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
```
|
|
@@ -94,15 +94,15 @@ function MyComponent(props) {
|
|
|
94
94
|
hasLoaded,
|
|
95
95
|
// ERROR: property tdoosCollection does not exist on...
|
|
96
96
|
tdoosCollection
|
|
97
|
-
// ERROR Property
|
|
98
|
-
} = useResources(({
|
|
97
|
+
// ERROR Property ursdI does not exist on type...
|
|
98
|
+
} = useResources(({userId}) => ({todos: {params: {userId}}}), {ursdI: "oops"});
|
|
99
99
|
```
|
|
100
100
|
|
|
101
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
102
|
|
|
103
103
|
```tsx
|
|
104
104
|
// type out these props
|
|
105
|
-
const getResources = (props: {
|
|
105
|
+
const getResources = (props: {userId: string}) => ({todos: {params: {userId}}});
|
|
106
106
|
|
|
107
107
|
function MyComponent() {
|
|
108
108
|
const {
|
|
@@ -112,8 +112,8 @@ function MyComponent(props) {
|
|
|
112
112
|
// ERROR: property tdoosCollection does not exist on...
|
|
113
113
|
tdoosCollection
|
|
114
114
|
// ERROR no overload matches this call...
|
|
115
|
-
// since
|
|
116
|
-
} = useResources(getResources, {
|
|
115
|
+
// since ursdI is being passed when we expect userId
|
|
116
|
+
} = useResources(getResources, {ursdI: "oops"});
|
|
117
117
|
```
|
|
118
118
|
|
|
119
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:
|
|
@@ -122,8 +122,10 @@ function MyComponent(props) {
|
|
|
122
122
|
import {type ExecutorFunction, useResources} from 'resourcerer';
|
|
123
123
|
|
|
124
124
|
// "todos" and "todoItem" will come up as type hints, both as the type parameters and the Resource Config Object keys
|
|
125
|
-
|
|
126
|
-
|
|
125
|
+
// the second generic are the types for the props argument.
|
|
126
|
+
const getResources: ExecutorFunction<"todos" | "todoItem", {userId: number}> = (props) => ({
|
|
127
|
+
// ERROR: number not assignable to string. Because we set this in our "url" method!
|
|
128
|
+
todos: {path: {userId}},
|
|
127
129
|
todoItem: {}
|
|
128
130
|
});
|
|
129
131
|
|
|
@@ -135,8 +137,8 @@ function MyComponent(props) {
|
|
|
135
137
|
// ERROR: property tdoosCollection does not exist on...
|
|
136
138
|
tdoosCollection
|
|
137
139
|
// ERROR no overload matches this call...
|
|
138
|
-
// since
|
|
139
|
-
} = useResources(getResources, {
|
|
140
|
+
// since ursdI is being passed when we expect userId
|
|
141
|
+
} = useResources(getResources, {ursdI: "oops"});
|
|
140
142
|
```
|
|
141
143
|
|
|
142
144
|
Up to you how you want your types!
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "resourcerer",
|
|
3
|
-
"version": "2.0.0-beta",
|
|
3
|
+
"version": "2.0.0-beta-1",
|
|
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"],
|
package/resourcerer.d.ts
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
|
+
import Model from "./build/lib/model.js";
|
|
1
2
|
import Collection from "./build/lib/collection.js";
|
|
2
3
|
|
|
3
4
|
export * from "./build/index.js";
|
|
4
5
|
|
|
6
|
+
type GetModelOptions<C> =
|
|
7
|
+
C extends Model<infer A, infer O> ? [A, O]
|
|
8
|
+
: C extends Collection<infer M, infer O> ? [M, O]
|
|
9
|
+
: never;
|
|
10
|
+
|
|
5
11
|
declare module "resourcerer" {
|
|
6
12
|
type WithModelSuffix<K extends ResourceKeys, C> =
|
|
7
13
|
C extends Collection<any, any> ? `${K}Collection` : `${K}Model`;
|
|
8
14
|
export type LoadingStates = "error" | "loading" | "loaded" | "pending";
|
|
9
15
|
export type ResourceConfigObj<K extends ResourceKeys> = {
|
|
10
|
-
data?:
|
|
16
|
+
data?: Partial<GetModelOptions<InstanceType<ModelMap[K]>>[0]>;
|
|
11
17
|
dependsOn?: boolean;
|
|
12
18
|
force?: boolean;
|
|
13
19
|
lazy?: boolean;
|
|
14
20
|
noncritical?: boolean;
|
|
15
21
|
options?: { [key: string]: any };
|
|
16
|
-
|
|
22
|
+
// ts is not happy with this but this will make our typings easier
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
path?: Parameters<InstanceType<ModelMap[K]>["url"]>[0];
|
|
17
25
|
params?: { [key: string]: any };
|
|
18
26
|
prefetches?: { [key: string]: any }[];
|
|
19
27
|
provides?: (
|
|
@@ -25,7 +33,7 @@ declare module "resourcerer" {
|
|
|
25
33
|
export interface ModelMap {}
|
|
26
34
|
export type ResourceKeys = Extract<keyof ModelMap, string>;
|
|
27
35
|
|
|
28
|
-
export type ExecutorFunction<T extends ResourceKeys, O =
|
|
36
|
+
export type ExecutorFunction<T extends ResourceKeys, O = Record<string, never>> = (props: O) => {
|
|
29
37
|
[Key in T]?: ResourceConfigObj<Key>;
|
|
30
38
|
};
|
|
31
39
|
|