-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Is your feature request related to a problem? Please describe.
I'm hosting a blog post on /blog/[slug].svelte. This blog post is powered by /blog/[slug].json, generated by an endpoint. In the endpoint we want to include some html which serves as the body of the blog post. This html is composed by markdown, and its location is defined by an articleUrl parameter.
The final output for /blog/[slug].json might look something like this:
{
"slug": "my-article",
"articleUrl": "/articles/my-article.md",
"articleHtml": "<h1>My article</h1>\n<p>This is my article</p>"
}Making relative requests to fetch additional content, let's say /articles/my-article.md, is awkward if you use something like node-fetch. You have to make assumptions about the hosted environment, ergo:
import fetch from "node-fetch";
export async function get(request) {
(...)
const url = new URL(project.articleUrl, "http://localhost:3000");
const res = await fetch(url);
const markdown = await res.text();
project.articleHtml = renderMarkdown(markdown);
return {
headers: {
'Content-Type': 'application/json'
},
body: project
}
}Describe the solution you'd like
If fetch was available in endpoints, similar to how they are implemented in load() for pages, this could easily be solved like:
export async function get(request, fetch) {
(...)
const res = await fetch(project.articleUrl);
const markdown = await res.text();
project.articleHtml = renderMarkdown(markdown);
return {
headers: {
'Content-Type': 'application/json'
},
body: project
}
}Describe alternatives you've considered
node-fetch would normally be an option, but since we are dealing a relative path to a locally hosted file there are the shortcomings elaborated on above.
How important is this feature to you?
It's not really obvious how to solve this particular issue without this feature, so it could help prevent some headaches in the future if there was parity with load().