Skip to content

Commit 71feddf

Browse files
committed
Exports for proxy types.
1 parent 9287875 commit 71feddf

File tree

4 files changed

+45
-32
lines changed

4 files changed

+45
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Type narrowing for `FormPath` and its relatives, to filter the paths based on a specific type, like `FormPath<T, Date>`.
13+
- Proxy types: `FieldProxy`, `FormFieldProxy` and `ArrayProxy`.
1314

1415
### Fixed
1516

src/lib/client/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ export {
1010
fieldProxy,
1111
formFieldProxy,
1212
stringProxy,
13-
arrayProxy
13+
arrayProxy,
14+
type FieldProxy,
15+
type ArrayProxy,
16+
type FormFieldProxy
1417
} from './proxies.js';
1518

1619
/////////////////////////////////////////////////////////////////////

src/lib/client/proxies.ts

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -235,35 +235,37 @@ function _stringProxy<T extends Record<string, unknown>, Path extends FormPath<T
235235
}
236236

237237
// eslint-disable-next-line @typescript-eslint/no-explicit-any
238-
type ArrayFieldErrors = any[];
238+
type ValueErrors = any[];
239+
240+
export type ArrayProxy<T, Path = string, Errors = ValueErrors> = {
241+
path: Path;
242+
values: Writable<T & unknown[]>;
243+
errors: Writable<string[] | undefined>;
244+
valueErrors: Writable<Errors>;
245+
};
239246

