sveltekit-superforms
Advanced tools
Comparing version 0.5.7 to 0.5.8
@@ -56,3 +56,3 @@ import { type MaybePromise, type SubmitFunction } from '$app/forms'; | ||
error: App.Error; | ||
}) => App.PageData['flash']; | ||
}) => any; | ||
}; | ||
@@ -87,20 +87,4 @@ export type EnhancedForm<T extends AnyZodObject> = { | ||
export declare function stringProxy<T extends Record<string, unknown>, Type extends 'int' | 'boolean'>(form: Writable<T>, field: keyof T, type: Type): Writable<string>; | ||
interface IntArrayProxy extends Writable<number[]> { | ||
toggle(id: number): void; | ||
add(id: number): void; | ||
remove(id: number): void; | ||
length: number; | ||
} | ||
export declare function fieldProxy<T extends Record<string, unknown>, K extends keyof T, S = T[K]>(form: Writable<T>, field: K): Writable<S>; | ||
/** | ||
* Creates a number[] store that will accept only integers and pass its values | ||
* to a string in the form, comma-separated as default. | ||
* @param form The form | ||
* @param field Form field | ||
* @param options allowNaN, separator | ||
*/ | ||
export declare function intArrayProxy<T extends Record<string, unknown>>(form: Writable<T>, field: keyof T, options?: { | ||
allowNaN?: boolean; | ||
separator?: string; | ||
}): IntArrayProxy; | ||
/** | ||
* Initializes a SvelteKit form, for convenient handling of values, errors and sumbitting data. | ||
@@ -107,0 +91,0 @@ * @param {Validation} form Usually data.form from PageData. |
@@ -8,5 +8,5 @@ import { enhance, applyAction } from '$app/forms'; | ||
import { browser } from '$app/environment'; | ||
import { stringify } from 'devalue'; | ||
import { stringify, parse } from 'devalue'; | ||
import { deepEqual } from '..'; | ||
import { getFlash, updateFlash } from 'sveltekit-flash-message/client'; | ||
//import { getFlash, updateFlash } from './sveltekit-flash-message/client'; | ||
var FetchStatus; | ||
@@ -79,26 +79,20 @@ (function (FetchStatus) { | ||
} | ||
/** | ||
* Creates a number[] store that will accept only integers and pass its values | ||
* to a string in the form, comma-separated as default. | ||
* @param form The form | ||
* @param field Form field | ||
* @param options allowNaN, separator | ||
*/ | ||
export function intArrayProxy(form, field, options = {}) { | ||
options = { allowNaN: false, separator: ',', ...options }; | ||
function toArray(val) { | ||
if (typeof val !== 'string' || !val) | ||
return []; | ||
return val | ||
.split(',') | ||
.map((s) => parseInt(s, 10)) | ||
.filter((n) => (options.allowNaN ? true : !isNaN(n))); | ||
export function fieldProxy(form, field) { | ||
function unserialize(val) { | ||
try { | ||
return parse(val); | ||
} | ||
catch { | ||
return undefined; | ||
} | ||
} | ||
const initialValue = stringify(get(form)[field]); | ||
form.update((f) => ({ ...f, [field]: initialValue })); | ||
const proxy = derived(form, ($form) => { | ||
return toArray($form[field]); | ||
}); | ||
return unserialize($form[field]); | ||
}, initialValue); | ||
function update(updater) { | ||
form.update((f) => ({ | ||
...f, | ||
[field]: updater(toArray(f[field])).join(',') | ||
[field]: updater(unserialize(f[field])) | ||
})); | ||
@@ -110,18 +104,40 @@ } | ||
set(val) { | ||
form.update((f) => ({ ...f, [field]: val.join(options.separator) })); | ||
}, | ||
toggle(id) { | ||
update((r) => r.includes(id) ? r.filter((i) => i !== id) : [...r, id]); | ||
}, | ||
add(id) { | ||
update((r) => [...r, id]); | ||
}, | ||
remove(id) { | ||
update((r) => r.filter((i) => i !== id)); | ||
}, | ||
get length() { | ||
return get(proxy).length; | ||
form.update((f) => ({ ...f, [field]: stringify(val) })); | ||
} | ||
}; | ||
} | ||
/* | ||
interface ArrayProxy<S> extends Writable<S[]> { | ||
toggle(id: S): void; | ||
add(id: S): void; | ||
remove(id: S): void; | ||
length: number; | ||
} | ||
export function arrayProxy< | ||
T extends Record<string, unknown> = Record<string, unknown>, | ||
K extends keyof T = keyof T, | ||
S = T[K] | ||
>(form: Writable<T>, field: keyof T): ArrayProxy<S> { | ||
const proxy = fieldProxy(form, field); | ||
return { | ||
...proxy, | ||
toggle(id: S) { | ||
proxy.update((r) => | ||
r.includes(id) ? r.filter((i) => i !== id) : [...r, id] | ||
); | ||
}, | ||
add(id: S) { | ||
proxy.update((r) => [...r, id]); | ||
}, | ||
remove(id: S) { | ||
proxy.update((r) => r.filter((i) => i !== id)); | ||
}, | ||
get length() { | ||
return get(proxy).length; | ||
} | ||
}; | ||
} | ||
*/ | ||
/** | ||
@@ -459,3 +475,3 @@ * Initializes a SvelteKit form, for convenient handling of values, errors and sumbitting data. | ||
options.clearOnSubmit == 'message')) { | ||
getFlash(page).set(undefined); | ||
//getFlash(page).set(undefined); | ||
} | ||
@@ -466,3 +482,3 @@ //d('Submitting'); | ||
case 'json': | ||
submit.data.set('json', stringify(get(data))); | ||
submit.data.set('__superform_json', stringify(get(data))); | ||
break; | ||
@@ -539,9 +555,13 @@ case 'formdata': | ||
} | ||
getFlash(page).set(options.flashMessage({ | ||
/* | ||
getFlash(page).set( | ||
options.flashMessage({ | ||
...result, | ||
status: result.status ?? status | ||
})); | ||
}) | ||
); | ||
*/ | ||
} | ||
else { | ||
updateFlash(page); | ||
//updateFlash(page); | ||
} | ||
@@ -548,0 +568,0 @@ } |
@@ -30,4 +30,5 @@ import { type RequestEvent } from '@sveltejs/kit'; | ||
noErrors?: boolean; | ||
jsonFields?: (keyof z.infer<T>)[]; | ||
}): Promise<Validation<T>>; | ||
export declare function actionResult<T extends Record<string, unknown> | string>(type: T extends string ? 'redirect' : 'success' | 'failure' | 'error', data?: T, status?: number): Response; | ||
export {}; |
@@ -65,3 +65,3 @@ import { fail, json } from '@sveltejs/kit'; | ||
} | ||
function formDataToValidation(schema, fields, data) { | ||
function formDataToValidation(schema, fields, data, jsonFields) { | ||
const output = {}; | ||
@@ -74,2 +74,5 @@ for (const key of fields) { | ||
} | ||
else if (jsonFields.includes(key)) { | ||
output[key] = entry === null ? undefined : parse(entry); | ||
} | ||
else { | ||
@@ -238,17 +241,16 @@ output[key] = parseEntry(key, entry); | ||
let empty = false; | ||
function parseJson(value) { | ||
if (!value) | ||
return {}; | ||
try { | ||
return parse(value); | ||
function parseFormData(data) { | ||
if (data.has('__superform_json')) { | ||
const value = data.get('__superform_json')?.toString(); | ||
if (!value) | ||
return {}; | ||
try { | ||
return parse(value); | ||
} | ||
catch (_) { | ||
return {}; | ||
} | ||
} | ||
catch (_) { | ||
return {}; | ||
} | ||
} | ||
function parseFormData(data) { | ||
if (data.has('json')) | ||
return parseJson(data.get('json')?.toString()); | ||
else | ||
return formDataToValidation(schema, schemaKeys, data); | ||
return formDataToValidation(schema, schemaKeys, data, (options.jsonFields ?? [])); | ||
} | ||
@@ -255,0 +257,0 @@ async function tryParseFormData(request) { |
{ | ||
"name": "sveltekit-superforms", | ||
"version": "0.5.7", | ||
"version": "0.5.8", | ||
"author": "Andreas Söderlund <ciscoheat@gmail.com> (https://blog.encodeart.dev)", | ||
@@ -5,0 +5,0 @@ "description": "Supercharge your SvelteKit forms with this powerhouse of a library!", |
@@ -395,12 +395,2 @@ # sveltekit-superforms 💥 | ||
## sveltekit-flash-message support | ||
The sister library to `sveltekit-superforms` is called [sveltekit-flash-message](https://github.com/ciscoheat/sveltekit-flash-message), a useful addon since the `message` property of `Validation<T>` doesn't persist when redirecting to a different page. If you have it installed and configured, you only need to specify this option to make things work: | ||
```ts | ||
flashMessage: (errorResult: ActionResult<'error'>) => App.PageData['flash']; | ||
``` | ||
The flash message works automatically for every case except errors, so this callback is needed to transform the `ActionResult` `error` into your flash message type. | ||
## The last one: Breaking free from FormData | ||
@@ -407,0 +397,0 @@ |
83111
15
1216
866