@timbereye/sdk 0.1.0 → 0.2.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.
@@ -3,7 +3,10 @@ export declare const DEFAULT_MAX_RETRIES = 2;
3
3
  export declare const DEFAULT_TIMEOUT_MS = 60000;
4
4
  export type FetchLike = (input: string, init: RequestInit) => Promise<Response>;
5
5
  export interface ClientOptions {
6
- /** Default `https://api.timbereye.com/v1`. */
6
+ /**
7
+ * The API's address. `new Timbereye()` uses this option, else `TIMBEREYE_API_URL` from the environment (where
8
+ * there is one), else `https://api.timbereye.com/v1`.
9
+ */
7
10
  baseUrl?: string;
8
11
  /** Defaults to the global `fetch`. */
9
12
  fetch?: FetchLike;
@@ -68,6 +71,15 @@ export interface Operation {
68
71
  /** For POSTs that only compute (`/places/validate`): the API ignores an idempotency key. */
69
72
  noIdempotency?: boolean;
70
73
  headers?: Record<string, string>;
74
+ /** The answer is a file (a PDF), not JSON: the call resolves to a `BinaryResponse`. */
75
+ binary?: boolean;
76
+ }
77
+ /** A file the API answered with (`application/pdf`): its bytes, type and the file name it suggests. */
78
+ export interface BinaryResponse {
79
+ data: Uint8Array;
80
+ contentType: string;
81
+ /** From `Content-Disposition`, when the API names one. */
82
+ fileName: string | null;
71
83
  }
72
84
  /** Seconds or an HTTP date, as milliseconds; null when absent or unparseable. */
73
85
  export declare function parseRetryAfter(value: string | null, nowMs?: number): number | null;
package/dist/cjs/http.js CHANGED
@@ -90,7 +90,7 @@ class Transport {
90
90
  const url = this.baseUrl + op.path + buildQuery(op.query);
91
91
  const headers = {
92
92
  Authorization: `Bearer ${this.apiKey}`,
93
- Accept: "application/json",
93
+ Accept: op.binary ? "application/pdf, application/json" : "application/json",
94
94
  ...op.headers,
95
95
  };
96
96
  if (this.options.mode)
@@ -115,7 +115,7 @@ class Transport {
115
115
  let retryAfterMs = null;
116
116
  let res;
117
117
  try {
118
- res = await this.attempt(url, { method: op.method, headers, body }, timeoutMs, opts.signal);
118
+ res = await this.attempt(url, { method: op.method, headers, body }, timeoutMs, opts.signal, op.binary === true);
119
119
  }
120
120
  catch (e) {
121
121
  if (e instanceof errors_js_1.AbortError)
@@ -124,6 +124,16 @@ class Transport {
124
124
  }
125
125
  if (res) {
126
126
  notify(opts.onResponse, res, key ?? null);
127
+ if (res.status >= 200 && res.status < 300 && op.binary && res.bytes) {
128
+ const disposition = res.headers["content-disposition"] ?? "";
129
+ const named = /filename="?([^";]+)"?/i.exec(disposition);
130
+ const file = {
131
+ data: res.bytes,
132
+ contentType: res.headers["content-type"] ?? "application/octet-stream",
133
+ fileName: named ? named[1] : null,
134
+ };
135
+ return file;
136
+ }
127
137
  if (res.status >= 200 && res.status < 300) {
128
138
  try {
129
139
  return (0, case_js_1.toCamel)(JSON.parse(res.text));
@@ -163,7 +173,7 @@ class Transport {
163
173
  const ceiling = Math.min(MAX_BACKOFF_MS, BASE_BACKOFF_MS * 2 ** attempt);
164
174
  return Math.round(ceiling * (0.5 + 0.5 * this.random()));
165
175
  }
166
- async attempt(url, init, timeoutMs, signal) {
176
+ async attempt(url, init, timeoutMs, signal, binary = false) {
167
177
  const controller = new AbortController();
168
178
  let timedOut = false;
169
179
  const timer = setTimeout(() => {
@@ -174,10 +184,15 @@ class Transport {
174
184
  signal?.addEventListener("abort", onAbort, { once: true });
175
185
  try {
176
186
  const res = await this.fetchImpl(url, { ...init, signal: controller.signal });
177
- const text = await res.text();
187
+ // A file is read as bytes; an error is JSON whatever was asked for.
188
+ const bytes = binary && res.status >= 200 && res.status < 300
189
+ ? new Uint8Array(await res.arrayBuffer())
190
+ : undefined;
191
+ const text = bytes === undefined ? await res.text() : "";
178
192
  return {
179
193
  status: res.status,
180
194
  text,
195
+ bytes,
181
196
  requestId: res.headers.get("Request-Id"),
182
197
  retryAfter: res.headers.get("Retry-After"),
183
198
  headers: headersOf(res.headers),
@@ -5,8 +5,8 @@ export type { TimbereyeOptions, WebhookHelpers, WebhookResponse, WebhookRouteHan
5
5
  export { TimbereyeError, InvalidRequestError, AuthenticationError, PermissionError, RateLimitError, ApiError, ConnectionError, TimeoutError, AbortError, WebhookSignatureError, } from "./errors.js";
6
6
  export type { ErrorType } from "./errors.js";
7
7
  export type { Page, PagedResult, ListResult, AutoPaging } from "./pagination.js";
8
- export { verifyWebhook } from "./webhooks.js";
9
- export type { VerifyOptions, HeadersInput, WebhookRequest } from "./webhooks.js";
8
+ export { verifyWebhook, generateTestWebhookHeaders } from "./webhooks.js";
9
+ export type { VerifyOptions, HeadersInput, WebhookRequest, TestWebhookParams } from "./webhooks.js";
10
10
  export { formatLength, formatDiameter, formatWeight, formatVolume } from "./units.js";
11
11
  export type { DisplayUnits, UnitsSource } from "./units.js";
12
12
  export { toCamel, toSnake } from "./case.js";
package/dist/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toSnake = exports.toCamel = exports.formatVolume = exports.formatWeight = exports.formatDiameter = exports.formatLength = exports.verifyWebhook = exports.WebhookSignatureError = exports.AbortError = exports.TimeoutError = exports.ConnectionError = exports.ApiError = exports.RateLimitError = exports.PermissionError = exports.AuthenticationError = exports.InvalidRequestError = exports.TimbereyeError = exports.default = exports.Timbereye = void 0;
3
+ exports.toSnake = exports.toCamel = exports.formatVolume = exports.formatWeight = exports.formatDiameter = exports.formatLength = exports.generateTestWebhookHeaders = exports.verifyWebhook = exports.WebhookSignatureError = exports.AbortError = exports.TimeoutError = exports.ConnectionError = exports.ApiError = exports.RateLimitError = exports.PermissionError = exports.AuthenticationError = exports.InvalidRequestError = exports.TimbereyeError = exports.default = exports.Timbereye = void 0;
4
4
  var client_js_1 = require("./client.js");
5
5
  Object.defineProperty(exports, "Timbereye", { enumerable: true, get: function () { return client_js_1.Timbereye; } });
6
6
  var client_js_2 = require("./client.js");
@@ -18,6 +18,7 @@ Object.defineProperty(exports, "AbortError", { enumerable: true, get: function (
18
18
  Object.defineProperty(exports, "WebhookSignatureError", { enumerable: true, get: function () { return errors_js_1.WebhookSignatureError; } });
19
19
  var webhooks_js_1 = require("./webhooks.js");
20
20
  Object.defineProperty(exports, "verifyWebhook", { enumerable: true, get: function () { return webhooks_js_1.verifyWebhook; } });
21
+ Object.defineProperty(exports, "generateTestWebhookHeaders", { enumerable: true, get: function () { return webhooks_js_1.generateTestWebhookHeaders; } });
21
22
  var units_js_1 = require("./units.js");
22
23
  Object.defineProperty(exports, "formatLength", { enumerable: true, get: function () { return units_js_1.formatLength; } });
23
24
  Object.defineProperty(exports, "formatDiameter", { enumerable: true, get: function () { return units_js_1.formatDiameter; } });