Remote Functions: deterministic paths
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
Description
Describe the problem
Sometimes I want to write some internal-use APIs without building a full REST architecture. That way, I can send some cross-origin requests between the applications I own.
The use case that came up is managing logout logic between two applications:
On app.example.com there is a logout form:
<form method="POST" action="accounts.example.com/?/logout">
<button>Log out</button>
</form>
And on accounts.example.com I can write a traditional SvelteKit Form Action to receive the cross-origin POST request in src/routes/+page.server.ts.
If a remote form named logout exists on accounts.example.com, it's not possible to rely on their path since it may change in between builds. Example generated path: ?/remote=q5s0im%2Flogout.
Describe the proposed solution
Some way to provide deterministic paths for remote form methods ahead of build time; especially if current workarounds may not be made available long-term.
Brainstorming some ways this can be exposed to SvelteKit developers:
- Remote functions defined anywhere in
src/routes/**/*.remote.{ts|js}would generate a path the same way named Form Actions do today; and if there's a conflict with a same-named form action then there would be a build error.
Example:// file: src/routes/auth.remote.ts import { form } from '$app/server' // exposed as `accounts.example.com/?/logout` export const logout = form(() => { /* do stuff */ } - A third
optionsparameter is provided for remote functions to set things like{ url: '/api/logout' } - A config option to make paths determined based on location in the project's file structure and method name, rather than a (seemingly) random hash being generated.
Example:src/lib/auth.remote.ts#logout->/_remote/lib/logout - (cool, but understandably complex) A generated SDK for all remote functions in a given application. There may need to be considerations for 'private' functions (see https://github.com/sveltejs/kit/issues/15712)
It would be nice to have deterministic paths for query or command as well.
I understand the intent of Remote Functions and RPC in general is to not care too much about endpoint paths and to instead generate an SDK; which in SvelteKit's case is a build-time detail. At least, we could provide a simple escape hatch for these infrequent needs like a single method.
Alternatives considered
- A named Form Action
- A
+server.tsPOST endpoint
Importance
would make my life easier
Additional Information
This relates to another feature request that would also require deterministic paths:
- #14336
Especially if the 'generated SDK' idea is pursued.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the remote functions in src/routes/**/*.remote.{ts|js} and the traditional Form Action in src/routes/+page.server.ts, then review the related issues #14336 and #15712. The issue presents several possible designs, so completion would require an agreed deterministic-path behavior for remote form methods and possibly query or command methods, plus validation of the chosen design.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 38/100