qbo_auth: захват intuit_tid в логах и ошибках; документ требований Intuit (intuit_requirements.md)
All checks were successful
ci / test (push) Successful in 41s
ci / deploy (push) Successful in 2m49s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-19 20:03:33 +03:00
parent 92a85f8d9d
commit 98930c5b40
3 changed files with 44 additions and 4 deletions

View File

@@ -3,7 +3,10 @@ export const TOKEN_URL = "https://oauth.platform.intuit.com/oauth2/v1/tokens/bea
export const apiBase = (env: "sandbox" | "production"): string =>
env === "sandbox" ? "https://sandbox-quickbooks.api.intuit.com" : "https://quickbooks.api.intuit.com";
export type FetchFn = (url: string, init: { method: string; headers: Record<string, string>; body: string }) => Promise<{ ok: boolean; status: number; json(): Promise<unknown> }>;
export type FetchFn = (url: string, init: { method: string; headers: Record<string, string>; body: string }) => Promise<{ ok: boolean; status: number; json(): Promise<unknown>; headers?: { get(name: string): string | null } }>;
/** Идентификатор запроса Intuit (заголовок intuit_tid): нужен поддержке Intuit для разбора ошибок. */
export const intuitTid = (r: { headers?: { get(name: string): string | null } }): string | undefined => r.headers?.get("intuit_tid") ?? undefined;
export interface TokenSet {
accessToken: string;
@@ -40,7 +43,8 @@ async function tokenCall(fetchFn: FetchFn, creds: { clientId: string; clientSecr
if (!r.ok) {
const err = typeof j.error === "string" ? j.error : "unknown";
// в сообщение попадает только код ошибки Intuit, без тел с токенами
throw new TokenError(`Intuit token endpoint: ${r.status} ${err}`, r.status, err === "invalid_grant");
const tid = intuitTid(r);
throw new TokenError(`Intuit token endpoint: ${r.status} ${err}${tid ? ` intuit_tid=${tid}` : ""}`, r.status, err === "invalid_grant");
}
const at = j.access_token, rt = j.refresh_token;
if (typeof at !== "string" || typeof rt !== "string") throw new TokenError("в ответе Intuit нет токенов", r.status, false);

View File

@@ -1,5 +1,5 @@
import { SPECS, type EntitySpec, type Row } from "./mirror.js";
import type { FetchFn } from "./oauth.js";
import { intuitTid, type FetchFn } from "./oauth.js";
export interface Q {
query(sql: string, params?: unknown[]): Promise<{ rows: any[] }>;
@@ -55,10 +55,12 @@ export class QboSync {
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]}`);
throw new Error(`QuickBooks ${r.status} на ${path.split("?")[0]}${tid ? ` intuit_tid=${tid}` : ""}`);
}
throw new Error(`QuickBooks: не удалось выполнить ${path.split("?")[0]} за 5 попыток`);
}