Skip to content

Commit ff5094c

Browse files
committed
update preprocess to be ephemeral
1 parent 435223b commit ff5094c

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,32 @@ Allows you to select if the form should be reset. By default, the form never res
222222

223223
Allows you to preprocess any data you have set when the form is submitted. This will run prior to any parsing on the client. For example if you would need to convert an input of type 'date' to an ISO string on the client before submitting. If this is a promise, it will be awaited before continuing.
224224

225+
> Preprocessed data creates a `$state.snapshot()` of your form data. Thus it is ephemeral, that way if the command fails, your form data is not already processed.
226+
225227
```html
226228
<script lang="ts">
227229
const cmd = new CommandForm(schema, {
228230
preprocess: (data) => {
229-
cmd.set({ someDate: new Date(data.someDate).toISOString() });
231+
return { ...data, name: data.name.trim() };
232+
}
233+
// ... other options
234+
});
235+
</script>
236+
237+
<input type="date" bind:value="{cmd.form.someDate}" />
238+
```
239+
240+
---
241+
242+
#### `options.onSuccess()`
243+
244+
Runs when the form is submitted. \*The data available inside of this function is the result of your preprocess function if you have one. This can also be a promise.
245+
246+
```html
247+
<script lang="ts">
248+
const cmd = new CommandForm(schema, {
249+
onSubmit: (data) => {
250+
toast.loading('Submitting data...please wait!');
230251
}
231252
// ... other options
232253
});

src/lib/command-form/command-form.svelte.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ export class CommandForm<Schema extends StandardSchemaV1, TOut> {
5454
}
5555
}
5656

57-
private async parseForm(): Promise<SchemaData<Schema>> {
58-
return standardValidate(this.schema, this.form as StandardSchemaV1.InferInput<Schema>);
57+
private async parseForm(
58+
data?: StandardSchemaV1.InferInput<Schema>
59+
): Promise<SchemaData<Schema>> {
60+
const target = data ?? this.form;
61+
return standardValidate(this.schema, target);
5962
}
6063

6164
// set path to keyof schema or string
@@ -97,14 +100,22 @@ export class CommandForm<Schema extends StandardSchemaV1, TOut> {
97100
this._result = null;
98101

99102
try {
100-
await this.options.preprocess?.(this.form)
101-
const parsed = await this.parseForm();
103+
104+
const workingCopy = $state.snapshot(this.form)
105+
106+
const preprocessed =
107+
(await this.options.preprocess?.(workingCopy)) ?? workingCopy;
108+
109+
const parsed = await this.parseForm(preprocessed);
110+
102111
await this.options.onSubmit?.(parsed);
103112

104113
const res = await this.options.command(parsed);
114+
105115
this._result = res;
106116

107117
this.issues = null;
118+
108119
await this.options.onSuccess?.(res);
109120

110121
if (this.options.invalidate) {

src/lib/types/command-form.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type CommandFormOptions<TIn, TOut> = {
1111
invalidate?: string | string[] | 'all';
1212
command: RemoteCommand<TIn, TOut>;
1313
reset?: 'onSuccess' | 'always' | 'onError';
14-
preprocess?: (data: TIn) => Promise<void> | void;
14+
preprocess?: (data: TIn) => Promise<TIn> | TIn;
1515
onSubmit?: (data: TIn) => Promise<void> | void;
1616
onSuccess?: (result: Awaited<TOut>) => Promise<void> | void;
1717
onError?: (err: unknown) => Promise<void> | void;

src/routes/+page.svelte

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
},
1515
onError: async (err) => {
1616
console.error('error', err);
17+
},
18+
preprocess: (data) => {
19+
return { ...data, name: 'preprocessed' };
1720
}
1821
});
1922
</script>

src/routes/test.remote.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { schema } from "./test.schema.ts";
33
import { SchemaValidationError } from "$lib/index.ts";
44

55

6-
export const test = command(schema, async () => {
7-
8-
throw new SchemaValidationError([{ path: ['email'], message: 'Name is invalid server error!!' }])
6+
export const test = command(schema, async (data) => {
7+
console.log(data)
8+
throw new SchemaValidationError([{ path: ['name'], message: 'Name is invalid server error!!' }])
99
})

0 commit comments

Comments
 (0)