@cargo-ai/api 1.0.42 → 1.0.44

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"params.d.ts","sourceRoot":"","sources":["../../../src/fetcher/params.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAsCH,eAAO,MAAM,eAAe,YAAa,OAAO,MAAM,EAAE,GAAG,CAAC,KAAG,MA0C9D,CAAC;AAIF;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,YAAa,MAAM,YAAY,MAAM,KAAG,MAQ9D,CAAC"}
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Serialize a query-params object into a `?a=1&b=2` string, matching Axios'
3
+ * default `paramsSerializer` so the backend keeps receiving the exact same
4
+ * wire format it did under the Axios client. The notable Axios conventions
5
+ * reproduced here:
6
+ *
7
+ * - `null` / `undefined` values are omitted.
8
+ * - A top-level array uses the bracket form: `ids=[1,2]` → `ids[]=1&ids[]=2`.
9
+ * - Arrays nested inside an object are indexed:
10
+ * `filter={tags:[a,b]}` → `filter[tags][0]=a&filter[tags][1]=b`.
11
+ * - Nested objects use bracket paths: `filter={x:1}` → `filter[x]=1`.
12
+ *
13
+ * Encoding matches Axios' `AxiosURLSearchParams` byte-for-byte: `encodeURIComponent`
14
+ * then un-escape the characters Axios leaves literal (`:`, `$`, `,`) and render
15
+ * space as `+`. Brackets stay percent-encoded (`%5B`/`%5D`), as Axios does for
16
+ * object/array params. (Plain `URLSearchParams` diverges here — it encodes `:`
17
+ * and `,` — so we encode by hand.)
18
+ */
19
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
+ const axiosEncode = (value) => encodeURIComponent(String(value))
21
+ .replace(/%3A/gi, ":")
22
+ .replace(/%24/g, "$")
23
+ .replace(/%2C/gi, ",")
24
+ .replace(/%20/g, "+");
25
+ const buildNested = (path, value, pairs) => {
26
+ if (value === undefined || value === null) {
27
+ return;
28
+ }
29
+ if (Array.isArray(value)) {
30
+ // Nested arrays are indexed, matching Axios.
31
+ value.forEach((item, index) => {
32
+ buildNested(`${path}[${index}]`, item, pairs);
33
+ });
34
+ return;
35
+ }
36
+ if (typeof value === "object") {
37
+ for (const [key, nested] of Object.entries(value)) {
38
+ buildNested(`${path}[${key}]`, nested, pairs);
39
+ }
40
+ return;
41
+ }
42
+ pairs.push([path, String(value)]);
43
+ };
44
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
+ export const serializeParams = (params) => {
46
+ if (params === undefined) {
47
+ return "";
48
+ }
49
+ const pairs = [];
50
+ for (const [key, value] of Object.entries(params)) {
51
+ if (value === undefined || value === null) {
52
+ continue;
53
+ }
54
+ if (Array.isArray(value)) {
55
+ // A top-level array uses the empty-bracket form `key[]`, not an index.
56
+ value.forEach((item) => {
57
+ if (item === undefined || item === null) {
58
+ return;
59
+ }
60
+ if (typeof item === "object") {
61
+ buildNested(`${key}[]`, item, pairs);
62
+ }
63
+ else {
64
+ pairs.push([`${key}[]`, String(item)]);
65
+ }
66
+ });
67
+ continue;
68
+ }
69
+ if (typeof value === "object") {
70
+ for (const [nestedKey, nested] of Object.entries(value)) {
71
+ buildNested(`${key}[${nestedKey}]`, nested, pairs);
72
+ }
73
+ continue;
74
+ }
75
+ pairs.push([key, String(value)]);
76
+ }
77
+ const serialized = pairs
78
+ .map(([key, value]) => `${axiosEncode(key)}=${axiosEncode(value)}`)
79
+ .join("&");
80
+ return serialized.length > 0 ? `?${serialized}` : "";
81
+ };
82
+ const ABSOLUTE_URL = /^([a-z][a-z\d+\-.]*?:)?\/\//i;
83
+ /**
84
+ * Join a base URL and an endpoint the way Axios' `combineURLs` did: exactly one
85
+ * slash between them regardless of whether the endpoint has a leading slash.
86
+ * Most endpoints are relative (`connection/connectors`), a few are rooted
87
+ * (`/initWorkspace`) — both must yield `…/v1/<endpoint>`. Naive concatenation
88
+ * produced `…/v1connection/...`. Absolute endpoints pass through untouched,
89
+ * matching Axios.
90
+ */
91
+ export const combineUrl = (baseUrl, endpoint) => {
92
+ if (ABSOLUTE_URL.test(endpoint)) {
93
+ return endpoint;
94
+ }
95
+ const base = baseUrl.replace(/\/+$/, "");
96
+ const relative = endpoint.replace(/^\/+/, "");
97
+ return relative.length > 0 ? `${base}/${relative}` : base;
98
+ };
@@ -0,0 +1,29 @@
1
+ import type { FetchLike } from "./client.js";
2
+ /**
3
+ * A body-carrying request (multipart POST or binary PUT) with optional upload
4
+ * progress. Two transports back it:
5
+ *
6
+ * - `XMLHttpRequest` when available (browsers). It's the only web API that
7
+ * reports *upload* progress — `fetch` cannot — so the file-upload progress
8
+ * bars keep working. `XMLHttpRequest` is referenced as a runtime global
9
+ * (never imported), so it doesn't affect edge/Node bundles.
10
+ * - `fetch` otherwise (Node/CLI, edge). No incremental upload progress; the
11
+ * caller reports coarse per-request progress instead.
12
+ */
13
+ export type ProgressSendRequest = {
14
+ method: "POST" | "PUT";
15
+ url: string;
16
+ headers: Record<string, string>;
17
+ body: FormData | Blob;
18
+ onProgress?: (loaded: number, total: number) => void;
19
+ fetchImpl: FetchLike;
20
+ };
21
+ export type ProgressSendResponse = {
22
+ ok: boolean;
23
+ status: number;
24
+ statusText: string;
25
+ getHeader: (name: string) => string | null;
26
+ text: () => Promise<string>;
27
+ };
28
+ export declare const sendWithProgress: (request: ProgressSendRequest) => Promise<ProgressSendResponse>;
29
+ //# sourceMappingURL=send.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send.d.ts","sourceRoot":"","sources":["../../../src/fetcher/send.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C;;;;;;;;;;GAUG;AAEH,MAAM,MAAM,mBAAmB,GAAG;IAChC,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,IAAI,EAAE,QAAQ,GAAG,IAAI,CAAC;IACtB,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACrD,SAAS,EAAE,SAAS,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;IAC3C,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;CAC7B,CAAC;AAyFF,eAAO,MAAM,gBAAgB,YAClB,mBAAmB,KAC3B,QAAQ,oBAAoB,CAGJ,CAAC"}
@@ -0,0 +1,73 @@
1
+ // Per-request budget, matching the fetcher's `fetch`/`fetchBlob` timeout (and
2
+ // Axios' old `timeout: 360 * 1000`). Applied per upload POST / per S3 part PUT,
3
+ // so a stalled connection aborts instead of hanging the upload forever.
4
+ const REQUEST_TIMEOUT_MS = 360 * 1000;
5
+ const canUseXhr = () => typeof XMLHttpRequest !== "undefined";
6
+ const sendViaXhr = (request) => {
7
+ const { method, url, headers, body, onProgress } = request;
8
+ return new Promise((resolve, reject) => {
9
+ const xhr = new XMLHttpRequest();
10
+ xhr.open(method, url);
11
+ xhr.timeout = REQUEST_TIMEOUT_MS;
12
+ for (const [key, value] of Object.entries(headers)) {
13
+ xhr.setRequestHeader(key, value);
14
+ }
15
+ if (onProgress !== undefined) {
16
+ xhr.upload.onprogress = (event) => {
17
+ if (event.lengthComputable) {
18
+ onProgress(event.loaded, event.total);
19
+ }
20
+ };
21
+ }
22
+ xhr.onload = () => {
23
+ resolve({
24
+ ok: xhr.status >= 200 && xhr.status < 300,
25
+ status: xhr.status,
26
+ statusText: xhr.statusText,
27
+ getHeader: (name) => xhr.getResponseHeader(name),
28
+ text: () => Promise.resolve(xhr.responseText),
29
+ });
30
+ };
31
+ xhr.onerror = () => reject(new Error("Network error"));
32
+ xhr.onabort = () => reject(new Error("Upload aborted"));
33
+ xhr.ontimeout = () => reject(new Error("Upload timed out"));
34
+ xhr.send(body);
35
+ });
36
+ };
37
+ const sendViaFetch = async (request) => {
38
+ const { method, url, headers, body, onProgress, fetchImpl } = request;
39
+ // `fetch` has no timeout option — enforce the same per-request budget the
40
+ // XHR path gets (and Axios had) so a stalled upload aborts instead of hanging.
41
+ const controller = new AbortController();
42
+ const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
43
+ let response;
44
+ try {
45
+ response = await fetchImpl(url, {
46
+ method,
47
+ headers,
48
+ body,
49
+ signal: controller.signal,
50
+ });
51
+ }
52
+ finally {
53
+ clearTimeout(timeout);
54
+ }
55
+ // `fetch` gives us no upload progress, so surface a single 100% completion
56
+ // tick once the request is sent and a response is in hand. A `FormData` body
57
+ // has no readable size (and a Blob may be empty), so use a non-zero sentinel
58
+ // when needed — reporting `(0, 0)` would leave callers stuck at 0%.
59
+ if (onProgress !== undefined) {
60
+ const total = body instanceof Blob && body.size > 0 ? body.size : 1;
61
+ onProgress(total, total);
62
+ }
63
+ return {
64
+ ok: response.ok,
65
+ status: response.status,
66
+ statusText: response.statusText,
67
+ getHeader: (name) => response.headers.get(name),
68
+ text: () => response.text(),
69
+ };
70
+ };
71
+ export const sendWithProgress = (request) => canUseXhr() && request.onProgress !== undefined
72
+ ? sendViaXhr(request)
73
+ : sendViaFetch(request);