argus-face-sdk 0.1.0 → 0.1.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 CHANGED
@@ -157,9 +157,17 @@ The SDK retries a per-minute 429 twice, with backoff (set `maxRateLimitRetries`)
157
157
 
158
158
  ## Cost
159
159
 
160
- The API does not return the cost or the balance on a response. What a call costs depends on the key:
160
+ What a call costs depends on the key:
161
161
 
162
- - **Partner key with a monthly quota.** Every call counts one, including `config`, `flashChallenge` and `startIdCapture`. It is counted when the key is checked, before the request is validated. A 502 is not given back.
162
+ - **Partner key with a monthly quota.** A check (`verifyFace`, `screenId`, `matchSelfieToId`) counts one, and only once the server has accepted its input: a 400, 409, 413, 415 or 422 costs nothing, and neither does a replayed result. A 5xx is given back unless a model had already answered. `config`, `flashChallenge`, `startIdCapture`, `getFaceCheck`, clips and `eraseTemplates` are free, though still rate-limited. Each of these calls reports its cost:
163
+
164
+ ```ts
165
+ const argus = new ArgusFace({ apiKey, onCost: (c) => log(c) }); // { method, path, unitsCharged, quotaRemaining }
166
+ argus.lastCost; // the most recent call's cost (use onCost when calls run in parallel)
167
+ error.cost; // on a failed call, what it cost (usually unitsCharged: 0)
168
+ ```
169
+
170
+ `quotaRemaining` is the checks left this month, `"unlimited"` for a key without a quota, or `null` if the server couldn't read it.
163
171
  - **Founder key.** A face check is 20 units by default, at EUR 0.005 a unit (Argus can change this). A sandbox check is free. `usage()` shows the month's units. The sandbox returns a fixture in the live shape; pass `sandboxFail: true` to get the failed one.
164
172
  - **Face Terminal key** (`baseUrl: "https://face.unbackoffice.com"`). One prepaid check per valid `verifyFace`. Flash challenges are free. A 502 or 503 is given back.
165
173
 
package/dist/client.d.ts CHANGED
@@ -19,6 +19,20 @@ export interface ArgusFaceOptions {
19
19
  sleep?: (ms: number) => Promise<void>;
20
20
  /** Allow constructing the client in a browser. Don't: it exposes your key to every visitor. */
21
21
  dangerouslyAllowBrowser?: boolean;
22
+ /**
23
+ * Called after every call that reports its cost (face and ID calls on a key with a monthly quota): what it
24
+ * counted and what is left this month. Safe with calls running at the same time, unlike `lastCost`.
25
+ */
26
+ onCost?: (cost: CallCost) => void;
27
+ }
28
+ /** What a call counted against a monthly quota, from the X-Argus-Units-Charged / X-Argus-Quota-Remaining headers. */
29
+ export interface CallCost {
30
+ method: string;
31
+ path: string;
32
+ /** 0 or 1. */
33
+ unitsCharged: number;
34
+ /** Checks left this month, "unlimited" for a key without a quota, or null when the server could not read it. */
35
+ quotaRemaining: number | "unlimited" | null;
22
36
  }
23
37
  /** A live selfie capture from `argus-face-sdk/browser` (or liveness.js): the clip and the challenge report. */
