@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/esm/http.js CHANGED
@@ -85,7 +85,7 @@ export class Transport {
85
85
  const url = this.baseUrl + op.path + buildQuery(op.query);
86
86
  const headers = {
87
87
  Authorization: `Bearer ${this.apiKey}`,
88
- Accept: "application/json",
88
+ Accept: op.binary ? "application/pdf, application/json" : "application/json",
89
89
  ...op.headers,
90
90
  };
91
91
  if (this.options.mode)
@@ -110,7 +110,7 @@ export class Transport {
110
110
  let retryAfterMs = null;
111
111
  let res;
112
112
  try {
113
- res = await this.attempt(url, { method: op.method, headers, body }, timeoutMs, opts.signal);
113
+ res = await this.attempt(url, { method: op.method, headers, body }, timeoutMs, opts.signal, op.binary === true);
114
114
  }
115
115
  catch (e) {
116
116
  if (e instanceof AbortError)
@@ -119,6 +119,16 @@ export class Transport {
119
119
  }
120
120
  if (res) {
121
121
  notify(opts.onResponse, res, key ?? null);
122
+ if (res.status >= 200 && res.status < 300 && op.binary && res.bytes) {
123
+ const disposition = res.headers["content-disposition"] ?? "";
124
+ const named = /filename="?([^";]+)"?/i.exec(disposition);
125
+ const file = {
126
+ data: res.bytes,
127
+ contentType: res.headers["content-type"] ?? "application/octet-stream",
128
+ fileName: named ? named[1] : null,
129
+ };
130
+ return file;
131
+ }
122
132
  if (res.status >= 200 && res.status < 300) {
123
133
  try {
124
134
  return toCamel(JSON.parse(res.text));
@@ -158,7 +168,7 @@ export class Transport {
158
168
  const ceiling = Math.min(MAX_BACKOFF_MS, BASE_BACKOFF_MS * 2 ** attempt);
159
169
  return Math.round(ceiling * (0.5 + 0.5 * this.random()));
160
170
  }
161
- async attempt(url, init, timeoutMs, signal) {
171
+ async attempt(url, init, timeoutMs, signal, binary = false) {
162
172
  const controller = new AbortController();
163
173
  let timedOut = false;
164
174
  const timer = setTimeout(() => {
@@ -169,10 +179,15 @@ export class Transport {
169
179
  signal?.addEventListener("abort", onAbort, { once: true });
170
180
  try {
171
181
  const res = await this.fetchImpl(url, { ...init, signal: controller.signal });
172
- const text = await res.text();
182
+ // A file is read as bytes; an error is JSON whatever was asked for.
183
+ const bytes = binary && res.status >= 200 && res.status < 300
184
+ ? new Uint8Array(await res.arrayBuffer())
185
+ : undefined;
186
+ const text = bytes === undefined ? await res.text() : "";
173
187
  return {
174
188
  status: res.status,
175
189
  text,
190
+ bytes,
176
191
  requestId: res.headers.get("Request-Id"),
177
192
  retryAfter: res.headers.get("Retry-After"),
178
193
  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/esm/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export { Timbereye } from "./client.js";
2
2
  export { Timbereye as default } from "./client.js";
3
3
  export { TimbereyeError, InvalidRequestError, AuthenticationError, PermissionError, RateLimitError, ApiError, ConnectionError, TimeoutError, AbortError, WebhookSignatureError, } from "./errors.js";
4
- export { verifyWebhook } from "./webhooks.js";
4
+ export { verifyWebhook, generateTestWebhookHeaders } from "./webhooks.js";
5
5
  export { formatLength, formatDiameter, formatWeight, formatVolume } from "./units.js";
6
6
  export { toCamel, toSnake } from "./case.js";