resourcerer 1.0.0-alpha-3 → 1.0.0-beta-4

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/index.js CHANGED
@@ -12,9 +12,3 @@ export {
12
12
  } from './lib/utils.js';
13
13
  export {ModelMap, ResourceKeys, ResourcesConfig} from './lib/config.js';
14
14
  export {useResources, withResources} from './lib/resourcerer.js';
15
- export {
16
- findDataCarrier,
17
- findDataChild,
18
- getRenderedResourceComponents,
19
- waitsFor
20
- } from './test/test-utils.js';
package/lib/collection.js CHANGED
@@ -22,17 +22,24 @@ export default class Collection {
22
22
  * through the parse method and {silent: true} to not trigger any updates. in the default
23
23
  * constructor, use:
24
24
  *
25
- * * model {Model} - dynamically overrides the static model property for the type of Model that
25
+ * * Model {Model} - dynamically overrides the static model property for the type of Model that
26
26
  * this collection's models should be instances of
27
27
  * * comparator {function|string} - dynamically overrides the static comparator property used to
28
28
  * sort the collection
29
29
  */
30
30
  constructor(models, options={}) {
31
+ const RESERVED_OPTION_KEYS = ['Model', 'comparator', 'silent', 'parse'];
32
+
31
33
  this.Model = options.Model || this.constructor.Model;
32
34
  this.comparator = options.comparator || this.constructor.comparator;
33
35
 
34
36
  this._reset();
35
37
 
38
+ this.urlOptions = Object.keys(options).reduce((memo, key) => Object.assign(
39
+ memo,
40
+ !RESERVED_OPTION_KEYS.includes(key) ? {[key]: options[key]} : {}
41
+ ), {});
42
+
36
43
  if (models) {
37
44
  // silent for completeness, i guess. but a nothing triggered here because it's impossible for
38
45
  // a listener to have been attached at this point
@@ -54,6 +61,13 @@ export default class Collection {
54
61
  */
55
62
  static cacheFields = []
56
63
 
64
+ /**
65
+ * Use this to tell resourcerer to track this collection's request time via the `track` method
66
+ * added in the resourcerer configuration file. This can be a boolean or a function that returns a
67
+ * boolean. If the latter, it takes a the resource config object as an argument.
68
+ */
69
+ static measure = false
70
+
57
71
  /**
58
72
  * Similar to the method for an individual model, this maps through each model in the collection
59
73
  * and returns its data attributes.
package/lib/config.js CHANGED
@@ -106,12 +106,12 @@ export const ResourcesConfig = {
106
106
  * but that won't handle complex values (arrays and objects). To add support for that, override
107
107
  * this method with any logic or stringify library you want.
108
108
  *
109
- * @param {object} data - request data
109
+ * @param {object} params - request params
110
110
  * @param {object} options - request options map
111
111
  * @return {string} URL query parameter string
112
112
  */
113
- stringify(data, options) {
114
- return new URLSearchParams(data).toString();
113
+ stringify(params, options) {
114
+ return new URLSearchParams(params).toString();
115
115
  },
116
116
 
117
117
  /** {function}: Hook to send resource request times to a tracking service. */
package/lib/model.js CHANGED
@@ -19,12 +19,15 @@ export default class Model {
19
19
  /**
20
20
  * @param {object} attributes - initial server data representation to be kept on the model
21
21
  * @param {object} options - generic map. in the default constructor, use:
22
+ * url() method
22
23
  * * parse {boolean} - if true, runs the initial attributes through the parse function beefore
23
24
  * setting on the model
24
25
  * * collection {Collection} - links this model to a collection, if applicable
25
26
  * * ...any other options that .set() takes
26
27
  */
27
28
  constructor(attributes={}, options={}) {
29
+ const RESERVED_OPTION_KEYS = ['parse', 'collection', 'silent', 'unset'];
30
+
28
31
  this.cid = uniqueId('c');
29
32
  this.attributes = {};
30
33
 
@@ -36,6 +39,11 @@ export default class Model {
36
39
  attributes = this.parse(attributes, options) || {};
37
40
  }
38
41
 
42
+ this.urlOptions = Object.keys(options).reduce((memo, key) => Object.assign(
43
+ memo,
44
+ !RESERVED_OPTION_KEYS.includes(key) ? {[key]: options[key]} : {}
45
+ ), {});
46
+
39
47
  this.set({...result(this.constructor, 'defaults'), ...attributes}, options);
40
48
  }
41
49
 
@@ -60,6 +68,13 @@ export default class Model {
60
68
  */
61
69
  static defaults = {}
62
70
 
71
+ /**
72
+ * Use this to tell resourcerer to track this model's request time via the `track` method added
73
+ * in the resourcerer configuration file. This can be a boolean or a function that returns a
74
+ * boolean. If the latter, it takes a the resource config object as an argument.
75
+ */
76
+ static measure = false
77
+
63
78
  /**
64
79
  * Returns a copy of the model's `attributes` object. Use this method to get the current entire
65
80
  * server data representation.
@@ -307,8 +322,9 @@ export default class Model {
307
322
  *
308
323
  * @return {string} the url endpoint to request for this particular model instance
309
324
  */
310
- url() {
311
- var base = result(this, 'urlRoot') || result(this.collection, 'url') || urlError();
325
+ url(options=this.urlOptions) {
326
+ var base = result(this, 'urlRoot', options) || result(this.collection, 'url', options) ||
327
+ urlError();
312
328
 
313
329
  if (this.isNew()) {
314
330
  return base;
package/lib/prefetch.js CHANGED
@@ -13,14 +13,14 @@ const PREFETCH_TIMEOUT = 50;
13
13
  *
14
14
  * @param {function} getResources - function that takes props as an argument
15
15
  * and should return a map of resources. Identical to those used in
16
- * `withResources` HOC
16
+ * `useResources`/`withResources`
17
17
  * @param {object} expectedProps - props in the form expected when the resource
18
18
  * would be needed
19
19
  * @return {function} callback to be invoked onMouseEnter of appropriate DOM el
20
20
  */
21
21
  export default (getResources, expectedProps={}) => {
22
22
  var fetched,
23
- resources = Object.entries(getResources(expectedProps, ResourceKeys) || {});
23
+ resources = Object.entries(getResources(ResourceKeys, expectedProps) || {});
24
24
 
25
25
  return (evt) => {
26
26
  var {target} = evt,
package/lib/request.js CHANGED
@@ -35,12 +35,11 @@ export const existsInCache = (key) => !!ModelCache.get(key);
35
35
  * @param {function} Model - the model constructor
36
36
  * @param {object} options - options object used for fetching that can include:
37
37
  *
38
- * * attributes {object} - attributes to pass a Model instance
39
- * * data {object} - data to pass into the fetch method
38
+ * * data {object|object[]} - attributes or models to pass a Model or Collection instance, resp.
39
+ * * params {object} - query params to pass into the fetch method
40
40
  * * fetch {boolean} - whether fetch the model after creation
41
41
  * * forceFetch {boolean} - force the fetch to be made if the model is already cached
42
42
  * * method {string} - request type (GET|POST|PUT|DELETE)
43
- * * models {array} - models to pass a Collection instance
44
43
  * * options {object} - options to pass to the Model constructor
45
44
  * * prefetch {boolean} - whether the request should be treated as a prefetched resource
46
45
  *
@@ -52,24 +51,24 @@ export default (key, Model, options={}) => {
52
51
  _promise;
53
52
 
54
53
  options = {
55
- attributes: {},
56
- data: {},
54
+ data: Model && Model.prototype instanceof Collection ? [] : {},
55
+ params: {},
56
+ options: {},
57
57
  fetch: true,
58
58
  forceFetch: false,
59
59
  method: 'GET',
60
- models: [],
61
60
  ...options
62
61
  };
63
62
 
64
63
  if (!loadingCache[key]) {
65
64
  _promise = new Promise((resolve, reject) => {
66
65
  if (!model || options.forceFetch) {
67
- model = model || new Model(options[getFirstArgPropertyName(Model)], options.options || {});
66
+ model = model || new Model(options.data, options.options);
68
67
 
69
68
  if (options.fetch) {
70
69
  addToLoadingCache = true;
71
70
 
72
- model.fetch({data: options.data}).then(
71
+ model.fetch({params: options.params}).then(
73
72
  ([newModel, response]) => {
74
73
  delete loadingCache[key];
75
74
  ModelCache.put(key, newModel, options.component);
@@ -111,14 +110,3 @@ export default (key, Model, options={}) => {
111
110
  // this way we can attach more .then() handlers
112
111
  return loadingCache[key];
113
112
  };
114
-
115
- /**
116
- * Helper to determine whether to pass the `models` property or the `attributes`
117
- * value of an options hash as a model instantiation's first argument.
118
- *
119
- * @param {Model|Collection} Constructor - model constructor
120
- * @return {string} property name to pass to model instantiation
121
- */
122
- export function getFirstArgPropertyName(Constructor) {
123
- return Constructor.prototype instanceof Collection ? 'models' : 'attributes';
124
- }