resourcerer 1.2.3 → 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.
Files changed (59) hide show
  1. package/README.md +133 -164
  2. package/build/index.d.ts +9 -0
  3. package/build/index.js +9 -0
  4. package/build/lib/collection.d.ts +107 -0
  5. package/build/lib/collection.js +251 -0
  6. package/build/lib/config.d.ts +20 -0
  7. package/build/lib/config.js +23 -0
  8. package/build/lib/error-boundary.d.ts +12 -0
  9. package/build/lib/error-boundary.js +18 -0
  10. package/build/lib/events.d.ts +11 -0
  11. package/build/lib/events.js +12 -0
  12. package/build/lib/model-cache.d.ts +13 -0
  13. package/build/lib/model-cache.js +62 -0
  14. package/build/lib/model.d.ts +48 -0
  15. package/build/lib/model.js +170 -0
  16. package/build/lib/prefetch.d.ts +3 -0
  17. package/build/lib/prefetch.js +27 -0
  18. package/build/lib/request.d.ts +20 -0
  19. package/build/lib/request.js +53 -0
  20. package/build/lib/resourcerer.d.ts +13 -0
  21. package/build/lib/resourcerer.js +406 -0
  22. package/build/lib/sync.d.ts +18 -0
  23. package/build/lib/sync.js +58 -0
  24. package/build/lib/types.d.ts +66 -0
  25. package/build/lib/types.js +1 -0
  26. package/build/lib/utils.d.ts +14 -0
  27. package/build/lib/utils.js +87 -0
  28. package/docs/advanced_topics.md +9 -77
  29. package/docs/collection.md +28 -28
  30. package/docs/migrating_to_2.0.md +72 -0
  31. package/docs/model.md +21 -21
  32. package/docs/typescript.md +72 -22
  33. package/package.json +27 -30
  34. package/resourcerer.d.ts +58 -0
  35. package/test/collection.test.js +305 -316
  36. package/test/config.test.js +21 -75
  37. package/test/events.test.js +42 -41
  38. package/test/model-cache.test.js +99 -77
  39. package/test/model.test.js +204 -209
  40. package/test/prefetch.test.js +35 -61
  41. package/test/request.test.js +104 -109
  42. package/test/sync.test.js +122 -109
  43. package/test/utils.test.js +173 -159
  44. package/index.d.ts +0 -270
  45. package/index.js +0 -14
  46. package/lib/collection.js +0 -584
  47. package/lib/config.js +0 -131
  48. package/lib/constants.js +0 -6
  49. package/lib/error-boundary.js +0 -47
  50. package/lib/events.js +0 -19
  51. package/lib/model-cache.js +0 -131
  52. package/lib/model.js +0 -372
  53. package/lib/prefetch.js +0 -59
  54. package/lib/request.js +0 -126
  55. package/lib/resourcerer.js +0 -985
  56. package/lib/sync.js +0 -111
  57. package/lib/utils.js +0 -220
  58. package/test/model-mocks.js +0 -110
  59. package/test/setup.js +0 -63
