sveltejs / sveltejs/kit

Allow the 2XX code to be defined in successful Form Actions

Open
#10,111 1 comment 1 reaction 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

An api called through a Form Action might return any 2xx Success status code.

Currently, if an Action is successful and returns a data object, it can only return a 200 code.

Unlike the failure type, it does not appear that the status can be altered.

As far as I can tell, the only option I have available is to add a custom status: 2xx to the return object and retrieve it via data.status

Describe the proposed solution

It would be nice to see an option to provide a similar pattern as is made available via fail. Something like:

+page.server.ts

import { fail, succeed } from '@sveltejs/kit';

(...)

if (error) {
    return fail( 500, { error } )
};

return succeed( status, { data } );

actions.js

if (!data.status){
    return {
	type: 'success',
	status: 200,
	data
}
return {
    type: 'success',
    status: data.status
    data: data.data
}
Alternatives considered

No response

Importance

would make my life easier

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 at packages/kit/src/runtime/server/page/actions.js around the successful action handling referenced in the issue, and compare it with the existing fail pattern. Define how a successful Form Action can preserve a caller-provided 2xx status while retaining 200 as the default; done means the response status and returned data follow that behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.