/** * `@snoutdata/client`: one project's data, identity, files, live topics, functions and AI, from * its URL and a key. * * import { snout } from '@snoutdata/client' * const db = snout('https://.api.snoutdata.com', '') * * Every product lives under that one URL at a fixed prefix (`/v1/data`, `/v1/identity`, * `/v1/files`, `/v1/live`, `/v1/fn`, `/push/v1`, `/ai/v1`). Every request carries the key as `Snout-Key`, * and as `Authorization` the signed-in person's access token, or the key itself when nobody is * signed in. Row-level security reads that token, which is why the app key is safe in a browser: * it grants what your policies grant the app role, and nothing else. * * A server not yet updated to the new names takes `{ vocabulary: 'old' }`, which speaks the * layout 0.3.x spoke (`/rest/v1`, `apikey`, ...) with the same calls. */ import { AiClient } from './ai.js'; import { AuthClient, type AuthOptions } from './auth.js'; import { FunctionsClient } from './functions.js'; import { type Fetch, type Vocabulary } from './http.js'; import { RealtimeClient, type ChannelOptions, type RealtimeChannel, type RealtimeOptions, type SendStatus } from './realtime.js'; import { PushClient } from './push.js'; import { TableBuilder, type CountMethod, type QueryBuilder, type Row } from './rest.js'; import { StorageClient } from './storage.js'; export { keyHeader, type Vocabulary } from './http.js'; export * from './ai.js'; export * from './auth.js'; export * from './cookies.js'; export * from './functions.js'; export * from './push.js'; export * from './realtime.js'; export * from './rest.js'; export * from './storage.js'; /** * The shape `snoutdata gen types typescript` writes. With it, `table('todos')` knows the * table's columns; without it, rows are plain records. */ export interface Database { [schema: string]: { Tables: Record; Views?: Record; Functions?: Record; }; } type Schema = DB extends Database ? DB[S] : never; type Relations = Schema extends never ? Record : Schema['Tables'] & NonNullable['Views']>; type RelationName = Extract, string>; type RowOf = N extends keyof Relations ? Relations[N] extends { Row: infer R; } ? R : Row : Row; export interface ClientOptions { db?: { schema?: S; }; /** Options for `identity`. */ auth?: AuthOptions; /** Options for `live`. */ realtime?: RealtimeOptions; global?: { headers?: Record; fetch?: Fetch; }; /** * `new` (the default) speaks our own layout: `/v1/data` and its neighbours, the `Snout-Key` * header, the identity server's new routes and fields, and the `live:` wire. `old` speaks * the layout 0.3.x spoke, for a server that has not been updated to the new names yet. The * calls, their options and their results are the same either way. */ vocabulary?: Vocabulary; } export declare class SnoutClient { readonly url: string; /** Which names this client speaks on the wire. */ readonly vocabulary: Vocabulary; readonly dataUrl: string; readonly identityUrl: string; readonly filesUrl: string; readonly fnUrl: string; /** The `wss://` base of `live`. */ readonly liveUrl: string; readonly pushUrl: string; readonly aiUrl: string; /** People, sessions and the token every other product sends. */ readonly identity: AuthClient; /** Files in spaces. */ readonly files: StorageClient; /** Snout Functions. */ readonly fn: FunctionsClient; /** Live topics: signals, the roster and table changes. */ readonly live: RealtimeClient; /** Snout Push: devices, topics, sends and receipts. */ readonly push: PushClient; /** The project's AI gateway: its models, chat and embeddings. */ readonly ai: AiClient; /** The data API's base, for code that checks where it points. */ readonly rest: { url: string; }; /** @deprecated Since 0.4.0: `dataUrl`. Removed in 0.5.0. */ readonly restUrl: string; /** @deprecated Since 0.4.0: `identityUrl`. Removed in 0.5.0. */ readonly authUrl: string; /** @deprecated Since 0.4.0: `filesUrl`. Removed in 0.5.0. */ readonly storageUrl: string; /** @deprecated Since 0.4.0: `fnUrl`. Removed in 0.5.0. */ readonly functionsUrl: string; /** @deprecated Since 0.4.0: `liveUrl`. Removed in 0.5.0. */ readonly realtimeUrl: string; /** @deprecated Since 0.4.0: `identity`, the same object. Removed in 0.5.0. */ readonly auth: AuthClient; /** @deprecated Since 0.4.0: `files`, the same object. Removed in 0.5.0. */ readonly storage: StorageClient; /** @deprecated Since 0.4.0: `fn`, the same object. Removed in 0.5.0. */ readonly functions: FunctionsClient; /** @deprecated Since 0.4.0: `live`, the same object. Removed in 0.5.0. */ readonly realtime: RealtimeClient; private readonly transport; private readonly schemaName; /** @deprecated Since 0.4.0: use `table()`, the same function. Removed in 0.5.0. */ from: SnoutClient['table']; /** @deprecated Since 0.4.0: use `procedure()`, the same function. Removed in 0.5.0. */ rpc: SnoutClient['procedure']; /** @deprecated Since 0.4.0: use `live.topic()`, the same function. Removed in 0.5.0. */ channel: (name: string, options?: ChannelOptions) => RealtimeChannel; constructor(url: string, appKey: string, options?: ClientOptions); /** A table or view. Awaiting the builder it returns sends the request. */ table>(relation: N): TableBuilder>; /** The same client, pointed at another schema (which must be exposed to the data API). */ schema(name: T): SnoutClient; /** Calls a Postgres function. Filters and modifiers apply to what it returns: `any` unless typed. */ procedure(fn: string, args?: Record, options?: { head?: boolean; get?: boolean; count?: CountMethod; }): QueryBuilder; getChannels(): RealtimeChannel[]; removeChannel(channel: RealtimeChannel): Promise; removeAllChannels(): Promise; } /** * A client for one project: its URL, and a key (the app key in a browser, the server key on a * server, which must never reach a browser). */ export declare function snout(url: string, appKey: string, options?: ClientOptions): SnoutClient; /** @deprecated Since 0.4.0: `snout()`, the same function. Removed in 0.5.0. */ export declare const createClient: typeof snout;