@@ -83,9 +83,9 @@ Using `dependsOn` in simple cases like the one highlighted in the [README](https
83
83
  Another way to effectively have a dependent resource is to use a conditional in your `getResources` method:
84
84
 
85
85
  ```js
86
- const getResources = (ResourceKeys, props) => ({
87
- [ResourceKeys.TODOS]: {},
88
- ...(props.todoId ? {[ResourceKeys.TODO_ITEM]: {data: {id: props.todoId}} : {})
86
+ const getResources = (props) => ({
87
+ todoos: {},
88
+ ...(props.todoId ? {todoItem: {data: {id: props.todoId}} : {})
89
89
  });
90
90
  ```
91
91
 
@@ -101,10 +101,10 @@ In general, using `dependsOn` is much more preferable, both in terms of semantic
101
101
  That doesn’t mean that the conditional can’t be useful—it’s just that its use should be relegated to components that have two discrete forms—one in which the dependent prop is always present, and one in which the dependent prop is never present. If you’re unsure whether a prop might exist, notably because it comes from a providing resource, you should use `dependsOn`. A good example of when to use a conditional is in this fake component that sometimes fetches a user model and sometimes fetches an order model depending on the presence of an `orderId` prop:
102
102
 
103
103
  ```js
104
- const getResources = (ResourceKeys, {userId, orderId}) => ({
105
- ...userId ? {[ResourceKeys.USER]: {options: {userId}} : {},
104
+ const getResources = ({userId, orderId}) => ({
105
+ ...userId ? {user: {path: {userId}} : {},
106
106
  ...!userId && orderId ? {
107
- [ResourceKeys.ORDER]: {
107
+ order: {
108
108
  noncritical: true,
109
109
  data: {id: orderId}
110
110
  }
@@ -117,74 +117,6 @@ In this case, when the component is used as an order component (denoted by the p
117
117
  For all other uses of dependent resources, we should use `dependsOn`.
118
118
 
119
119
 
120
- ## Unfetched Resources
121
-
122
- You may find, at some point in your application, that you have a `PUT` endpoint for a resource but no `GET`; the 'read portion' of the resource is received as part of some parent resource. For example, imagine you have an accounts resource at `/accounts/{account_id}`, whose response has a `config` property with some account configuration settings. To update the configuration, you make a `PUT` to `/accounts/{account_id}/config`. But reading comes from the parent resource. `resourcerer` supports this via a `providesModels` static property on the model:
123
-
124
- ```js
125
- // resources_conifg.js
126
- import {ResourceKeys, UnfetchedResources} from 'resourcerer';
127
-
128
- ResourceKeys.add({
129
- ACCOUNT: 'account',
130
- ACCOUNT_CONFIG: 'accountConfig'
131
- });
132
-
133
- // add the ACCOUNT_CONFIG key to our set of UnfetchedResources
134
- UnfetchedResources.add(ResourceKeys.ACCOUNT_CONFIG);
135
-
136
-
137
- // account_model.js
138
- export default class AccountModel extends Model {
139
- url() {
140
- return `/accounts/${this.accountId}`;
141
- }
142
-
143
- static dependencies = ['accountId'];
144
-
145
- static providesModels = (accountModel, ResourceKeys) => [{
146
- data: accountModel.get('config'),
147
- modelKey: ResourceKeys.ACCOUNT_CONFIG,
148
- options: {accountModel}
149
- }]
150
- })
151
- ```
152
-
153
- The `providesModels` property is a function that takes the parent model and the `ResourceKeys` as arguments and returns an array of resource configs. The resource configs have the same schema as the those used in our general executor functions. What this tells `resourcerer` to do is, after the parent model returns, instantiate the child model(s) and place them into the `ModelCache`. In other components, you can then access the model you know to exist in a `withResources` or `useResources` declaration:
154
-
155
- ```js
156
- // child_component.jsx, rendered only after the account model is known to return
157
- @withResources((ResourceKeys, props) => ({[ResourceKeys.ACCOUNT_CONFIG]: {}}))
158
- class ChildComponent extends React.Component {
159
- // component has this.props.accountConfigModel from the cache!
160
- onClickSomething() {
161
- // and now you can update the config directly :)
162
- this.props.accountConfigModel.save();
163
- }
164
- }
165
- ```
166
-
167
- In general, this should be used in cases where you can ascertain that the parent model has returned before trying to access the child model. However, if by chance it has not, and the child is not found in the cache, `resourcerer` will still not attempt to fetch it, because it is listed within the `UnfetchedResources` set. In that case, the model will get instantiated with no seed data and passed as a prop.
168
-
169
- Also, note that the `modelKey` property is required here instead of optionally being inferred from the resource config's object property, as is the case in our general `useResources`/`withResources` declarations. This is because here, in contrast, the models are simply placed in the cache and not actually used as props for any component, so they don't need to be named. Accordingly, resource configs are also returned as a list here instead of an object.
170
-
171
- The resource config objects within `providesModels` have the same schema, as mentioned, as normal. But they also accept an additional optional property, `shouldCache`, which is a function that takes the parent model and the resource config as an argument. If the function exists and returns false, the model will not get instantiated nor placed in the cache:
172
-
173
- ```js
174
- // account_model.js
175
- export default class AccountModel extends Model {
176
- // ...
177
- static dependencies = ['accountId'];
178
-
179
- static providesModels = (accountModel, ResourceKeys) => [{
180
- data: accountModel.get('config'),
181
- modelKey: ResourceKeys.ACCOUNT_CONFIG,
182
- options: {accountModel},
183
- // if this returns false, account config won't get instantiated and placed in the ModelCache
184
- shouldCache: (accountModel, config) => accountModel.get('state') === 'ACTIVE'
185
- }]
186
- })
187
- ```
188
120
 
189
121
  ## Loading Overlays
190
122
 
@@ -198,7 +130,7 @@ Use it like:
198
130
  ```jsx
