Repository navigation
Expand file tree
/
Copy pathhref.js
More file actions
67 lines (60 loc) · 1.45 KB
/
Copy pathhref.js
File metadata and controls
67 lines (60 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as collection from '@lcf.vs/collection'
import api from '../lib/api.js'
function route (template) {
return Object.freeze({
fill (values) {
return Object.freeze({
toString () {
return template.replace(':id', encodeURIComponent(values.id))
}
})
},
parse (href) {
return Object.freeze({
id: decodeURIComponent(new URL(href).pathname.split('/').at(-1))
})
},
toString () {
return template
}
})
}
const routes = api.hrefAdapter({
build (model, values) {
return values === undefined
? model.toString()
: model.fill(values).toString()
},
parse (model, href) {
return model.parse(href)
}
})
const runtime = api.runtime({
hrefs: routes
})
const accountById = route('https://api.localhost/accounts/:id')
const accountHref = api.href(
accountById,
(account, { read }) => ({
id: read(account, 'id')
})
)
const target = api
.withRuntime(runtime)
.withCollection(api.collection.withHref('https://api.localhost'))
.withType({
fields: {
id: collection.property.withName('id'),
name: collection.property.withName('name')
},
itemHref: accountHref,
name: 'accounts'
})
const accounts = target.types().accounts
const document = accounts.items(accounts.collection.href, [{
id: 'a/b',
name: 'Lcf.vs'
}])
const href = document.collection.items[0].href
console.log(href)
console.log(accounts.parseHref(accountHref, href))