Skip to content

Commit 740e24a

Browse files
committed
ActionResult can now handled by customRequest.
Closes #471
1 parent 0cfddcc commit 740e24a

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- New validation library: [class-validator](https://github.com/typestack/class-validator)!
1313
- Exported `SuperFormData` and `SuperFormErrors` types for superForm.
1414
- Exported `ZodObjectType`, `ZodObjectTypes` and `ZodValidation` types for the Zod adapter.
15+
- [customRequest](https://superforms.rocks/concepts/events#customrequest) can now handle an `ActionResult` as well, for better error handling.
1516

1617
### Fixed
1718

src/lib/client/superForm.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ export type FormOptions<
107107
validators: (validators: Exclude<ValidatorsOption<T>, 'clear'>) => void;
108108
/**
109109
* Use a custom fetch or XMLHttpRequest implementation for this form submission. It must return an ActionResult in the response body.
110-
* If the request is using a XMLHttprequest, the promise must be resolved when the request is complete.
110+
* If the request is using a XMLHttpRequest, the promise must be resolved when the request has been completed, not before.
111111
*/
112112
customRequest: (
113-
validators: (input: Parameters<SubmitFunction>[0]) => Promise<Response | XMLHttpRequest>
113+
validators: (
114+
input: Parameters<SubmitFunction>[0]
115+
) => Promise<Response | XMLHttpRequest | ActionResult>
114116
) => void;
115117
}
116118
) => MaybePromise<unknown | void>;
@@ -1599,7 +1601,9 @@ export function superForm<
15991601

16001602
let currentRequest: AbortController | null;
16011603
let customRequest:
1602-
| ((input: Parameters<SubmitFunction>[0]) => Promise<Response | XMLHttpRequest>)
1604+
| ((
1605+
input: Parameters<SubmitFunction>[0]
1606+
) => Promise<Response | XMLHttpRequest | ActionResult>)
16031607
| undefined = undefined;
16041608

16051609
const enhanced = kitEnhance(FormElement, async (submitParams) => {
@@ -1979,10 +1983,16 @@ export function superForm<
19791983
if (customRequest) {
19801984
if (!cancelled) _submitCancel();
19811985
const response = await customRequest(submitParams);
1982-
const result: ActionResult =
1983-
response instanceof Response
1984-
? deserialize(await response.text())
1985-
: deserialize(response.responseText);
1986+
1987+
let result: ActionResult;
1988+
1989+
if (response instanceof Response) {
1990+
result = deserialize(await response.text());
1991+
} else if (response instanceof XMLHttpRequest) {
1992+
result = deserialize(response.responseText);
1993+
} else {
1994+
result = response;
1995+
}
19861996

19871997
if (result.type === 'error') result.status = response.status;
19881998
validationResponse({ result });

0 commit comments

Comments
 (0)