export type ActionStatus = 'completed' | 'queued' | 'awaiting_approval' | 'denied' | 'unavailable' | 'uncertain' | 'cancelled'; export type ActionResult = { id?: string; operation?: string; status: ActionStatus; data?: unknown; approvalUrl?: string; reason?: string }; type Operation = { name: string; readOnly: boolean; available: boolean }; type Capabilities = { operations: Operation[]; readiness: { enabled: boolean } }; type RunOptions = { idempotencyKey?: string; scheduleAt?: string }; const DEFAULT_ORIGIN = "https://yourpairmade.com"; export class PairmadeHttpError extends Error { constructor(readonly statusCode: number, readonly body: unknown) { super('Pairmade request failed with HTTP ' + statusCode); } } export class PairmadeClient { private readonly origin: string; private readonly token: string; constructor(options: { origin?: string; token?: string } = {}) { this.origin = (options.origin ?? process.env.PAIRMADE_API_ORIGIN ?? DEFAULT_ORIGIN).replace(/\/+$/, ''); const url = new URL(this.origin); if (url.protocol !== 'https:' || url.username || url.password || url.pathname !== '/' || url.search || url.hash) throw new Error('Use an HTTPS Pairmade origin.'); this.token = options.token ?? process.env.YOUR_TOKEN ?? ''; if (!this.token) throw new Error('Set YOUR_TOKEN in the server environment.'); } private async request(path: string, init: RequestInit = {}): Promise { const headers = new Headers(init.headers); headers.set('Authorization', 'Bearer ' + this.token); const response = await fetch(this.origin + path, { ...init, headers, redirect: 'error' }); const raw = await response.text(); let body: unknown = null; try { body = raw ? JSON.parse(raw) : null; } catch { body = raw; } if (!response.ok) throw new PairmadeHttpError(response.status, body); return body as T; } capabilities(): Promise { return this.request('/api/v1/capabilities'); } async run(name: string, input: Record, options: RunOptions = {}): Promise { const current = await this.capabilities(); const operation = current.operations.find((item) => item.name === name && item.available); if (!operation) throw new Error('Operation is not available to this connection: ' + name); const headers = new Headers({ 'Content-Type': 'application/json' }); if (!operation.readOnly) { if (!options.idempotencyKey) throw new Error('Mutations require idempotencyKey; reuse the same key and input when retrying.'); headers.set('Idempotency-Key', options.idempotencyKey); } const body = { input, ...(options.scheduleAt ? { scheduleAt: options.scheduleAt } : {}) }; return this.request('/api/v1/operations/' + encodeURIComponent(name), { method: 'POST', headers, body: JSON.stringify(body) }); } status(id: string): Promise { return this.request('/api/v1/actions/' + encodeURIComponent(id)); } cancel(id: string): Promise { return this.request('/api/v1/actions/' + encodeURIComponent(id) + '/cancel', { method: 'POST' }); } } // Returned ActionResult.status is preserved; inspect queued, awaiting_approval, and uncertain states explicitly.