@guanriyue/decurl/codec
@guanriyue/decurl/codec defines field rules for URL search params. It focuses on how URL keys map to business values, and how business values are serialized back to URLSearchParams.
Decode logic can be a plain function, or it can use primitives from @guanriyue/decurl/decode.
import { createURLSearchParamsCodec, defineFields, field } from '@guanriyue/decurl/codec';
field
field(definition) freezes the type of a single FieldCodec. The smallest codec can be a plain object, but field is recommended because it helps TypeScript preserve information such as mode, defaultValue, decode, and encode more reliably.
import { field } from '@guanriyue/decurl/codec';
const keyword = field({
decode: (value: string) => {
const nextValue = value.trim();
return nextValue.length > 0 ? nextValue : undefined;
},
});
Common properties:
For more design details, see FieldCodec.
defineFields
defineFields(fields) defines a set of Search Fields. For fields without an explicit name, it uses the object key as the URL key.
import { defineFields, field } from '@guanriyue/decurl/codec';
const searchFields = defineFields({
keyword: field({
name: ['keyword', 'q'],
decode: (value: string) => value.trim() || undefined,
}),
view: field({
decode: (value: string) => {
return value === 'list' || value === 'grid' ? value : undefined;
},
defaultValue: 'list',
}),
});
When a field has multiple name values, the first one is the canonical key and the rest are legacy aliases. See Search Fields for details.
createURLSearchParamsCodec
createURLSearchParamsCodec(fields) creates { decode, encode }.
import { createURLSearchParamsCodec, defineFields, field } from '@guanriyue/decurl/codec';
const fields = defineFields({
page: field({
name: ['page', 'p'],
decode: (value: string) => {
const page = Number(value);
return Number.isInteger(page) && page >= 1 ? page : undefined;
},
defaultValue: 1,
}),
});
const codec = createURLSearchParamsCodec(fields);
const values = codec.decode(new URLSearchParams('?p=2'));
const nextSearch = codec.encode({ page: 3 }, { base: '?p=2&keyword=router' });
Common options for encode(values, options?):
Types