/** A function which can be used as `replacer` or `reviver`. */ export type NjsonFunction = (this: unknown, key: number | string, value: unknown) => unknown; /** A function that transforms the results or the `Array` of _keys_ to be serialized. */ export type NjsonReplacer = NjsonFunction | (number | string)[]; /** NJSON parsing options. */ export interface NjsonParseOptions { /** If `true` the `key` argument passed to `reviver` is of type `number` when reviving an `Array`. */ numberKey?: boolean; /** * A function that transforms the results. This function is called for each member of the object. * If a member contains nested objects, the nested objects are transformed before the parent object is. */ reviver?: NjsonFunction; } /** NJSON serializing options. */ export interface NjsonStringifyOptions { /** Specifies the method to be used to serialize `Date` objects. */ date?: "iso" | "string" | "time" | "utc"; /** If `true` the `key` argument passed to `replacer` is of type `number` when replacing an `Array`. */ numberKey?: boolean; /** If `true` the `stack` is omitted when serializing `Error` objects. */ omitStack?: boolean; /** A function that transforms the results or the `Array` of _keys_ to be serialized. */ replacer?: NjsonReplacer; /** If `true` the _keys_ of objects are alphabetically sorted before being serialized. */ sortKeys?: boolean; /** Adds indentation, white space, and line break characters to the return-value NJSON text to make it easier to read. */ space?: number | string; /** When a `string` is longer than `stringLength` character it is considered a reference. */ stringLength?: number; /** If `false`, `undefined` values ar skipped (as JSON.stringify does). */ undef?: boolean; } /** * Converts a Next JavaScript Object Notation (NJSON) string into an object. * * @param text A valid NJSON string. * @param options A `NjsonParseOptions` specifying the parsing options. */ declare function parse(text: string, options?: NjsonParseOptions): T; /** * Converts a Next JavaScript Object Notation (NJSON) string into an object. * * @param text A valid NJSON string. * @param reviver A function that transforms the results. This function is called for each member of the object. * If a member contains nested objects, the nested objects are transformed before the parent object is. */ declare function parse(text: string, reviver?: NjsonFunction): T; /** * Converts a JavaScript value to a Next JavaScript Object Notation (NJSON) string. * * @param value A JavaScript value, any value, to be converted. * @param options A `NjsonStringifyOptions` specifying the serializing options. */ declare function stringify(value: unknown, options?: NjsonStringifyOptions): string; /** * Converts a JavaScript value to a Next JavaScript Object Notation (NJSON) string. * * @param value A JavaScript value, any value, to be converted. * @param replacer A function that transforms the results or the `Array` of _keys_ to be serialized. * @param space Adds indentation, white space, and line break characters to the return-value NJSON text to make it easier to read. */ declare function stringify(value: unknown, replacer?: NjsonReplacer | null, space?: number | string): string; /** Next JavaScript Object Notation. */ export declare const NJSON: { readonly parse: typeof parse; readonly stringify: typeof stringify; }; declare global { namespace Express { interface Response { /** * Sends NJSON response. * * @param body A value sent as body. * @param options A `NjsonStringifyOptions` specifying the body serializing options; defaults to `ExpressNjsonOptions.stringify` passed to `expressNJSON`. */ njson(body?: any, options?: NjsonStringifyOptions): Promise; } } interface Response { /** * Parses the response body with `NJSON.parse`. * * @param options A `NjsonParseOptions` specifying the body parsing options; defaults to `options` passed to `fetchNJSON`. */ readonly njson: (options?: NjsonParseOptions) => Promise; } } /** `expressNJSON` middleware options. */ export interface ExpressNjsonOptions { /** A `NjsonParseOptions` specifying the body parsing options. */ parse?: NjsonParseOptions; /** A `NjsonStringifyOptions` specifying the body serializing options; can be overridden while calling `res.njson()`. */ stringify?: NjsonStringifyOptions; } /** * An Express middleware which works as NJSON body parser and installs the `Express.Response.njson` method. * * @param options A `ExpressNjsonOptions` specifying the options to be used while serializing and parsing bodies. */ export declare function expressNJSON(options?: ExpressNjsonOptions): (req: any, res: any, next: any) => any; /** * Installs the `Response.njson` method. * * @param options The default `NjsonParseOptions` specifying the body parsing options. */ export declare function fetchNJSON(options?: NjsonParseOptions): void; export {};