Files
knock/src/lib/api.ts
T
JoshiCodes 5fc816a380 OAuth flows can be cancelled; discovery no longer waits for a pending login
- Only login, logout, refresh and revoke queue behind each other;
  discovery, test, userinfo and introspection run at once
- Cancel in the OAuth panel (and Cancel on the request) stops the flow,
  frees the callback port and the login lock
- Undefined variables in issuer, endpoints or client id are reported
  before any request is sent
- Keyring commands run off the UI thread, so a KWallet prompt cannot
  freeze the window
- One click on a collection opens its settings; the chevron folds it
- Until an environment is picked, local (or the first one) is active;
  the environment editor says when the shown one is not active
2026-11-27 18:24:04 +01:00

147 lines
5.8 KiB
TypeScript

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<CollectionView>('collection_open', { path }),
collectionCreate: (path: string, name: string) =>
invoke<CollectionView>('collection_create', { path, name }),
collectionGet: (id: string) => invoke<CollectionView>('collection_get', { id }),
collectionClose: (id: string) => invoke<void>('collection_close', { id }),
recent: () => invoke<string[]>('recent_collections'),
recentForget: (path: string) => invoke<void>('recent_forget', { path }),
requestGet: (col: string, id: string) => invoke<Request>('request_get', { col, id }),
requestSave: (col: string, id: string, request: Request) =>
invoke<CollectionView>('request_save', { col, id, request }),
requestCreate: (col: string, folder: string, request: Request) =>
invoke<EditResult>('request_create', { col, folder, request }),
folderCreate: (col: string, parent: string, name: string) =>
invoke<EditResult>('folder_create', { col, parent, name }),
rename: (col: string, id: string, name: string) =>
invoke<EditResult>('node_rename', { col, id, name }),
duplicate: (col: string, id: string) => invoke<EditResult>('node_duplicate', { col, id }),
remove: (col: string, id: string) => invoke<CollectionView>('node_delete', { col, id }),
move: (col: string, id: string, folder: string, index: number | null) =>
invoke<EditResult>('node_move', { col, id, folder, index }),
folderGet: (col: string, id: string) => invoke<FolderMeta>('folder_get', { col, id }),
folderSave: (col: string, id: string, meta: FolderMeta) =>
invoke<CollectionView>('folder_save', { col, id, meta }),
manifestGet: (col: string) => invoke<Manifest>('manifest_get', { col }),
manifestSave: (col: string, manifest: Manifest) =>
invoke<CollectionView>('manifest_save', { col, manifest }),
envGet: (col: string, id: string) => invoke<Environment>('env_get', { col, id }),
envSave: (col: string, id: string, env: Environment) =>
invoke<CollectionView>('env_save', { col, id, env }),
envDelete: (col: string, id: string) => invoke<CollectionView>('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<void>('secret_set', { col, env, name, value, target }),
varsList: (args: ScopeArgs) =>
invoke<{ vars: VarInfo[]; dynamic: [string, string][] }>('vars_list', { args }),
runtimeList: (col: string | null) => invoke<Record<string, string>>('runtime_list', { col }),
runtimeSet: (col: string | null, name: string, value: string | null, persist: boolean) =>
invoke<void>('runtime_set', { col, name, value, persist }),
globalsGet: () => invoke<Record<string, string>>('globals_get'),
globalsSave: (vars: Record<string, string>) => invoke<void>('globals_save', { vars }),
send: (args: ScopeArgs & { tab: string; history_body_limit: number }) =>
invoke<SendResult>('request_send', { args }),
cancel: (tab: string) => invoke<void>('request_cancel', { tab }),
preview: (args: ScopeArgs) => invoke<Preview>('request_preview', { args }),
responseBody: (id: string) => invoke<ArrayBuffer>('response_body', { id }),
responseSave: (id: string, path: string) => invoke<void>('response_save', { id, path }),
cookies: (col: string) => invoke<CookieInfo[]>('cookies_list', { col }),
cookieDelete: (col: string, c?: { domain: string; path: string; name: string }) =>
invoke<void>('cookie_delete', { col, domain: c?.domain, path: c?.path, name: c?.name }),
history: (query: string, limit?: number) =>
invoke<HistoryEntry[]>('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<void>('history_delete', { id }),
settingGet: (key: string) => invoke<string | null>('setting_get', { key }),
settingSet: (key: string, value: string) => invoke<void>('setting_set', { key, value }),
pathParams: (url: string) => invoke<string[]>('path_params', { url }),
version: () => invoke<string>('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<OauthStatus>('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<boolean>('oauth_callback', { url }),
cancel: (key?: string) => invoke<number>('oauth_cancel', { key: key ?? null })
};