Types

Internal Type reference

Config

type ActionLogType = "info" | "error" | "warn" | "debug";
type ActionLoggerLevels = Array<{ level: ActionLogType }>;
type BaseErrorMap = { "internal-server-error": string };
 
type ActionRouterConfig<TErrorCode extends string> = {
  /**
   * Name of a router instance
   */
  name?: string | DefaultRouterName;
  logging?: ActionLoggerLevels;
  error?: {
    codes: Record<TErrorCode, string> & Partial<BaseErrorMap>;
  };
};

ActionRequest

Represents an action request object with a context. It contains the information regarding an action request. Each action request will have it's own action request object.

type ActionRequest<TContext> = {
  context: TContext;
  headers: ReturnType<typeof headers>;
  cookies: ReturnType<typeof cookies>;
};

ActionResponse

Each action requests will have it's own action response object. Registered action handlers can invoke these provided methods from this object to send data (helper method), error (helper method) or even throwing redirects or not found errors to the client;

type ActionSuccessResponse<TData> = {
  success: true;
  data: TData;
};
 
type ActionErrorResponse<TCode> = {
  success: false;
  error: {
    code: TCode;
    message: string;
  };
};
 
type ActionResponse<TErrorCode extends string> = {
  /**
   * @returns formatted(JSON) data response
   */
  data: <TData>(data: TData) => ActionSuccessResponse<TData>;
  /**
   * @returns formatted(JSON) error response
   */
  error: <TCode extends TErrorCode>(
    code: TCode,
    message?: string
  ) => ActionErrorResponse<TCode>;
  /**
   * @returns Helper to create error with new code just in time.
   */
  createError: <T extends string>(
    code: T,
    message: string
  ) => ActionErrorResponse<T>;
  /**
   * @see {redirect}
   */
  redirect: typeof redirect;
  /**
   * @see {notFound}
   */
  notFound: typeof notFound;
};

ActionMiddleware

type ActionMiddleware<TContext, TErrorCode, TReturn> = (
  request: ActionRequest<TContext>
) => Promise<TReturn>;
 
type ActionMiddlewareOptions = {
  name: string;
};

ActionHandler

type ActionHandler<TContext, TErrorCode extends string, TReturn> = (
  request: ActionRequest<TContext>,
  response: ActionResponse<TErrorCode>
) => Promise<TReturn>;
 
type ActionHandlerOptions = {
  name: string;
};