240247
export function arrayProxy<T extends Record<string, unknown>, Path extends FormPathArrays<T>>(
241248
// eslint-disable-next-line @typescript-eslint/no-explicit-any
242249
superForm: SuperForm<T, any>,
243250
path: Path,
244251
options?: { taint?: TaintOption }
245-
): {
246-
path: Path;
247-
values: Writable<FormPathType<T, Path> & unknown[]>;
248-
errors: Writable<string[] | undefined>;
249-
valueErrors: Writable<ArrayFieldErrors>;
250-
} {
252+
): ArrayProxy<FormPathType<T, Path>, Path> {
251253
const formErrors = fieldProxy(
252254
superForm.errors,
253255
// eslint-disable-next-line @typescript-eslint/no-explicit-any
254256
`${path}` as any
255257
);
256258

257-
const onlyFieldErrors = derived<typeof formErrors, ArrayFieldErrors>(formErrors, ($errors) => {
258-
const output: ArrayFieldErrors = [];
259+
const onlyFieldErrors = derived<typeof formErrors, ValueErrors>(formErrors, ($errors) => {
260+
const output: ValueErrors = [];
259261
for (const key in $errors) {
260262
if (key == '_errors') continue;
261263
output[key as unknown as number] = $errors[key];
262264
}
263-
return output as ArrayFieldErrors;
265+
return output as ValueErrors;
264266
});
265267

266-
function updateArrayErrors(errors: Record<number, unknown>, value: ArrayFieldErrors) {
268+
function updateArrayErrors(errors: Record<number, unknown>, value: ValueErrors) {
267269
for (const key in errors) {
268270
if (key == '_errors') continue;
269271
errors[key] = undefined;
@@ -276,15 +278,15 @@ export function arrayProxy<T extends Record<string, unknown>, Path extends FormP
276278
return errors;
277279
}
278280

279-
const fieldErrors: Writable<ArrayFieldErrors> = {
281+
const fieldErrors: Writable<ValueErrors> = {
280282
subscribe: onlyFieldErrors.subscribe,
281-
update(upd: Updater<ArrayFieldErrors>) {
283+
update(upd: Updater<ValueErrors>) {
282284
formErrors.update(($errors) =>
283285
// @ts-expect-error Type is correct
284286
updateArrayErrors($errors, upd($errors))
285287
);
286288
},
287-
set(value: ArrayFieldErrors) {
289+
set(value: ValueErrors) {
288290
// @ts-expect-error Type is correct
289291
formErrors.update(($errors) => updateArrayErrors($errors, value));
290292
}
@@ -327,18 +329,20 @@ export function arrayProxy<T extends Record<string, unknown>, Path extends FormP
327329
};
328330
}
329331

332+
export type FormFieldProxy<T, Path = string> = {
333+
path: Path;
334+
value: SuperFieldProxy<T>;
335+
errors: Writable<string[] | undefined>;
336+
constraints: Writable<InputConstraint | undefined>;
337+
tainted: Writable<boolean | undefined>;
338+
};
339+
330340
export function formFieldProxy<T extends Record<string, unknown>, Path extends FormPathLeaves<T>>(
331341
// eslint-disable-next-line @typescript-eslint/no-explicit-any
332342
superForm: SuperForm<T, any>,
333343
path: Path,
334344
options?: ProxyOptions
335-
): {
336-
path: Path;
337-
value: SuperFieldProxy<FormPathType<T, Path>>;
338-
errors: Writable<string[] | undefined>;
339-
constraints: Writable<InputConstraint | undefined>;
340-
tainted: Writable<boolean | undefined>;
341-
} {
345+
): FormFieldProxy<FormPathType<T, Path>, Path> {
342346
const path2 = splitPath(path);
343347
// Filter out array indices, the constraints structure doesn't contain these.
344348
const constraintsPath = path2.filter((p) => /\D/.test(String(p))).join('.');
@@ -390,13 +394,7 @@ export function formFieldProxy<T extends Record<string, unknown>, Path extends F
390394
};
391395
}
392396

393-
type SuperFieldProxy<T> = {
394-
subscribe: Readable<T>['subscribe'];
395-
set(this: void, value: T, options?: { taint?: TaintOption }): void;
396-
update(this: void, updater: Updater<T>, options?: { taint?: TaintOption }): void;
397-
};
398-
399-
function updateProxyField<T extends Record<string, unknown>, Path extends FormPath<T>>(
397+
function updateProxyField<T extends Record<string, unknown>, Path extends string>(
400398
obj: T,
401399
path: (string | number | symbol)[],
402400
updater: Updater<FormPathType<T, Path>>
@@ -412,7 +410,13 @@ function updateProxyField<T extends Record<string, unknown>, Path extends FormPa
412410
return obj;
413411
}
414412

415-
function superFieldProxy<T extends Record<string, unknown>, Path extends FormPath<T>>(
413+
type SuperFieldProxy<T> = {
414+
subscribe: Readable<T>['subscribe'];
415+
set(this: void, value: T, options?: { taint?: TaintOption }): void;
416+
update(this: void, updater: Updater<T>, options?: { taint?: TaintOption }): void;
417+
};
418+
419+
function superFieldProxy<T extends Record<string, unknown>, Path extends string>(
416420
superForm: SuperForm<T>,
417421
path: Path,
418422
baseOptions?: ProxyOptions
@@ -454,11 +458,13 @@ function isSuperForm<T extends Record<string, unknown>>(
454458
return isSuperForm;
455459
}
456460

461+
export type FieldProxy<T> = Writable<T>;
462+
457463
export function fieldProxy<T extends Record<string, unknown>, Path extends FormPath<T>>(
458464
form: Writable<T> | SuperForm<T, unknown>,
459465
path: Path,
460466
options?: ProxyOptions
461-
): Writable<FormPathType<T, Path>> {
467+
): FieldProxy<FormPathType<T, Path>> {
462468
const path2 = splitPath(path);
463469

464470
if (isSuperForm(form, options)) {

src/lib/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ export {
4444
type FormPathLeaves,
4545
type FormPathArrays,
4646
type FormPathType,
47-
type ChangeEvent
47+
type ChangeEvent,
48+
type FieldProxy,
49+
type ArrayProxy,
50+
type FormFieldProxy
4851
} from './client/index.js';
4952

5053
export { splitPath } from './stringPath.js';

0 commit comments

Comments
 (0)