resourcerer 1.2.3 → 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 +133 -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 +122 -109
- package/test/utils.test.js +173 -159
- package/index.d.ts +0 -270
- 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 -985
- package/lib/sync.js +0 -111
- package/lib/utils.js +0 -220
- package/test/model-mocks.js +0 -110
- package/test/setup.js +0 -63
package/index.d.ts
DELETED
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
// this will make CONST_CASE a camelCase but also leave camelCase alone
|
|
2
|
-
type Camelize<T extends string> = T extends `${infer A}_${infer B}` ?
|
|
3
|
-
`${Lowercase<A>}${Camelize<Capitalize<B>>}` :
|
|
4
|
-
T extends Uppercase<T> ?
|
|
5
|
-
Capitalize<Lowercase<T>> :
|
|
6
|
-
T extends `${infer A}${Uppercase<infer B>}` ?
|
|
7
|
-
Lowercase<T> :
|
|
8
|
-
T;
|
|
9
|
-
|
|
10
|
-
declare module 'resourcerer' {
|
|
11
|
-
type SyncOptions = {
|
|
12
|
-
attrs?: Record<string, any>;
|
|
13
|
-
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
14
|
-
params?: Record<string, any>;
|
|
15
|
-
url?: string;
|
|
16
|
-
minDuration?: number;
|
|
17
|
-
// they are free to add any other options they like
|
|
18
|
-
[key: string]: any;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
type SetOptions = {
|
|
22
|
-
silent?: boolean;
|
|
23
|
-
unset?: boolean;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
// for collection.set
|
|
27
|
-
type CSetOptions = {
|
|
28
|
-
parse?: boolean;
|
|
29
|
-
silent?: boolean;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
declare class Model<
|
|
33
|
-
T extends Record<string, any> = {[key: string]: any},
|
|
34
|
-
O extends Record<string, any> & SetOptions = {[key: string]: any}
|
|
35
|
-
> {
|
|
36
|
-
constructor(attrs?: T, options?: O): Model<T, O>;
|
|
37
|
-
|
|
38
|
-
// TODO: need to reference idAttribute here
|
|
39
|
-
id: T extends {id: string} ? string : string | undefined;
|
|
40
|
-
|
|
41
|
-
get<K extends keyof T>(key: K): T[K];
|
|
42
|
-
|
|
43
|
-
has(key: keyof T): boolean;
|
|
44
|
-
|
|
45
|
-
clear(options?: SetOptions): Model<T>;
|
|
46
|
-
|
|
47
|
-
unset(attr: keyof T, options?: SetOptions): Model<T>;
|
|
48
|
-
|
|
49
|
-
parse(response: Record<string, any>): T;
|
|
50
|
-
|
|
51
|
-
pick<K extends keyof T>(...attrs: K[]): Pick<T, K>;
|
|
52
|
-
|
|
53
|
-
set(attrs: Partial<T>, options?: SetOptions): Model<T>;
|
|
54
|
-
|
|
55
|
-
fetch(options?: {parse?: boolean} & SyncOptions & SetOptions): Promise<[Model<T>, Response]>;
|
|
56
|
-
|
|
57
|
-
save(attrs: Partial<T>, options?: {wait?: boolean; patch?: boolean;} & SyncOptions & SetOptions):
|
|
58
|
-
Promise<[Model<T>, Response]>;
|
|
59
|
-
|
|
60
|
-
sync(model: Model<T>, options?: SyncOptions): Promise<[any, Response]>;
|
|
61
|
-
|
|
62
|
-
destroy(options?: {wait?: boolean} & SyncOptions & SetOptions): Promise<[Model<T>, Response]>;
|
|
63
|
-
|
|
64
|
-
isNew(): boolean;
|
|
65
|
-
|
|
66
|
-
toJSON(): T;
|
|
67
|
-
|
|
68
|
-
url(urlOptions?: this["urlOptions"]): string;
|
|
69
|
-
|
|
70
|
-
urlRoot(urlOptions?: this["urlOptions"]): string;
|
|
71
|
-
|
|
72
|
-
collection?: Collection;
|
|
73
|
-
|
|
74
|
-
readonly urlOptions: Omit<O, keyof SetOptions>;
|
|
75
|
-
|
|
76
|
-
static cacheFields: Array<string | ((attrs: T) => Record<string, any>)>;
|
|
77
|
-
|
|
78
|
-
static dependencies: Array<string | ((attrs: T) => Record<string, any>)>;
|
|
79
|
-
|
|
80
|
-
static cacheTimeout: number;
|
|
81
|
-
|
|
82
|
-
static idAttribute: string;
|
|
83
|
-
|
|
84
|
-
static defaults: Partial<T> | (() => Partial<T>);
|
|
85
|
-
|
|
86
|
-
static measure: boolean | (() => void);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
type ModelArg<A> = Model<A> | Record<string, any>;
|
|
90
|
-
|
|
91
|
-
declare class Collection<
|
|
92
|
-
T extends Record<string, any> = {[key: string]: any},
|
|
93
|
-
O extends Record<string, any> & {Model?: new () => Model;} & CSetOptions = {[key: string]: any}
|
|
94
|
-
> {
|
|
95
|
-
constructor(
|
|
96
|
-
models?: ModelArg<T> | ModelArg<T>[],
|
|
97
|
-
options?: O
|
|
98
|
-
): Collection<T, O>;
|
|
99
|
-
|
|
100
|
-
length: number;
|
|
101
|
-
|
|
102
|
-
add(models: ModelArg<T> | ModelArg<T>[], options?: CSetOptions): Collection<T>;
|
|
103
|
-
|
|
104
|
-
remove(models: ModelArg<T> | ModelArg<T>[] | string | string[]): Collection<T>;
|
|
105
|
-
|
|
106
|
-
set(models: ModelArg<T> | ModelArg<T>[], options?: CSetOptions): Collection<T>;
|
|
107
|
-
|
|
108
|
-
reset(models: ModelArg<T>[], options?: CSetOptions): Collection<T>;
|
|
109
|
-
|
|
110
|
-
get(id?: string | ModelArg<T>): Model<T> | undefined;
|
|
111
|
-
|
|
112
|
-
has(id?: string | ModelArg<T>): boolean;
|
|
113
|
-
|
|
114
|
-
at(index: number): Model<T> | undefined;
|
|
115
|
-
|
|
116
|
-
toJSON(): T[];
|
|
117
|
-
|
|
118
|
-
sort(): Collection;
|
|
119
|
-
|
|
120
|
-
map<R = any>(predicate: (model: Model<T>) => R): R[];
|
|
121
|
-
|
|
122
|
-
find(predicate: (model: Model<T>) => boolean): Model<T>;
|
|
123
|
-
|
|
124
|
-
filter(predicate: (model: Model<T>) => boolean): Model<T>[];
|
|
125
|
-
|
|
126
|
-
findWhere<K extends keyof T>(attrs: Record<K, T[K]>): Model<T>;
|
|
127
|
-
|
|
128
|
-
where<K extends keyof T, B extends boolean = false>(attrs: Record<K, T[K]>, find?: B):
|
|
129
|
-
B extends true ?
|
|
130
|
-
Model<T> :
|
|
131
|
-
Model<T>[];
|
|
132
|
-
|
|
133
|
-
pluck<K extends keyof T>(prop: K): T[K][];
|
|
134
|
-
|
|
135
|
-
slice(start: number, end?: number): ModelArg<T>[];
|
|
136
|
-
|
|
137
|
-
sync(collection: Collection<T>, options?: SyncOptions): Promise<[any, Response]>;
|
|
138
|
-
|
|
139
|
-
parse(response: Record<string, any> | any[], options?: Record<string, any>): Array<T>;
|
|
140
|
-
|
|
141
|
-
create(
|
|
142
|
-
model: ModelArg<T>,
|
|
143
|
-
options?: {wait?: boolean} & SyncOptions & CSetOptions
|
|
144
|
-
): Promise<[Model<T>, Response]>;
|
|
145
|
-
|
|
146
|
-
fetch(options?: {parse?: boolean} & SyncOptions & CSetOptions): Promise<[Collection<T>, Response]>;
|
|
147
|
-
|
|
148
|
-
url(urlOptions?: this["urlOptions"]): string;
|
|
149
|
-
|
|
150
|
-
readonly urlOptions: Omit<O, keyof CSetOptions>;
|
|
151
|
-
|
|
152
|
-
static cacheFields: Array<string | ((attrs: T) => Record<string, any>)>;
|
|
153
|
-
|
|
154
|
-
static dependencies: Array<string | ((attrs: T) => Record<string, any>)>;
|
|
155
|
-
|
|
156
|
-
static modelIdAttribute: string;
|
|
157
|
-
|
|
158
|
-
static measure: boolean | (() => void);
|
|
159
|
-
|
|
160
|
-
static cacheTimeout: number;
|
|
161
|
-
|
|
162
|
-
static comparator: string |
|
|
163
|
-
((arg: Model<T>) => number | string) |
|
|
164
|
-
((arg1: Model<T>, arg2: Model<T>) => number);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
type LoadingTypes = 'loading' | 'loaded' | 'errored' | 'pending';
|
|
168
|
-
|
|
169
|
-
// this will be filled out by users
|
|
170
|
-
export interface ModelMap {}
|
|
171
|
-
|
|
172
|
-
type ResourceKeys = {
|
|
173
|
-
[key in keyof ModelMap]: key extends string ? Camelize<Uncapitalize<key>> : key
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
type ModelProperty<Str extends string> = {[key: `${Str}Model`]: Model};
|
|
177
|
-
type CollectionProperty<Str extends string> = {[key: `${Str}Collection`]: Collection};
|
|
178
|
-
|
|
179
|
-
interface UseResourcesResponse {
|
|
180
|
-
isLoading: boolean;
|
|
181
|
-
hasErrored: boolean;
|
|
182
|
-
hasLoaded: boolean;
|
|
183
|
-
hasInitiallyLoaded: boolean;
|
|
184
|
-
refetch: (fn: (keys: {[key: string]: ResourceKeys}) => ResourceKeys[]) => void;
|
|
185
|
-
setResourceState(newState: {[key: string]: any}): void;
|
|
186
|
-
isOrWillBeLoading(): boolean;
|
|
187
|
-
[key: `${string}LoadingState`]: LoadingTypes;
|
|
188
|
-
[key: `${string}Collection`]: Collection;
|
|
189
|
-
[key: `${string}Model`]: Model;
|
|
190
|
-
[key: `${string}Status`]: number;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
declare function haveAllLoaded(loadingStates: LoadingTypes | LoadingTypes[]): boolean;
|
|
194
|
-
declare function haveAnyErrored(loadingStates: LoadingTypes | LoadingTypes[]): boolean;
|
|
195
|
-
declare function areAnyLoading(loadingStates: LoadingTypes | LoadingTypes[]): boolean;
|
|
196
|
-
declare function areAnyPending(loadingStates: LoadingTypes | LoadingTypes[]): boolean;
|
|
197
|
-
|
|
198
|
-
declare function sync(model: Model | Collection, options?: SyncOptions): Promise<[any, Response]>;
|
|
199
|
-
|
|
200
|
-
type ResourceConfigObj = {
|
|
201
|
-
data?: {[key: string]: any};
|
|
202
|
-
dependsOn?: string[];
|
|
203
|
-
force?: boolean;
|
|
204
|
-
lazy?: boolean;
|
|
205
|
-
noncritical?: boolean;
|
|
206
|
-
options?: {[key: string]: any};
|
|
207
|
-
params?: {[key: string]: any};
|
|
208
|
-
prefetches?: {[key: string]: any}[];
|
|
209
|
-
provides?: {[key: string]: (model: Model | Collection) => any};
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
type ResourceValues = Extract<typeof ResourceKeys[keyof typeof ResourceKeys], string>;
|
|
213
|
-
type InvertedResourceKeys = {[Key in keyof ResourceKeys as ResourceKeys[Key]]: Key};
|
|
214
|
-
|
|
215
|
-
// TODO: how to only use those keys that were passed, not the whole resourcekeys obj
|
|
216
|
-
export type ExecutorFunction = (
|
|
217
|
-
ResourceKeys_: ResourceKeys,
|
|
218
|
-
props: {[key: string]: any}
|
|
219
|
-
// return type either has a resource key as the object key or is just a string with a modelKey property
|
|
220
|
-
) => Partial<Record<ResourceValues, ResourceConfigObj>> |
|
|
221
|
-
Partial<Record<string, ResourceConfigObj & {modelKey: ResourceValues}>>;
|
|
222
|
-
|
|
223
|
-
type WithModelSuffix<K, C> = C extends Collection ? `${K}Collection` : `${K}Model`;
|
|
224
|
-
|
|
225
|
-
declare function useResources(
|
|
226
|
-
getResources: ExecutorFunction,
|
|
227
|
-
props: {[key: string]: any}
|
|
228
|
-
): UseResourcesResponse & {
|
|
229
|
-
[Key in keyof Required<ReturnType<ExecutorFunction>> as
|
|
230
|
-
WithModelSuffix<Key, InstanceType<ModelMap[InvertedResourceKeys[Key]]>>]:
|
|
231
|
-
InstanceType<ModelMap[InvertedResourceKeys[Key]]>
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
export interface ModelCache {
|
|
235
|
-
get(key: string): Model | Collection | undefined;
|
|
236
|
-
put(key: string, model: Model | Collection, component: any): void;
|
|
237
|
-
register(key: string, component: any): void;
|
|
238
|
-
unregister(component: any, ...keys: string[]): void;
|
|
239
|
-
remove(key: string): void;
|
|
240
|
-
__removeAll__(): void;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
interface TrackOptions {
|
|
244
|
-
Resource: string;
|
|
245
|
-
params: {[key: string]: any};
|
|
246
|
-
options: {[key: string]: any};
|
|
247
|
-
duration: number;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
interface RequestPrefilter {
|
|
251
|
-
// TODO
|
|
252
|
-
error: (...args: any[]) => any;
|
|
253
|
-
headers: {[key: string]: string};
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
type ResourcerConfigOptions = {
|
|
257
|
-
cacheGracePeriod?: number;
|
|
258
|
-
errorBoundaryChild?: any;
|
|
259
|
-
log?: (err: Error) => void;
|
|
260
|
-
queryParamsPropName?: string;
|
|
261
|
-
stringify?: (params: {[key: string]: any}, options: {[key: string]: any}) => string;
|
|
262
|
-
track?: (name: string, options: TrackOptions) => void;
|
|
263
|
-
prefilter?: (options: {[key: string]: any}) => RequestPrefilter;
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
let ResourceKeys: ResourceKeys & {add(keys: Partial<ResourceKeys>): void;};
|
|
267
|
-
let ResourcesConfig: ResourcerConfigOptions & {set: (options: ResourcerConfigOptions) => void;};
|
|
268
|
-
let ModelMap: ModelMap & {add(keys: ModelMap): void;};
|
|
269
|
-
let ModelCache: ModelCache;
|
|
270
|
-
}
|
package/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export {default as Collection} from './lib/collection.js';
|
|
2
|
-
export {default as Model} from './lib/model.js';
|
|
3
|
-
export {default as sync, ajax} from './lib/sync.js';
|
|
4
|
-
export {default as prefetch} from './lib/prefetch.js';
|
|
5
|
-
export {default as request} from './lib/request.js';
|
|
6
|
-
export {default as ModelCache} from './lib/model-cache.js';
|
|
7
|
-
export {
|
|
8
|
-
hasErrored as haveAnyErrored,
|
|
9
|
-
hasLoaded as haveAllLoaded,
|
|
10
|
-
isLoading as areAnyLoading,
|
|
11
|
-
isPending as areAnyPending
|
|
12
|
-
} from './lib/utils.js';
|
|
13
|
-
export {ModelMap, ResourceKeys, ResourcesConfig} from './lib/config.js';
|
|
14
|
-
export {useResources, withResources} from './lib/resourcerer.js';
|