@databallr/sdk 0.2.1 → 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/README.md CHANGED
@@ -32,8 +32,16 @@ const standings = await dbr.get("/v1/nba/standings", { query: { season: 2026 } }
32
32
  network error is retried with backoff. `maxRetries` (default 2) and
33
33
  `timeoutMs` (default 30 s, per attempt) can be set on the client or per
34
34
  call, along with `signal` and `headers`.
35
- - Non-2xx responses throw `DataballrError` (`status`, `code`, `message`,
36
- `requestId`, `requiredScope`, `retryAfter`). Network failures throw
35
+ - Non-2xx responses throw `DataballrError` (`status`, `code`, `type`,
36
+ `retryable`, `message`, `requestId`, `requiredScope`, `retryAfter`).
37
+ `code` is UPPER_SNAKE for errors any call can hit: `INVALID_TOKEN` (401),
38
+ `INSUFFICIENT_SCOPE` (403), `RATE_LIMITED` (429), `UPSTREAM_ERROR` (502),
39
+ or `HTTP_ERROR` when the body is not the API's error envelope. An
40
+ operation's own errors (bad input, nothing found) may still carry a
41
+ sentence as `code` until the API moves them to codes, so branch on
42
+ `status` for those. The API does not send `type` yet, so it is
43
+ `undefined` and `retryable` is `false`; the client's own retries do not
44
+ depend on them. Network failures throw
37
45
  `DataballrConnectionError`, and timeouts its subclass
38
46
  `DataballrTimeoutError`.
39
47
  - Responses are returned exactly as the API sends them.
package/dist/client.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import { DataballrConnectionError, DataballrTimeoutError, errorFromResponse } from "./errors.js";
2
2
  import { methods } from "./generated/methods.js";
3
+ const PLACEHOLDER_RE = /\{([^}]+)\}/g;
4
+ const TRAILING_SLASHES_RE = /\/+$/;
3
5
  export const PRODUCTION_BASE_URL = "https://api.databallr.com";
4
6
  export const PRODUCTION_API_KEY_PREFIX = "dbrk_";
5
7
  const RETRY_STATUSES = new Set([429, 502, 503, 504]);
@@ -48,7 +50,7 @@ export class Databallr {
48
50
  (body, options) => this.#request("POST", path, { ...options, body }),
49
51
  ];
50
52
  }
51
- const placeholders = new Set(Array.from(target.matchAll(/\{([^}]+)\}/g), (m) => m[1]));
53
+ const placeholders = new Set(Array.from(target.matchAll(PLACEHOLDER_RE), (m) => m[1]));
52
54
  return [
53
55
  name,
54
56
  (params = {}, options) => {
@@ -162,7 +164,7 @@ function isJson(response) {
162
164
  return (response.headers.get("content-type") ?? "").includes("json");
163
165
  }
164
166
  function fillPath(template, params) {
165
- return template.replace(/\{([^}]+)\}/g, (_, name) => {
167
+ return template.replace(PLACEHOLDER_RE, (_, name) => {
166
168
  const value = params?.[name];
167
169
  if (value === undefined || value === null) {
168
170
  throw new Error(`Databallr: missing path parameter "${name}" for ${template}`);
@@ -195,5 +197,5 @@ function isBrowser() {
195
197
  return typeof g.window !== "undefined" && typeof g.document !== "undefined";
196
198
  }
197
199
  function trimSlash(url) {
198
- return url.replace(/\/+$/, "");
200
+ return url.replace(TRAILING_SLASHES_RE, "");
199
201
  }
package/dist/errors.d.ts CHANGED
@@ -1,7 +1,16 @@
1
1
  interface DataballrErrorFields {
2
2
  readonly status: number;
3
- /** The shared envelope's `error`, or `http_error` when the body is not that envelope. */
3
+ /**
4
+ * The shared envelope's `error` in UPPER_SNAKE (`INVALID_TOKEN`,
5
+ * `INSUFFICIENT_SCOPE`, `RATE_LIMITED`, ...), or `HTTP_ERROR` when the body
6
+ * is not that envelope. An older lowercase spelling from the API reads the
7
+ * same.
8
+ */
4
9
  readonly code: string;
10
+ /** The envelope's `type` (`validation`, `rate_limited`, `unavailable`, ...), when the API sends one. */
11
+ readonly type: string | undefined;
12
+ /** The envelope's `retryable`: true when retrying the same request can help. */
13
+ readonly retryable: boolean;
5
14
  readonly requestId: string | undefined;
6
15
  /** The scope a 403 says would allow the call, from `WWW-Authenticate`. */
7
16
  readonly requiredScope: string | undefined;
@@ -11,13 +20,15 @@ interface DataballrErrorFields {
11
20
  readonly body: unknown;
12
21
  }
13
22
  /**
14
- * Thrown for every non-2xx response. `code` is `invalid_token`,
15
- * `insufficient_scope`, `rate_limited`, ... or `http_error`.
23
+ * Thrown for every non-2xx response. `code` is `INVALID_TOKEN`,
24
+ * `INSUFFICIENT_SCOPE`, `RATE_LIMITED`, ... or `HTTP_ERROR`.
16
25
  */
17
26
  export declare class DataballrError extends Error implements DataballrErrorFields {
18
27
  readonly name = "DataballrError";
19
28
  readonly status: number;
20
29
  readonly code: string;
30
+ readonly type: string | undefined;
31
+ readonly retryable: boolean;
21
32
  readonly requestId: string | undefined;
22
33
  readonly requiredScope: string | undefined;
23
34
  readonly retryAfter: number | undefined;
package/dist/errors.js CHANGED
@@ -1,3 +1,4 @@
1
+ const WWW_AUTHENTICATE_SCOPE_RE = /\bscope="([^"]*)"/;
1
2
  export class DataballrError extends Error {
2
3
  name = "DataballrError";
3
4
  constructor(message, fields) {
@@ -18,17 +19,23 @@ export function errorFromResponse(response, text) {
18
19
  }
19
20
  catch { }
20
21
  const envelope = isEnvelope(body) ? body : undefined;
21
- const code = envelope?.error ?? "http_error";
22
+ const code = envelope ? wireCode(envelope.error) : "HTTP_ERROR";
22
23
  return new DataballrError(stringOrUndefined(envelope?.message) ??
23
24
  (envelope ? code : `Request failed with status ${response.status}`), {
24
25
  status: response.status,
25
26
  code,
27
+ type: stringOrUndefined(envelope?.type),
28
+ retryable: envelope?.retryable === true,
26
29
  requestId: stringOrUndefined(envelope?.request_id),
27
- requiredScope: response.headers.get("www-authenticate")?.match(/\bscope="([^"]*)"/)?.[1],
30
+ requiredScope: response.headers.get("www-authenticate")?.match(WWW_AUTHENTICATE_SCOPE_RE)?.[1],
28
31
  retryAfter: secondsFromRetryAfter(response.headers.get("retry-after")),
29
32
  body,
30
33
  });
31
34
  }
35
+ const LOWER_SNAKE_RE = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/;
36
+ function wireCode(error) {
37
+ return LOWER_SNAKE_RE.test(error) ? error.toUpperCase() : error;
38
+ }
32
39
  function secondsFromRetryAfter(header) {
33
40
  if (header === null)
34
41
  return undefined;
@@ -1,5 +1,5 @@
1
1
  /** First 12 hex characters of the SHA-256 of the key-callable spec these files came from. */
2
- export declare const SPEC_HASH = "05bc2e1baf0f";
2
+ export declare const SPEC_HASH = "bfc137c567e9";
3
3
  /**
4
4
  * Every generated method: GET operations by path, POST queries as
5
5
  * `POST <path>`. `Databallr` turns this table into `dbr.nba.players.search(...)`.
@@ -1,4 +1,4 @@
1
- export const SPEC_HASH = "05bc2e1baf0f";
1
+ export const SPEC_HASH = "bfc137c567e9";
2
2
  export const methods = {
3
3
  apps: {
4
4
  sixrings: {
@@ -2603,7 +2603,7 @@ export interface paths {
2603
2603
  export type webhooks = Record<string, never>;
2604
2604
  export interface components {
2605
2605
  schemas: {
2606
- /** @description Shared error envelope. Routes may use a subset of these keys, a nested `error: {code, message}` object, or a bare string. */
2606
+ /** @description Shared error envelope. `error` is an UPPER_SNAKE code (`INVALID_TOKEN`, `INSUFFICIENT_SCOPE`, `RATE_LIMITED`, `UPSTREAM_ERROR`) for errors any operation can return; an operation's own errors may still carry a sentence there instead. `message` is a human-readable sentence. Some routes use a subset of these keys or a nested `error: {code, message}` object. */
2607
2607
  ApiError: {
2608
2608
  details?: unknown;
2609
2609
  error?: unknown;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@databallr/sdk",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "TypeScript client for the databallr API (NBA and WNBA stats, SixRings)",
5
5
  "license": "MIT",
6
6
  "repository": {