import { invoke } from '@tauri-apps/api/core'; import type { CollectionView, CookieInfo, Environment, FolderMeta, HistoryEntry, Manifest, Preview, Request, SendResult, VarInfo } from './types'; export interface ScopeArgs { col: string | null; request_id: string | null; request: Request; env: string | null; } interface EditResult { id: string; collection: CollectionView; } export const api = { collectionOpen: (path: string) => invoke('collection_open', { path }), collectionCreate: (path: string, name: string) => invoke('collection_create', { path, name }), collectionGet: (id: string) => invoke('collection_get', { id }), collectionClose: (id: string) => invoke('collection_close', { id }), recent: () => invoke('recent_collections'), recentForget: (path: string) => invoke('recent_forget', { path }), requestGet: (col: string, id: string) => invoke('request_get', { col, id }), requestSave: (col: string, id: string, request: Request) => invoke('request_save', { col, id, request }), requestCreate: (col: string, folder: string, request: Request) => invoke('request_create', { col, folder, request }), folderCreate: (col: string, parent: string, name: string) => invoke('folder_create', { col, parent, name }), rename: (col: string, id: string, name: string) => invoke('node_rename', { col, id, name }), duplicate: (col: string, id: string) => invoke('node_duplicate', { col, id }), remove: (col: string, id: string) => invoke('node_delete', { col, id }), move: (col: string, id: string, folder: string, index: number | null) => invoke('node_move', { col, id, folder, index }), folderGet: (col: string, id: string) => invoke('folder_get', { col, id }), folderSave: (col: string, id: string, meta: FolderMeta) => invoke('folder_save', { col, id, meta }), manifestGet: (col: string) => invoke('manifest_get', { col }), manifestSave: (col: string, manifest: Manifest) => invoke('manifest_save', { col, manifest }), envGet: (col: string, id: string) => invoke('env_get', { col, id }), envSave: (col: string, id: string, env: Environment) => invoke('env_save', { col, id, env }), envDelete: (col: string, id: string) => invoke('env_delete', { col, id }), envSecrets: (col: string, env: string) => invoke<{ name: string; source: 'envvar' | 'keyring' | 'file' | null; value: string | null }[]>( 'env_secrets', { col, env } ), secretSet: ( col: string, env: string | null, name: string, value: string | null, target: 'keyring' | 'file' ) => invoke('secret_set', { col, env, name, value, target }), varsList: (args: ScopeArgs) => invoke<{ vars: VarInfo[]; dynamic: [string, string][] }>('vars_list', { args }), runtimeList: (col: string | null) => invoke>('runtime_list', { col }), runtimeSet: (col: string | null, name: string, value: string | null, persist: boolean) => invoke('runtime_set', { col, name, value, persist }), globalsGet: () => invoke>('globals_get'), globalsSave: (vars: Record) => invoke('globals_save', { vars }), send: (args: ScopeArgs & { tab: string; history_body_limit: number }) => invoke('request_send', { args }), cancel: (tab: string) => invoke('request_cancel', { tab }), preview: (args: ScopeArgs) => invoke('request_preview', { args }), responseBody: (id: string) => invoke('response_body', { id }), responseSave: (id: string, path: string) => invoke('response_save', { id, path }), cookies: (col: string) => invoke('cookies_list', { col }), cookieDelete: (col: string, c?: { domain: string; path: string; name: string }) => invoke('cookie_delete', { col, domain: c?.domain, path: c?.path, name: c?.name }), history: (query: string, limit?: number) => invoke('history_list', { query, limit }), historyGet: (id: number) => invoke<{ request: Request; response: SendResult['output']['response']; body_id: string | null }>( 'history_get', { id } ), historyDelete: (id: number | null) => invoke('history_delete', { id }), settingGet: (key: string) => invoke('setting_get', { key }), settingSet: (key: string, value: string) => invoke('setting_set', { key, value }), pathParams: (url: string) => invoke('path_params', { url }), version: () => invoke('app_version') }; export function errorText(e: unknown): string { if (typeof e === 'string') return e; if (e instanceof Error) return e.message; return JSON.stringify(e); } export interface TokenSet { access_token: string; refresh_token?: string | null; id_token?: string | null; token_type?: string | null; scope?: string | null; expires_at?: number | null; obtained_at: number; skew?: number | null; } export interface FlowRecord { id: string; at: string; kind: string; ok: boolean; steps: import('./types').Step[]; warnings: string[]; } export interface OauthStatus { key: string; redirect_uri: string; token: TokenSet | null; flows: FlowRecord[]; logout_url: string | null; } export type OauthArgs = ScopeArgs & { cfg: import('./types').AuthConfig }; export const oauthApi = { status: (args: OauthArgs) => invoke('oauth_status', { args }), run: (args: OauthArgs, what: 'login' | 'test' | 'logout' | 'refresh' | 'userinfo' | 'introspect' | 'revoke' | 'discovery') => invoke<{ steps: import('./types').Step[]; warnings: string[]; ok: boolean }>('oauth_run', { args, what }), callback: (url: string) => invoke('oauth_callback', { url }), cancel: (key?: string) => invoke('oauth_cancel', { key: key ?? null }) };