bknd
Version:
Lightweight Firebase/Supabase alternative built to run anywhere — incl. Next.js, React Router, Astro, Cloudflare, Bun, Node, AWS Lambda & more.
81 lines (80 loc) • 2.45 kB
TypeScript
import type { SafeUser } from "./auth";
import { AuthApi, type AuthApiOptions } from "./auth/api/AuthApi";
import { DataApi, type DataApiOptions } from "./data/api/DataApi";
import { MediaApi, type MediaApiOptions } from "./media/api/MediaApi";
import { SystemApi } from "./modules/SystemApi";
import type { BaseModuleApiOptions } from "./modules";
export type TApiUser = SafeUser;
export type ApiFetcher = (input: RequestInfo | URL, init?: RequestInit) => Response | Promise<Response>;
declare global {
interface Window {
__BKND__: {
user?: TApiUser;
};
}
}
type SubApiOptions<T extends BaseModuleApiOptions> = Omit<T, keyof BaseModuleApiOptions>;
export type ApiOptions = {
host?: string;
headers?: Headers;
key?: string;
storage?: {
getItem: (key: string) => string | undefined | null | Promise<string | undefined | null>;
setItem: (key: string, value: string) => void | Promise<void>;
removeItem: (key: string) => void | Promise<void>;
};
onAuthStateChange?: (state: AuthState) => void;
fetcher?: ApiFetcher;
verbose?: boolean;
verified?: boolean;
data?: SubApiOptions<DataApiOptions>;
auth?: SubApiOptions<AuthApiOptions>;
media?: SubApiOptions<MediaApiOptions>;
} & ({
token?: string;
user?: TApiUser;
} | {
request: Request;
});
export type AuthState = {
token?: string;
user?: TApiUser;
verified: boolean;
};
export declare class Api {
private options;
private token?;
private user?;
private verified;
private token_transport;
system: SystemApi;
data: DataApi;
auth: AuthApi;
media: MediaApi;
constructor(options?: ApiOptions);
get fetcher(): ApiFetcher;
get baseUrl(): string;
get tokenKey(): string;
private extractToken;
private get storage();
updateToken(token?: string, opts?: {
rebuild?: boolean;
trigger?: boolean;
}): void;
private markAuthVerified;
isAuthVerified(): boolean;
getAuthState(): AuthState;
isAuthenticated(): boolean;
getVerifiedAuthState(): Promise<AuthState>;
verifyAuth(): Promise<void>;
getUser(): TApiUser | null;
getParams(): Readonly<{
host: string;
token: string | undefined;
headers: Headers | undefined;
token_transport: "header" | "cookie" | "none";
verbose: boolean | undefined;
}>;
private buildApis;
}
export {};