///
declare enum SCAXER_STATE {
/**
* Scaxer has not been called yet.
*/
WAITING = 0,
/**
* Scaxer has been called and is being resolved.
*/
RESOLVING = 1,
/**
* Scaxer has been resolved successfully.
*/
FULFILLED = 2,
/**
* Scaxer has been rejected.
*/
REJECTED = 3,
/**
* Scaxer has been interrupted due to exceptions in handlers.
*/
INTERRUPTED = 4,
/**
* Scaxer has been resolved successfully and its data has been read for at least once.
*/
CONSUMED = 5
}
declare enum SCAXER_BLOCKING {
BLOCKING = "BLOCKING",
IGNORING = "IGNORING",
UPCOMING = "UPCOMING"
}
declare enum AJAX_CALL_METHOD {
GET = "get",
POST = "post",
PUT = "put",
DELETE = "delete",
OPTIONS = "options"
}
/**
* This ENUM will be exposed to users to select certain scheme type.
*/
declare enum TEMPLATE_TYPE {
AJAX_JSON_TEMPLATE = "AJAX_JSON_SCHEME"
}
/********************************************************* */
/************ Promise Function Related Types⬇️ *************/
/********************************************************* */
declare type TPromiseFunctionType = (param: TParamType) => Promise;
declare type TCreateGenericPromiseFunctionType = (config: TConfigType) => TPromiseFunctionType;
/********************************************************* */
/****************** Scaxer Related Types⬇️ *****************/
/********************************************************* */
/**
* Field types in Scaxer
*/
declare type TMapResultToDataType = (result: TResultType) => TDataType | undefined;
declare type TMapReasonToErrorType = (reason: TReasonType) => TErrorType | undefined;
declare type TOnFulfilmentType = (data?: TDataType, result?: TResultType) => void;
declare type TOnRejectionType = (error?: TErrorType, reason?: TReasonType) => void;
declare type THandleExceptionType = (exc: Error) => void;
/**
* Scaxer subscriber callback
*/
declare type TScaxerSubscriberType = () => void;
/**
* Scaxer caller api
*/
declare type TScaxerCallType = (p: TParamType, extra?: {
onFulfillment?: TOnFulfilmentType;
onRejection?: TOnRejectionType;
}) => void;
/**
* The data type of a scaxer.
*/
interface IScaxerData {
TParamType?: any;
TDataType?: any;
TErrorType?: any;
TResultType?: any;
TReasonType?: any;
}
/**
* Batch of scaxers' data type.
* This is used to define the data type for a batch of scaxer.
*/
declare type TScaxerDataTypeMap = {
[scaxerName: string]: IScaxerData;
};
/**
* Interface used to normalize the Scaxer constructor intake.
*/
interface IScaxerConfiguration {
promise?: TPromiseFunctionType;
blocking?: SCAXER_BLOCKING;
mapResultToData?: TMapResultToDataType;
mapReasonToError?: TMapReasonToErrorType;
onFulfillment?: TOnFulfilmentType;
onRejection?: TOnRejectionType;
handleException?: THandleExceptionType;
}
/**
* Scaxer sucessor configuration where another scaxer acts as the fulfillment successor
*/
interface IScaxerFulfillmentSuccessorConfiguration {
onFulfillment?: string;
}
/**
* Scaxer sucessor configuration where another scaxer acts as the rejection successor
*/
interface IScaxerRejectionSuccessorConfiguration {
onRejection?: string;
}
/**
* Scaxer configuration types for non-data-type-defined scaxer
*/
declare type TGenericBatchConfiguration = {
[key: string]: IScaxerConfiguration | IScaxerFulfillmentSuccessorConfiguration | IScaxerRejectionSuccessorConfiguration;
};
/**
* Scaxer configuration types for explicitly data-type-defined scaxer
*/
declare type TRestrictedScaxerBatchConfiguration = {
[K in keyof TScaxerDataTypes]?: IScaxerConfiguration | IScaxerFulfillmentSuccessorConfiguration | IScaxerRejectionSuccessorConfiguration;
};
/**
* Scaxer configuration types for all.
*/
declare type TScaxerBatchConfiguration = TRestrictedScaxerBatchConfiguration & TGenericBatchConfiguration;
/**
* Generic to return a specific scaxer view type. Refer to IScaxerView interface for details.
* This is used to get a specific scaxer view type from TScaxerDataTypeMap
* and a given scaxer name type, which must be string literal type.
*/
declare type TScaxerViewType> = IScaxerView;
/**
* Read-only scaxer type used to monitor and trigger the promise function call.
* This is the type of scaxer instance returned to user.
*/
interface IScaxerView {
readonly name: string;
readonly getState: () => SCAXER_STATE | undefined;
readonly getData: () => TDataType | undefined;
readonly getError: () => TErrorType | undefined;
readonly getResult: () => TResultType | undefined;
readonly getReason: () => TReasonType | undefined;
readonly call: TScaxerCallType;
readonly isFulfilled: () => boolean;
}
/**
* Scaxer type exposed to internal classes such as `attach`.
*/
interface IScaxerManager {
readonly name: string;
readonly subscriberList: TScaxerSubscriberType[];
readonly injectSubscription: (callBack: TScaxerSubscriberType) => (() => void);
readonly updateSubscriber: () => void;
}
/**
* Type of the scaxer map inside a pool instance.
*/
declare type TScaxerMap = {
[key: string]: (IScaxerView & IScaxerManager);
};
/**
* Scaxer intrisic strucutre and type defination.
*/
interface IScaxer {
readonly name: string;
readonly blocking: SCAXER_BLOCKING;
readonly promise: TPromiseFunctionType;
mapResultToData: TMapResultToDataType;
mapReasonToError: TMapReasonToErrorType;
onFulfillment: TOnFulfilmentType;
onRejection: TOnRejectionType;
handleException: THandleExceptionType;
}
/********************************************************* */
/******************* Pool Related Types⬇️ ******************/
/********************************************************* */
/**
* IPool provides all apis that needed to create/retrieve a scaxer.
*/
interface IGetScaxer {
>(scaxerName: K): TScaxerViewType;
(scaxerName: string): IScaxerView;
}
interface IPool {
name: string;
getScaxer: IGetScaxer;
register(newScaxer: TScaxerBatchConfiguration): void;
}
/**
* This interface will be used internally, essentially by `attach`.
*/
interface IPoolView {
getScaxerManager(scaxerName: string): IScaxerManager;
}
/********************************************************* */
/****************** Attach Related Types⬇️ *****************/
/********************************************************* */
declare type TAttachedComponentType = React.ComponentType> & {
render?: (props: TProps, ref?: React.LegacyRef) => React.ReactElement;
};
declare type TAttachWrapperComponentType = React.ForwardRefExoticComponent & React.RefAttributes>;
declare type TAttachHOC = >>(component: C) => TAttachWrapperComponentType>;
interface IAttach {
(poolName: string, scaxerNames: string[]): TAttachHOC;
}
/********************************************************* */
/****************** Scheme Related Types⬇️ *****************/
/********************************************************* */
/**
* This interface defines scheme internal structure
*/
interface ITemplate {
craftPromiseFunction: TCreateGenericPromiseFunctionType;
}
interface IStaticTemplate {
configure: (param: TConfigType) => void;
}
declare type TAjaxJsonConfigType = {
url: string;
method?: AJAX_CALL_METHOD;
getOptions?: () => RequestInit;
};
declare type TAjaxJsonParamType = {
[key: string]: boolean | string | number | string[] | number[] | boolean[] | TAjaxJsonParamType;
};
declare type TAjaxJsonResultType = any;
declare type TAjaxJsonTemplateConfigType = {
getOptions: () => RequestInit;
};
/**
* AjaxJson Scheme will create promise function that will fire an AJAX call whose content type
* and response content type are all in json format. Return code is supposed between 200 to 300
* for success or 400 to 600 for failure.
*/
declare class AjaxJsonTemplate implements IStaticTemplate, ITemplate {
/**
* dynamic options are global options customised by users, it will be dynamically generated in runtime
*/
private static getDynamicGlobalOptions;
/**
* Singleton instance for All IScheme object
*/
private static instance;
/**
* global options will be used across all instances, and it is fixed and hidden from users
*/
private static staticGlobalOptions;
/**
* Singleton AjaxJsonScheme constructor
*/
constructor();
/**
* Implement configure method in IStaticScheme interface
*/
configure: (config: TAjaxJsonTemplateConfigType) => void;
/**
* Craft a promise function whose promise body is a AJAX call with both request and response as JSON object
*/
craftPromiseFunction: TCreateGenericPromiseFunctionType;
}
declare const _default: AjaxJsonTemplate;
/**
* Map template name to template type.
*/
declare type TemplateMapType = {
[TEMPLATE_TYPE.AJAX_JSON_TEMPLATE]: typeof _default;
};
declare type TemplateConfigType = T extends ITemplate ? TConfigType : T;
declare type TemplateParamType = T extends ITemplate ? TParamType : T;
declare type TemplateResultType = T extends ITemplate ? TResultType : T;
declare type StaticSchemeConfigType = T extends IStaticTemplate ? TConfigType : T;
/**
* Create a promise function according to the specific template
* @param template template name of TEMPLATE_TYPE type
* @param config specific configuration used to create the promise function
*/
declare function createTemplatePromiseFunction, TConfigType extends TemplateConfigType, TParamType extends TemplateParamType, TResultType extends TemplateResultType>(template: TTemplateType, config: TConfigType): TPromiseFunctionType;
/**
* Change the default configurations of a template.
* @param template template name of TEMPLATE_TYPE type
* @param config configurations that will overwrite the template default behaviors
*/
declare function configureTemplate, TConfigType extends StaticSchemeConfigType>(template: TSchemeType, config: TConfigType): void;
/**
* Create a pool uniquely identified by a name.
* @param poolName a unique string used to identify the pool.
* @generic `N`: shape like { [key: string]: IScaxerView }
*/
declare function createPool(poolName: string, scaxersConfigs: TScaxerBatchConfiguration): IPool;
declare const attach: IAttach;
declare const AjaxCallMethod: typeof AJAX_CALL_METHOD;
declare const ScaxerState: typeof SCAXER_STATE;
declare const ScaxerBlocking: typeof SCAXER_BLOCKING;
declare const Template: {
changeTemplateSettings: typeof configureTemplate;
createPromise: typeof createTemplatePromiseFunction;
types: typeof TEMPLATE_TYPE;
};
export { AjaxCallMethod, IAttach, IGetScaxer, IPool, IPoolView, IScaxer, IScaxerConfiguration, IScaxerData, IScaxerManager, IScaxerView, IStaticTemplate, ITemplate, ScaxerBlocking, ScaxerState, TAttachHOC, TAttachWrapperComponentType, TAttachedComponentType, TCreateGenericPromiseFunctionType, THandleExceptionType, TMapReasonToErrorType, TMapResultToDataType, TOnFulfilmentType, TOnRejectionType, TPromiseFunctionType, TScaxerBatchConfiguration, TScaxerCallType, TScaxerDataTypeMap, TScaxerMap, TScaxerSubscriberType, TScaxerViewType, Template, attach, createPool };