File
Blob: ui/client/api.ts
| 1 | import { queryOptions } from '@tanstack/svelte-query'; |
| 2 | import { RequestError, requestJSON } from '../shared/http'; |
| 3 | |
| 4 | export { RequestError as APIError } from '../shared/http'; |
| 5 | |
| 6 | export interface TunnelSyncResult { |
| 7 | hostname: string; |
| 8 | target: string; |
| 9 | published: boolean; |
| 10 | publishedEndpoints: number; |
| 11 | error?: string; |
| 12 | } |
| 13 | |
| 14 | export interface SyncResult { |
| 15 | applied: boolean; |
| 16 | saved: boolean; |
| 17 | error?: string; |
| 18 | attemptedAt?: string; |
| 19 | tunnels: TunnelSyncResult[]; |
| 20 | } |
| 21 | |
| 22 | export interface ClientStatus { |
| 23 | apex: string; |
| 24 | connectedNodes: { |
| 25 | id?: number; |
| 26 | address?: string; |
| 27 | unknown?: boolean; |
| 28 | rendezvous?: boolean; |
| 29 | }[]; |
| 30 | synchronization: SyncResult; |
| 31 | pending: boolean; |
| 32 | retryAt?: string; |
| 33 | } |
| 34 | |
| 35 | export interface RegisteredTunnel { |
| 36 | hostname: string; |
| 37 | target: string; |
| 38 | headerHost: string; |
| 39 | } |
| 40 | |
| 41 | export interface DomainRecord { |
| 42 | message: string; |
| 43 | record: string; |
| 44 | type: string; |
| 45 | content: string; |
| 46 | } |
| 47 | |
| 48 | export function errorMessage(error: unknown): string { |
| 49 | const message = error instanceof Error ? error.message : "Request failed."; |
| 50 | return error instanceof RequestError && error.code === 'outcome_unknown' && typeof error.data?.applied !== 'boolean' |
| 51 | ? `${message} Result unknown; refresh before retrying.` : message; |
| 52 | } |
| 53 | |
| 54 | export function requestAPI<T = void>(path: string, options: RequestInit = {}): Promise<T> { |
| 55 | return requestJSON<T>(path, { timeout: 60_000, ...options }); |
| 56 | } |
| 57 | |
| 58 | export const hostnameQueryOptions = queryOptions({ |
| 59 | queryKey: ['client', 'hostnames'], |
| 60 | queryFn: ({ signal }) => requestAPI<RegisteredTunnel[]>('/api/ls', { signal }), |
| 61 | }); |
| 62 | |
| 63 | export const statusQueryOptions = queryOptions({ |
| 64 | queryKey: ['client', 'status'], |
| 65 | queryFn: ({ signal }) => requestAPI<ClientStatus>('/api/status', { signal }), |
| 66 | }); |
| 67 | |
| 68 | export interface DomainToken { |
| 69 | id: string; |
| 70 | hostname: string; |
| 71 | expiresAt?: string; |
| 72 | incomplete: boolean; |
| 73 | } |
| 74 | |
| 75 | export interface MintedToken { |
| 76 | grant: DomainToken; |
| 77 | token: string; |
| 78 | } |
| 79 | |
| 80 | export interface RevokeOutcome { |
| 81 | revoked: boolean; |
| 82 | indexError?: string; |
| 83 | } |