@captapi/sdk 0.1.0 → 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 +0 -0
- package/dist/core.d.ts +8 -0
- package/dist/core.js +102 -23
- package/dist/generated.d.ts +1412 -672
- package/dist/generated.js +352 -304
- package/package.json +51 -51
package/README.md
CHANGED
|
Binary file
|
package/dist/core.d.ts
CHANGED
|
@@ -29,5 +29,13 @@ export declare class HttpCore {
|
|
|
29
29
|
private readonly timeoutMs;
|
|
30
30
|
private readonly fetchImpl;
|
|
31
31
|
constructor(options?: CaptapiOptions);
|
|
32
|
+
private fail;
|
|
33
|
+
private parseBody;
|
|
34
|
+
private send;
|
|
32
35
|
get(path: string, params?: object): Promise<ApiEnvelope>;
|
|
36
|
+
/**
|
|
37
|
+
* POST multipart/form-data. ``file`` may be a local path (Node), Blob, or File.
|
|
38
|
+
* Other params become additional form fields.
|
|
39
|
+
*/
|
|
40
|
+
postMultipart(path: string, params?: object): Promise<ApiEnvelope>;
|
|
33
41
|
}
|
package/dist/core.js
CHANGED
|
@@ -13,6 +13,27 @@ export class CaptapiError extends Error {
|
|
|
13
13
|
this.detail = detail;
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
+
const RETRY_STATUSES = new Set([502, 503]);
|
|
17
|
+
const RETRY_DELAY_MS = 500;
|
|
18
|
+
function errorBag(body) {
|
|
19
|
+
if (!body || typeof body !== "object")
|
|
20
|
+
return null;
|
|
21
|
+
const root = body;
|
|
22
|
+
if (root.error && typeof root.error === "object") {
|
|
23
|
+
return root.error;
|
|
24
|
+
}
|
|
25
|
+
if (root.detail && typeof root.detail === "object") {
|
|
26
|
+
return root.detail;
|
|
27
|
+
}
|
|
28
|
+
return root;
|
|
29
|
+
}
|
|
30
|
+
function shouldRetry(status, body) {
|
|
31
|
+
const bag = errorBag(body);
|
|
32
|
+
if (bag && typeof bag.retryable === "boolean") {
|
|
33
|
+
return bag.retryable === true;
|
|
34
|
+
}
|
|
35
|
+
return RETRY_STATUSES.has(status);
|
|
36
|
+
}
|
|
16
37
|
export class HttpCore {
|
|
17
38
|
apiKey;
|
|
18
39
|
baseUrl;
|
|
@@ -29,39 +50,97 @@ export class HttpCore {
|
|
|
29
50
|
this.timeoutMs = options.timeoutMs ?? 120_000;
|
|
30
51
|
this.fetchImpl = options.fetch ?? globalThis.fetch;
|
|
31
52
|
}
|
|
53
|
+
fail(path, status, body) {
|
|
54
|
+
const bag = errorBag(body);
|
|
55
|
+
const detail = bag ?? body?.detail ?? body?.error ?? body;
|
|
56
|
+
const code = typeof bag?.code === "string"
|
|
57
|
+
? bag.code
|
|
58
|
+
: typeof body?.error === "string"
|
|
59
|
+
? body.error
|
|
60
|
+
: undefined;
|
|
61
|
+
const message = typeof bag?.message === "string"
|
|
62
|
+
? bag.message
|
|
63
|
+
: typeof detail === "string"
|
|
64
|
+
? detail
|
|
65
|
+
: JSON.stringify(detail ?? "unknown error");
|
|
66
|
+
return new CaptapiError(`Captapi ${path} returned ${status}: ${message}`, status, code, detail);
|
|
67
|
+
}
|
|
68
|
+
async parseBody(res) {
|
|
69
|
+
try {
|
|
70
|
+
return await res.json();
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async send(path, init) {
|
|
77
|
+
let last;
|
|
78
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
79
|
+
let res;
|
|
80
|
+
try {
|
|
81
|
+
res = await this.fetchImpl(this.baseUrl + path, {
|
|
82
|
+
...init,
|
|
83
|
+
headers: {
|
|
84
|
+
authorization: `Bearer ${this.apiKey}`,
|
|
85
|
+
accept: "application/json",
|
|
86
|
+
...(init.headers ?? {}),
|
|
87
|
+
},
|
|
88
|
+
signal: AbortSignal.timeout(this.timeoutMs),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
93
|
+
throw new CaptapiError(`Request to ${path} failed: ${message}`, 0, "network_error", err);
|
|
94
|
+
}
|
|
95
|
+
const body = await this.parseBody(res);
|
|
96
|
+
if (res.ok && body?.success !== false)
|
|
97
|
+
return body;
|
|
98
|
+
last = this.fail(path, res.status, body);
|
|
99
|
+
if (attempt === 0 && shouldRetry(res.status, body)) {
|
|
100
|
+
await new Promise((r) => setTimeout(r, RETRY_DELAY_MS));
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
throw last;
|
|
104
|
+
}
|
|
105
|
+
throw last ?? new CaptapiError(`Request to ${path} failed`, 0, "network_error");
|
|
106
|
+
}
|
|
32
107
|
async get(path, params = {}) {
|
|
33
108
|
const url = new URL(this.baseUrl + path);
|
|
34
109
|
for (const [k, v] of Object.entries(params)) {
|
|
35
110
|
if (v !== undefined && v !== null)
|
|
36
111
|
url.searchParams.set(k, String(v));
|
|
37
112
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
113
|
+
return this.send(url.pathname + url.search, { method: "GET" });
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* POST multipart/form-data. ``file`` may be a local path (Node), Blob, or File.
|
|
117
|
+
* Other params become additional form fields.
|
|
118
|
+
*/
|
|
119
|
+
async postMultipart(path, params = {}) {
|
|
120
|
+
const form = new FormData();
|
|
121
|
+
const entries = params;
|
|
122
|
+
const fileVal = entries.file;
|
|
123
|
+
if (fileVal == null) {
|
|
124
|
+
throw new CaptapiError("Missing required form field: file", 400, "missing_file");
|
|
44
125
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
126
|
+
if (typeof fileVal === "string") {
|
|
127
|
+
// Node: treat string as a filesystem path.
|
|
128
|
+
const fs = await import("node:fs");
|
|
129
|
+
const pathMod = await import("node:path");
|
|
130
|
+
const buf = fs.readFileSync(fileVal);
|
|
131
|
+
form.append("file", new Blob([buf]), pathMod.basename(fileVal));
|
|
48
132
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
body = await res.json();
|
|
133
|
+
else if (typeof Blob !== "undefined" && fileVal instanceof Blob) {
|
|
134
|
+
form.append("file", fileVal, fileVal.name || "upload.bin");
|
|
52
135
|
}
|
|
53
|
-
|
|
54
|
-
|
|
136
|
+
else {
|
|
137
|
+
form.append("file", fileVal);
|
|
55
138
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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);
|
|
139
|
+
for (const [k, v] of Object.entries(entries)) {
|
|
140
|
+
if (k === "file" || v === undefined || v === null)
|
|
141
|
+
continue;
|
|
142
|
+
form.append(k, String(v));
|
|
64
143
|
}
|
|
65
|
-
return body;
|
|
144
|
+
return this.send(path, { method: "POST", body: form });
|
|
66
145
|
}
|
|
67
146
|
}
|