@company-six/qbosync 0.1.0 → 0.3.0

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/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  /** The QBO Sync SDK for an app's server. See README.md. */
2
2
  export * from "./contract/index.js";
3
- export { QbosyncClient } from "./client.js";
3
+ export { QbosyncClient, SIGN_IN_PAGE_PATH, } from "./client.js";
4
4
  export { isNewerReport, verifySignature, } from "./statusReports.js";
@@ -44,12 +44,24 @@ export interface InvalidReport {
44
44
  document_type?: string;
45
45
  reasons: string[];
46
46
  }
47
+ /** One report of a call, in its place: one that reads, or why it does not. */
48
+ export type StatusReportEntry = {
49
+ index: number;
50
+ report: SyncStatusReport;
51
+ } | {
52
+ index: number;
53
+ invalid: InvalidReport;
54
+ };
47
55
  export type CheckedStatusReports =
48
- /** Genuine. Apply `reports`, answer each with a `StatusReportResult`, and answer `invalid` ones "error". */
56
+ /**
57
+ * Genuine. Apply `reports`, answer each with a `StatusReportResult`, and answer `invalid` ones "error".
58
+ * `entries` holds the same, every report in the order it came, for an app that answers them in order.
59
+ */
49
60
  {
50
61
  ok: true;
51
62
  reports: SyncStatusReport[];
52
63
  invalid: InvalidReport[];
64
+ entries: StatusReportEntry[];
53
65
  }
54
66
  /** 400 not a report call, 401 not from QBO Sync, 503 the keys could not be fetched (QBO Sync sends it again). */
