Documentation Index
Fetch the complete documentation index at: https://mintlify.com/sebamar88/bytekit/llms.txt
Use this file to discover all available pages before exploring further.
Type definitions and helpers for managing request state
Documentation Index
Fetch the complete documentation index at: https://mintlify.com/sebamar88/bytekit/llms.txt
Use this file to discover all available pages before exploring further.
import {
RequestState,
RequestStatus,
RequestContext,
createInitialState,
createLoadingState,
createSuccessState,
createErrorState
} from "bytekit";
// or
import { RequestState } from "bytekit/query-state";
type RequestStatus = "idle" | "loading" | "success" | "error";
Show properties
interface RequestContext {
queryKey?: string[];
url: string;
method: string;
timestamp: number;
requestId: string;
}
interface RequestLifecycleCallbacks<T = unknown> {
onStart?: (context: RequestContext) => void | Promise<void>;
onSuccess?: (data: T, context: RequestContext) => void | Promise<void>;
onError?: (error: ApiError, context: RequestContext) => void | Promise<void>;
onSettled?: (
data: T | undefined,
error: ApiError | undefined,
context: RequestContext
) => void | Promise<void>;
}
function createInitialState<T = unknown>(): RequestState<T>
import { createInitialState } from "bytekit";
const state = createInitialState<User>();
// {
// status: "idle",
// isLoading: false,
// isSuccess: false,
// isError: false,
// isIdle: true,
// isFetching: false,
// dataUpdatedAt: 0,
// errorUpdatedAt: 0
// }
function createLoadingState<T = unknown>(previousData?: T): RequestState<T>
import { createLoadingState } from "bytekit";
const state = createLoadingState(previousData);
// {
// status: "loading",
// data: previousData,
// isLoading: true,
// isFetching: true,
// ...
// }
function createSuccessState<T = unknown>(data: T): RequestState<T>
import { createSuccessState } from "bytekit";
const state = createSuccessState({ id: 1, name: "John" });
// {
// status: "success",
// data: { id: 1, name: "John" },
// isSuccess: true,
// dataUpdatedAt: Date.now(),
// ...
// }
function createErrorState<T = unknown>(
error: ApiError,
previousData?: T
): RequestState<T>
import { createErrorState } from "bytekit";
const state = createErrorState(error, previousData);
// {
// status: "error",
// error,
// data: previousData,
// isError: true,
// errorUpdatedAt: Date.now(),
// ...
// }
import {
QueryClient,
RequestState,
createInitialState,
createLoadingState,
createSuccessState,
createErrorState
} from "bytekit";
class MyQueryManager {
private state: RequestState<User> = createInitialState();
private queryClient = new QueryClient();
async fetchUser(id: string) {
// Set loading state
this.state = createLoadingState(this.state.data);
this.notifySubscribers();
try {
const data = await this.queryClient.query(
["user", id],
() => api.get(`/users/${id}`)
);
// Set success state
this.state = createSuccessState(data);
this.notifySubscribers();
return data;
} catch (error) {
// Set error state
this.state = createErrorState(error as ApiError, this.state.data);
this.notifySubscribers();
throw error;
}
}
private notifySubscribers() {
// Notify UI of state change
}
}
interface QueryClientEvents<T = unknown> {
"query:start": { context: RequestContext };
"query:success": { data: T; context: RequestContext };
"query:error": { error: ApiError; context: RequestContext };
"query:settled": {
data?: T;
error?: ApiError;
context: RequestContext;
};
"state:change": { state: RequestState<T>; context: RequestContext };
"cache:invalidate": { queryKey: string[] };
"cache:update": { queryKey: string[]; data: T };
}