question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Direct communication with external API

See original GitHub issue

The main idea is getting a shorter path to API from SSR and CSR without extra hops and proxies. For that, we should have different server URLs and client URLs for the same resource ID. Even now you can have different URLs for fetch between server and client but our serialization system detect resource by the whole URL and produce double request in that situation. This feature request is not something new and several times accrued in Sapper tracker for example https://github.com/sveltejs/sapper/issues/489 Also, I did some rouge PR for this here https://github.com/sveltejs/kit/pull/1406, and you can find some extra details there. It looks like we should more detail describe this feature and cases to make a clean solution.

It’s a little confusing theme, but I want to hear your opinions and ideas.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:18 (18 by maintainers)

github_iconTop GitHub Comments

5reactions
Rich-Harriscommented, May 12, 2021

There’s an issue around browser-sent request headers here: https://github.com/sveltejs/kit/issues/696. I think what we’d need to do here is take the initial info, init and normalise them into a Request object that includes whichever request headers we decide to include.

So perhaps the interface is actually more like this, which conveniently deals with the footgun mentioned above:

/**
 * @param {Request} req
 * @returns {Response}
 */
export async function serverFetch(req) {
  const data = await get_data_somehow(req.url);
  return new Response(data); // https://developer.mozilla.org/en-US/docs/Web/API/Response/Response
}
2reactions
Rich-Harriscommented, May 12, 2021

Oh, the custom fetch replacement idea is really interesting. Lots of flexibility there. I would maybe suggest this interface:

/**
 * @param {RequestInfo} info
 * @param {RequestInit} init
 * @returns {Response}
 */
export async function serverFetch(info, init) {
  // potential footgun: RequestInfo can be a string or a Request object with a url property
  // — people would need to decide for themselves whether to accommodate the latter
  const data = await get_data_somehow(info);
  return new Response(data); // https://developer.mozilla.org/en-US/docs/Web/API/Response/Response
}

There’s no need to pass fetch in as it’s globally available inside a SvelteKit app (along with Response, Request and Headers).

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Calls: What They Are & How to Make Them in 5 Easy Steps
An API call is the process of a client application submitting a request to an API and that API retrieving the requested data...
Read more >
How to Use an API: Just the Basics 2022 | TechnologyAdvice
An API is how two computers talk to each other. The server has the data and sets the language while the client uses...
Read more >
Getting started with External API and Webhooks
External API allows you to create workflows that send HTTP requests to external web servers, API endpoints, and URLs.
Read more >
The management approach for internal vs. external APIs
Internal and external APIs both perform the same basic function, in terms of delivering the data and integrations needed to facilitate ...
Read more >
How do APIs work? An in-depth guide - Tray.io
Many companies also use external APIs, which the developer community uses to launch products. Some examples include Twilio (communications API), ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found