24
38
  export interface LivenessCapture {
@@ -96,6 +110,8 @@ export interface IdentityResult {
96
110
  export declare class ArgusFace {
97
111
  #private;
98
112
  readonly baseUrl: string;
113
+ /** The cost of the most recent call that reported one (see `onCost` when calls run in parallel). */
114
+ lastCost: CallCost | null;
99
115
  constructor(opts: ArgusFaceOptions);
100
116
  /** Keep the key out of logs and console output. */
101
117
  toJSON(): {
package/dist/client.js CHANGED
@@ -1,7 +1,17 @@
1
1
  import { ArgusError, ArgusNetworkError, ArgusRateLimitError, ArgusValidationError, errorFor, } from "./errors.js";
2
2
  import { FACE_CLIP_MAX_BYTES, ID_CAPTURE_MAX_CHARS, ID_CLIP_MAX_BYTES, IMAGE_MAX_BYTES, prepare, } from "./files.js";
3
3
  export const DEFAULT_BASE_URL = "https://argus.unbackoffice.com";
4
- const VERSION = "0.1.0";
4
+ const VERSION = "0.1.1";
5
+ function costFrom(res, method, path) {
6
+ const charged = res.headers?.get?.("x-argus-units-charged");
7
+ if (charged == null || !/^\d+$/.test(charged))
8
+ return null;
9
+ const left = res.headers.get("x-argus-quota-remaining");
10
+ return {
11
+ method, path, unitsCharged: Number(charged),
12
+ quotaRemaining: left === "unlimited" ? "unlimited" : left != null && /^\d+$/.test(left) ? Number(left) : null,
13
+ };
14
+ }
5
15
  function isBrowser() {
6
16
  return typeof window !== "undefined" && typeof window.document !== "undefined";
7
17
  }
@@ -29,6 +39,9 @@ export class ArgusFace {
29
39
  #timeoutMs;
30
40
  #retries;
31
41
  #sleep;
42
+ #onCost;
43
+ /** The cost of the most recent call that reported one (see `onCost` when calls run in parallel). */
44
+ lastCost = null;
32
45
  constructor(opts) {
33
46
  if (!opts || typeof opts.apiKey !== "string" || !opts.apiKey.trim()) {
34
47
  throw new ArgusValidationError({ status: 0, detail: "apiKey is required" });
@@ -49,6 +62,7 @@ export class ArgusFace {
49
62
  this.#timeoutMs = opts.timeoutMs ?? 120_000;
50
63
  this.#retries = Math.max(0, Math.min(5, opts.maxRateLimitRetries ?? 2));
51
64
  this.#sleep = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
65
+ this.#onCost = opts.onCost;
52
66
  }
53
67
  /** Keep the key out of logs and console output. */
54
68
  toJSON() {
@@ -237,9 +251,19 @@ export class ArgusFace {
237
251
  clearTimeout(timer);
238
252
  signal?.removeEventListener("abort", onAbort);
239
253
  }
254
+ const cost = costFrom(res, method, path);
255
+ if (cost) {
256
+ this.lastCost = cost;
257
+ try {
258
+ this.#onCost?.(cost);
259
+ }
260
+ catch { /* a failing callback never breaks the call */ }
261
+ }
240
262
  if (res.ok)
241
263
  return res;
242
264
  const err = errorFor(res.status, await readBody(res), path);
265
+ if (cost)
266
+ Object.defineProperty(err, "cost", { value: cost, enumerable: false });
243
267
  if (err instanceof ArgusRateLimitError && err.retryable && attempt < this.#retries) {
244
268
  await this.#sleep(1000 * 2 ** attempt + Math.floor(Math.random() * 250));
245
269
  continue;
package/dist/errors.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { CallCost } from "./client.js";
1
2
  /** Typed errors for every way an Argus call can fail. All extend ArgusError, so one `instanceof ArgusError` catches them. */
2
3
  export interface ArgusErrorInit {
3
4
  status: number;
@@ -16,6 +17,8 @@ export declare class ArgusError extends Error {
16
17
  readonly body?: unknown;
17
18
  /** Whether trying the same call again later may succeed. */
18
19
  readonly retryable: boolean;
20
+ /** What the call counted against a monthly quota, when the server said (face and ID calls). */
21
+ readonly cost?: CallCost;
19
22
  constructor(init: ArgusErrorInit);
20
23
  }
21
24
  /** 400, 413, 415 or 422 from the server, or an input the SDK refused before sending (status 0). Not charged. */
package/dist/errors.js CHANGED
@@ -1,4 +1,3 @@
1
- /** Typed errors for every way an Argus call can fail. All extend ArgusError, so one `instanceof ArgusError` catches them. */
2
1
  export class ArgusError extends Error {
3
2
  /** HTTP status. 0 when the request never got an answer (network, timeout) or the SDK refused it before sending. */
4
3
  status;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { ArgusFace, DEFAULT_BASE_URL } from "./client.js";
2
- export type { ArgusFaceOptions, IdentityResult, IdLiveCapture, LivenessCapture, ScreenIdInput, VerifyFaceInput, VerifyIdentityInput, } from "./client.js";
2
+ export type { ArgusFaceOptions, CallCost, IdentityResult, IdLiveCapture, LivenessCapture, ScreenIdInput, VerifyFaceInput, VerifyIdentityInput, } from "./client.js";
3
3
  export { explainResult } from "./explain.js";
4
4
  export type { Explanation, Signal, SignalEffect } from "./explain.js";
5
5
  export { ArgusAllowanceError, ArgusAuthError, ArgusConflictError, ArgusError, ArgusNetworkError, ArgusNotFoundError, ArgusPermissionError, ArgusRateLimitError, ArgusUnavailableError, ArgusUpstreamError, ArgusValidationError, } from "./errors.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "argus-face-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Client SDK for the Argus face verification API: face match, ID document plus selfie, live capture.",
5
5
  "homepage": "https://argus.unbackoffice.com/developer",
6
6
  "repository": {