Skip to content

Commit c73f3e3

Browse files
Merge pull request #271 from usu/feat/short-self-links
Feat: short self links without apiRoot
2 parents 3472d3e + d518a84 commit c73f3e3

File tree

9 files changed

+131
-85
lines changed

9 files changed

+131
-85
lines changed

src/LoadingResource.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import LoadingCollection from './LoadingCollection'
22
import ResourceInterface from './interfaces/ResourceInterface'
33
import CollectionInterface from './interfaces/CollectionInterface'
4+
import { InternalConfig } from './interfaces/Config'
45

56
/**
67
* Creates a placeholder for an entity which has not yet finished loading from the API.
@@ -17,6 +18,7 @@ import CollectionInterface from './interfaces/CollectionInterface'
1718
class LoadingResource implements ResourceInterface {
1819
public _meta: {
1920
self: string | null,
21+
selfUrl: string | null,
2022
load: Promise<ResourceInterface>
2123
loading: boolean
2224
}
@@ -26,12 +28,13 @@ class LoadingResource implements ResourceInterface {
2628
/**
2729
* @param entityLoaded a Promise that resolves to a Resource when the entity has finished
2830
* loading from the API
29-
* @param absoluteSelf optional fully qualified URI of the entity being loaded, if available. If passed, the
31+
* @param self optional URI of the entity being loaded, if available. If passed, the
3032
* returned LoadingResource will return it in calls to .self and ._meta.self
3133
*/
32-
constructor (loadResource: Promise<ResourceInterface>, absoluteSelf: string | null = null) {
34+
constructor (loadResource: Promise<ResourceInterface>, self: string | null = null, config: InternalConfig | null = null) {
3335
this._meta = {
34-
self: absoluteSelf,
36+
self: self,
37+
selfUrl: self ? config?.apiRoot + self : null,
3538
load: loadResource,
3639
loading: true
3740
}
@@ -64,7 +67,7 @@ class LoadingResource implements ResourceInterface {
6467
try {
6568
return property(templateParams)._meta.load
6669
} catch (e) {
67-
throw new Error(`Property '${prop.toString()}' on resource ${absoluteSelf} was used like a relation, but no relation with this name was returned by the API (actual return value: ${JSON.stringify(property)})`)
70+
throw new Error(`Property '${prop.toString()}' on resource '${self}' was used like a relation, but no relation with this name was returned by the API (actual return value: ${JSON.stringify(property)})`)
6871
}
6972
}
7073
))

