@captapi/sdk 0.1.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 ADDED
Binary file
package/dist/core.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ /** Shared HTTP core for the generated Captapi SDK. */
2
+ export declare const DEFAULT_BASE_URL = "https://api.captapi.com";
3
+ /** Standard Captapi response envelope: `{ success, data }`. */
4
+ export interface ApiEnvelope<T = unknown> {
5
+ success: boolean;
6
+ data: T;
7
+ [key: string]: unknown;
8
+ }
9
+ export interface CaptapiOptions {
10
+ /** API key. Falls back to the CAPTAPI_API_KEY environment variable. */
11
+ apiKey?: string;
12
+ /** Override the API origin (default https://api.captapi.com). */
13
+ baseUrl?: string;
14
+ /** Per-request timeout in milliseconds (default 120000). */
15
+ timeoutMs?: number;
16
+ /** Custom fetch implementation (defaults to globalThis.fetch). */
17
+ fetch?: typeof globalThis.fetch;
18
+ }
19
+ /** Raised for every non-2xx response. Never fails silently. */
20
+ export declare class CaptapiError extends Error {
21
+ readonly status: number;
22
+ readonly code?: string;
23
+ readonly detail?: unknown;
24
+ constructor(message: string, status: number, code?: string, detail?: unknown);
25
+ }
26
+ export declare class HttpCore {
27
+ private readonly apiKey;
28
+ private readonly baseUrl;
29
+ private readonly timeoutMs;
30
+ private readonly fetchImpl;
31
+ constructor(options?: CaptapiOptions);
32
+ get(path: string, params?: object): Promise<ApiEnvelope>;
33
+ }
package/dist/core.js ADDED
@@ -0,0 +1,67 @@
1
+ /** Shared HTTP core for the generated Captapi SDK. */
2
+ export const DEFAULT_BASE_URL = "https://api.captapi.com";
3
+ /** Raised for every non-2xx response. Never fails silently. */
4
+ export class CaptapiError extends Error {
5
+ status;
6
+ code;
7
+ detail;
8
+ constructor(message, status, code, detail) {
9
+ super(message);
10
+ this.name = "CaptapiError";
11
+ this.status = status;
12
+ this.code = code;
13
+ this.detail = detail;
14
+ }
15
+ }
16
+ export class HttpCore {
17
+ apiKey;
18
+ baseUrl;
19
+ timeoutMs;
20
+ fetchImpl;
21
+ constructor(options = {}) {
22
+ const key = options.apiKey ??
23
+ (typeof process !== "undefined" ? process.env?.CAPTAPI_API_KEY : undefined);
24
+ if (!key) {
25
+ throw new CaptapiError("Missing API key. Pass { apiKey } or set CAPTAPI_API_KEY. Get one at https://captapi.com/dashboard/api-keys", 401, "missing_api_key");
26
+ }
27
+ this.apiKey = key;
28
+ this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
29
+ this.timeoutMs = options.timeoutMs ?? 120_000;
30
+ this.fetchImpl = options.fetch ?? globalThis.fetch;
31
+ }
32
+ async get(path, params = {}) {
33
+ const url = new URL(this.baseUrl + path);
34
+ for (const [k, v] of Object.entries(params)) {
35
+ if (v !== undefined && v !== null)
36
+ url.searchParams.set(k, String(v));
37
+ }
38
+ let res;
39
+ try {
40
+ res = await this.fetchImpl(url, {
41
+ headers: { authorization: `Bearer ${this.apiKey}`, accept: "application/json" },
42
+ signal: AbortSignal.timeout(this.timeoutMs),
43
+ });
44
+ }
45
+ catch (err) {
46
+ const message = err instanceof Error ? err.message : String(err);
47
+ throw new CaptapiError(`Request to ${path} failed: ${message}`, 0, "network_error", err);
48
+ }
49
+ let body;
50
+ try {
51
+ body = await res.json();
52
+ }
53
+ catch {
54
+ body = undefined;
55
+ }
56
+ if (!res.ok || body?.success === false) {
57
+ const detail = body?.detail ?? body?.error ?? body;
58
+ const code = typeof body?.detail === "object" && body?.detail?.error
59
+ ? String(body.detail.error)
60
+ : typeof body?.error === "string"
61
+ ? body.error
62
+ : undefined;
63
+ throw new CaptapiError(`Captapi ${path} returned ${res.status}: ${typeof detail === "string" ? detail : JSON.stringify(detail ?? "unknown error")}`, res.status, code, detail);
64
+ }
65
+ return body;
66
+ }
67
+ }