remix-typedjson
Version:
This package is a replacement for superjson to use in your Remix app. It handles a subset of types that `superjson` supports, but is faster and smaller.
31 lines (30 loc) • 1.3 kB
TypeScript
type NonJsonTypes = 'date' | 'set' | 'map' | 'regexp' | 'bigint' | 'undefined' | 'infinity' | '-infinity' | 'nan' | 'error' | string;
type MetaType = Record<string, NonJsonTypes>;
type CustomTypeEntry<T> = {
type: string;
is: (value: unknown) => boolean;
serialize: (value: T) => string;
deserialize: (value: string) => T;
};
export declare function registerCustomType<T>(entry: CustomTypeEntry<T>): void;
declare function serialize<T>(data: T): TypedJsonResult;
declare function deserialize<T>({ json, meta }: TypedJsonResult): T | null;
export declare const splitKey: (key: string) => string[];
declare function applyMeta<T>(data: T, meta: MetaType): T;
type TypedJsonResult = {
json?: string | null;
meta?: MetaType;
};
type StrigifyParameters = Parameters<typeof JSON.stringify>;
declare function stringify<T>(data: T, replacer?: StrigifyParameters[1], space?: StrigifyParameters[2]): string;
declare function parse<T>(json: string): T | null;
declare const typedjson: {
serialize: typeof serialize;
stringify: typeof stringify;
deserialize: typeof deserialize;
parse: typeof parse;
applyMeta: typeof applyMeta;
};
export { applyMeta, deserialize, parse, serialize, stringify };
export type { MetaType, TypedJsonResult };
export default typedjson;