199
131
  import {useResources} from 'resourcerer';
200
132
 
201
- const getResources = ({TODOS}, props) => ({[TODOS]: {}});
133
+ const getResources = (props) => ({todos: {}});
202
134
 
203
135
  export default function UserTodos(props) {
204
136
  const {isLoading, hasInitiallyLoaded, todosCollection} = useResources(getResources, props);
@@ -235,8 +167,8 @@ this:
235
167
  ```jsx
236
168
  import {useResources} from 'resourcerer';
237
169
 
238
- const getResources = ({TODO}, props) => ({
239
- [TODO]: {
170
+ const getResources = (props) => ({
171
+ todo: {
240
172
  data: {id: props.id},
241
173
  fetch: !!props.id
242
174
  }
@@ -3,12 +3,12 @@
3
3
  A collection is a client-side abstraction for the latest-saved server state of a list of objects (a model, by contrast, represents a single persisted object in that resource). A collection _must_ have one thing:
4
4
  a `url`. This instance property can be a string literal or a function.
5
5
 
6
- ```js
7
- class MyTodos extends Collection {
6
+ ```ts
7
+ class MyTodos extends Collection<ModelType> {
8
8
  url = '/todos'
9
9
  }
10
10
 
11
- class MyTodos extends Collection {
11
+ class MyTodos extends Collection<ModelType> {
12
12
  url({subtype}) {
13
13
  return `/todos/${subtype}`;
14
14
  }
@@ -47,17 +47,17 @@ This property tells resourcerer how to determine whether to make a new request o
47
47
  The number of milliseconds to keep all collections of this class in the cache after all client components stop referencing it. Note that this is on a collection _class_ basis and not an _instance_ basis because the latter can introduce race conditions into your application.
48
48
 
49
49
  ### `static` measure
50
- `boolean|function`
50
+ `boolean | (obj: ResourceConfigObject) => boolean`
51
51
 
52
52
  A boolean or function that accepts a [resource configuration object](https://github.com/noahgrant/resourcerer#nomenclature) and returns a boolean, telling resourcerer to track this collection's request time and report it via the `track` method setup in [configuration](https://github.com/noahgrant/resourcerer#configuring-resourcerer).
53
53
 
54
- ### `static` modelIdAttribute
54
+ ### `static` idAttribute
55
55
 
56
56
  Use this as a shortcut when you don't want to define a custom Model class just because the collection doesn't contain the default id field (which is `'id'`), ie:
57
57
 
58
58
  ```js
59
59
  // the collection will index its models based on the `name` property instead of the default `id` property
60
- static modelIdAttribute = 'name'
60
+ static idAttribute = 'name'
61
61
  ```
62
62
 
63
63
 
@@ -67,13 +67,13 @@ static modelIdAttribute = 'name'
67
67
  ### constructor
68
68
 
69
69
  ```js
70
- constructor: void (models: Array<Object|Model>, options: object)
70
+ constructor: (models: Array<Record<string, any> | Model>, options: object) => void
71
71
  ```
72
72
 
73
- The Collection's constructor gets passed any initial models, as well as the [options](https://github.com/noahgrant/resourcerer#options) from the executor function. Override this to add some custom logic or instance variables for the collection&mdash;just be sure to pass the arguments to its `.super()` call, as well:
73
+ The Collection's constructor gets passed any initial models, as well as the [options](https://github.com/noahgrant/resourcerer#path) from the executor function. Override this to add some custom logic or instance variables for the collection&mdash;just be sure to pass the arguments to its `.super()` call, as well:
74
74
 
75
75
  ```js
76
- class MyCollection extends Collection {
76
+ class MyCollection extends Collection<ModelType> {
77
77
  constructor(models, options={}) {
78
78
  super(models, options);
79
79
 
@@ -87,19 +87,19 @@ class MyCollection extends Collection {
87
87
  }
88
88
  ```
89
89
 
90
- Passing in a `Model` option or a `comparator` option to an instance's constructor will override the statically defined properties on its constructor. Other `options` fields (the ones passed from the executor function [options](https://github.com/noahgrant/resourcerer#options) property) are passed to the `url` as shown in the example above.
90
+ Passing in a `Model` option or a `comparator` option to an instance's constructor will override the statically defined properties on its constructor. Other `path` fields (the ones passed from the executor function [options](https://github.com/noahgrant/resourcerer#path) property) are passed to the `url` as shown in the example above.
91
91
 
92
92
 
93
93
  ### add
94
94
  ```js
95
- add: Collection (models: (Object|Model)|Array<Object|Model>, options: Object)
95
+ add: (models: Record<string, any> | Model | Array<Record<string, any> | Model>, options: Object) => this
96
96
  ```
97
97
 
98
98
  Add a new entry or list of entries into the collection. Each entry can be an object of data or a Model instance. Will trigger an update in all subscribed components unless the `trigger: true` option is passed. You can also pass a `parse: true` option to run the model through its [parse](/docs/model.md#parse) method before setting its properties. If an entry already exists on the collection, the new properties will get merged into its existing model.
99
99
 
100
100
  ### create
101
101
  ```js
102
- create: Promise<[Model, Response]> (model: (Object|Model), options: Object)
102
+ create: (model: Record<string, any> | Model, options: Object) => Promise<[Model, Response]>
103
103
  ```
104
104
 
105
105
  Adds a new entry to the collection and persists it to the server. This is literally the equivalent to calling `collection.add()` and then `model.save()`. Because it also instantiates the new model, be sure to pass any path params you need in your url as the options argument (the same [options](https://github.com/noahgrant/resourcerer#options) in the resource config object). The returned Promise is the same as is returned from [Model#save](/docs/model.md#save). If the request errors, the model is auto-removed from the collection. Pass the `wait: true` option to wait to add the new model until after the save request returns. Subscribed components will update when the new entry is added as well as when the request returns.
@@ -108,7 +108,7 @@ Adds a new entry to the collection and persists it to the server. This is litera
108
108
 
109
109
  ### fetch
110
110
  ```js
111
- fetch: Promise<[Collection, Response]> (options: Object)
111
+ fetch: (options: object) => Promise<[this, Response]>
112
112
  ```
113
113
 
114
114
  This is the method that `resourcerer` uses internally to get server data and set its parsed response as models on the collection. This should rarely need to be used in your application. Subscribed components will update when the request returns.
@@ -118,21 +118,21 @@ This is the method that `resourcerer` uses internally to get server data and set
118
118
 
119
119
  ### get
120
120
  ```js
121
- get: Model? (identifier: string|number)
121
+ get: (identifier: string | number) => Model | undefined
122
122
  ```
123
123
 
124
124
  Collections index their Model instances by either the Model's [`idAttribute`](/docs/model.md#static-idattribute) or, equivalently as a shortcut, by the collection's own static [`modelIdAttribute`](#static-modelidattribute) property. The `.get()` method takes an id value and returns the quick-lookup model instance if one exists.
125
125
 
126
126
  ### has
127
127
  ```js
128
- has: boolean (identifier: string|number|Model|object)
128
+ has: (identifier: string |number | Model | object) => boolean
129
129
  ```
130
130
 
131
131
  Returns whether or not a model exists in a collection. You can pass the model instance itself, a model's data, or a model's id.
132
132
 
133
133
  ### parse
134
134
  ```js
135
- parse: Array<Object> (response: any)
135
+ parse: (response: any) => Array<Object>
136
136
  ```
137
137
 
138
138
  This method takes in the raw server data response and should return the data in the form that should be set on the Collection. It defaults to the identity function, which might suffice for many endpoints. Override this for your custom needs. For example, for some search results with some metadata (in an API that returns a JSON object), we want to set the results as the collection:
@@ -149,21 +149,21 @@ parse(response) {
149
149
 
150
150
  ### remove
151
151
  ```js
152
- remove: Collection (models: (Object|Model)|Array<Object|Model>, options: Object)
152
+ remove: (models: Record<string, any> | Model | Array<Record<string, any> | Model>, options: Object) => this
153
153
  ```
154
154
 
155
155
  Use this to remove a model or models from the collection, which should not often be needed. You can pass in anything or a list of anything that can be accepted via [.get()](#get). Pass in `silent: true` for subscribed components _not_ to get rerendered.
156
156
 
157
157
  ### reset
158
158
  ```js
159
- reset: Collection (models: Array<Object|Model>, options: Object)
159
+ reset: (models: Array<Record<string, any> | Model>, options: Object) => this
160
160
  ```
161
161
 
162
162
  Removes all models and their references from a collection and replaces them with the models passed in. Pass in `silent: true` for subscribed components _not_ to get rerendered, and `parse: true` to have data get parsed before being set on their respective models.
163
163
 
164
164
  ### set
165
165
  ```js
166
- set: Collection (models: (Object|Model)|Array<Object|Model>, options: Object)
166
+ set: (models: Record<string, any> | Model | Array<Record<string, any> | Model>, options: Object) => this
167
167
  ```
168
168
 
169
169
  This is the method that many other write methods (`add`, `remove`, `save`, `reset`, etc) use under the hood, and it should _rarely if ever_ need to be used directly in your application. Sets new data as models and merges existing data with their models, and sorts as necessary. Pass in `silent: true` for subscribed components _not_ to get rerendered, and `parse: true` to have data get parsed before being set on their respective models.
@@ -182,7 +182,7 @@ Collection.sync = Model.sync = function(model, options) {
182
182
 
183
183
  ### toJSON
184
184
  ```js
185
- toJSON: Array<Object> ()
185
+ toJSON: () => ModelType[]
186
186
  ```
187
187
 
188
188
  Returns each model's data objects in a new array.
@@ -191,56 +191,56 @@ Returns each model's data objects in a new array.
191
191
 
192
192
  ### at
193
193
  ```js
194
- at: Model (index: number)
194
+ at: (index: number) => Model
195
195
  ```
196
196
 
197
197
  Returns the model at a given index in the collection. Index can be negative to count backwards from the end.
198
198
 
199
199
  ### filter
200
200
  ```js
201
- filter: Array<Model> (predicate: function)
201
+ filter: (predicate: (Model) => boolean) Model[]
202
202
  ```
203
203
 
204
204
  Same signature as Array.prototype.filter across a collection's models.
205
205
 
206
206
  ### find
207
207
  ```js
208
- find: Model? (predicate: function)
208
+ find: (predicate: (Model) => boolean) => Model | undefined
209
209
  ```
210
210
 
211
211
  Same signature as Array.prototype.find across a collection's models.
212
212
 
213
213
  ### findWhere
214
214
  ```js
215
- findWhere: Model? (attrs: object)
215
+ findWhere: (attrs: Partial<ModelType>) => Model | undefined
216
216
  ```
217
217
 
218
218
  Returns the model matching the attributes passed in, or undefined if no match is found. Like `.find` but a shorthand that uses matching attribute values instead of a predicate function.
219
219
 
220
220
  ### map
221
221
  ```js
222
- map: Array<any> (predicate: function)
222
+ map: (predicate: (Model) => any) => any[]
223
223
  ```
224
224
 
225
225
  Same signature as Array.prototype.map across a collection's models.
226
226
 
227
227
  ### pluck
228
228
  ```js
229
- pluck: Array<any> (attribute: string)
229
+ pluck: (attribute: string) => any[]
230
230
  ```
231
231
 
232
232
  Returns a list of the specified attribute value for all models.
233
233
 
234
234
  ### slice
235
235
  ```js
236
- slice: Array<Model> (startIndex:number[, endIndex: number])
236
+ slice: (startIndex: number[, endIndex: number]) => Model[]
237
237
  ```
238
238
 
239
239
  Same signature as Array.prototype.slice across a collection's models.
240
240
 
241
241
  ### where
242
242
  ```js
243
- where: Array<Model> (attrs: object)
243
+ where: (attrs: Partial<ModelType>) => Model[]
244
244
  ```
245
245
 
246
246
  Returns a list of models matching the data values passed in. Like `.filter` but a shorthand that uses matching data values instead of a predicate function.
@@ -0,0 +1,72 @@
1
+ # Migrating to v2.0
2
+
3
+ The core methodology and functionality of `resourcerer` has not changed in version 2.0, but many of the APIs have. Here is a list of the changes:
4
+
5
+ 1. `ModelMap#add` has been removed. Import `register` from `resourcerer` instead.
6
+
7
+ 1. There are no longer any `ResourceKeys` that are separate from the keys set on the `ModelMap`. They are one and the same:
8
+
9
+ ```js
10
+ // previous
11
+ ResourceKeys.add({
12
+ TODOS: "todos",
13
+ });
14
+
15
+ ModelMap.add({ [ResourceKeys.TODOS]: TodosCollection });
16
+
17
+ // now
18
+ register({
19
+ // `todos` is the resource key you will use in useResources Executor Functions
20
+ todos: TodosCollection,
21
+ });
22
+ ```
23
+
24
+ Typescript will ensure that your resource keys are consistent when using `useResources`. If you're not using Typescript, you'll have to manage this yourself.
25
+
26
+ Many other 2.0 changes stem from this change.
27
+
28
+ 1. [ExecutorFunctions](/#nomenclature) only take a single argument, the `props` argument. Previously, the first argument was the `ResourceKeys` object.
29
+
30
+ 1. `static cacheFields` in Models and Collections have been removed. Use `static dependencies` instead.
31
+
32
+ 1. `static modelIdAttribute` on a Collection, which changed the data attribute demarcating uniqueness in the Collection's models without having to create a new model, has been renamed to just `static idAttribute`, the same as on the Model itself.
33
+
34
+ 1. In the [Resource Configuration Object](/#nomenclature), the `options` property has been renamed to `path`, since it is effectively only used to provide values to url path parameters.
35
+
36
+ 1. The utility methods `haveAllLoaded`, `areAnyLoading`, and `haveAnyErrored` have been placed under a top-level `Utils` object and are now named `Utils.hasLoaded`, `Utils.isLoading`, and `Utils.hasErrored`, resepectively, to provide consistency with other loading state names. Their signatures have not changed.
37
+
38
+ 1. The [refetch](/#refetching) method returned from `useResources` no longer accepts a function as an argument. Pass an array of resource keys directly instead:
39
+
40
+ ```js
41
+ // previously
42
+ refetch(({todos}) => [todos]);
43
+
44
+ // now
45
+ refetch([todos]);
46
+ ```
47
+
48
+ 1. [dependsOn](/#serial-requests) is no longer an array of strings that checks existence of prop fields. To be more versatile, it is a boolean, and will fetch the resource whenever its conditions evaluate to true:
49
+
50
+ ```js
51
+ // previously
52
+ dependsOn: ["userId"]
53
+
54
+ // now
55
+ dependsOn: !!props.userId
56
+ ```
57
+
58
+ 1. [provides](/#serial-requests) is now just a function that returns an object, which can add as multiple values to component state simultaneously:
59
+
60
+ ```js
61
+ // previously
62
+ provides: {userId: (queueItemModel) => queueItemModel.get('userId')}
63
+
64
+ // now
65
+ provides: (queueItemModel) => ({userId: queueItemModel.get('userId')})
66
+ ```
67
+
68
+ As such, the special spread character has been removed.
69
+
70
+ 1. `providesModels`, which turned arbitraray data into a listenable model, has been removed.
71
+
72
+ 1. The configuration option `queryParamsPropName`, which auto-flattened url parameters nested within a prop, has been removed.
package/docs/model.md CHANGED
@@ -5,16 +5,16 @@ a `url`. This can come in several forms:
5
5
 
6
6
  * a string literal
7
7
 
8
- ```js
9
- class MyModel extends Model {
8
+ ```ts
9
+ class MyModel extends Model<ModelType> {
10
10
  url = '/endpoint'
11
11
  }
12
12
  ```
13
13
 
14
14
  * a function
15
15
 
16
- ```js
17
- class MyModel extends Model {
16
+ ```ts
17
+ class MyModel extends Model<ModelType> {
18
18
  url() {
19
19
  return '/endpoint';
20
20
  }
@@ -41,7 +41,7 @@ that you might find useful in rendering your data-hydrated components.
41
41
  ## Properties
42
42
 
43
43
  ### `static` dependencies
44
- `Array<string|function>`
44
+ `Array<string | function>`
45
45
 
46
46
  This property tells resourcerer how to determine whether to make a new request or to take a model out of the cache. It is an array of strings or functions from which its cache key is calculated. See the [cacheKey](https://github.com/noahgrant/resourcerer#caching-resources-with-modelcache) section for more info.
47
47
 
@@ -56,12 +56,12 @@ The number of milliseconds to keep all models of this class in the cache after a
56
56
  Override this to be the property name of the Model's unique identifier if it something other than `'id'`. Each model instance will get an `id` instance property that will be equal to that value. i.e. if the Model class has `static idAttribute = 'email'` and the Model is instantiated with `{email: 'noah@gmail.com'}`, then `model.id === 'noah@gmail.com'`.
57
57
 
58
58
  ### `static` defaults
59
- `object|function`
59
+ `Record<string, any>` | () => Record<string, any>`
60
60
 
61
61
  An object or function that returns object with attribute keys and their default values. If set, then when the model is instantiated, any missing data get set to these values.
62
62
 
63
63
  ### `static` measure
64
- `boolean|function`
64
+ `boolean | (obj: ResourceConfigObject) => boolean)`
65
65
 
66
66
  A boolean or function that accepts a [resource configuration object](https://github.com/noahgrant/resourcerer#nomenclature) and returns a boolean, telling resourcerer to track this model's request time and report it via the `track` method setup in [configuration](https://github.com/noahgrant/resourcerer#configuring-resourcerer).
67
67
 
@@ -72,13 +72,13 @@ A boolean or function that accepts a [resource configuration object](https://git
72
72
  ### constructor
73
73
 
74
74
  ```js
75
- constructor: void (initialData: Object, options: object)
75
+ constructor: (initialData: object, options: object) => void
76
76
  ```
77
77
 
78
78
  The Model's constructor gets passed any initial data, as well as the [options](https://github.com/noahgrant/resourcerer#options) from the executor function. Override this to add some custom logic or instance variables for the model&mdash;just be sure to pass the arguments to its `.super()` call, as well:
79
79
 
80
- ```js
81
- class MyModel extends Model {
80
+ ```ts
81
+ class MyModel extends Model<ModelType> {
82
82
  constructor(initialData, options={}) {
83
83
  super(initialData, options);
84
84
 
@@ -103,21 +103,21 @@ Returns the model's data in a new object.
103
103
 
104
104
  ### get
105
105
  ```js
106
- get: any (attribute: string)
106
+ get: (attribute: keyof ModelType) => ModelType[keyof ModelType]
107
107
  ```
108
108
 
109
109
  Returns the value at the given attribute key.
110
110
 
111
111
  ### has
112
112
  ```js
113
- has: boolean (attribute: string)
113
+ has: (attribute: string) => boolean
114
114
  ```
115
115
 
116
116
  Returns true if the model has a defined value at the given attribute key
117
117
 
118
118
  ### isNew
119
119
  ```js
120
- isNew: boolean ()
120
+ isNew: () => boolean
121
121
  ```
122
122
 
123
123
  By default, a model is considered 'new' if it does not have an id (or a value at the [`idAttribute`](#static-idattribute) proeprty. It is used to determine:
@@ -136,7 +136,7 @@ isNew() {
136
136
 
137
137
  ### parse
138
138
  ```js
139
- parse: object (response: any)
139
+ parse: (response: any) => ModelType
140
140
  ```
141
141
 
142
142
  This method takes in the raw server data response and should return the data in the form that should be set on the Model. It defaults to the identity function, which might suffice for many endpoints. Override this for your custom needs. For example, maybe your server overly-nests the resource in a `schema` property:
@@ -149,35 +149,35 @@ parse(response) {
149
149
 
150
150
  ### set
151
151
  ```js
152
- set: Model (data: object, options: object)
152
+ set: (data: object, options: object) => this
153
153
  ```
154
154
 
155
155
  This is the main avenue by which a model's properties get values assigned. It's called internally by several public methods, including `save`, `unset`, and `clear`. Pass a `silent: true` option for this not to trigger a re-render for subscribed components.
156
156
 
157
157
  ### unset
158
158
  ```js
159
- unset: Model (attribute: string, options: object)
159
+ unset: (attribute: string, options?: {silent: boolean}) => this
160
160
  ```
161
161
 
162
162
  Removes the attribute from the model's data. Pass a `silent: true` option for this not to trigger a re-render for subscribed components.
163
163
 
164
164
  ### clear
165
165
  ```js
166
- clear: Model (options: object)
166
+ clear: (options?: {silent: boolean}) => this
167
167
  ```
168
168
 
169
169
  Removes all data from the model. Pass a `silent: true` option for this not to trigger a re-render for subscribed components.
170
170
 
171
171
  ### pick
172
172
  ```js
173
- pick: object (...data: Array<string>)
173
+ pick: <K extends keyof ModelType>(...data: K[]) => Record<K, ModelType[K]>
174
174
  ```
175
175
 
176
176
  Handy helper method to only return a subset of a model's data, as opposed to the whole thing like [`.toJSON()`](#tojson) does.
177
177
 
178
178
  ### save
179
179
  ```js
180
- save: Promise<[Model, Response]> (data: object, options: object)
180
+ save: (attrs: Partial<ModelType>, options?: {wait?: boolean; patch?: boolean}) => Promise<[Model, Response]>
181
181
  ```
182
182
 
183
183
  Use this to persist data mutations to the server. If [`.isNew()`](#isnew) is true, the request will be sent as a POST. Otherwise, it will be sent as a PUT with the whole resource, or a PATCH with only `data` sent over if the `patch: true` option is passed. When the request returns, the server data is passed through the [`.parse()`](#parse) method before being set on the model. Pass the `wait: true` option to wait to add the data until after the server responds. Subscribed components will update when the new entry is added as well as when the request returns. If the request errors, all changes will be reverted and components updated.
@@ -186,7 +186,7 @@ Use this to persist data mutations to the server. If [`.isNew()`](#isnew) is tru
186
186
 
187
187
  ### destroy
188
188
  ```js
189
- destroy: Promise<[Model, Response]> (options: object)
189
+ destroy: (options?: {wait: true; silent: true}) => Promise<[Model, Response]>
190
190
  ```
191
191
 
192
192
  Use this to remove the send a DELETE request at this model's url to the server. The model is removed from its collection if it belongs to one. If [`.isNew()`](#isnew) is false (signifying that the model was never persisted in the first place), a request is not sent, but the model is still removed from its collection. Pass the `wait: true` option to wait to remove the model until after the server responds. Subscribed components will update when the model is removed. If the request errors, the model will get added back to its collection and components will get updated.
@@ -195,7 +195,7 @@ Use this to remove the send a DELETE request at this model's url to the server.
195
195
 
196
196
  ### fetch
197
197
  ```js
198
- fetch: Promise<[Model, Response]> (options: Object)
198
+ fetch: (options: Object) => Promise<[Model, Response]>
199
199
  ```
200
200
 
201
201
  This is the method that `resourcerer` uses internally to get server data and set its parsed response as models on the collection. This should rarely need to be used in your application. Subscribed components will update when the request returns.