@charliethomson/better-session
Version:
### Adds specific and useful typing, and security through obscurity to the browser storage
70 lines (69 loc) • 3.27 kB
TypeScript
import { z } from "zod";
export type SessionShim<T> = {
from: (str: string | null) => T | null;
to: (value: T) => string;
};
declare global {
interface Window {
__BetterSession__StorageLocation?: Storage;
__BetterSession__Encrypt?: boolean;
}
}
export declare class SessionAccessor<K extends string, T> {
private shim;
private get storage();
private get encrypt();
key: K;
private secret;
constructor(key: K, shim: SessionShim<T>);
get(): T | null;
set(value: T): void;
remove(): void;
}
export declare const createShim: <T>(from: (str: string | null) => T | null, to: (value: T) => string) => SessionShim<T>;
type CustomBoolean = {
on: string;
off: string;
};
type BooleanOptions = {
obfuscate?: false;
constants?: never;
} | {
obfuscate: true;
constants?: CustomBoolean;
};
export declare const s: {
number: <K extends string>(key: K) => SessionAccessor<K, number>;
string: <K_1 extends string>(key: K_1) => SessionAccessor<K_1, string>;
shape: <K_2 extends string, Shape extends z.ZodRawShape>(key: K_2, shape: Shape) => SessionAccessor<K_2, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<Shape>, (z.baseObjectOutputType<Shape> extends infer T_1 extends object ? { [k_1 in keyof T_1]: undefined extends z.baseObjectOutputType<Shape>[k_1] ? never : k_1; } : never)[keyof Shape]> extends infer T ? { [k in keyof T]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<Shape>, (z.baseObjectOutputType<Shape> extends infer T_1 extends object ? { [k_1 in keyof T_1]: undefined extends z.baseObjectOutputType<Shape>[k_1] ? never : k_1; } : never)[keyof Shape]>[k]; } : never>;
object: <K_3 extends string, Schema extends z.ZodType<unknown, z.ZodTypeDef, unknown>>(key: K_3, validator: Schema) => SessionAccessor<K_3, z.TypeOf<Schema>>;
type: <T_2>() => <K_4 extends string>(key: K_4) => SessionAccessor<K_4, T_2>;
boolean: <K_5 extends string>(key: K_5, options?: BooleanOptions) => SessionAccessor<K_5, boolean>;
date: <K_6 extends string>(key: K_6) => SessionAccessor<K_6, Date>;
custom: <K_7 extends string, T_3>(key: K_7, shim: SessionShim<T_3>) => SessionAccessor<K_7, T_3>;
};
export type Session<Configuration extends Record<string, SessionAccessor<any, any>>> = Configuration & {
get<K extends keyof Configuration>(key: K): Configuration[K] extends SessionAccessor<any, infer T> ? T | null : never;
set<K extends keyof Configuration>(key: K, value: Configuration[K] extends SessionAccessor<any, infer T> ? T | null : never): void;
remove<K extends keyof Configuration>(key: K): void;
clear(): void;
};
export type CreateSessionOptions = {
/**
* @default: 'dev'
*/
mode: "dev" | "prod";
/**
* @summary Whether to encrypt the values (NOTE: only in prod mode)
* NOTE: Values will be base64encoded regardless of this setting
* @default false
*/
encrypt?: boolean;
/**
* @summary Where to store to
* @default: sessionStorage
*/
storage?: Storage;
};
export declare const createSession: <Configuration extends Record<string, SessionAccessor<any, any>>>(configuration: Configuration, options?: CreateSessionOptions) => Session<Configuration>;
export {};