sveltejs / sveltejs/kit

Utility function to invoke a form action

Open
#9,444 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

forms
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

Currently form's action can only be used with a HTML <form>

<form method="POST"  action="?/load" use:enhance>
    <button type="submit">CALL</button>
</form>

It would be nice to be able to call them via a Javascript function...

Describe the proposed solution

We could have a function to call any action.
Exemple :

async function doSomething() {
   const data = await callAction('?/load', { /* an object that represents the POST parameters */ })
   // ... using data
}

Even better, maybe we could have typed functions automatically generated by Sveltekit :

<script lang="ts">
    import { load } from './$actions'; // Generated by sveltekit, based on actions defined on +page.server.ts

    async function doSomething() {
        const data = await load( { /* an object that represents the POST parameters */ } ); 
        // ... using data (which is correctly typed)
    }
</script>
Alternatives considered

No response

Importance

nice to have

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the form action flow using <form action="?/load" use:enhance> and the +page.server.ts actions described in the issue. Determine the API for invoking an action from JavaScript and whether generated $actions functions are in scope; done means the behavior, parameters, return data, and typing are specified clearly enough to implement and test.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.