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
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { isDeepEqual, result, uniqueId, urlError } from "./utils.js";
|
|
2
|
+
import Events from "./events.js";
|
|
3
|
+
import sync from "./sync.js";
|
|
4
|
+
const RESERVED_OPTION_KEYS = [
|
|
5
|
+
"Model",
|
|
6
|
+
"comparator",
|
|
7
|
+
"parse",
|
|
8
|
+
"collection",
|
|
9
|
+
"silent",
|
|
10
|
+
"method",
|
|
11
|
+
"unset",
|
|
12
|
+
];
|
|
13
|
+
export default class Model extends Events {
|
|
14
|
+
cid;
|
|
15
|
+
id;
|
|
16
|
+
attributes;
|
|
17
|
+
urlOptions = {};
|
|
18
|
+
collection;
|
|
19
|
+
lazy;
|
|
20
|
+
refetching;
|
|
21
|
+
measure;
|
|
22
|
+
isEmptyModel;
|
|
23
|
+
constructor(attributes, options = {}) {
|
|
24
|
+
super();
|
|
25
|
+
this.cid = uniqueId("c");
|
|
26
|
+
this.attributes = {};
|
|
27
|
+
if (options.collection) {
|
|
28
|
+
this.collection = options.collection;
|
|
29
|
+
}
|
|
30
|
+
if (options.parse) {
|
|
31
|
+
attributes = this.parse(attributes, options) || {};
|
|
32
|
+
}
|
|
33
|
+
this.urlOptions = Object.keys(options).reduce((memo, key) => Object.assign(memo, !RESERVED_OPTION_KEYS.includes(key) ? { [key]: options[key] } : {}), {});
|
|
34
|
+
this.set({ ...result(this.constructor, "defaults"), ...attributes }, options);
|
|
35
|
+
}
|
|
36
|
+
static idAttribute = "id";
|
|
37
|
+
static dependencies = [];
|
|
38
|
+
static cacheTimeout;
|
|
39
|
+
static defaults = {};
|
|
40
|
+
static measure = false;
|
|
41
|
+
toJSON() {
|
|
42
|
+
return { ...this.attributes };
|
|
43
|
+
}
|
|
44
|
+
sync(...args) {
|
|
45
|
+
return sync.call(this, ...args);
|
|
46
|
+
}
|
|
47
|
+
get(attr) {
|
|
48
|
+
return this.attributes[attr];
|
|
49
|
+
}
|
|
50
|
+
pick(...attrs) {
|
|
51
|
+
return attrs.reduce((memo, attr) => Object.assign(memo, this.has(attr) ? { [attr]: this.get(attr) } : {}), {});
|
|
52
|
+
}
|
|
53
|
+
has(attr) {
|
|
54
|
+
return ![undefined, null].includes(this.get(attr));
|
|
55
|
+
}
|
|
56
|
+
set(attrs = {}, options = {}) {
|
|
57
|
+
const prevId = this.id;
|
|
58
|
+
let hasSomethingChanged = false;
|
|
59
|
+
for (let attr of Object.keys(attrs)) {
|
|
60
|
+
if (!hasSomethingChanged && !isDeepEqual(this.attributes[attr], attrs[attr])) {
|
|
61
|
+
hasSomethingChanged = true;
|
|
62
|
+
}
|
|
63
|
+
options.unset ?
|
|
64
|
+
delete this.attributes[attr]
|
|
65
|
+
: (this.attributes[attr] = attrs[attr]);
|
|
66
|
+
}
|
|
67
|
+
if (this.constructor.idAttribute in attrs) {
|
|
68
|
+
this.id = this.get(this.constructor.idAttribute);
|
|
69
|
+
}
|
|
70
|
+
if (!options.silent && hasSomethingChanged) {
|
|
71
|
+
this.triggerUpdate();
|
|
72
|
+
if (this.collection && prevId && prevId !== this.id) {
|
|
73
|
+
this.collection._updateModelReference(this.id, prevId, this);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
unset(attr, options) {
|
|
79
|
+
return this.set({ [attr]: undefined }, { unset: true, ...options });
|
|
80
|
+
}
|
|
81
|
+
clear(options) {
|
|
82
|
+
const attrs = {};
|
|
83
|
+
for (let key of Object.keys(this.attributes)) {
|
|
84
|
+
attrs[key] = undefined;
|
|
85
|
+
}
|
|
86
|
+
return this.set(attrs, { unset: true, ...options });
|
|
87
|
+
}
|
|
88
|
+
fetch(options = {}) {
|
|
89
|
+
options = { parse: true, method: "GET", ...options };
|
|
90
|
+
return this.sync(this, options).then(([json, response]) => {
|
|
91
|
+
const serverAttrs = options.parse ? this.parse(json, options) : json;
|
|
92
|
+
this.set(serverAttrs, options);
|
|
93
|
+
this.triggerUpdate();
|
|
94
|
+
return [this, response];
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
save(attrs, options = {}) {
|
|
98
|
+
const previousAttributes = this.toJSON();
|
|
99
|
+
options = { parse: true, ...options };
|
|
100
|
+
attrs = attrs || this.toJSON();
|
|
101
|
+
if (!options.wait) {
|
|
102
|
+
this.set(attrs, options);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
options.attrs = { ...this.attributes, ...attrs };
|
|
106
|
+
}
|
|
107
|
+
options.method =
|
|
108
|
+
this.isNew() ? "POST"
|
|
109
|
+
: options.patch ? "PATCH"
|
|
110
|
+
: "PUT";
|
|
111
|
+
if (options.method === "PATCH" && !options.attrs) {
|
|
112
|
+
options.attrs = attrs;
|
|
113
|
+
}
|
|
114
|
+
return this.sync(this, options)
|
|
115
|
+
.then(([json, response]) => {
|
|
116
|
+
let serverAttrs = options.parse ? this.parse(json, options) : json;
|
|
117
|
+
if (options.wait) {
|
|
118
|
+
serverAttrs = { ...attrs, ...serverAttrs };
|
|
119
|
+
}
|
|
120
|
+
this.set(serverAttrs, { silent: true, ...options });
|
|
121
|
+
this.triggerUpdate();
|
|
122
|
+
return [this, response];
|
|
123
|
+
})
|
|
124
|
+
.catch((response) => {
|
|
125
|
+
if (!options.wait) {
|
|
126
|
+
this.clear({ silent: true }).set(previousAttributes, options);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
this.attributes = previousAttributes;
|
|
130
|
+
}
|
|
131
|
+
return Promise.reject(response);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
destroy(options = {}) {
|
|
135
|
+
const request = this.isNew() ? Promise.resolve([]) : this.sync(this, { method: "DELETE", ...options }), collection = this.collection;
|
|
136
|
+
if (!options.wait) {
|
|
137
|
+
this.triggerUpdate();
|
|
138
|
+
this.collection?.remove(this, { silent: true });
|
|
139
|
+
}
|
|
140
|
+
return request
|
|
141
|
+
.then(([json, response]) => {
|
|
142
|
+
if (options.wait && !this.isNew()) {
|
|
143
|
+
this.triggerUpdate();
|
|
144
|
+
this.collection?.remove(this, { silent: true });
|
|
145
|
+
}
|
|
146
|
+
return [this, response];
|
|
147
|
+
})
|
|
148
|
+
.catch((response) => {
|
|
149
|
+
if (!options.wait && !this.isNew()) {
|
|
150
|
+
collection?.add(this);
|
|
151
|
+
}
|
|
152
|
+
return Promise.reject(response);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
url(options = this.urlOptions) {
|
|
156
|
+
const base = result(this, "urlRoot", options) || result(this.collection, "url", options) || urlError();
|
|
157
|
+
if (this.isNew()) {
|
|
158
|
+
return base;
|
|
159
|
+
}
|
|
160
|
+
return (base.replace(/[^/]$/, "$&/") +
|
|
161
|
+
window.encodeURIComponent(this.get(this.constructor.idAttribute)));
|
|
162
|
+
}
|
|
163
|
+
parse(response, options) {
|
|
164
|
+
return response;
|
|
165
|
+
}
|
|
166
|
+
isNew() {
|
|
167
|
+
return !this.has(this.constructor.idAttribute);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
Object.assign(Model.prototype, Events);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ModelMap } from "./config.js";
|
|
2
|
+
import { noOp, once } from "./utils.js";
|
|
3
|
+
import { getCacheKey } from "./resourcerer.js";
|
|
4
|
+
import request from "./request.js";
|
|
5
|
+
const PREFETCH_TIMEOUT = 50;
|
|
6
|
+
export default (getResources, expectedProps = {}) => {
|
|
7
|
+
let fetched;
|
|
8
|
+
const resources = Object.entries(getResources(expectedProps) || {});
|
|
9
|
+
return (evt) => {
|
|
10
|
+
const { target } = evt;
|
|
11
|
+
let prefetchTimeout;
|
|
12
|
+
if (target && !fetched) {
|
|
13
|
+
prefetchTimeout = window.setTimeout(() => {
|
|
14
|
+
resources.forEach(([name, config]) => {
|
|
15
|
+
const resourceKey = config.resourceKey || name;
|
|
16
|
+
request(getCacheKey({ resourceKey, ...config }), ModelMap[resourceKey], config)
|
|
17
|
+
.catch(noOp);
|
|
18
|
+
});
|
|
19
|
+
fetched = true;
|
|
20
|
+
}, PREFETCH_TIMEOUT);
|
|
21
|
+
target.addEventListener("mouseleave", once(_onMouseLeave.bind(null, prefetchTimeout)));
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
function _onMouseLeave(timeout) {
|
|
26
|
+
window.clearTimeout(timeout);
|
|
27
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import Model from "./model.js";
|
|
2
|
+
import Collection from "./collection.js";
|
|
3
|
+
import { SyncOptions } from "./sync.js";
|
|
4
|
+
interface RequestOptions extends Pick<SyncOptions, "params"> {
|
|
5
|
+
data?: any;
|
|
6
|
+
component?: NonNullable<unknown>;
|
|
7
|
+
options?: Record<string, any>;
|
|
8
|
+
path?: Record<string, any>;
|
|
9
|
+
fetch?: boolean;
|
|
10
|
+
force?: boolean;
|
|
11
|
+
lazy?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare const getFromCache: (key: string) => Model<Record<string, any>, Record<string, any>> | Collection<object, object> | undefined;
|
|
14
|
+
export declare const existsInCache: (key: string) => boolean;
|
|
15
|
+
declare const _default: (key: string, Model: {
|
|
16
|
+
new (data: Record<string, any>, options: RequestOptions["options"]): Model;
|
|
17
|
+
} | {
|
|
18
|
+
new (data: Record<string, any>[], options: RequestOptions["options"]): Collection;
|
|
19
|
+
}, options?: RequestOptions) => Promise<[Model | Collection] | [Model | Collection, number]>;
|
|
20
|
+
export default _default;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import Collection from "./collection.js";
|
|
2
|
+
import ModelCache from "./model-cache.js";
|
|
3
|
+
const loadingCache = {};
|
|
4
|
+
export const getFromCache = (key) => ModelCache.get(key);
|
|
5
|
+
export const existsInCache = (key) => !!ModelCache.get(key);
|
|
6
|
+
export default (key, Model, options = {}) => {
|
|
7
|
+
let cachedModel = ModelCache.get(key);
|
|
8
|
+
let addToLoadingCache;
|
|
9
|
+
let _promise;
|
|
10
|
+
options = {
|
|
11
|
+
data: Model && "prototype" in Model && Model.prototype instanceof Collection ? [] : {},
|
|
12
|
+
params: {},
|
|
13
|
+
options: {},
|
|
14
|
+
fetch: true,
|
|
15
|
+
force: false,
|
|
16
|
+
lazy: false,
|
|
17
|
+
...options,
|
|
18
|
+
};
|
|
19
|
+
if (!loadingCache[key]) {
|
|
20
|
+
_promise = new Promise((resolve, reject) => {
|
|
21
|
+
if (!cachedModel || cachedModel.lazy || options.force) {
|
|
22
|
+
const model = cachedModel || new Model(options.data, { ...options.options, ...options.path });
|
|
23
|
+
if (options.fetch && !options.lazy) {
|
|
24
|
+
addToLoadingCache = true;
|
|
25
|
+
model.fetch({ params: options.params }).then(([newModel, response]) => {
|
|
26
|
+
delete loadingCache[key];
|
|
27
|
+
delete model.lazy;
|
|
28
|
+
ModelCache.put(key, newModel, options.component);
|
|
29
|
+
resolve([newModel, response?.status]);
|
|
30
|
+
}, (response) => {
|
|
31
|
+
delete loadingCache[key];
|
|
32
|
+
reject(response?.status);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
options.lazy && !ModelCache.get(key) ? (model.lazy = true) : null;
|
|
37
|
+
ModelCache.put(key, model, options.component);
|
|
38
|
+
resolve([model]);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
ModelCache.register(key, options.component);
|
|
43
|
+
resolve([cachedModel]);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
if (addToLoadingCache) {
|
|
47
|
+
loadingCache[key] = _promise;
|
|
48
|
+
}
|
|
49
|
+
return _promise;
|
|
50
|
+
}
|
|
51
|
+
ModelCache.register(key, options.component);
|
|
52
|
+
return loadingCache[key];
|
|
53
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ModelMap } from "./config.js";
|
|
2
|
+
import React, { type ComponentClass } from "react";
|
|
3
|
+
import ErrorBoundary from "./error-boundary.js";
|
|
4
|
+
import type { ExecutorFunction, ResourceConfigObj, InternalResourceConfigObj, ResourceKeys, UseResourcesResponse, WithModelSuffix } from "./types.js";
|
|
5
|
+
export declare function useResources<T extends ResourceKeys>(getResources: (props: Record<string, any>) => {
|
|
6
|
+
[Key in T]?: ResourceConfigObj;
|
|
7
|
+
}, _props: Record<string, any>): UseResourcesResponse & {
|
|
8
|
+
[Key in T as WithModelSuffix<Key, InstanceType<(typeof ModelMap)[Key]>>]: InstanceType<(typeof ModelMap)[Key]>;
|
|
9
|
+
};
|
|
10
|
+
export declare const withResources: (getResources: ExecutorFunction) => (Component: ComponentClass<Record<string, any>>) => (props: Record<string, any>) => React.CElement<{
|
|
11
|
+
children: React.ReactElement<any, string | React.JSXElementConstructor<any>>;
|
|
12
|
+
}, ErrorBoundary>;
|
|
13
|
+
export declare function getCacheKey({ resourceKey, params, path, data, }: InternalResourceConfigObj): string;
|