qbo_auth: discovery-документ, клиент API с разбором Fault, отключение с отзывом токена; анкета и требования Intuit
All checks were successful
ci / test (push) Successful in 39s
ci / deploy (push) Successful in 2m41s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-19 20:18:06 +03:00
parent 98930c5b40
commit 46d0fbab6d
12 changed files with 482 additions and 53 deletions

View File

@@ -1,5 +1,6 @@
import { SPECS, type EntitySpec, type Row } from "./mirror.js";
import { intuitTid, type FetchFn } from "./oauth.js";
import { QboApi } from "./api.js";
import type { FetchFn } from "./oauth.js";
export interface Q {
query(sql: string, params?: unknown[]): Promise<{ rows: any[] }>;
@@ -11,7 +12,7 @@ export interface SyncDeps {
query: Q["query"];
/** Действующий access token (qbo_auth.getAccessToken). */
getToken(opts?: { force?: boolean }): Promise<{ accessToken: string; realmId: string; apiBase: string }>;
fetchFn: (url: string, init: { method: string; headers: Record<string, string> }) => ReturnType<FetchFn>;
fetchFn: (url: string, init: { method: string; headers: Record<string, string>; body?: string }) => ReturnType<FetchFn>;
environment: string;
sleep?: (ms: number) => Promise<void>;
log?: (msg: string, extra?: Record<string, unknown>) => void;
@@ -41,28 +42,18 @@ export interface SyncResult {
export class QboSync {
private running = false;
constructor(private readonly d: SyncDeps) {}
constructor(private readonly d: SyncDeps) {
this.api = new QboApi(d);
}
get isRunning(): boolean {
return this.running;
}
/** Вызов API QuickBooks: один повтор с принудительным обновлением токена при 401, повторы с паузой при 429/5xx. */
private async get(path: string): Promise<any> {
const sleep = this.d.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
let force = false;
for (let attempt = 0; attempt < 5; attempt++) {
const t = await this.d.getToken({ force });
const url = `${t.apiBase}/v3/company/${t.realmId}/${path}${path.includes("?") ? "&" : "?"}minorversion=75`;
const r = await this.d.fetchFn(url, { method: "GET", headers: { Authorization: "Bearer " + t.accessToken, Accept: "application/json" } });
const tid = intuitTid(r);
if (r.ok) return r.json();
this.d.log?.("ошибка API QuickBooks", { status: r.status, path: path.split("?")[0], intuit_tid: tid, attempt });
if (r.status === 401 && !force) { force = true; continue; }
if (r.status === 429 || r.status >= 500) { await sleep(2000 * (attempt + 1)); continue; }
throw new Error(`QuickBooks ${r.status} на ${path.split("?")[0]}${tid ? ` intuit_tid=${tid}` : ""}`);
}
throw new Error(`QuickBooks: не удалось выполнить ${path.split("?")[0]} за 5 попыток`);
private readonly api: QboApi;
/** Вызов API QuickBooks через общий клиент (повторы, разбор Fault, intuit_tid). */
private get(path: string): Promise<any> {
return this.api.get(path);
}
private async fetchAll(entity: string): Promise<any[]> {