Show HN: Write ajax requests with template literals(github.com)
github.com
Show HN: Write ajax requests with template literals
https://github.com/Legitcode/legible
9 comments
I think the syntax is nicer if you take a look at the example of doing partial requests. I made this library for fun, but I'll be splitting out my requests into partials for an entire api. I think you get benefits from using the partials more so than your example. Either way, it was made just for fun :)
partial:
partial:
const twitter = {
register: partial`
url: https://api.twitter.com/register,
method: POST
`
}
twitter.register`
body: ${{
email: '[email protected]',
password: 'Tester'
}}
`
Without template literals: const twitter = {
register: {
url: 'https://api.myapp.com/register',
method: 'POST',
}
}
let response = await request({
...twitter.register,
body: body,
})The second example could be more concise IMHO:
const twitter = {
register: function(opts){
return request({
url: '...',
method: 'POST',
...opts
});
}
}
const resp = await twitter.register({ body });
EDIT: made it even more concise! -- back to real work :/Sure, keep adding many methods and make new functions every time. I totally get what you're saying. I just like the syntactic sugar of Template Literals :)
Plus you lose all the benefits of linters, type checkers, & even things like syntax highlighting.
I agree. Unfortunately I think this is only subjectively better to the author, provides less functionality than the example it compared itself too, and adds an extra dependency.
The only thing I disagree with is making an extra dependency sound like such a bad thing. Nobody uses fetch directly. You always wrap it into a more suitable api for your project. So either you're going to do that yourself, or add in a small library like this.
Such mischief. Template literals make me feel very yucky. A totally unnecessary complication.
yikes
better than:
Perhaps it's just personal style, but the readability of both is similar and it feels like you've created a subset of a JSON or YAML parser that isn't as bulletproof / flexible.
I did enjoy the example of how template literals are parsed under the hood.