src/Resource.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { InternalConfig } from './interfaces/Config'
1414
class Resource implements ResourceInterface {
1515
public _meta: {
1616
self: string,
17+
selfUrl: string,
1718
load: Promise<ResourceInterface>
1819
loading: boolean
1920
}
@@ -65,7 +66,8 @@ class Resource implements ResourceInterface {
6566
this._meta = {
6667
...storeData._meta,
6768
load: loadResource,
68-
self: this.config.apiRoot + storeData._meta.self
69+
self: storeData._meta.self,
70+
selfUrl: this.config.apiRoot + storeData._meta.self
6971
}
7072
}
7173

src/ResourceCreator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ResourceCreator {
4949
// Resource is loading --> return LoadingResource
5050
if (meta.loading) {
5151
const loadResource = (meta.load as Promise<StoreData>).then(storeData => this.wrapData(storeData))
52-
return new LoadingResource(loadResource, this.config.apiRoot + meta.self)
52+
return new LoadingResource(loadResource, meta.self, this.config)
5353

5454
// Resource is not loading --> wrap actual data
5555
} else {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
252252
if (rel.templated) {
253253
return urltemplate.parse(rel.href).expand(templateParams)
254254
}
255-
return axios.defaults.baseURL + rel.href
255+
return rel.href
256256
}
257257

258258
/**

src/interfaces/ResourceInterface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { StoreData, VirtualStoreData } from './StoreData'
77
interface ResourceInterface {
88
_meta: {
99
self: string | null
10+
selfUrl: string | null
1011
load: Promise<ResourceInterface>
1112
loading: boolean
1213
deleting?: boolean

tests/apiOperations.spec.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('Using dollar methods', () => {
7575

7676
// then
7777
await letNetworkRequestFinish()
78-
expect(await load).toMatchObject({ id: 1, _meta: { self: 'http://localhost/camps' } })
78+
expect(await load).toMatchObject({ id: 1, _meta: { self: '/camps' } })
7979
done()
8080
})
8181

@@ -99,7 +99,7 @@ describe('Using dollar methods', () => {
9999

100100
// then
101101
await letNetworkRequestFinish()
102-
expect(await load).toMatchObject({ id: 2, _meta: { self: 'http://localhost/camps' } })
102+
expect(await load).toMatchObject({ id: 2, _meta: { self: '/camps' } })
103103
done()
104104
})
105105

@@ -161,14 +161,14 @@ describe('Using dollar methods', () => {
161161

162162
// then
163163
await letNetworkRequestFinish()
164-
expect(await load).toMatchObject({ id: 1, _meta: { self: 'http://localhost/camps/1' } })
165-
expect(vm.api.get('/camps/1')).toMatchObject({ id: 1, _meta: { self: 'http://localhost/camps/1' } })
164+
expect(await load).toMatchObject({ id: 1, _meta: { self: '/camps/1' } })
165+
expect(vm.api.get('/camps/1')).toMatchObject({ id: 1, _meta: { self: '/camps/1' } })
166166
expect(vm.api.get('/campTypes/20')).toMatchObject({
167167
id: 20,
168168
name: 'camp',
169169
js: true,
170170
targetGroup: 'Kids',
171-
_meta: { self: 'http://localhost/campTypes/20' }
171+
_meta: { self: '/campTypes/20' }
172172
})
173173
done()
174174
})
@@ -223,14 +223,14 @@ describe('Using dollar methods', () => {
223223

224224
// then
225225
await letNetworkRequestFinish()
226-
expect(await load).toMatchObject({ id: 1, _meta: { self: 'http://localhost/camps/1' } })
227-
expect(vm.api.get('/camps/1')).toMatchObject({ id: 1, _meta: { self: 'http://localhost/camps/1' } })
226+
expect(await load).toMatchObject({ id: 1, _meta: { self: '/camps/1' } })
227+
expect(vm.api.get('/camps/1')).toMatchObject({ id: 1, _meta: { self: '/camps/1' } })
228228
expect(vm.api.get('/campTypes/20')).toMatchObject({
229229
id: 20,
230230
name: 'camp',
231231
js: true,
232232
targetGroup: 'Kids',
233-
_meta: { self: 'http://localhost/campTypes/20' }
233+
_meta: { self: '/campTypes/20' }
234234
})
235235
done()
236236
})
@@ -268,7 +268,7 @@ describe('Using dollar methods', () => {
268268

269269
// then
270270
await letNetworkRequestFinish()
271-
expect(await load).toMatchObject({ some: 'thing', _meta: { self: 'http://localhost/camps' } })
271+
expect(await load).toMatchObject({ some: 'thing', _meta: { self: '/camps' } })
272272
done()
273273
})
274274

@@ -303,7 +303,7 @@ describe('Using dollar methods', () => {
303303

304304
// then
305305
await letNetworkRequestFinish()
306-
expect(await load).toMatchObject({ some: 'thing', _meta: { self: 'http://localhost/camps' } })
306+
expect(await load).toMatchObject({ some: 'thing', _meta: { self: '/camps' } })
307307
done()
308308
})
309309

@@ -341,7 +341,7 @@ describe('Using dollar methods', () => {
341341

342342
// then
343343
await letNetworkRequestFinish()
344-
expect(hrefPromise).resolves.toEqual('http://localhost/camps/1/activities')
344+
expect(hrefPromise).resolves.toEqual('/camps/1/activities')
345345
done()
346346
})
347347

@@ -359,7 +359,7 @@ describe('Using dollar methods', () => {
359359

360360
// then
361361
await letNetworkRequestFinish()
362-
expect(hrefPromise).resolves.toEqual('http://localhost/camps/1/users/999')
362+
expect(hrefPromise).resolves.toEqual('/camps/1/users/999')
363363
done()
364364
})
365365

@@ -377,7 +377,7 @@ describe('Using dollar methods', () => {
377377

378378
// then
379379
await letNetworkRequestFinish()
380-
expect(hrefPromise).resolves.toEqual('http://localhost/books')
380+
expect(hrefPromise).resolves.toEqual('/books')
381381
done()
382382
})
383383

@@ -432,7 +432,7 @@ describe('Using dollar methods', () => {
432432
await letNetworkRequestFinish()
433433
const result = (await load).items
434434
expect(result).toHaveLength(1)
435-
expect(result[0]).toMatchObject({ id: 123, _meta: { self: 'http://localhost/items/123' } })
435+
expect(result[0]).toMatchObject({ id: 123, _meta: { self: '/items/123' } })
436436
done()
437437
})
438438

@@ -467,7 +467,7 @@ describe('Using dollar methods', () => {
467467
await letNetworkRequestFinish()
468468
const result = (await load).items
469469
expect(result).toHaveLength(1)
470-
expect(result[0]).toMatchObject({ id: 123, _meta: { self: 'http://localhost/items/123' } })
470+
expect(result[0]).toMatchObject({ id: 123, _meta: { self: '/items/123' } })
471471
done()
472472
})
473473

@@ -555,7 +555,7 @@ describe('Using dollar methods', () => {
555555
id: 1028,
556556
name: 'The first chapter',
557557
_meta: {
558-
self: 'http://localhost/chapters/1028'
558+
self: '/chapters/1028'
559559
}
560560
})
561561
done()
@@ -643,7 +643,7 @@ describe('Using dollar methods', () => {
643643
id: 1028,
644644
name: 'The first chapter',
645645
_meta: {
646-
self: 'http://localhost/chapters/1028'
646+
self: '/chapters/1028'
647647
}
648648
})
649649
done()

tests/resources/root-with-link.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"href": "/"
88
},
99
"books": {
10-
"href": "http://localhost/books{/id}",
10+
"href": "/books{/id}",
1111
"templated": true
1212
}
1313
}

tests/resources/templated-link.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"href": "/camps/1"
77
},
88
"users": {
9-
"href": "http://localhost/camps/1/users{/id}",
9+
"href": "/camps/1/users{/id}",
1010
"templated": true
1111
}
1212
}
@@ -24,7 +24,7 @@
2424
"/camps/1": {
2525
"id": 1,
2626
"users": {
27-
"href": "http://localhost/camps/1/users{/id}",
27+
"href": "/camps/1/users{/id}",
2828
"templated": true
2929
},
3030
"_meta": {
@@ -36,7 +36,7 @@
3636
"/camps/1": {
3737
"id": 1,
3838
"users": {
39-
"href": "http://localhost/camps/1/users{/id}",
39+
"href": "/camps/1/users{/id}",
4040
"templated": true
4141
},
4242
"_meta": {
@@ -51,4 +51,4 @@
5151
}
5252
}
5353
}
54-
}
54+
}

0 commit comments

Comments
 (0)