Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adapters to pass in platform specific context #3426

Closed
fezproof opened this issue Jan 20, 2022 · 2 comments · May be fixed by patooworld/kit#3
Closed

Allow adapters to pass in platform specific context #3426

fezproof opened this issue Jan 20, 2022 · 2 comments · May be fixed by patooworld/kit#3
Labels
adapters - general Support for functionality general to all adapters feature / enhancement New feature or request

Comments

@fezproof
Copy link
Contributor

fezproof commented Jan 20, 2022

Describe the problem

Related #2844 #2807

As of #3384, hooks and endpoints receive a standard Request object. This simplified and standardised the API for adapters a lot, but also means that no custom context can be passed into the app for the developer to access.

As an example, when using the adapter-cloudflare and deploying to Cloudflare pages, you can pass in KV stores and Durable Objects to your functions. With the current Cloudflare adapter, you cannot access the env that is being passed in.

export default {
	async fetch(req, env) { // All context needed in env
		// ...
		if (url.pathname.startsWith(prefix)) {
			/** @type {Response} */
			const res = await env.ASSETS.fetch(req); // <-- env used to get static assets
	         // ...
		try {
			return await app.render(req); // <-- env not passed to the app
		} catch (e) {
                // ..

With the new Request Response type pattern implemented, it is now not possible to pass it in at all.

This would also be relevant to Netlify, Vercel, and any custom adapter for larger teams using something like Azure or AWS.

Describe the proposed solution

The most straight forward way to solve this would be to pass in the request to app.render as part of an object, with the adapter environment passed in as another

return await app.render({ request, env });

This would be another breaking change for the adapter API, but shouldn't change anything for applications, as the request is already passed into handlers as part of an object

export async function post({ params, request, env }) {
  await env.TodoList.put("Task", "Clean Kitchen")
  const out = await env.TodoList.get("Task");

Another generic type should also be exposed to Handler and Hook functions to allow typing for the environment.

Alternatives considered

There are possibilities of using globals, but this could cause issues in most environments.

Importance

would make my life easier

Additional Information

This is my first issue here, but am really impressed with how everything is amazing with svelte kit so far. Absolutely an inspiration <3
Am happy to create a PR for this if the changes make sense.
Hello from Perth Australia!

@bluwy bluwy added adapters - general Support for functionality general to all adapters feature / enhancement New feature or request labels Jan 20, 2022
@benmccann
Copy link
Member

Agreed that this is needed 😄 Closing as duplicate of #2426

@tylerbrostrom
Copy link

tylerbrostrom commented Jan 20, 2022

@benmccann Is it possible to opt for this issue over #2426?

This issue concretely illustrates the problem and includes a proposed solution, taking the changes from #3384 into account.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters - general Support for functionality general to all adapters feature / enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants