@getanyapi/sdk 0.9.6 → 0.9.8

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.d.cts CHANGED
@@ -8,8 +8,31 @@ interface RunResult<T> {
8
8
  provider: "AnyAPI";
9
9
  /** Amount charged in USD for this call. */
10
10
  costUsd: number;
11
- /** Number of result rows returned (present on per-result SKUs). */
12
- items?: number;
11
+ /**
12
+ * Number of result rows returned. Always present on the wire: the gateway's field
13
+ * carries no `omitempty`, and a metadata-only replay preserves the original count.
14
+ */
15
+ items: number;
16
+ /**
17
+ * True when the gateway served a stored response for a repeated Idempotency-Key instead
18
+ * of running the SKU again. A replay is not billed twice. Always present on the wire.
19
+ *
20
+ * A replay whose stored payload is no longer retained (a 24h TTL expiry, or a payload
21
+ * that exceeded the storage size cap) arrives with the metadata only and `output` null,
22
+ * which {@link unwrap} rejects. See SPEC 2.3.
23
+ */
24
+ replayed: boolean;
25
+ /**
26
+ * Opaque handle to the full unshaped result, cached for about 15 minutes, so it can be
27
+ * re-read and re-shaped for free via GET /v1/results/{id}. Absent when the gateway did
28
+ * not cache the result.
29
+ */
30
+ resultId?: string;
31
+ /**
32
+ * Why a requested jq reshape did not apply. The run was still billed and `output` carries
33
+ * the full unshaped result. Absent when no jq was requested or it succeeded.
34
+ */
35
+ jqError?: string;
13
36
  /** Optional server nudge when a large result was returned untrimmed. */
14
37
  hint?: string;
15
38
  }
@@ -35,8 +58,31 @@ interface BareRunResult<T> {
35
58
  provider: "AnyAPI";
36
59
  /** Amount charged in USD for this call. */
37
60
  costUsd: number;
38
- /** Number of result rows returned (present on per-result SKUs). */
39
- items?: number;
61
+ /**
62
+ * Number of result rows returned. Always present on the wire: the gateway's field
63
+ * carries no `omitempty`, and a metadata-only replay preserves the original count.
64
+ */
65
+ items: number;
66
+ /**
67
+ * True when the gateway served a stored response for a repeated Idempotency-Key instead
68
+ * of running the SKU again. A replay is not billed twice. Always present on the wire.
69
+ *
70
+ * A replay whose stored payload is no longer retained (a 24h TTL expiry, or a payload
71
+ * that exceeded the storage size cap) arrives with the metadata only and `output` null,
72
+ * which {@link unwrap} rejects. See SPEC 2.3.
73
+ */
74
+ replayed: boolean;
75
+ /**
76
+ * Opaque handle to the full unshaped result, cached for about 15 minutes, so it can be
77
+ * re-read and re-shaped for free via GET /v1/results/{id}. Absent when the gateway did
78
+ * not cache the result.
79
+ */
80
+ resultId?: string;
81
+ /**
82
+ * Why a requested jq reshape did not apply. The run was still billed and `output` carries
83
+ * the full unshaped result. Absent when no jq was requested or it succeeded.
84
+ */
85
+ jqError?: string;
40
86
  /** Optional server nudge when a large result was returned untrimmed. */
41
87
  hint?: string;
42
88
  }
@@ -45,7 +91,10 @@ interface BareRunResult<T> {
45
91
  * matching entity. Narrows Output<T> to T.
46
92
  *
47
93
  * Two overloads: a found-data RunResult may be empty (throws when found is false); a bare
48
- * result always carries its data (returns output directly, never throws).
94
+ * result always carries its data (returns output directly).
95
+ *
96
+ * Either shape throws AnyAPIError when `output` is null: an idempotent replay can outlive
97
+ * its stored payload, and the caller must not receive `null` typed as T. See SPEC 2.3.
49
98
  */
50
99
  declare function unwrap<T>(result: BareRunResult<T>): T;
51
100
  declare function unwrap<T>(result: RunResult<T>): T;
@@ -294,7 +343,10 @@ declare function paginate<Item, Page extends RunResult<unknown> | BareRunResult<
294
343
  declare class AnyAPIError extends Error {
295
344
  /** HTTP status code, or 0 for transport-level failures (connection/timeout). */
296
345
  readonly status: number;
297
- /** The x-request-id response header when present, else undefined. */
346
+ /**
347
+ * The gateway's X-Anyapi-Request-Id response header when present (falling back to a
348
+ * proxy-set x-request-id), else undefined. Quote it to AnyAPI support.
349
+ */
298
350
  readonly requestId?: string;
299
351
  /** Stable gateway error code when the JSON body includes one, else undefined. */
300
352
  readonly code?: string;
package/dist/index.d.ts CHANGED
@@ -8,8 +8,31 @@ interface RunResult<T> {
8
8
  provider: "AnyAPI";
9
9
  /** Amount charged in USD for this call. */
10
10
  costUsd: number;
11
- /** Number of result rows returned (present on per-result SKUs). */
12
- items?: number;
11
+ /**
12
+ * Number of result rows returned. Always present on the wire: the gateway's field
13
+ * carries no `omitempty`, and a metadata-only replay preserves the original count.
14
+ */
15
+ items: number;
16
+ /**
17
+ * True when the gateway served a stored response for a repeated Idempotency-Key instead
18
+ * of running the SKU again. A replay is not billed twice. Always present on the wire.
19
+ *
20
+ * A replay whose stored payload is no longer retained (a 24h TTL expiry, or a payload
21
+ * that exceeded the storage size cap) arrives with the metadata only and `output` null,
22
+ * which {@link unwrap} rejects. See SPEC 2.3.
23
+ */
24
+ replayed: boolean;
25
+ /**
26
+ * Opaque handle to the full unshaped result, cached for about 15 minutes, so it can be
27
+ * re-read and re-shaped for free via GET /v1/results/{id}. Absent when the gateway did
28
+ * not cache the result.
29
+ */
30
+ resultId?: string;
31
+ /**
32
+ * Why a requested jq reshape did not apply. The run was still billed and `output` carries
33
+ * the full unshaped result. Absent when no jq was requested or it succeeded.
34
+ */
35
+ jqError?: string;
13
36
  /** Optional server nudge when a large result was returned untrimmed. */
14
37
  hint?: string;
15
38
  }
@@ -35,8 +58,31 @@ interface BareRunResult<T> {
35
58
  provider: "AnyAPI";
36
59
  /** Amount charged in USD for this call. */
37
60
  costUsd: number;
38
- /** Number of result rows returned (present on per-result SKUs). */
39
- items?: number;
61
+ /**
62
+ * Number of result rows returned. Always present on the wire: the gateway's field
63
+ * carries no `omitempty`, and a metadata-only replay preserves the original count.
64
+ */
65
+ items: number;
66
+ /**
67
+ * True when the gateway served a stored response for a repeated Idempotency-Key instead
68
+ * of running the SKU again. A replay is not billed twice. Always present on the wire.
69
+ *
70
+ * A replay whose stored payload is no longer retained (a 24h TTL expiry, or a payload
71
+ * that exceeded the storage size cap) arrives with the metadata only and `output` null,
72
+ * which {@link unwrap} rejects. See SPEC 2.3.
73
+ */
74
+ replayed: boolean;
75
+ /**
76
+ * Opaque handle to the full unshaped result, cached for about 15 minutes, so it can be
77
+ * re-read and re-shaped for free via GET /v1/results/{id}. Absent when the gateway did
78
+ * not cache the result.
79
+ */
80
+ resultId?: string;
81
+ /**
82
+ * Why a requested jq reshape did not apply. The run was still billed and `output` carries
83
+ * the full unshaped result. Absent when no jq was requested or it succeeded.
84
+ */
85
+ jqError?: string;
40
86
  /** Optional server nudge when a large result was returned untrimmed. */
41
87
  hint?: string;
42
88
  }
@@ -45,7 +91,10 @@ interface BareRunResult<T> {
45
91
  * matching entity. Narrows Output<T> to T.
46
92
  *
47
93
  * Two overloads: a found-data RunResult may be empty (throws when found is false); a bare
48
- * result always carries its data (returns output directly, never throws).
94
+ * result always carries its data (returns output directly).
95
+ *
96
+ * Either shape throws AnyAPIError when `output` is null: an idempotent replay can outlive
97
+ * its stored payload, and the caller must not receive `null` typed as T. See SPEC 2.3.
49
98
  */
50
99
  declare function unwrap<T>(result: BareRunResult<T>): T;
51
100
  declare function unwrap<T>(result: RunResult<T>): T;
@@ -294,7 +343,10 @@ declare function paginate<Item, Page extends RunResult<unknown> | BareRunResult<
294
343
  declare class AnyAPIError extends Error {
295
344
  /** HTTP status code, or 0 for transport-level failures (connection/timeout). */
296
345
  readonly status: number;
297
- /** The x-request-id response header when present, else undefined. */
346
+ /**
347
+ * The gateway's X-Anyapi-Request-Id response header when present (falling back to a
348
+ * proxy-set x-request-id), else undefined. Quote it to AnyAPI support.
349
+ */
298
350
  readonly requestId?: string;
299
351
  /** Stable gateway error code when the JSON body includes one, else undefined. */
300
352
  readonly code?: string;
package/dist/index.js CHANGED
@@ -2,7 +2,10 @@
2
2
  var AnyAPIError = class extends Error {
3
3
  /** HTTP status code, or 0 for transport-level failures (connection/timeout). */
4
4
  status;
5
- /** The x-request-id response header when present, else undefined. */
5
+ /**
6
+ * The gateway's X-Anyapi-Request-Id response header when present (falling back to a
7
+ * proxy-set x-request-id), else undefined. Quote it to AnyAPI support.
8
+ */
6
9
  requestId;
7
10
  /** Stable gateway error code when the JSON body includes one, else undefined. */
8
11
  code;
@@ -37,6 +40,14 @@ var ConnectionError = class extends AnyAPIError {
37
40
  };
38
41
  var TimeoutError = class extends AnyAPIError {
39
42
  };
43
+ var REQUEST_ID_HEADERS = ["x-anyapi-request-id", "x-request-id"];
44
+ function requestIdOf(headers) {
45
+ for (const name of REQUEST_ID_HEADERS) {
46
+ const value = headers.get(name);
47
+ if (value) return value;
48
+ }
49
+ return void 0;
50
+ }
40
51
  function errorFromStatus(status, message, requestId, code) {
41
52
  switch (status) {
42
53
  case 400:
@@ -407,7 +418,7 @@ async function agentSignup(options = {}) {
407
418
  0
408
419
  );
409
420
  }
410
- const requestId = response.headers.get("x-request-id") ?? void 0;
421
+ const requestId = requestIdOf(response.headers);
411
422
  const text = await response.text().catch(() => "");
412
423
  if (response.status !== 200) {
413
424
  let message = `request failed with status ${response.status}`;
@@ -750,7 +761,7 @@ var AnyAPI = class {
750
761
  }
751
762
  throw connErr;
752
763
  }
753
- const requestId = response.headers.get("x-request-id") ?? void 0;
764
+ const requestId = requestIdOf(response.headers);
754
765
  if (response.status === 200) {
755
766
  const text = await response.text();
756
767
  try {
@@ -788,8 +799,12 @@ var AnyAPI = class {
788
799
  };
789
800
 
790
801
  // src/core/types.ts
802
+ var OUTPUT_NOT_RETAINED = "the run output was not retained: this response is an idempotent replay whose stored payload has expired or was too large to store, so only the run metadata came back. Re-run the request without the idempotency key (or with a fresh one) to fetch the data again.";
791
803
  function unwrap(result) {
792
804
  const output = result.output;
805
+ if (output === null || output === void 0) {
806
+ throw new AnyAPIError(OUTPUT_NOT_RETAINED, 200);
807
+ }
793
808
  if (output !== null && typeof output === "object" && "found" in output) {
794
809
  const env = output;
795
810
  if (env.found) {