A model to create some URL templates etched objects.
import url from '@etchedjs/url'
export const entity = url.extend({
segments: {
set entity (value) {
// to be validated
}
}
})
export const entityById = entity.extend({
segments: {
set id (value) {
// to be validated
}
}
})
export const entityRelated = entityById.extend({
segments: {
set related (value) {
// to be validated
}
}
})
export const home = url.extend({
origin: 'https://domain.tld'
})
export const accounts = entity.extend(home, {
segments: {
entity: 'accounts'
}
})
export const accountById = entityById.extend(accounts)
export const accountFriends = entityRelated.extend(accountById, {
segments: {
related: 'friends'
}
})
export const accountFriendsSearch = accountFriends.extend({
search: {
set name (value) {
// to be validated
}
}
})
const href = 'https://domain.tld/accounts/1/friends?name=Pierre&name=Paul'
console.log(accountFriendsSearch.fill({
search: {
name: ['Pierre', 'Paul']
},
segments: {
id: 1
}
}).toString())
/*
'https://domain.tld/accounts/1/friends?name=Pierre&name=Paul'
*/
console.log(accountFriendsSearch.parse(href).toString())
/*
'https://domain.tld/accounts/1/friends?name=Pierre&name=Paul'
*/
console.log(accountFriendsSearch.parse(href))
/*
{
hash: '',
origin: 'https://domain.tld',
search: {
name: [ 'Pierre', 'Paul' ],
[Symbol(Symbol.iterator)]: [GeneratorFunction: [Symbol.iterator]]
},
segments: {
entity: 'accounts',
id: '1',
related: 'friends',
[Symbol(Symbol.iterator)]: [GeneratorFunction: [Symbol.iterator]]
},
parse: [Function: parse],
toString: [Function: toString]
}
*/
The url
is an etched model that can be inherited with the following properties
Must be a string as described on https://developer.mozilla.org/en-US/docs/Web/API/URL/hash
Must be a string as described on https://developer.mozilla.org/en-US/docs/Web/API/URL/origin
Must be an etched instance of the default iterable url.search
Must be an etched instance of the default iterable url.segments
A method that returns a new model based on the current one, and the provided models
A method that returns a new instance based on the current one, and the provided mixins
A method that returns an instance of the current template but fulfilled by the provided string
A method that returns a route based on the current instance
A method that returns a string based on the current instance
MIT