55
67
  | {
@@ -103,21 +103,25 @@ export async function checkStatusReports(keys, rawBody, signature) {
103
103
  return { ok: false, status: 400, error: `At most ${MAX_STATUS_REPORTS} reports a request; this one has ${list.length}` };
104
104
  const reports = [];
105
105
  const invalid = [];
106
+ const entries = [];
106
107
  list.forEach((entry, index) => {
107
108
  const parsed = syncStatusReportSchema.safeParse(entry);
108
109
  if (parsed.success) {
109
110
  reports.push(parsed.data);
111
+ entries.push({ index, report: parsed.data });
110
112
  return;
111
113
  }
112
114
  const e = (entry ?? {});
113
- invalid.push({
115
+ const bad = {
114
116
  index,
115
117
  ...(typeof e.source_ref === "string" ? { source_ref: e.source_ref } : {}),
116
118
  ...(typeof e.document_type === "string" ? { document_type: e.document_type } : {}),
117
119
  reasons: parsed.error.issues.map(formatIssue),
118
- });
120
+ };
121
+ invalid.push(bad);
122
+ entries.push({ index, invalid: bad });
119
123
  });
120
- return { ok: true, reports, invalid };
124
+ return { ok: true, reports, invalid, entries };
121
125
  }
122
126
  /** Whether a report should be applied: only one newer than the last applied, so a delayed report never hides a newer one. */
123
127
  export function isNewerReport(report, lastAppliedSequence) {
@@ -0,0 +1,38 @@
1
+ import crypto from "node:crypto";
2
+ import type { PublicSigningKey } from "./statusReports.ts";
3
+ /**
4
+ * For an app's TESTS only: calls to its status report route as QBO Sync would make them.
5
+ *
6
+ * Only QBO Sync signs real reports, with a private key only it holds. These helpers make a throwaway key
7
+ * pair, sign a body with it the way QBO Sync does, and answer the public half where the SDK looks for it,
8
+ * so a test can drive `checkStatusReports` (or the app's route) end to end:
9
+ *
10
+ * const key = createTestSigningKey();
11
+ * const client = new QbosyncClient({ baseUrl, appName, ...stores, fetch: testKeysFetch(baseUrl, [key]) });
12
+ * const { body, headers } = signStatusReports(key, { reports: [report] });
13
+ * await client.checkStatusReports(body, headers[SIGNATURE_HEADER]);
14
+ *
15
+ * Never use a key from here outside a test: anyone can make one.
16
+ */
17
+ export interface TestSigningKey {
18
+ kid: string;
19
+ privateKey: crypto.KeyObject;
20
+ /** The public half, as QBO Sync publishes it at `/.well-known/jwks.json`. */
21
+ publicKey: PublicSigningKey;
22
+ }
23
+ /** A new Ed25519 key pair, `kid` naming it as QBO Sync names its keys. */
24
+ export declare function createTestSigningKey(kid?: string): TestSigningKey;
25
+ /**
26
+ * Signs a call's body as QBO Sync does: `t=<unix seconds>,kid=<key id>,ed25519=<base64url signature of
27
+ * "<t>." + body>`. An object is sent as JSON; a string is signed exactly as given (to test a body that is not
28
+ * JSON). `now` (unix seconds) makes an old or a future signature.
29
+ */
30
+ export declare function signStatusReports(key: Pick<TestSigningKey, "kid" | "privateKey">, body: unknown, now?: number): {
31
+ body: string;
32
+ headers: Record<string, string>;
33
+ };
34
+ /**
35
+ * A `fetch` for the client's `fetch` option that answers `<baseUrl>/.well-known/jwks.json` with these keys'
36
+ * public halves, and passes every other request to `fallback` (the real `fetch` by default).
37
+ */
38
+ export declare function testKeysFetch(baseUrl: string, keys: ReadonlyArray<Pick<TestSigningKey, "publicKey">>, fallback?: typeof fetch): typeof fetch;
@@ -0,0 +1,31 @@
1
+ import crypto from "node:crypto";
2
+ import { JWKS_PATH, SIGNATURE_HEADER } from "./contract/protocol.js";
3
+ /** A new Ed25519 key pair, `kid` naming it as QBO Sync names its keys. */
4
+ export function createTestSigningKey(kid = "test-key") {
5
+ const { privateKey, publicKey } = crypto.generateKeyPairSync("ed25519");
6
+ const { x } = publicKey.export({ format: "jwk" });
7
+ return { kid, privateKey, publicKey: { kty: "OKP", crv: "Ed25519", x, kid } };
8
+ }
9
+ /**
10
+ * Signs a call's body as QBO Sync does: `t=<unix seconds>,kid=<key id>,ed25519=<base64url signature of
11
+ * "<t>." + body>`. An object is sent as JSON; a string is signed exactly as given (to test a body that is not
12
+ * JSON). `now` (unix seconds) makes an old or a future signature.
13
+ */
14
+ export function signStatusReports(key, body, now = Math.floor(Date.now() / 1000)) {
15
+ const raw = typeof body === "string" ? body : JSON.stringify(body);
16
+ const signature = crypto.sign(null, Buffer.from(`${now}.${raw}`, "utf8"), key.privateKey).toString("base64url");
17
+ return { body: raw, headers: { "Content-Type": "application/json", [SIGNATURE_HEADER]: `t=${now},kid=${key.kid},ed25519=${signature}` } };
18
+ }
19
+ /**
20
+ * A `fetch` for the client's `fetch` option that answers `<baseUrl>/.well-known/jwks.json` with these keys'
21
+ * public halves, and passes every other request to `fallback` (the real `fetch` by default).
22
+ */
23
+ export function testKeysFetch(baseUrl, keys, fallback = (...args) => fetch(...args)) {
24
+ const jwks = `${baseUrl.trim().replace(/\/+$/, "")}${JWKS_PATH}`;
25
+ return (async (input, init) => {
26
+ const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
27
+ if (url === jwks)
28
+ return Response.json({ keys: keys.map((k) => k.publicKey) });
29
+ return fallback(input, init);
30
+ });
31
+ }
package/package.json CHANGED
@@ -1,40 +1,44 @@
1
- {
2
- "name": "@company-six/qbosync",
3
- "version": "0.1.0",
4
- "description": "Connect an app to QBO Sync: send invoices and credit memos to QuickBooks, and hear back what happened to them.",
5
- "license": "UNLICENSED",
6
- "type": "module",
7
- "sideEffects": false,
8
- "engines": {
9
- "node": ">=20"
10
- },
11
- "exports": {
12
- ".": {
13
- "types": "./dist/index.d.ts",
14
- "default": "./dist/index.js"
15
- },
16
- "./contract": {
17
- "types": "./dist/contract/index.d.ts",
18
- "default": "./dist/contract/index.js"
19
- },
20
- "./supabase": {
21
- "types": "./dist/supabase.d.ts",
22
- "default": "./dist/supabase.js"
23
- }
24
- },
25
- "files": [
26
- "dist",
27
- "sql/*.sql",
28
- "README.md"
29
- ],
30
- "scripts": {
31
- "build": "tsc -p tsconfig.json",
32
- "prepublishOnly": "npm run build"
33
- },
34
- "peerDependencies": {
35
- "zod": "^4.1.13"
36
- },
37
- "publishConfig": {
38
- "access": "public"
39
- }
40
- }
1
+ {
2
+ "name": "@company-six/qbosync",
3
+ "version": "0.3.0",
4
+ "description": "Connect an app to QBO Sync: send invoices and credit memos to QuickBooks, and hear back what happened to them.",
5
+ "license": "UNLICENSED",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "engines": {
9
+ "node": ">=20"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "default": "./dist/index.js"
15
+ },
16
+ "./contract": {
17
+ "types": "./dist/contract/index.d.ts",
18
+ "default": "./dist/contract/index.js"
19
+ },
20
+ "./supabase": {
21
+ "types": "./dist/supabase.d.ts",
22
+ "default": "./dist/supabase.js"
23
+ },
24
+ "./testing": {
25
+ "types": "./dist/testing.d.ts",
26
+ "default": "./dist/testing.js"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "sql/*.sql",
32
+ "README.md"
33
+ ],
34
+ "scripts": {
35
+ "build": "tsc -p tsconfig.json",
36
+ "prepublishOnly": "npm run build"
37
+ },
38
+ "peerDependencies": {
39
+ "zod": "^4.1.13"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ }
44
+ }