Skip to content

Commit 86c80a3

Browse files
committed
for consistens naming (prefer Resource & Collection over StoreValue)
1 parent c1865af commit 86c80a3

14 files changed

+197
-197
lines changed
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,74 @@
11
import { isEntityReference } from './halHelpers'
2-
import LoadingStoreCollection from './LoadingStoreCollection'
3-
import Resource from './interfaces/Resource'
4-
import Collection from './interfaces/Collection'
2+
import LoadingCollection from './LoadingCollection'
3+
import ResourceInterface from './interfaces/ResourceInterface'
4+
import CollectionInterface from './interfaces/CollectionInterface'
55
import { Link } from './interfaces/StoreData'
6-
import StoreValue from './StoreValue'
6+
import Resource from './Resource'
77

88
/**
99
* Filter out items that are marked as deleting (eager removal)
1010
*/
11-
function filterDeleting (array: Array<Resource>): Array<Resource> {
11+
function filterDeleting (array: Array<ResourceInterface>): Array<ResourceInterface> {
1212
return array.filter(entry => !entry._meta.deleting)
1313
}
1414

15-
class HasItems extends StoreValue {
15+
class Collection extends Resource {
1616
/**
1717
* Get items excluding ones marked as 'deleting' (eager remove)
1818
* The items property should always be a getter, in order to make the call to mapArrayOfEntityReferences
1919
* lazy, since that potentially fetches a large number of entities from the API.
2020
*/
21-
public get items (): Array<Resource> {
21+
public get items (): Array<ResourceInterface> {
2222
return filterDeleting(this._mapArrayOfEntityReferences(this._storeData.items))
2323
}
2424

2525
/**
2626
* Get all items including ones marked as 'deleting' (lazy remove)
2727
*/
28-
public get allItems (): Array<Resource> {
28+
public get allItems (): Array<ResourceInterface> {
2929
return this._mapArrayOfEntityReferences(this._storeData.items)
3030
}
3131

3232
/**
3333
* Returns a promise that resolves to the collection object, once all items have been loaded
3434
*/
35-
public $loadItems () :Promise<Collection> {
35+
public $loadItems () :Promise<CollectionInterface> {
3636
return this._itemLoader(this._storeData.items)
3737
}
3838

3939
/**
4040
* Returns a promise that resolves to the collection object, once all items have been loaded
4141
*/
42-
private _itemLoader (array: Array<Link>) : Promise<Collection> {
42+
private _itemLoader (array: Array<Link>) : Promise<CollectionInterface> {
4343
if (!this._containsUnknownEntityReference(array)) {
44-
return Promise.resolve(this as unknown as Collection) // we know that this object must be of type Collection
44+
return Promise.resolve(this as unknown as CollectionInterface) // we know that this object must be of type CollectionInterface
4545
}
4646

4747
// eager loading of 'fetchAllUri' (e.g. parent for embedded collections)
4848
if (this.config.avoidNPlusOneRequests) {
49-
return this.apiActions.reload(this as unknown as Collection) as Promise<Collection> // we know that reload resolves to a type Collection
49+
return this.apiActions.reload(this as unknown as CollectionInterface) as Promise<CollectionInterface> // we know that reload resolves to a type CollectionInterface
5050

51-
// no eager loading: replace each reference (Link) with a StoreValue (Resource)
51+
// no eager loading: replace each reference (Link) with a Resource (ResourceInterface)
5252
} else {
5353
const arrayWithReplacedReferences = this._replaceEntityReferences(array)
5454

5555
return Promise.all(
5656
arrayWithReplacedReferences.map(entry => entry._meta.load)
57-
).then(() => this as unknown as Collection) // we know that this object must be of type Collection
57+
).then(() => this as unknown as CollectionInterface) // we know that this object must be of type CollectionInterface
5858
}
5959
}
6060

6161
/**
6262
* Given an array, replaces any entity references in the array with the entity loaded from the Vuex store
6363
* (or from the API if necessary), and returns that as a new array. In case some of the entity references in
64-
* the array have not finished loading yet, returns a LoadingStoreCollection instead.
64+
* the array have not finished loading yet, returns a LoadingCollection instead.
6565
* @param array possibly mixed array of values and references
6666
* @param fetchAllUri URI that allows fetching all array items in a single network request, if known
6767
* @param fetchAllProperty property in the entity from fetchAllUri that will contain the array
68-
* @returns array the new array with replaced items, or a LoadingStoreCollection if any of the array
68+
* @returns array the new array with replaced items, or a LoadingCollection if any of the array
6969
* elements is still loading.
7070
*/
71-
private _mapArrayOfEntityReferences (array: Array<Link>): Array<Resource> {
71+
private _mapArrayOfEntityReferences (array: Array<Link>): Array<ResourceInterface> {
7272
if (!this._containsUnknownEntityReference(array)) {
7373
return this._replaceEntityReferences(array)
7474
}
@@ -77,18 +77,18 @@ class HasItems extends StoreValue {
7777

7878
// eager loading of 'fetchAllUri' (e.g. parent for embedded collections)
7979
if (this.config.avoidNPlusOneRequests) {
80-
return LoadingStoreCollection.create(itemsLoaded)
80+
return LoadingCollection.create(itemsLoaded)
8181

82-
// no eager loading: replace each reference (Link) with a StoreValue (Resource)
82+
// no eager loading: replace each reference (Link) with a Resource (ResourceInterface)
8383
} else {
84-
return LoadingStoreCollection.create(itemsLoaded, this._replaceEntityReferences(array))
84+
return LoadingCollection.create(itemsLoaded, this._replaceEntityReferences(array))
8585
}
8686
}
8787

8888
/**
89-
* Replace each item in array with a proper StoreValue (or LoadingStoreValue)
89+
* Replace each item in array with a proper Resource (or LoadingResource)
9090
*/
91-
private _replaceEntityReferences (array: Array<Link>): Array<Resource> {
91+
private _replaceEntityReferences (array: Array<Link>): Array<ResourceInterface> {
9292
return array
9393
.filter(entry => isEntityReference(entry))
9494
.map(entry => this.apiActions.get(entry.href))
@@ -102,4 +102,4 @@ class HasItems extends StoreValue {
102102
}
103103
}
104104

105-
export default HasItems
105+
export default Collection
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
import LoadingStoreValue from './LoadingStoreValue'
2-
import Resource from './interfaces/Resource'
1+
import LoadingResource from './LoadingResource'
2+
import ResourceInterface from './interfaces/ResourceInterface'
33

4-
class LoadingStoreCollection {
4+
class LoadingCollection {
55
/**
66
* Returns a placeholder for an array that has not yet finished loading from the API. The array placeholder
77
* will respond to functional calls (like .find(), .map(), etc.) with further LoadingStoreCollections or
88
* LoadingStoreValues. If passed the existingContent argument, random access and .length will also work.
99
* @param loadArray Promise that resolves once the array has finished loading
1010
* @param existingContent optionally set the elements that are already known, for random access
1111
*/
12-
static create (loadArray: Promise<Array<Resource> | undefined>, existingContent: Array<Resource> = []): Array<Resource> {
12+
static create (loadArray: Promise<Array<ResourceInterface> | undefined>, existingContent: Array<ResourceInterface> = []): Array<ResourceInterface> {
1313
// if Promsise resolves to undefined, provide empty array
14-
// this could happen if items is accessed from a LoadingStoreValue, which resolves to a normal entity without 'items'
14+
// this could happen if items is accessed from a LoadingResource, which resolves to a normal entity without 'items'
1515
const loadArraySafely = loadArray.then(array => array ?? [])
1616

17-
// proxy array function 'find' to a LoadingStoreValue (Resource)
17+
// proxy array function 'find' to a LoadingResource (Resource)
1818
const singleResultFunctions = ['find']
1919
singleResultFunctions.forEach(func => {
2020
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2121
existingContent[func] = (...args: any[]) => {
22-
const resultLoaded = loadArraySafely.then(array => array[func](...args) as Resource)
23-
return new LoadingStoreValue(resultLoaded)
22+
const resultLoaded = loadArraySafely.then(array => array[func](...args) as ResourceInterface)
23+
return new LoadingResource(resultLoaded)
2424
}
2525
})
2626

27-
// proxy array functions with multiple results to a LoadingStoreCollection (Array<Resource>)
27+
// proxy array functions with multiple results to a LoadingCollection (Array<ResourceInterface>)
2828
const arrayResultFunctions = ['map', 'flatMap', 'filter']
2929
arrayResultFunctions.forEach(func => {
3030
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3131
existingContent[func] = (...args: any[]) => {
32-
const resultLoaded = loadArraySafely.then(array => array[func](...args) as Array<Resource>) // TODO: return type for .map() is not necessarily an Array<Resource>
33-
return LoadingStoreCollection.create(resultLoaded)
32+
const resultLoaded = loadArraySafely.then(array => array[func](...args) as Array<ResourceInterface>) // TODO: return type for .map() is not necessarily an Array<ResourceInterface>
33+
return LoadingCollection.create(resultLoaded)
3434
}
3535
})
3636
return existingContent
3737
}
3838
}
3939

40-
export default LoadingStoreCollection
40+
export default LoadingCollection
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
import LoadingStoreCollection from './LoadingStoreCollection'
2-
import Resource from './interfaces/Resource'
3-
import Collection from './interfaces/Collection'
1+
import LoadingCollection from './LoadingCollection'
2+
import ResourceInterface from './interfaces/ResourceInterface'
3+
import CollectionInterface from './interfaces/CollectionInterface'
44

55
/**
66
* Creates a placeholder for an entity which has not yet finished loading from the API.
7-
* Such a LoadingStoreValue can safely be used in Vue components, since it will render as an empty
7+
* Such a LoadingResource can safely be used in Vue components, since it will render as an empty
88
* string and Vue's reactivity system will replace it with the real data once that is available.
99
*
10-
* Accessing nested functions in a LoadingStoreValue yields another LoadingStoreValue:
11-
* new LoadingStoreValue(...).author().organization() // gives another LoadingStoreValue
10+
* Accessing nested functions in a LoadingResource yields another LoadingResource:
11+
* new LoadingResource(...).author().organization() // gives another LoadingResource
1212
*
13-
* Using a LoadingStoreValue or a property of a LoadingStoreValue in a view renders to empty strings:
14-
* let user = new LoadingStoreValue(...)
13+
* Using a LoadingResource or a property of a LoadingResource in a view renders to empty strings:
14+
* let user = new LoadingResource(...)
1515
* 'The "' + user + '" is called "' + user.name + '"' // gives 'The "" is called ""'
1616
*/
17-
class LoadingStoreValue implements Resource {
17+
class LoadingResource implements ResourceInterface {
1818
public _meta: {
1919
self: string | null,
20-
load: Promise<Resource>
20+
load: Promise<ResourceInterface>
2121
loading: boolean
2222
}
2323

24-
private loadResource: Promise<Resource>
24+
private loadResource: Promise<ResourceInterface>
2525

2626
/**
27-
* @param entityLoaded a Promise that resolves to a StoreValue when the entity has finished
27+
* @param entityLoaded a Promise that resolves to a Resource when the entity has finished
2828
* loading from the API
2929
* @param absoluteSelf optional fully qualified URI of the entity being loaded, if available. If passed, the
30-
* returned LoadingStoreValue will return it in calls to .self and ._meta.self
30+
* returned LoadingResource will return it in calls to .self and ._meta.self
3131
*/
32-
constructor (loadResource: Promise<Resource>, absoluteSelf: string | null = null) {
32+
constructor (loadResource: Promise<ResourceInterface>, absoluteSelf: string | null = null) {
3333
this._meta = {
3434
self: absoluteSelf,
3535
load: loadResource,
@@ -39,28 +39,28 @@ class LoadingStoreValue implements Resource {
3939
this.loadResource = loadResource
4040

4141
const handler = {
42-
get: function (target: LoadingStoreValue, prop: string | number | symbol) {
43-
// This is necessary so that Vue's reactivity system understands to treat this LoadingStoreValue
42+
get: function (target: LoadingResource, prop: string | number | symbol) {
43+
// This is necessary so that Vue's reactivity system understands to treat this LoadingResource
4444
// like a normal object.
4545
if (prop === Symbol.toPrimitive) {
4646
return () => ''
4747
}
4848

49-
// This is necessary so that Vue's reactivity system understands to treat this LoadingStoreValue
49+
// This is necessary so that Vue's reactivity system understands to treat this LoadingResource
5050
// like a normal object.
5151
if (['then', Symbol.toStringTag, 'state', 'getters', '$options', '_isVue', '__file', 'render', 'constructor'].includes(prop as string)) {
5252
return undefined
5353
}
5454

55-
// proxy to properties that actually exist on LoadingStoreValue (_meta, $reload, etc.)
55+
// proxy to properties that actually exist on LoadingResource (_meta, $reload, etc.)
5656
if (Reflect.has(target, prop)) {
5757
return Reflect.get(target, prop)
5858
}
5959

60-
// Proxy to all other unknown properties: return a function that yields another LoadingStoreValue
60+
// Proxy to all other unknown properties: return a function that yields another LoadingResource
6161
const loadProperty = loadResource.then(resource => resource[prop])
6262

63-
const result = templateParams => new LoadingStoreValue(loadProperty.then(property => {
63+
const result = templateParams => new LoadingResource(loadProperty.then(property => {
6464
try {
6565
return property(templateParams)._meta.load
6666
} catch (e) {
@@ -76,28 +76,28 @@ class LoadingStoreValue implements Resource {
7676
return new Proxy(this, handler)
7777
}
7878

79-
get items (): Array<Resource> {
80-
return LoadingStoreCollection.create(this.loadResource.then(resource => (resource as Collection).items))
79+
get items (): Array<ResourceInterface> {
80+
return LoadingCollection.create(this.loadResource.then(resource => (resource as CollectionInterface).items))
8181
}
8282

83-
get allItems (): Array<Resource> {
84-
return LoadingStoreCollection.create(this.loadResource.then(resource => (resource as Collection).allItems))
83+
get allItems (): Array<ResourceInterface> {
84+
return LoadingCollection.create(this.loadResource.then(resource => (resource as CollectionInterface).allItems))
8585
}
8686

87-
public $reload (): Promise<Resource> {
87+
public $reload (): Promise<ResourceInterface> {
8888
// Skip reloading entities that are already loading
8989
return this._meta.load
9090
}
9191

92-
public $loadItems (): Promise<Collection> {
93-
return this._meta.load.then(resource => (resource as Collection).$loadItems())
92+
public $loadItems (): Promise<CollectionInterface> {
93+
return this._meta.load.then(resource => (resource as CollectionInterface).$loadItems())
9494
}
9595

96-
public $post (data: unknown): Promise<Resource | null> {
96+
public $post (data: unknown): Promise<ResourceInterface | null> {
9797
return this._meta.load.then(resource => resource.$post(data))
9898
}
9999

100-
public $patch (data: unknown): Promise<Resource> {
100+
public $patch (data: unknown): Promise<ResourceInterface> {
101101
return this._meta.load.then(resource => resource.$patch(data))
102102
}
103103

@@ -114,4 +114,4 @@ class LoadingStoreValue implements Resource {
114114
}
115115
}
116116

117-
export default LoadingStoreValue
117+
export default LoadingResource

src/StoreValue.ts renamed to src/Resource.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import urltemplate from 'url-template'
22
import { isTemplatedLink, isVirtualLink, isEntityReference } from './halHelpers'
3-
import Resource from './interfaces/Resource'
3+
import ResourceInterface from './interfaces/ResourceInterface'
44
import ApiActions from './interfaces/ApiActions'
55
import { StoreData } from './interfaces/StoreData'
6-
import StoreValueCreator from './StoreValueCreator'
6+
import ResourceCreator from './ResourceCreator'
77
import { InternalConfig } from './interfaces/Config'
88

99
/**
10-
* Represents an actual StoreValue, by wrapping the given Vuex store storeData. The storeData must not be loading.
10+
* Represents an actual Resource, by wrapping the given Vuex store storeData. The storeData must not be loading.
1111
* If the storeData has been loaded into the store before but is currently reloading, the old storeData will be
1212
* returned, along with a ._meta.load promise that resolves when the reload is complete.
1313
*/
14-
class StoreValue implements Resource {
14+
class Resource implements ResourceInterface {
1515
public _meta: {
1616
self: string,
17-
load: Promise<Resource>
17+
load: Promise<ResourceInterface>
1818
loading: boolean
1919
}
2020

@@ -25,10 +25,10 @@ class StoreValue implements Resource {
2525
/**
2626
* @param storeData fully loaded entity storeData from the Vuex store
2727
* @param apiActions inject dependency: API actions
28-
* @param storeValueCreator inject dependency StoreValue factory
28+
* @param resourceCreator inject dependency Resource factory
2929
* @param config inject dependency: config options
3030
*/
31-
constructor (storeData: StoreData, apiActions: ApiActions, storeValueCreator: StoreValueCreator, config: InternalConfig) {
31+
constructor (storeData: StoreData, apiActions: ApiActions, resourceCreator: ResourceCreator, config: InternalConfig) {
3232
this.apiActions = apiActions
3333
this.config = config
3434
this._storeData = storeData
@@ -58,7 +58,7 @@ class StoreValue implements Resource {
5858

5959
// Use a trivial load promise to break endless recursion, except if we are currently reloading the storeData from the API
6060
const loadResource = storeData._meta.reloading
61-
? (storeData._meta.load as Promise<StoreData>).then(reloadedData => storeValueCreator.wrap(reloadedData))
61+
? (storeData._meta.load as Promise<StoreData>).then(reloadedData => resourceCreator.wrap(reloadedData))
6262
: Promise.resolve(this)
6363

6464
// Use a shallow clone of _meta, since we don't want to overwrite the ._meta.load promise or self link in the Vuex store
@@ -69,19 +69,19 @@ class StoreValue implements Resource {
6969
}
7070
}
7171

72-
$reload (): Promise<Resource> {
72+
$reload (): Promise<ResourceInterface> {
7373
return this.apiActions.reload(this)
7474
}
7575

76-
$post (data: unknown): Promise<Resource | null> {
76+
$post (data: unknown): Promise<ResourceInterface | null> {
7777
if (this.isVirtual()) {
7878
throw new Error('$post is not implemented for virtual resources')
7979
}
8080

8181
return this.apiActions.post(this._meta.self, data)
8282
}
8383

84-
$patch (data: unknown): Promise<Resource> {
84+
$patch (data: unknown): Promise<ResourceInterface> {
8585
if (this.isVirtual()) {
8686
throw new Error('$patch is not implemented for virtual resources')
8787
}
@@ -120,4 +120,4 @@ class StoreValue implements Resource {
120120
}
121121
}
122122

123-
export default StoreValue
123+
export default Resource

0 commit comments

Comments
 (0)