@orth/sdk 0.3.1 → 0.5.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/dist/index.d.mts +23 -1
- package/dist/index.d.ts +23 -1
- package/dist/index.js +50 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +48 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -32,6 +32,28 @@ interface RunResponse {
|
|
|
32
32
|
data: unknown;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Error thrown by {@link Orthogonal.run} on a non-2xx response.
|
|
37
|
+
*
|
|
38
|
+
* Extends `Error` (so existing `catch (e) { e.message }` keeps working) but also
|
|
39
|
+
* carries the structured server payload. On a contract violation the API returns
|
|
40
|
+
* an `_orthogonal` self-correction hint (expected schema, unexpected/missing
|
|
41
|
+
* fields, numeric bounds) — exposed here as `orthogonal` so an agent can fix the
|
|
42
|
+
* request and retry instead of only seeing the terse message.
|
|
43
|
+
*/
|
|
44
|
+
declare class OrthogonalRunError extends Error {
|
|
45
|
+
/** HTTP status of the failed response. */
|
|
46
|
+
status: number;
|
|
47
|
+
/** The API's `_orthogonal` self-correction hint, when present. */
|
|
48
|
+
orthogonal?: unknown;
|
|
49
|
+
/** The full parsed response body. */
|
|
50
|
+
responseBody?: unknown;
|
|
51
|
+
constructor(message: string, opts: {
|
|
52
|
+
status: number;
|
|
53
|
+
orthogonal?: unknown;
|
|
54
|
+
responseBody?: unknown;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
35
57
|
/**
|
|
36
58
|
* Orthogonal SDK client
|
|
37
59
|
*
|
|
@@ -62,4 +84,4 @@ declare class Orthogonal {
|
|
|
62
84
|
run(options: RunOptions): Promise<RunResponse>;
|
|
63
85
|
}
|
|
64
86
|
|
|
65
|
-
export { Orthogonal, type OrthogonalConfig, type RunOptions, type RunResponse, Orthogonal as default };
|
|
87
|
+
export { Orthogonal, type OrthogonalConfig, OrthogonalRunError, type RunOptions, type RunResponse, Orthogonal as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -32,6 +32,28 @@ interface RunResponse {
|
|
|
32
32
|
data: unknown;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Error thrown by {@link Orthogonal.run} on a non-2xx response.
|
|
37
|
+
*
|
|
38
|
+
* Extends `Error` (so existing `catch (e) { e.message }` keeps working) but also
|
|
39
|
+
* carries the structured server payload. On a contract violation the API returns
|
|
40
|
+
* an `_orthogonal` self-correction hint (expected schema, unexpected/missing
|
|
41
|
+
* fields, numeric bounds) — exposed here as `orthogonal` so an agent can fix the
|
|
42
|
+
* request and retry instead of only seeing the terse message.
|
|
43
|
+
*/
|
|
44
|
+
declare class OrthogonalRunError extends Error {
|
|
45
|
+
/** HTTP status of the failed response. */
|
|
46
|
+
status: number;
|
|
47
|
+
/** The API's `_orthogonal` self-correction hint, when present. */
|
|
48
|
+
orthogonal?: unknown;
|
|
49
|
+
/** The full parsed response body. */
|
|
50
|
+
responseBody?: unknown;
|
|
51
|
+
constructor(message: string, opts: {
|
|
52
|
+
status: number;
|
|
53
|
+
orthogonal?: unknown;
|
|
54
|
+
responseBody?: unknown;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
35
57
|
/**
|
|
36
58
|
* Orthogonal SDK client
|
|
37
59
|
*
|
|
@@ -62,4 +84,4 @@ declare class Orthogonal {
|
|
|
62
84
|
run(options: RunOptions): Promise<RunResponse>;
|
|
63
85
|
}
|
|
64
86
|
|
|
65
|
-
export { Orthogonal, type OrthogonalConfig, type RunOptions, type RunResponse, Orthogonal as default };
|
|
87
|
+
export { Orthogonal, type OrthogonalConfig, OrthogonalRunError, type RunOptions, type RunResponse, Orthogonal as default };
|
package/dist/index.js
CHANGED
|
@@ -21,11 +21,41 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
Orthogonal: () => Orthogonal,
|
|
24
|
+
OrthogonalRunError: () => OrthogonalRunError,
|
|
24
25
|
default: () => index_default
|
|
25
26
|
});
|
|
26
27
|
module.exports = __toCommonJS(index_exports);
|
|
27
28
|
var VERSION = "0.1.0";
|
|
28
29
|
var BASE_URL = "https://api.orth.sh";
|
|
30
|
+
function formatError(raw, status) {
|
|
31
|
+
if (raw == null) return `Request failed with status ${status}`;
|
|
32
|
+
if (typeof raw === "string") {
|
|
33
|
+
return raw.length > 0 ? raw : `Request failed with status ${status}`;
|
|
34
|
+
}
|
|
35
|
+
if (typeof raw === "object") {
|
|
36
|
+
const obj = raw;
|
|
37
|
+
if (typeof obj.message === "string" && obj.message.length > 0) return obj.message;
|
|
38
|
+
if (typeof obj.error === "string" && obj.error.length > 0) return obj.error;
|
|
39
|
+
try {
|
|
40
|
+
const json = JSON.stringify(raw);
|
|
41
|
+
if (json && json !== "{}" && json !== "[]" && json !== "null") return json;
|
|
42
|
+
return `Request failed with status ${status}`;
|
|
43
|
+
} catch {
|
|
44
|
+
return `Request failed with status ${status}`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return String(raw);
|
|
48
|
+
}
|
|
49
|
+
var OrthogonalRunError = class _OrthogonalRunError extends Error {
|
|
50
|
+
constructor(message, opts) {
|
|
51
|
+
super(message);
|
|
52
|
+
this.name = "OrthogonalRunError";
|
|
53
|
+
this.status = opts.status;
|
|
54
|
+
this.orthogonal = opts.orthogonal;
|
|
55
|
+
this.responseBody = opts.responseBody;
|
|
56
|
+
Object.setPrototypeOf(this, _OrthogonalRunError.prototype);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
29
59
|
var Orthogonal = class {
|
|
30
60
|
constructor(config) {
|
|
31
61
|
if (!config.apiKey) {
|
|
@@ -52,14 +82,28 @@ var Orthogonal = class {
|
|
|
52
82
|
});
|
|
53
83
|
const data = await response.json();
|
|
54
84
|
if (!response.ok) {
|
|
85
|
+
const orthogonal = data?._orthogonal;
|
|
55
86
|
if (response.status === 401) {
|
|
56
|
-
throw new
|
|
87
|
+
throw new OrthogonalRunError(
|
|
88
|
+
"Invalid API key. Visit https://orthogonal.com to get one!",
|
|
89
|
+
{ status: 401, orthogonal, responseBody: data }
|
|
90
|
+
);
|
|
57
91
|
}
|
|
58
92
|
if (response.status === 402) {
|
|
59
|
-
|
|
93
|
+
const rawError2 = data.data?.error || data.error;
|
|
94
|
+
const hasServerMessage = rawError2 !== void 0 && rawError2 !== null && rawError2 !== "";
|
|
95
|
+
throw new OrthogonalRunError(
|
|
96
|
+
hasServerMessage ? formatError(rawError2, response.status) : "Insufficient credits. Top up your balance at https://www.orthogonal.com/dashboard/balance",
|
|
97
|
+
{ status: 402, orthogonal, responseBody: data }
|
|
98
|
+
);
|
|
60
99
|
}
|
|
61
|
-
const
|
|
62
|
-
|
|
100
|
+
const rawError = data.data?.error || data.error;
|
|
101
|
+
const errorMessage = formatError(rawError, response.status);
|
|
102
|
+
throw new OrthogonalRunError(errorMessage, {
|
|
103
|
+
status: response.status,
|
|
104
|
+
orthogonal,
|
|
105
|
+
responseBody: data
|
|
106
|
+
});
|
|
63
107
|
}
|
|
64
108
|
return data;
|
|
65
109
|
}
|
|
@@ -67,6 +111,7 @@ var Orthogonal = class {
|
|
|
67
111
|
var index_default = Orthogonal;
|
|
68
112
|
// Annotate the CommonJS export names for ESM import in node:
|
|
69
113
|
0 && (module.exports = {
|
|
70
|
-
Orthogonal
|
|
114
|
+
Orthogonal,
|
|
115
|
+
OrthogonalRunError
|
|
71
116
|
});
|
|
72
117
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { OrthogonalConfig, RunOptions, RunResponse } from \"./types\";\n\nconst VERSION = \"0.1.0\";\nconst BASE_URL = \"https://api.orth.sh\";\n\n/**\n * Orthogonal SDK client\n *\n * @example\n * ```typescript\n * import Orthogonal from \"@orth/sdk\";\n *\n * const orthogonal = new Orthogonal({\n * apiKey: process.env.ORTHOGONAL_API_KEY\n * });\n *\n * const response = await orthogonal.run({\n * api: \"andi\",\n * path: \"/api/v1/search\",\n * query: { q: \"hello world\" }\n * });\n *\n * console.log(response.data);\n * ```\n */\nexport class Orthogonal {\n private apiKey: string;\n private customHeaders: Record<string, string>;\n\n constructor(config: OrthogonalConfig) {\n if (!config.apiKey) {\n throw new Error(\"Orthogonal API key is required\");\n }\n this.apiKey = config.apiKey;\n this.customHeaders = config.headers || {};\n }\n\n /**\n * Call any API on the Orthogonal platform\n */\n async run(options: RunOptions): Promise<RunResponse> {\n const { api, path, query, body } = options;\n\n const response = await fetch(`${BASE_URL}/v1/run`, {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": `@orth/sdk/${VERSION}`,\n \"X-Orthogonal-Source\": \"sdk\",\n ...this.customHeaders,\n },\n body: JSON.stringify({ api, path, query, body }),\n });\n\n const data = await response.json();\n\n if (!response.ok) {\n // Provide helpful error messages for common cases\n if (response.status === 401) {\n throw new
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { OrthogonalConfig, RunOptions, RunResponse } from \"./types\";\n\nconst VERSION = \"0.1.0\";\nconst BASE_URL = \"https://api.orth.sh\";\n\n/**\n * Coerce an error payload into a human-readable string.\n *\n * Upstream providers surface errors as structured objects (e.g. People Data Labs\n * returns `{type: [...], message: \"...\"}`). Passing those to `new Error()` coerces\n * to `\"[object Object]\"`, which is useless to SDK consumers. We prefer the most\n * specific string field when present, and fall back to JSON so callers can still\n * see the full payload.\n */\nfunction formatError(raw: unknown, status: number): string {\n if (raw == null) return `Request failed with status ${status}`;\n if (typeof raw === \"string\") {\n return raw.length > 0 ? raw : `Request failed with status ${status}`;\n }\n if (typeof raw === \"object\") {\n const obj = raw as Record<string, unknown>;\n // Only use .message / .error if they are non-empty strings; otherwise fall\n // through to JSON.stringify so callers still see the full payload.\n if (typeof obj.message === \"string\" && obj.message.length > 0) return obj.message;\n if (typeof obj.error === \"string\" && obj.error.length > 0) return obj.error;\n try {\n const json = JSON.stringify(raw);\n // \"{}\" / \"[]\" / \"null\" aren't useful; fall back to the status message.\n if (json && json !== \"{}\" && json !== \"[]\" && json !== \"null\") return json;\n return `Request failed with status ${status}`;\n } catch {\n return `Request failed with status ${status}`;\n }\n }\n return String(raw);\n}\n\n/**\n * Error thrown by {@link Orthogonal.run} on a non-2xx response.\n *\n * Extends `Error` (so existing `catch (e) { e.message }` keeps working) but also\n * carries the structured server payload. On a contract violation the API returns\n * an `_orthogonal` self-correction hint (expected schema, unexpected/missing\n * fields, numeric bounds) — exposed here as `orthogonal` so an agent can fix the\n * request and retry instead of only seeing the terse message.\n */\nexport class OrthogonalRunError extends Error {\n /** HTTP status of the failed response. */\n status: number;\n /** The API's `_orthogonal` self-correction hint, when present. */\n orthogonal?: unknown;\n /** The full parsed response body. */\n responseBody?: unknown;\n\n constructor(\n message: string,\n opts: { status: number; orthogonal?: unknown; responseBody?: unknown },\n ) {\n super(message);\n this.name = \"OrthogonalRunError\";\n this.status = opts.status;\n this.orthogonal = opts.orthogonal;\n this.responseBody = opts.responseBody;\n // Restore the prototype chain for instanceof across the compiled target.\n Object.setPrototypeOf(this, OrthogonalRunError.prototype);\n }\n}\n\n/**\n * Orthogonal SDK client\n *\n * @example\n * ```typescript\n * import Orthogonal from \"@orth/sdk\";\n *\n * const orthogonal = new Orthogonal({\n * apiKey: process.env.ORTHOGONAL_API_KEY\n * });\n *\n * const response = await orthogonal.run({\n * api: \"andi\",\n * path: \"/api/v1/search\",\n * query: { q: \"hello world\" }\n * });\n *\n * console.log(response.data);\n * ```\n */\nexport class Orthogonal {\n private apiKey: string;\n private customHeaders: Record<string, string>;\n\n constructor(config: OrthogonalConfig) {\n if (!config.apiKey) {\n throw new Error(\"Orthogonal API key is required\");\n }\n this.apiKey = config.apiKey;\n this.customHeaders = config.headers || {};\n }\n\n /**\n * Call any API on the Orthogonal platform\n */\n async run(options: RunOptions): Promise<RunResponse> {\n const { api, path, query, body } = options;\n\n const response = await fetch(`${BASE_URL}/v1/run`, {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": `@orth/sdk/${VERSION}`,\n \"X-Orthogonal-Source\": \"sdk\",\n ...this.customHeaders,\n },\n body: JSON.stringify({ api, path, query, body }),\n });\n\n const data = await response.json();\n\n if (!response.ok) {\n const orthogonal = (data as any)?._orthogonal;\n // Provide helpful error messages for common cases\n if (response.status === 401) {\n throw new OrthogonalRunError(\n \"Invalid API key. Visit https://orthogonal.com to get one!\",\n { status: 401, orthogonal, responseBody: data },\n );\n }\n if (response.status === 402) {\n // 402 now covers several distinct cases: insufficient credits, a per-key\n // daily spend limit, and an account-wide daily spend limit. Surface the\n // server's specific message when it provided one so callers know which\n // applies. We test `rawError` directly (not formatError's output) so a\n // real message that happens to start with \"Request failed\" isn't mistaken\n // for the no-message fallback.\n const rawError = data.data?.error || data.error;\n const hasServerMessage =\n rawError !== undefined && rawError !== null && rawError !== \"\";\n throw new OrthogonalRunError(\n hasServerMessage\n ? formatError(rawError, response.status)\n : \"Insufficient credits. Top up your balance at https://www.orthogonal.com/dashboard/balance\",\n { status: 402, orthogonal, responseBody: data },\n );\n }\n // Check for nested error in data.error (e.g., from target API). Use `||`\n // (not `??`) so that falsy values like `\"\"` still fall through the chain\n // to the status-based fallback inside `formatError`.\n const rawError = data.data?.error || data.error;\n const errorMessage = formatError(rawError, response.status);\n throw new OrthogonalRunError(errorMessage, {\n status: response.status,\n orthogonal,\n responseBody: data,\n });\n }\n\n return data as RunResponse;\n }\n}\n\n// Export types\nexport * from \"./types\";\n\n// Default export\nexport default Orthogonal;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,IAAM,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;AAAA,MACR,SAAS;AAAA,QACP,eAAe,UAAU,KAAK,MAAM;AAAA,QACpC,gBAAgB;AAAA,QAChB,cAAc,aAAa,OAAO;AAAA,QAClC,uBAAuB;AAAA,QACvB,GAAG,KAAK;AAAA,MACV;AAAA,MACA,MAAM,KAAK,UAAU,EAAE,KAAK,MAAM,OAAO,KAAK,CAAC;AAAA,IACjD,CAAC;AAED,UAAM,OAAO,MAAM,SAAS,KAAK;AAEjC,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,aAAc,MAAc;AAElC,UAAI,SAAS,WAAW,KAAK;AAC3B,cAAM,IAAI;AAAA,UACR;AAAA,UACA,EAAE,QAAQ,KAAK,YAAY,cAAc,KAAK;AAAA,QAChD;AAAA,MACF;AACA,UAAI,SAAS,WAAW,KAAK;AAO3B,cAAMA,YAAW,KAAK,MAAM,SAAS,KAAK;AAC1C,cAAM,mBACJA,cAAa,UAAaA,cAAa,QAAQA,cAAa;AAC9D,cAAM,IAAI;AAAA,UACR,mBACI,YAAYA,WAAU,SAAS,MAAM,IACrC;AAAA,UACJ,EAAE,QAAQ,KAAK,YAAY,cAAc,KAAK;AAAA,QAChD;AAAA,MACF;AAIA,YAAM,WAAW,KAAK,MAAM,SAAS,KAAK;AAC1C,YAAM,eAAe,YAAY,UAAU,SAAS,MAAM;AAC1D,YAAM,IAAI,mBAAmB,cAAc;AAAA,QACzC,QAAQ,SAAS;AAAA,QACjB;AAAA,QACA,cAAc;AAAA,MAChB,CAAC;AAAA,IACH;AAEA,WAAO;AAAA,EACT;AACF;AAMA,IAAO,gBAAQ;","names":["rawError"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
var VERSION = "0.1.0";
|
|
3
3
|
var BASE_URL = "https://api.orth.sh";
|
|
4
|
+
function formatError(raw, status) {
|
|
5
|
+
if (raw == null) return `Request failed with status ${status}`;
|
|
6
|
+
if (typeof raw === "string") {
|
|
7
|
+
return raw.length > 0 ? raw : `Request failed with status ${status}`;
|
|
8
|
+
}
|
|
9
|
+
if (typeof raw === "object") {
|
|
10
|
+
const obj = raw;
|
|
11
|
+
if (typeof obj.message === "string" && obj.message.length > 0) return obj.message;
|
|
12
|
+
if (typeof obj.error === "string" && obj.error.length > 0) return obj.error;
|
|
13
|
+
try {
|
|
14
|
+
const json = JSON.stringify(raw);
|
|
15
|
+
if (json && json !== "{}" && json !== "[]" && json !== "null") return json;
|
|
16
|
+
return `Request failed with status ${status}`;
|
|
17
|
+
} catch {
|
|
18
|
+
return `Request failed with status ${status}`;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return String(raw);
|
|
22
|
+
}
|
|
23
|
+
var OrthogonalRunError = class _OrthogonalRunError extends Error {
|
|
24
|
+
constructor(message, opts) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "OrthogonalRunError";
|
|
27
|
+
this.status = opts.status;
|
|
28
|
+
this.orthogonal = opts.orthogonal;
|
|
29
|
+
this.responseBody = opts.responseBody;
|
|
30
|
+
Object.setPrototypeOf(this, _OrthogonalRunError.prototype);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
4
33
|
var Orthogonal = class {
|
|
5
34
|
constructor(config) {
|
|
6
35
|
if (!config.apiKey) {
|
|
@@ -27,14 +56,28 @@ var Orthogonal = class {
|
|
|
27
56
|
});
|
|
28
57
|
const data = await response.json();
|
|
29
58
|
if (!response.ok) {
|
|
59
|
+
const orthogonal = data?._orthogonal;
|
|
30
60
|
if (response.status === 401) {
|
|
31
|
-
throw new
|
|
61
|
+
throw new OrthogonalRunError(
|
|
62
|
+
"Invalid API key. Visit https://orthogonal.com to get one!",
|
|
63
|
+
{ status: 401, orthogonal, responseBody: data }
|
|
64
|
+
);
|
|
32
65
|
}
|
|
33
66
|
if (response.status === 402) {
|
|
34
|
-
|
|
67
|
+
const rawError2 = data.data?.error || data.error;
|
|
68
|
+
const hasServerMessage = rawError2 !== void 0 && rawError2 !== null && rawError2 !== "";
|
|
69
|
+
throw new OrthogonalRunError(
|
|
70
|
+
hasServerMessage ? formatError(rawError2, response.status) : "Insufficient credits. Top up your balance at https://www.orthogonal.com/dashboard/balance",
|
|
71
|
+
{ status: 402, orthogonal, responseBody: data }
|
|
72
|
+
);
|
|
35
73
|
}
|
|
36
|
-
const
|
|
37
|
-
|
|
74
|
+
const rawError = data.data?.error || data.error;
|
|
75
|
+
const errorMessage = formatError(rawError, response.status);
|
|
76
|
+
throw new OrthogonalRunError(errorMessage, {
|
|
77
|
+
status: response.status,
|
|
78
|
+
orthogonal,
|
|
79
|
+
responseBody: data
|
|
80
|
+
});
|
|
38
81
|
}
|
|
39
82
|
return data;
|
|
40
83
|
}
|
|
@@ -42,6 +85,7 @@ var Orthogonal = class {
|
|
|
42
85
|
var index_default = Orthogonal;
|
|
43
86
|
export {
|
|
44
87
|
Orthogonal,
|
|
88
|
+
OrthogonalRunError,
|
|
45
89
|
index_default as default
|
|
46
90
|
};
|
|
47
91
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { OrthogonalConfig, RunOptions, RunResponse } from \"./types\";\n\nconst VERSION = \"0.1.0\";\nconst BASE_URL = \"https://api.orth.sh\";\n\n/**\n * Orthogonal SDK client\n *\n * @example\n * ```typescript\n * import Orthogonal from \"@orth/sdk\";\n *\n * const orthogonal = new Orthogonal({\n * apiKey: process.env.ORTHOGONAL_API_KEY\n * });\n *\n * const response = await orthogonal.run({\n * api: \"andi\",\n * path: \"/api/v1/search\",\n * query: { q: \"hello world\" }\n * });\n *\n * console.log(response.data);\n * ```\n */\nexport class Orthogonal {\n private apiKey: string;\n private customHeaders: Record<string, string>;\n\n constructor(config: OrthogonalConfig) {\n if (!config.apiKey) {\n throw new Error(\"Orthogonal API key is required\");\n }\n this.apiKey = config.apiKey;\n this.customHeaders = config.headers || {};\n }\n\n /**\n * Call any API on the Orthogonal platform\n */\n async run(options: RunOptions): Promise<RunResponse> {\n const { api, path, query, body } = options;\n\n const response = await fetch(`${BASE_URL}/v1/run`, {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": `@orth/sdk/${VERSION}`,\n \"X-Orthogonal-Source\": \"sdk\",\n ...this.customHeaders,\n },\n body: JSON.stringify({ api, path, query, body }),\n });\n\n const data = await response.json();\n\n if (!response.ok) {\n // Provide helpful error messages for common cases\n if (response.status === 401) {\n throw new
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { OrthogonalConfig, RunOptions, RunResponse } from \"./types\";\n\nconst VERSION = \"0.1.0\";\nconst BASE_URL = \"https://api.orth.sh\";\n\n/**\n * Coerce an error payload into a human-readable string.\n *\n * Upstream providers surface errors as structured objects (e.g. People Data Labs\n * returns `{type: [...], message: \"...\"}`). Passing those to `new Error()` coerces\n * to `\"[object Object]\"`, which is useless to SDK consumers. We prefer the most\n * specific string field when present, and fall back to JSON so callers can still\n * see the full payload.\n */\nfunction formatError(raw: unknown, status: number): string {\n if (raw == null) return `Request failed with status ${status}`;\n if (typeof raw === \"string\") {\n return raw.length > 0 ? raw : `Request failed with status ${status}`;\n }\n if (typeof raw === \"object\") {\n const obj = raw as Record<string, unknown>;\n // Only use .message / .error if they are non-empty strings; otherwise fall\n // through to JSON.stringify so callers still see the full payload.\n if (typeof obj.message === \"string\" && obj.message.length > 0) return obj.message;\n if (typeof obj.error === \"string\" && obj.error.length > 0) return obj.error;\n try {\n const json = JSON.stringify(raw);\n // \"{}\" / \"[]\" / \"null\" aren't useful; fall back to the status message.\n if (json && json !== \"{}\" && json !== \"[]\" && json !== \"null\") return json;\n return `Request failed with status ${status}`;\n } catch {\n return `Request failed with status ${status}`;\n }\n }\n return String(raw);\n}\n\n/**\n * Error thrown by {@link Orthogonal.run} on a non-2xx response.\n *\n * Extends `Error` (so existing `catch (e) { e.message }` keeps working) but also\n * carries the structured server payload. On a contract violation the API returns\n * an `_orthogonal` self-correction hint (expected schema, unexpected/missing\n * fields, numeric bounds) — exposed here as `orthogonal` so an agent can fix the\n * request and retry instead of only seeing the terse message.\n */\nexport class OrthogonalRunError extends Error {\n /** HTTP status of the failed response. */\n status: number;\n /** The API's `_orthogonal` self-correction hint, when present. */\n orthogonal?: unknown;\n /** The full parsed response body. */\n responseBody?: unknown;\n\n constructor(\n message: string,\n opts: { status: number; orthogonal?: unknown; responseBody?: unknown },\n ) {\n super(message);\n this.name = \"OrthogonalRunError\";\n this.status = opts.status;\n this.orthogonal = opts.orthogonal;\n this.responseBody = opts.responseBody;\n // Restore the prototype chain for instanceof across the compiled target.\n Object.setPrototypeOf(this, OrthogonalRunError.prototype);\n }\n}\n\n/**\n * Orthogonal SDK client\n *\n * @example\n * ```typescript\n * import Orthogonal from \"@orth/sdk\";\n *\n * const orthogonal = new Orthogonal({\n * apiKey: process.env.ORTHOGONAL_API_KEY\n * });\n *\n * const response = await orthogonal.run({\n * api: \"andi\",\n * path: \"/api/v1/search\",\n * query: { q: \"hello world\" }\n * });\n *\n * console.log(response.data);\n * ```\n */\nexport class Orthogonal {\n private apiKey: string;\n private customHeaders: Record<string, string>;\n\n constructor(config: OrthogonalConfig) {\n if (!config.apiKey) {\n throw new Error(\"Orthogonal API key is required\");\n }\n this.apiKey = config.apiKey;\n this.customHeaders = config.headers || {};\n }\n\n /**\n * Call any API on the Orthogonal platform\n */\n async run(options: RunOptions): Promise<RunResponse> {\n const { api, path, query, body } = options;\n\n const response = await fetch(`${BASE_URL}/v1/run`, {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": `@orth/sdk/${VERSION}`,\n \"X-Orthogonal-Source\": \"sdk\",\n ...this.customHeaders,\n },\n body: JSON.stringify({ api, path, query, body }),\n });\n\n const data = await response.json();\n\n if (!response.ok) {\n const orthogonal = (data as any)?._orthogonal;\n // Provide helpful error messages for common cases\n if (response.status === 401) {\n throw new OrthogonalRunError(\n \"Invalid API key. Visit https://orthogonal.com to get one!\",\n { status: 401, orthogonal, responseBody: data },\n );\n }\n if (response.status === 402) {\n // 402 now covers several distinct cases: insufficient credits, a per-key\n // daily spend limit, and an account-wide daily spend limit. Surface the\n // server's specific message when it provided one so callers know which\n // applies. We test `rawError` directly (not formatError's output) so a\n // real message that happens to start with \"Request failed\" isn't mistaken\n // for the no-message fallback.\n const rawError = data.data?.error || data.error;\n const hasServerMessage =\n rawError !== undefined && rawError !== null && rawError !== \"\";\n throw new OrthogonalRunError(\n hasServerMessage\n ? formatError(rawError, response.status)\n : \"Insufficient credits. Top up your balance at https://www.orthogonal.com/dashboard/balance\",\n { status: 402, orthogonal, responseBody: data },\n );\n }\n // Check for nested error in data.error (e.g., from target API). Use `||`\n // (not `??`) so that falsy values like `\"\"` still fall through the chain\n // to the status-based fallback inside `formatError`.\n const rawError = data.data?.error || data.error;\n const errorMessage = formatError(rawError, response.status);\n throw new OrthogonalRunError(errorMessage, {\n status: response.status,\n orthogonal,\n responseBody: data,\n });\n }\n\n return data as RunResponse;\n }\n}\n\n// Export types\nexport * from \"./types\";\n\n// Default export\nexport default Orthogonal;\n"],"mappings":";AAEA,IAAM,UAAU;AAChB,IAAM,WAAW;AAWjB,SAAS,YAAY,KAAc,QAAwB;AACzD,MAAI,OAAO,KAAM,QAAO,8BAA8B,MAAM;AAC5D,MAAI,OAAO,QAAQ,UAAU;AAC3B,WAAO,IAAI,SAAS,IAAI,MAAM,8BAA8B,MAAM;AAAA,EACpE;AACA,MAAI,OAAO,QAAQ,UAAU;AAC3B,UAAM,MAAM;AAGZ,QAAI,OAAO,IAAI,YAAY,YAAY,IAAI,QAAQ,SAAS,EAAG,QAAO,IAAI;AAC1E,QAAI,OAAO,IAAI,UAAU,YAAY,IAAI,MAAM,SAAS,EAAG,QAAO,IAAI;AACtE,QAAI;AACF,YAAM,OAAO,KAAK,UAAU,GAAG;AAE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