///
import { IncomingMessage } from "http";
import { Context, Provider } from "react";
import { Cookies, CookieConsent } from "../util/cookies";
export declare type PropertyValueType = any;
export declare type ContextValueType = any;
export declare type GlobalAppStatePropertySetter = (values: Set, cookieConsent: CookieConsent, value: T) => Promise;
export declare type GlobalAppStatePropertySetterProxy = (value: T) => void;
/**
* This is how you describe a global app state property.
* We're talking about an object literal that has
* certain properties defined on it.
*/
export interface GlobalAppStatePropertyParameters {
/**
* This will be the name of your global app state property.
*/
key: string;
/**
* A custom plural form for the `key` of your global app state property.
* If you don't like the plural form generated by the library based
* on the `key`, then you can specify how the plural form should look
* here.
*/
keyPlural?: string;
/**
* A default state value for your global app state property
* to fall back to if all attempts initializing/restoring
* state would fail. Must be a valid value — or in other
* words, the value must exist in `defaultValues`.
*/
defaultValue: T;
/**
* A set of values enumerating the states that will be regarded
* as valid states for your global app state property to be in.
*/
defaultValues: T[];
/**
* An object literal containing functions that initialize/restore
* the state of your global app state property, server-side and
* client-side respectively.
*/
initializeValue?: {
/**
* A function for initializing/restoring the state of your
* global app state property server-side when performing
* an on-demand server-side render.
* @param values A set of values enumerating the states that are regarded as valid states for your global app state property
* @param defaultValue This global app state property's defaultValue
* @param cookies An object literal representing any cookies sent with the client's request
* @param req The request itself
* @returns A valid state value synchronously or asynchronously
*/
serverSide?(values?: Set, defaultValue?: T, cookies?: Cookies, req?: IncomingMessage): T | Promise;
/**
* A function for initializing/restoring the state of your
* global app state property client-side when the `App`
* component has mounted.
* @param values A set of values enumerating the states that are regarded as valid states for your global app state property
* @param existingValue A pre-existing state value or the `defaultValue`
* @returns A valid state value synchronously or asynchronously
*/
clientSide?(values?: Set, existingValue?: T): T | Promise;
};
/**
* Indicates that encountering a `urlParam` leads to the state of your
* global app state property becoming the value of that `urlParam`.
* Encountering a `urlParam` is a short way of saying that
* the `query` property of the argument passed to
* `getInitialProps` has a key that matches
* the key of your global app
* state property.
*/
onURLParam?: boolean;
/**
* An object literal containing functions that retrieve a set
* of values enumerating the states that will be regarded
* as valid states for your global app state property.
* The sets returned by these functions override
* the `defaultValues`.
*/
getValues?: {
/**
* A function for retrieving the set of values server-side.
* @returns The set of values either synchronously or asynchronously
*/
serverSide?(): T[] | Promise;
/**
* A function for retrieving the set of values client-side.
* @returns The set of values either synchronously or asynchronously
*/
clientSide?(): T[] | Promise;
};
/**
* A function that gets invoked when the state of your
* global app state property is explicitly changed.
* The function must be asynchronous.
* @param values A set of values enumerating the states that are regarded as valid states for your global app state property
* @param cookieConsent Indicates whether the client has given consent to cookies or not
* @param value The state value that your global app state property is being set to
*/
setValue?(values: Set, cookieConsent: CookieConsent, value: T): Promise;
/**
* Indicates that your global app state property should be
* treated as sensitive information. Defaults to false.
*/
isSensitiveInformation?: boolean;
/**
* An object literal. Lets you link your global app state property
* to a `React.Context`, such that the state of your global app
* state property controls what data is in the `Context`.
* If controlContext is defined, `controlContext.Context`
* and/or `controlContext.ContextProvider` must also be
* defined.
*/
controlContext?: {
/**
* A transformation that is applied to the state value of
* your global app state property before it's placed into
* the `Context`. The transformation can either be
* synchronous or asynchronous, but if it's
* asynchronous, you have to indicate it
* using the `controlContext.isAsync`
* flag.
* @param value The state value of your global app state property
* @returns The data that is placed into the `Context` either synchronously or asynchronously
*/
transformValue?(value: T): C | Promise;
/**
* Indicates that `controlContext.transformValue` is defined
* and returns a `Promise` (is asynchronous).
* Defaults to false.
*/
isAsync?: boolean;
/**
* Indicates that `controlContext.transformValue` is defined
* and returns or (if it's a `Promise`) resolves to something
* that can be serialized server-side by Next.js and then
* hydrated client-side. Defaults to false.
*/
isSerializable?: boolean;
/**
* A `React.Context`.
*/
context?: Context;
/**
* It doesn't strictly have to be a `Context.Provider`.
* It just has to be similar enough — more specifically,
* it has to be a component that wraps its children
* and takes a prop called `value`.
*/
ContextProvider?: Provider;
};
}
export interface DehydratedGlobalAppStateProperty {
value: T;
values: T[];
serializedContext?: C;
}
export declare type HydratedGlobalAppStatePropertyType = T | Set | GlobalAppStatePropertySetterProxy;
declare class GlobalAppStateProperty {
readonly key: string;
readonly keyPlural: string;
readonly setterName: string;
readonly isSensitiveInformation?: boolean;
readonly onURLParam?: boolean;
private readonly initializeValue?;
private readonly getValues?;
private setValue;
private readonly controlContext?;
private state;
constructor({ key, keyPlural, defaultValue, defaultValues, ...parameters }: GlobalAppStatePropertyParameters);
initializeStateServerSide(cookies: Cookies, req: IncomingMessage): Promise;
injectDehydratedState({ value, values, serializedContext, }: DehydratedGlobalAppStateProperty): void;
initializeStateClientSide(existingValue: T, existingValues: Set): Promise;
onURLParamCallback(values: Set, existingValue: T, newValue: T): Promise;
get value(): T;
get values(): Set;
get setter(): GlobalAppStatePropertySetter;
get contextValue(): C | undefined;
get ContextProvider(): Provider | undefined;
}
export default GlobalAppStateProperty;
//# sourceMappingURL=GlobalAppStateProperty.d.ts.map