UNPKG

@appello/common

Version:

Common package with many useful features for web and mobile development

304 lines (243 loc) 11.9 kB
/// <reference types="react" /> import * as react from 'react'; import { Ref, RefCallback, EffectCallback, DependencyList, Dispatch, SetStateAction } from 'react'; import { UseFormProps } from 'react-hook-form'; import { PathValue, Path } from 'dot-path-value'; declare function useCodeTimer(): { seconds: number; inProgress: boolean; sendCode: (cb?: VoidFunction, passSeconds?: number) => void; }; declare function useCombinedRef<T>(...refs: Ref<T>[]): RefCallback<T>; type ElementType = HTMLElement | Element | Window | Document; type Nullable<T> = T | null; type NotNullable<T> = T extends null ? never : T; type Nillable<T> = T | null | undefined; type NotNillable<T> = T extends null | undefined ? never : T; type Unidentifiable<T> = T | undefined; type Negative<T> = T | undefined | null | false; type ValueOf<T> = T[keyof T]; type AnyObject = Record<any, any>; type AnyPromise = Promise<any>; type PartialRecord<T extends keyof any, K> = { [P in T]?: K; }; type PartialDeep<T> = { [P in keyof T]?: T[P] extends object ? PartialDeep<T[P]> : T[P]; }; type ResponseErrors = Record<string, string>; type UnknownFunction = (...args: unknown[]) => unknown; type AnyFunction = (...args: any[]) => any; type PromiseType<T> = T extends Promise<infer U> ? U : T; type RequiredKeys<T> = { [K in keyof T]-?: object extends { [P in K]: T[K]; } ? never : K; }[keyof T]; type OptionalKeys<T> = { [K in keyof T]-?: object extends { [P in K]: T[K]; } ? K : never; }[keyof T]; type Merge<T, K> = Omit<T, keyof K> & K; type AsyncReturnType<T extends (...args: any) => Promise<any>> = T extends (...args: any) => Promise<infer R> ? R : any; type FirstArgument<T extends (...args: any) => any> = T extends (arg: infer A, ...args: any) => any ? A : never; declare const checkIsServer: () => boolean; declare const checkNavigator: () => boolean; type DebounceOptions$1 = { leading?: boolean; trailing?: boolean; maxWait?: number; }; declare function debounce<T extends AnyFunction>(func: T, wait?: number, options?: DebounceOptions$1): { (...args: Parameters<T>): ReturnType<T> | undefined; cancel: () => void; flush: () => any; pending: () => boolean; }; declare const deepMergeObjects: <T extends object>(target: T, source: unknown) => T; declare const entries: <T>(o: T) => [Extract<keyof T, string | number>, T[keyof T]][]; declare function getDirtyValues<DirtyFields extends Record<string, unknown>, Values extends Record<keyof DirtyFields, unknown>>(dirtyFields: DirtyFields, values: Values): Partial<typeof values>; declare const getFileExtension: (fileName: string) => Unidentifiable<string>; type Prototype = '[object Undefined]' | '[object Null]' | '[object Boolean]' | '[object Number]' | '[object String]' | '[object Symbol]' | '[object BigInt]' | '[object Object]' | '[object Function]' | '[object Array]' | '[object Date]' | '[object RegExp]' | '[object Error]' | '[object Arguments]' | '[object Set]' | '[object Map]' | '[object WeakSet]' | '[object WeakMap]' | '[object ArrayBuffer]' | '[object DataView]' | '[object Promise]' | '[object Generator]' | '[object GeneratorFunction]' | '[object AsyncFunction]' | '[object Reflect]' | '[object Proxy]'; declare function getProto(value: any): Prototype; declare function getWeekdaysNames(options?: { longFormat?: boolean; }): string[]; declare const isArray: (arg: any) => arg is any[]; declare const isDate: (value: any) => value is Date; declare const isEmpty: (value: any) => boolean; declare const isFloat: (value: any) => value is number; declare const isFunction: (value: any) => value is Function; declare const isInt: (value: any) => value is number; declare function isNil(value: any): value is null | undefined; declare const isNumber: (value: any) => value is number; declare const isObject: (value: any) => value is object; declare const isPrimitive: (value: any) => boolean; declare const isPromise: (value: any) => value is AnyPromise; declare const isString: (value: any) => value is string; declare const isSymbol: (value: any) => value is symbol; declare const keys: <T>(o: T) => (keyof T)[]; declare function makeFormData(object: any, formData?: FormData, prefix?: string): FormData; interface MakeQueryStringOptions { withPrefix?: boolean; } declare function makeQueryString<T extends string | number>(params: Record<string, T | undefined>, options?: MakeQueryStringOptions): string; type FormDataEntry = [key: string, value: any]; declare function mapFormData(formData: FormData, callback: (item: FormDataEntry) => FormDataEntry): FormData; declare const mergeCollectionByKey: <T extends AnyObject = AnyObject>(oldCollect: T[], newCollect: T[], type?: 'push' | 'unshift', key?: keyof T | ((item: T) => string | number)) => T[]; declare function noop(): void; declare function omit<T extends Record<any, any>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>; declare function pick<T extends Record<any, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>; declare function sleep(ms: number): Promise<void>; type ThrottleOptions = { leading?: boolean; trailing?: boolean; maxWait?: number; }; declare function throttle<T extends AnyFunction>(func: T, wait?: number, options?: ThrottleOptions): { (...args: Parameters<T>): ReturnType<T> | undefined; cancel: () => void; flush: () => any; pending: () => boolean; }; declare const toBase64: (file: File) => Promise<Nullable<string | ArrayBuffer>>; declare function toFinite(value: any): number; interface DebounceOptions extends DebounceOptions$1 { wait?: number; } declare function useDebounceCallback<T extends AnyFunction>(fn: T, options?: DebounceOptions): { debounce: { (...args: Parameters<T>): any; cancel: () => void; flush: () => any; pending: () => boolean; }; cancel: () => void; flush: () => any; }; declare function useDebounceEffect(effect: EffectCallback, deps?: DependencyList, options?: DebounceOptions): void; declare function useDefault<T>(defaultValue: T, initialValue: T): [T, Dispatch<SetStateAction<T>>]; declare function useFirstMountState(): boolean; type SyncPolicyLiteral = 'default-values-first' | 'storage-first'; interface WebStorage { getItem(key: string, ...args: any[]): any; setItem(key: string, value: any, ...args: any[]): any; removeItem(key: string, ...args: any[]): any; } interface FormPersistProps<T extends AnyObject = AnyObject> { key: string; storage?: WebStorage; excludeFields?: (keyof T)[]; includeFields?: (keyof T)[]; } declare const useHookFormPersist: <T extends AnyObject = AnyObject>({ key: keyStorage, storage, excludeFields, includeFields, }: FormPersistProps<T>) => { syncFromStorage: ({ defaultValues, syncPolicy, }: { defaultValues?: UseFormProps<T>['defaultValues']; syncPolicy?: SyncPolicyLiteral; }) => Promise<T>; syncToStorage: (watchedValues: T) => void; clear: () => Promise<void>; }; type UseIntervalProps = { fn: () => void; delay?: number; immediateStart?: boolean; immediateCallFn?: boolean; onStop?: () => void; }; declare function useInterval({ fn, delay, onStop, immediateStart, immediateCallFn, }: UseIntervalProps): { start: () => void; stop: () => void; setState: react.Dispatch<react.SetStateAction<boolean>>; }; declare function useIsClient(): boolean; declare function useIsMounted(): () => boolean; declare function useLatest<T>(value: T): react.MutableRefObject<T>; declare function useManualUpdate(): VoidFunction; type Func = (this: any, ...args: any[]) => any; declare function useMemoCallback<T extends Func>(fn: T): T; declare const useMountEffect: (fn: () => void) => void; type ShouldUpdateFunc<T> = (prev: Unidentifiable<T>, next: T) => boolean; declare function usePrevious<T>(state: T, shouldUpdate?: ShouldUpdateFunc<T>): T | undefined; interface UseQueryParamsBuilderProps<T extends AnyObject = AnyObject> { requestCb: (params: T) => Promise<any>; defaultParams: T; options?: Pick<DebounceOptions, 'wait'>; } declare const useQueryParamsBuilder: <T extends AnyObject = AnyObject>({ requestCb, defaultParams, options, }: UseQueryParamsBuilderProps<T>) => { loading: boolean; queryParams: T; updateQueryParams: (params: Partial<T>, withDebounce?: boolean) => void; reset: () => void; }; type OptionKeys<T> = { label: Path<T>; value: Path<T>; }; declare function useSelectOptions<T extends object, TKeys extends OptionKeys<T>, P extends AnyObject>(data: T[] | undefined, keys: TKeys, additionalProps?: (item: T) => P): ({ value: PathValue<T, TKeys["value"]>; label: PathValue<T, TKeys["label"]>; } & P)[]; declare function useStateObject<T extends AnyObject>(initialValue: T): { state: T; handleUpdate: (arg: ((obj: T) => Partial<T>) | Partial<T>) => void; }; interface UseStepReturn { step: number; increment: () => void; decrement: () => void; reset: () => void; setStep: Dispatch<SetStateAction<number>>; } declare function useStep(initialValue?: number): UseStepReturn; interface UseSwitchValueReturn { value: boolean; on(): void; off(): void; toggle(): void; set: Dispatch<SetStateAction<boolean>>; } declare function useSwitchValue(initial: boolean): UseSwitchValueReturn; declare const useUnmountEffect: (fn: () => void) => void; declare function useUpdateEffect(effect: EffectCallback, deps: DependencyList): void; interface AwsImageServiceResizeOptions { width?: number; height?: number; fit?: 'cover' | 'contain' | 'fill' | 'inside' | 'outside'; background?: { r: number; g: number; b: number; alpha: number; }; } interface AwsImageServiceSmartCropOptions { faceIndex?: number; padding?: number; } interface AwsImageServiceProps { resize?: AwsImageServiceResizeOptions; flatten?: boolean; grayscale?: boolean; flip?: boolean; flop?: boolean; negate?: boolean; normalise?: boolean; tint?: { r: number; g: number; b: number; }; smartCrop?: AwsImageServiceSmartCropOptions; } declare class AwsImageService { private static instance; private readonly awsUrl; private readonly awsBucket; private constructor(); static initialize(awsUrl: string, awsBucket: string): AwsImageService; private static getInstance; static genAwsImgUrl(key: string, options?: AwsImageServiceProps): string; } export { type AnyFunction, type AnyObject, type AnyPromise, type AsyncReturnType, AwsImageService, type AwsImageServiceProps, type AwsImageServiceResizeOptions, type AwsImageServiceSmartCropOptions, type DebounceOptions$1 as DebounceOptions, type ElementType, type FirstArgument, type FormPersistProps, type Merge, type Negative, type Nillable, type NotNillable, type NotNullable, type Nullable, type OptionalKeys, type PartialDeep, type PartialRecord, type PromiseType, type RequiredKeys, type ResponseErrors, type ThrottleOptions, type Unidentifiable, type UnknownFunction, type ValueOf, checkIsServer, checkNavigator, debounce, deepMergeObjects, entries, getDirtyValues, getFileExtension, getProto, getWeekdaysNames, isArray, isDate, isEmpty, isFloat, isFunction, isInt, isNil, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, keys, makeFormData, makeQueryString, mapFormData, mergeCollectionByKey, noop, omit, pick, sleep, throttle, toBase64, toFinite, useCodeTimer, useCombinedRef, useDebounceCallback, useDebounceEffect, useDefault, useFirstMountState, useHookFormPersist, useInterval, useIsClient, useIsMounted, useLatest, useManualUpdate, useMemoCallback, useMountEffect, usePrevious, useQueryParamsBuilder, useSelectOptions, useStateObject, useStep, useSwitchValue, useUnmountEffect, useUpdateEffect };