@rowbind/sdk 0.0.0-stage → 0.1.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +537 -2
- package/dist/generated/catalog.d.ts +22 -0
- package/dist/generated/catalog.js +7 -0
- package/dist/generated/http.d.ts +1740 -0
- package/dist/generated/http.js +843 -0
- package/dist/generated/raw.d.ts +39 -0
- package/dist/generated/raw.js +14 -0
- package/dist/generated/validators.d.ts +1411 -0
- package/dist/generated/validators.js +993 -0
- package/dist/http-error.d.ts +5 -0
- package/dist/http-error.js +2 -0
- package/dist/http-request.d.ts +17 -0
- package/dist/http-request.js +73 -0
- package/dist/index.d.ts +622 -0
- package/dist/index.js +256 -0
- package/dist/inputs.d.ts +145 -0
- package/dist/inputs.js +46 -0
- package/dist/ledger.d.ts +98 -0
- package/dist/ledger.js +218 -0
- package/dist/raw-invoice-query.d.ts +2 -0
- package/dist/raw-invoice-query.js +10 -0
- package/dist/transport.d.ts +344 -0
- package/dist/transport.js +276 -0
- package/dist/wire.d.ts +16 -0
- package/dist/wire.js +8 -0
- package/examples/catalog-invoice.mts +30 -0
- package/examples/connection-health.mts +21 -0
- package/examples/department-quickbooks/example.mts +12 -0
- package/examples/department-quickbooks/tsconfig.json +4 -0
- package/examples/facturar.mts +34 -0
- package/examples/first-customer.mts +72 -0
- package/examples/invoice-balances.mts +60 -0
- package/examples/invoice-settlement.mts +86 -0
- package/examples/invoice-sync.mts +46 -0
- package/examples/invoice-traces.mts +65 -0
- package/examples/invoice.mts +27 -0
- package/examples/mixed-providers/example.mts +12 -0
- package/examples/mixed-providers/tsconfig.json +4 -0
- package/examples/native-query.mts +28 -0
- package/examples/native-quickstart.mts +46 -0
- package/examples/observed-payments.mts +29 -0
- package/examples/quickstart.mts +11 -0
- package/examples/reimbursement-batch.mts +36 -0
- package/examples/resume-invoice.mts +52 -0
- package/examples/tsconfig.editor.json +11 -0
- package/examples/webhook-receiver.mts +142 -0
- package/package.json +78 -4
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type ResponseMetadata = {
|
|
2
|
+
requestId?: string;
|
|
3
|
+
};
|
|
4
|
+
export type RowbindFetch = (url: URL, init: RequestInit) => Promise<Response>;
|
|
5
|
+
type PilotRequest = RequestInit & {
|
|
6
|
+
rowbind?: {
|
|
7
|
+
origin: URL;
|
|
8
|
+
key: string;
|
|
9
|
+
deadline: number;
|
|
10
|
+
fetch?: RowbindFetch | undefined;
|
|
11
|
+
};
|
|
12
|
+
preserveServiceErrors?: readonly string[];
|
|
13
|
+
responseMetadata?: ResponseMetadata;
|
|
14
|
+
};
|
|
15
|
+
/** Transport used by the generated operations. Retry idempotent reads, replacements and keyed admissions. */
|
|
16
|
+
export declare function sendToRowbind<T>(path: string, options?: PilotRequest): Promise<T>;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
2
|
+
import { httpError } from "./http-error.js";
|
|
3
|
+
import { LedgerError } from "./ledger.js";
|
|
4
|
+
function retryAfterMs(header) {
|
|
5
|
+
if (!header)
|
|
6
|
+
return 0;
|
|
7
|
+
if (/^\d+$/.test(header))
|
|
8
|
+
return Number(header) * 1000;
|
|
9
|
+
const date = Date.parse(header);
|
|
10
|
+
return Number.isFinite(date) ? Math.max(0, date - Date.now()) : 0;
|
|
11
|
+
}
|
|
12
|
+
/** Transport used by the generated operations. Retry idempotent reads, replacements and keyed admissions. */
|
|
13
|
+
export async function sendToRowbind(path, options) {
|
|
14
|
+
const { rowbind, preserveServiceErrors, responseMetadata, ...request } = options ?? {};
|
|
15
|
+
if (!rowbind)
|
|
16
|
+
throw new LedgerError("invalid_configuration", "Missing Rowbind project credentials.");
|
|
17
|
+
const url = new URL(path, rowbind.origin);
|
|
18
|
+
if (url.origin !== rowbind.origin.origin)
|
|
19
|
+
throw new LedgerError("invalid_configuration", "The request must stay on the Rowbind origin.");
|
|
20
|
+
const headers = new Headers(request.headers);
|
|
21
|
+
const execute = rowbind.fetch ?? fetch;
|
|
22
|
+
headers.set("Authorization", `Bearer ${rowbind.key}`);
|
|
23
|
+
const idempotent = request.method === "GET" || request.method === "PUT" || (request.method === "POST" && headers.has("Idempotency-Key"));
|
|
24
|
+
const attempts = idempotent ? 3 : 1;
|
|
25
|
+
let serviceError;
|
|
26
|
+
let requestId;
|
|
27
|
+
for (let attempt = 0; attempt < attempts; attempt++) {
|
|
28
|
+
const remaining = rowbind.deadline - Date.now();
|
|
29
|
+
if (remaining <= 0)
|
|
30
|
+
break;
|
|
31
|
+
serviceError = undefined;
|
|
32
|
+
requestId = undefined;
|
|
33
|
+
let retryAfter = 0;
|
|
34
|
+
try {
|
|
35
|
+
const response = await execute(url, {
|
|
36
|
+
...request, headers, redirect: "error", signal: AbortSignal.timeout(Math.min(remaining, 8_000)),
|
|
37
|
+
});
|
|
38
|
+
requestId = response.headers.get("X-Request-Id") ?? undefined;
|
|
39
|
+
// Orval supplies T from the operation. PilotTransport validates this JSON
|
|
40
|
+
// against that operation's generated Zod schema before exposing it.
|
|
41
|
+
if (response.ok) {
|
|
42
|
+
const result = await response.json();
|
|
43
|
+
if (responseMetadata && requestId !== undefined)
|
|
44
|
+
responseMetadata.requestId = requestId;
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
if (![408, 429, 500, 502, 503, 504].includes(response.status)) {
|
|
48
|
+
const error = httpError.safeParse(await response.json().catch(() => null));
|
|
49
|
+
throw new LedgerError(error.success ? error.data.code : "http_error", error.success ? error.data.message : `Rowbind HTTP ${response.status}.`, requestId);
|
|
50
|
+
}
|
|
51
|
+
retryAfter = retryAfterMs(response.headers.get("Retry-After"));
|
|
52
|
+
if (response.status === 503) {
|
|
53
|
+
const error = httpError.safeParse(await response.json().catch(() => null));
|
|
54
|
+
if (error.success && (error.data.code === "core_storage_unavailable" || preserveServiceErrors?.includes(error.data.code))) {
|
|
55
|
+
serviceError = new LedgerError(error.data.code, error.data.message, requestId);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else
|
|
59
|
+
await response.body?.cancel();
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (error instanceof LedgerError)
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
const delay = Math.max(retryAfter, Math.floor(Math.random() * 200 * 2 ** attempt));
|
|
66
|
+
if (attempt + 1 === attempts || Date.now() + delay >= rowbind.deadline)
|
|
67
|
+
break;
|
|
68
|
+
await sleep(delay);
|
|
69
|
+
}
|
|
70
|
+
if (serviceError)
|
|
71
|
+
throw serviceError;
|
|
72
|
+
throw new LedgerError("transport_unavailable", "Rowbind did not return a result within the HTTP retry budget. Keep the same operation or idempotency key.", requestId);
|
|
73
|
+
}
|