baseflow-client
Version:
Official TypeScript/JavaScript client for BaseFlow - a powerful BaaS with OAuth authentication, RPC functions, database indexes, real-time features, and Supabase-compatible API
236 lines (235 loc) • 6.63 kB
TypeScript
/**
* Defines the options for creating a BaseFlow client.
*/
export interface BaseFlowClientOptions {
/**
* The URL of the BaseFlow server.
*/
url: string;
/**
* The API key for the BaseFlow server.
*/
apiKey: string;
/**
* Optional headers to send with every request.
*/
headers?: Record<string, string>;
/**
* Optional fetch implementation to use for requests.
*/
fetch?: typeof fetch;
}
/**
* Defines the structure of a BaseFlow API response.
*/
export interface BaseFlowResponse<T = any> {
/**
* The data returned by the API.
*/
data: T | null;
/**
* The error returned by the API, if any.
*/
error: BaseFlowError | null;
/**
* The HTTP status code of the response.
*/
status: number;
/**
* The HTTP status text of the response.
*/
statusText: string;
}
/**
* Defines the structure of a BaseFlow API error.
*/
export interface BaseFlowError {
/**
* The error message.
*/
message: string;
/**
* The error code.
*/
code?: string;
/**
* Additional details about the error.
*/
details?: any;
}
/**
* Defines the options for a query.
*/
export interface QueryOptions {
/**
* The columns to select.
*/
select?: string;
/**
* The filter to apply.
*/
where?: string;
/**
* The order to sort the results by.
*/
order?: string;
/**
* The maximum number of results to return.
*/
limit?: number;
/**
* The number of results to skip.
*/
offset?: number;
}
/**
* Defines the options for an insert operation.
*/
export interface InsertOptions {
/**
* The columns to return after the insert.
*/
returning?: string;
/**
* The on-conflict clause.
*/
onConflict?: string;
}
/**
* Defines the options for an update operation.
*/
export interface UpdateOptions {
/**
* The columns to return after the update.
*/
returning?: string;
}
/**
* Defines the options for a delete operation.
*/
export interface DeleteOptions {
/**
* The columns to return after the delete.
*/
returning?: string;
}
/**
* Defines the interface for a query builder.
*/
export interface QueryBuilder<T = any> {
/**
* Starts a select query.
* @param columns The columns to select.
*/
select(columns?: string): PostgrestQueryBuilder<T>;
/**
* Starts an insert query.
* @param data The data to insert.
*/
insert(data: Partial<T> | Partial<T>[]): PostgrestBuilder<T>;
/**
* Starts an update query.
* @param data The data to update.
*/
update(data: Partial<T>): PostgrestFilterBuilder<T>;
/**
* Starts a delete query.
*/
delete(): PostgrestFilterBuilder<T>;
}
/**
* Defines the base interface for a Postgrest builder, making it a "thenable".
*/
export interface PostgrestBuilder<T = any> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult1 = BaseFlowResponse<T>, TResult2 = never>(onfulfilled?: ((value: BaseFlowResponse<T>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
}
/**
* Defines the interface for a Postgrest filter builder.
*/
export interface PostgrestFilterBuilder<T = any> extends PostgrestBuilder<T> {
eq(column: keyof T, value: any): PostgrestFilterBuilder<T>;
neq(column: keyof T, value: any): PostgrestFilterBuilder<T>;
gt(column: keyof T, value: any): PostgrestFilterBuilder<T>;
gte(column: keyof T, value: any): PostgrestFilterBuilder<T>;
lt(column: keyof T, value: any): PostgrestFilterBuilder<T>;
lte(column: keyof T, value: any): PostgrestFilterBuilder<T>;
like(column: keyof T, pattern: string): PostgrestFilterBuilder<T>;
ilike(column: keyof T, pattern: string): PostgrestFilterBuilder<T>;
is(column: keyof T, value: null | boolean): PostgrestFilterBuilder<T>;
in(column: keyof T, values: any[]): PostgrestFilterBuilder<T>;
contains(column: keyof T, value: any): PostgrestFilterBuilder<T>;
containedBy(column: keyof T, value: any): PostgrestFilterBuilder<T>;
rangeGt(column: keyof T, value: any): PostgrestFilterBuilder<T>;
rangeGte(column: keyof T, value: any): PostgrestFilterBuilder<T>;
rangeLt(column: keyof T, value: any): PostgrestFilterBuilder<T>;
rangeLte(column: keyof T, value: any): PostgrestFilterBuilder<T>;
rangeAdjacent(column: keyof T, value: any): PostgrestFilterBuilder<T>;
overlaps(column: keyof T, value: any): PostgrestFilterBuilder<T>;
textSearch(column: keyof T, query: string): PostgrestFilterBuilder<T>;
match(query: Record<keyof T, any>): PostgrestFilterBuilder<T>;
not(column: keyof T, operator: string, value: any): PostgrestFilterBuilder<T>;
or(filters: string): PostgrestFilterBuilder<T>;
filter(column: keyof T, operator: string, value: any): PostgrestFilterBuilder<T>;
}
/**
* Defines the interface for a Postgrest query builder.
*/
export interface PostgrestQueryBuilder<T = any> extends PostgrestFilterBuilder<T> {
order(column: keyof T, options?: {
ascending?: boolean;
nullsFirst?: boolean;
}): PostgrestQueryBuilder<T>;
limit(count: number): PostgrestQueryBuilder<T>;
range(from: number, to: number): PostgrestQueryBuilder<T>;
single(): PostgrestFilterBuilder<T>;
maybeSingle(): PostgrestFilterBuilder<T>;
}
/**
* Auth event types
*/
export type AuthEvent = {
type: 'SIGNED_IN';
user: any;
} | {
type: 'SIGNED_OUT';
} | {
type: 'USER_UPDATED';
user: any;
} | {
type: 'PASSWORD_RECOVERY';
user: any;
} | {
type: 'TOKEN_REFRESHED';
user: any;
};
/**
* Auth session interface
*/
export interface AuthSession {
access_token: string;
refresh_token?: string;
user: any;
expires_at?: number;
}
/**
* Auth user interface
*/
export interface AuthUser {
id: string;
email: string;
name?: string;
avatar_url?: string;
created_at: string;
updated_at: string;
last_sign_in?: string;
email_confirmed?: boolean;
role?: string;
user_metadata?: Record<string, any>;
app_metadata?: Record<string, any>;
}