• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ecamp / hal-json-vuex / 13321877237

13 Feb 2025 10:43PM UTC coverage: 84.708%. Remained the same
13321877237

push

github

web-flow
Update babel monorepo to v7.26.8

159 of 206 branches covered (77.18%)

Branch coverage included in aggregate %.

262 of 291 relevant lines covered (90.03%)

740.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.48
/src/StoreValue.js
1
import urltemplate from 'url-template'
2
import { isTemplatedLink, isEntityReference, isCollection } from './halHelpers.js'
3
import QueryablePromise from './QueryablePromise.js'
4
import EmbeddedCollection from './EmbeddedCollection.js'
5
import CanHaveItems from './CanHaveItems.js'
6

7
/**
8
 * Creates an actual StoreValue, by wrapping the given Vuex store data. The data must not be loading.
9
 * If the data has been loaded into the store before but is currently reloading, the old data will be
10
 * returned, along with a ._meta.load promise that resolves when the reload is complete.
11
 * @param data fully loaded entity data from the Vuex store
12
 */
13
class StoreValue extends CanHaveItems {
14
  constructor (data, { get, reload, post, patch, del, isUnknown }, StoreValueCreator, config) {
15
    super({ get, reload, isUnknown }, config)
1,413✔
16

17
    this.apiActions = { get, reload, post, patch, del, isUnknown }
1,413✔
18
    this.config = config
1,413✔
19

20
    Object.keys(data).forEach(key => {
1,413✔
21
      const value = data[key]
5,334✔
22
      if (key === 'allItems' && isCollection(data)) return
5,334!
23
      if (key === 'items' && isCollection(data)) {
5,334✔
24
        this.addItemsGetter(data[key], data._meta.self, key)
144✔
25
      } else if (Array.isArray(value) && value.length > 0 && isEntityReference(value[0])) { // need min. 1 item to detect an embedded collection
5,190✔
26
        this[key] = () => new EmbeddedCollection(value, data._meta.self, key, { get, reload, isUnknown }, config, data._meta.load)
150✔
27
      } else if (isEntityReference(value)) {
5,040✔
28
        this[key] = () => this.apiActions.get(value.href)
954✔
29
      } else if (isTemplatedLink(value)) {
4,086✔
30
        this[key] = templateParams => this.apiActions.get(urltemplate.parse(value.href).expand(templateParams || {}))
24!
31
      } else {
32
        this[key] = value
4,062✔
33
      }
34
    })
35

36
    // Use a trivial load promise to break endless recursion, except if we are currently reloading the data from the API
37
    const loadedPromise = data._meta.load && !data._meta.load[Symbol.for('done')]
1,413✔
38
      ? data._meta.load.then(reloadedData => StoreValueCreator.wrap(reloadedData))
105✔
39
      : QueryablePromise.resolve(this)
40

41
    // Use a shallow clone of _meta, since we don't want to overwrite the ._meta.load promise or self link in the Vuex store
42
    this._meta = { ...data._meta, load: loadedPromise, self: this.config.apiRoot + data._meta.self }
1,413✔
43
  }
44

45
  $reload () {
46
    return this.apiActions.reload(this._meta.self)
3✔
47
  }
48

49
  $loadItems () {
50
    return this._meta.load
6✔
51
  }
52

53
  $post (data) {
54
    return this.apiActions.post(this._meta.self, data)
6✔
55
  }
56

57
  $patch (data) {
58
    return this.apiActions.patch(this._meta.self, data)
6✔
59
  }
60

61
  $del () {
62
    return this.apiActions.del(this._meta.self)
6✔
63
  }
64
}
65

66
export default StoreValue
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc