@renai-labs/sdk 0.1.13 → 0.1.14

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/client.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { createClient } from "./generated/client";
2
2
  import { client as defaultClient } from "./generated/client.gen";
3
3
  import { RenClient } from "./generated/sdk.gen";
4
+ import { ApiError } from "./error";
4
5
  export function createRenClient(config = {}) {
5
6
  const { auth, ...rest } = config;
6
7
  const merged = { credentials: "include", ...rest };
@@ -16,9 +17,7 @@ export function createRenClient(config = {}) {
16
17
  const path = request ? new URL(request.url).pathname : undefined;
17
18
  const method = request?.method;
18
19
  const status = response?.status;
19
- if (body && typeof body === "object")
20
- return Object.assign(body, { status, path, method });
21
- return { body, status, path, method };
20
+ return new ApiError({ status, path, method, body });
22
21
  });
23
22
  return new RenClient({ client });
24
23
  }
@@ -0,0 +1,15 @@
1
+ export type ApiErrorInit = {
2
+ status?: number;
3
+ path?: string;
4
+ method?: string;
5
+ body: unknown;
6
+ };
7
+ export declare class ApiError extends Error {
8
+ readonly status?: number;
9
+ readonly path?: string;
10
+ readonly method?: string;
11
+ readonly error?: string;
12
+ readonly body: unknown;
13
+ constructor(init: ApiErrorInit);
14
+ }
15
+ export declare function isApiError(value: unknown): value is ApiError;
package/dist/error.js ADDED
@@ -0,0 +1,35 @@
1
+ export class ApiError extends Error {
2
+ status;
3
+ path;
4
+ method;
5
+ error;
6
+ body;
7
+ constructor(init) {
8
+ super(buildMessage(init));
9
+ this.name = "ApiError";
10
+ this.status = init.status;
11
+ this.path = init.path;
12
+ this.method = init.method;
13
+ this.error = extractDetail(init.body);
14
+ this.body = init.body;
15
+ }
16
+ }
17
+ export function isApiError(value) {
18
+ return value instanceof ApiError;
19
+ }
20
+ function extractDetail(body) {
21
+ if (body && typeof body === "object" && "error" in body) {
22
+ const detail = body.error;
23
+ if (typeof detail === "string" && detail.trim())
24
+ return detail;
25
+ }
26
+ return undefined;
27
+ }
28
+ function buildMessage({ status, path, method, body }) {
29
+ const detail = extractDetail(body);
30
+ if (detail)
31
+ return detail;
32
+ const route = [method, path].filter(Boolean).join(" ");
33
+ const code = status ? ` (${status})` : "";
34
+ return route ? `${route}${code} failed` : `Request failed${code || ""}`;
35
+ }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./generated/types.gen";
2
2
  export { createRenClient, RenClient, type RenClientConfig } from "./client";
3
+ export { ApiError, isApiError, type ApiErrorInit } from "./error";
3
4
  export { bearer, pat, session, type AuthStrategy } from "./auth";
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./generated/types.gen";
2
2
  export { createRenClient, RenClient } from "./client";
3
+ export { ApiError, isApiError } from "./error";
3
4
  export { bearer, pat, session } from "./auth";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@renai-labs/sdk",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "TypeScript SDK for the Ren API, generated from OpenAPI.",
5
5
  "license": "MIT",
6
6
  "author": "Ren Labs, Inc.",