t7m
Version:
Transformer for Elysia and Hono
113 lines • 4.45 kB
TypeScript
import type { AnyCache, Cache } from './cache';
import type { OnlyPossiblyUndefined } from './types';
/**
* Include function type.
* @template TInput The type of the input object.
* @template TOutput The type of the output object.
* @template K The key of the output object to include.
* @template Props The type of the props object.
*/
type IncludeFunction<TInput, TOutput, K extends keyof TOutput, Props> = (input: TInput, props: Props, forwardedIncludes: string[]) => Promise<TOutput[K]> | TOutput[K];
/**
* Abstract transformer class.
* @template TInput The type of the input object.
* @template TOutput The type of the output object.
* @template Props The type of the props object.
* @template Includes The type of the includes object.
*/
declare abstract class AbstractTransformer<TInput, TOutput, Props extends Record<string, unknown> | undefined = undefined, Includes extends keyof OnlyPossiblyUndefined<TOutput> = keyof OnlyPossiblyUndefined<TOutput>> {
protected clearCacheOnTransform: boolean;
/**
* Creates a new transformer instance.
* @param params - Configuration options for the transformer.
* @param params.clearCacheOnTransform - Whether to clear the cache after each transform call. Defaults to `true`.
*/
constructor(params?: {
clearCacheOnTransform?: boolean;
});
/**
* Abstract method that must be implemented by subclasses to provide the core transformation logic.
* @param input The input object to transform.
* @param props Optional props object for additional parameters.
* @returns The transformed output object.
*/
protected abstract data(input: TInput, props: Props): TOutput | Promise<TOutput>;
/**
* Map of include functions for each possible include.
* @template K The key of the output object to include.
* @template Props The type of the props object.
*/
protected readonly includesMap: Partial<{
[K in Includes]: IncludeFunction<TInput, TOutput, K, Props>;
}>;
readonly cache: Record<string, AnyCache>;
clearCache: () => void;
private _clearCache;
private originalClearCacheOnTransform;
private disableClearCacheForTransformers;
private restoreClearCacheForTransformers;
transformers: Record<string, AnyAbstractTransformer | Cache<() => AnyAbstractTransformer>>;
private onBeforeTransform;
private onAfterTransform;
/**
* Transforms a single input object.
* @param params The parameters for the transformation.
* @returns The transformed output object.
*/
transform(params: {
input: TInput;
includes?: Includes[];
unsafeIncludes?: string[];
} & (Props extends undefined ? {
props?: never;
} : {
props: Props;
})): Promise<TOutput>;
/**
* Transforms multiple input objects.
* @param params The parameters for the transformation.
* @returns The transformed output objects.
*/
transformMany(params: {
inputs: TInput[];
includes?: Includes[];
unsafeIncludes?: string[];
} & (Props extends undefined ? {
props?: never;
} : {
props: Props;
})): Promise<TOutput[]>;
/**
* Transforms a single input object. (Easier to use in generic functions)
* @param params The parameters for the transformation.
* @returns The transformed output object.
*/
_transform(params: {
input: TInput;
props: Props;
includes?: Includes[];
unsafeIncludes?: string[];
}): Promise<TOutput>;
/**
* Transforms multiple input objects. (Easier to use in generic functions)
* @param params The parameters for the transformation.
* @returns The transformed output objects.
*/
_transformMany(params: {
inputs: TInput[];
props: Props;
includes?: Includes[];
unsafeIncludes?: string[];
}): Promise<TOutput[]>;
/**
* Transforms a single input object.
* @param input The input object to transform.
* @param props Optional props object for additional parameters.
* @param includes Optional array of includes to transform.
* @returns The transformed output object.
*/
private __transform;
}
export { AbstractTransformer, type IncludeFunction };
export type AnyAbstractTransformer = AbstractTransformer<any, any, any, any>;
//# sourceMappingURL=abstractTransformer.d.ts.map