nexting
Version:
A comprehensive, type-safe full-stack library for TypeScript/JavaScript applications. Provides server actions, API controllers, React hooks, error handling, and professional logging - all with complete type safety and inference.
78 lines (72 loc) • 3.16 kB
text/typescript
import * as swr from 'swr';
import { SWRConfiguration } from 'swr';
import * as swr_dist__internal from 'swr/dist/_internal';
import { AsyncState } from 'async-xtate';
import { StatusCodes } from 'http-status-codes';
import * as swr_mutation from 'swr/mutation';
import { SWRMutationConfiguration } from 'swr/mutation';
interface ServerErrorProps {
message: string;
uiMessage?: string;
code?: string;
status?: StatusCodes;
}
declare class ServerError extends Error {
code?: string;
status: StatusCodes;
uiMessage?: string;
constructor(props: ServerErrorProps);
toJSON(): {
message: string;
code: string | undefined;
status: StatusCodes;
uiMessage: string | undefined;
};
}
/**
* Tipo genérico que representa cualquier acción generada por makeServerAction
*/
type GenericAction = ((...args: any[]) => Promise<AsyncState<any, ReturnType<ServerError['toJSON']>>>) | (() => Promise<AsyncState<any, ReturnType<ServerError['toJSON']>>>);
/**
* Extrae los parámetros de entrada de una acción
*/
type InferActionInput<T extends GenericAction> = T extends (...args: infer P) => any ? P extends [infer First] ? First : P extends [] ? void : P : never;
/**
* Extrae el tipo de datos de respuesta exitosa de una acción
*/
type InferActionOutput<T extends GenericAction> = T extends (...args: any[]) => Promise<AsyncState<infer R, any>> ? R : never;
/**
* Extrae el tipo de error de una acción
*/
type InferActionError<T extends GenericAction> = T extends (...args: any[]) => Promise<AsyncState<any, infer E>> ? E : never;
declare function makeServerActionImmutableHook<TAction extends GenericAction>(options: {
key: string;
action: TAction;
}): {
useAction: (hookOptions: {
skip?: boolean;
context: InferActionInput<TAction>;
options?: SWRConfiguration<InferActionOutput<TAction>, InferActionError<TAction>>;
}) => swr.SWRResponse<InferActionOutput<TAction>, InferActionError<TAction>, SWRConfiguration<InferActionOutput<TAction>, InferActionError<TAction>, (arg: {
context?: NonNullable<InferActionInput<TAction>> | undefined;
key: string;
}) => swr_dist__internal.FetcherResponse<InferActionOutput<TAction>>> | undefined>;
makeKey: <T extends InferActionInput<TAction>>(context?: T) => {
context?: NonNullable<T> | undefined;
key: string;
};
};
declare function makeServerActionMutationHook<TAction extends GenericAction>(options: {
key: string;
action: TAction;
}): {
useAction: (hookOptions?: {
context?: Record<string, unknown>;
options?: SWRMutationConfiguration<InferActionOutput<TAction>, InferActionError<TAction>>;
}) => swr_mutation.SWRMutationResponse<InferActionOutput<TAction>, InferActionError<TAction>, string | false | readonly [any, ...unknown[]] | Record<any, any> | (() => swr.Arguments) | null | undefined, InferActionInput<TAction>>;
makeKey: (context?: Record<string, unknown>) => {
context?: Record<string, unknown> | undefined;
key: string;
};
};
export { makeServerActionImmutableHook, makeServerActionMutationHook };