@credal/sdk 0.1.6 → 0.1.7
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/cjs/Client.js +2 -2
- package/dist/cjs/core/auth/index.d.ts +2 -2
- package/dist/cjs/core/fetcher/EndpointMetadata.d.ts +13 -0
- package/dist/cjs/core/fetcher/EndpointMetadata.js +2 -0
- package/dist/cjs/core/fetcher/EndpointSupplier.d.ts +12 -0
- package/dist/cjs/core/fetcher/EndpointSupplier.js +22 -0
- package/dist/cjs/core/fetcher/Fetcher.d.ts +4 -2
- package/dist/cjs/core/fetcher/Fetcher.js +3 -2
- package/dist/cjs/core/fetcher/index.d.ts +2 -0
- package/dist/cjs/core/fetcher/index.js +3 -1
- package/dist/cjs/core/fetcher/requestWithRetries.js +20 -16
- package/dist/cjs/core/headers.d.ts +2 -3
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/Client.mjs +2 -2
- package/dist/esm/core/auth/index.d.mts +2 -2
- package/dist/esm/core/fetcher/EndpointMetadata.d.mts +13 -0
- package/dist/esm/core/fetcher/EndpointMetadata.mjs +1 -0
- package/dist/esm/core/fetcher/EndpointSupplier.d.mts +12 -0
- package/dist/esm/core/fetcher/EndpointSupplier.mjs +19 -0
- package/dist/esm/core/fetcher/Fetcher.d.mts +4 -2
- package/dist/esm/core/fetcher/Fetcher.mjs +3 -2
- package/dist/esm/core/fetcher/index.d.mts +2 -0
- package/dist/esm/core/fetcher/index.mjs +1 -0
- package/dist/esm/core/fetcher/requestWithRetries.mjs +20 -16
- package/dist/esm/core/headers.d.mts +2 -3
- package/dist/esm/version.d.mts +1 -1
- package/dist/esm/version.mjs +1 -1
- package/package.json +8 -13
package/dist/cjs/Client.js
CHANGED
|
@@ -49,8 +49,8 @@ class CredalClient {
|
|
|
49
49
|
this._options = Object.assign(Object.assign({}, _options), { headers: (0, headers_js_1.mergeHeaders)({
|
|
50
50
|
"X-Fern-Language": "JavaScript",
|
|
51
51
|
"X-Fern-SDK-Name": "@credal/sdk",
|
|
52
|
-
"X-Fern-SDK-Version": "0.1.
|
|
53
|
-
"User-Agent": "@credal/sdk/0.1.
|
|
52
|
+
"X-Fern-SDK-Version": "0.1.7",
|
|
53
|
+
"User-Agent": "@credal/sdk/0.1.7",
|
|
54
54
|
"X-Fern-Runtime": core.RUNTIME.type,
|
|
55
55
|
"X-Fern-Runtime-Version": core.RUNTIME.version,
|
|
56
56
|
}, _options === null || _options === void 0 ? void 0 : _options.headers) });
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { AuthProvider } from "./AuthProvider.js";
|
|
2
|
-
export {
|
|
1
|
+
export type { AuthProvider } from "./AuthProvider.js";
|
|
2
|
+
export type { AuthRequest } from "./AuthRequest.js";
|
|
3
3
|
export { BasicAuth } from "./BasicAuth.js";
|
|
4
4
|
export { BearerToken } from "./BearerToken.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type SecuritySchemeKey = string;
|
|
2
|
+
/**
|
|
3
|
+
* A collection of security schemes, where the key is the name of the security scheme and the value is the list of scopes required for that scheme.
|
|
4
|
+
* All schemes in the collection must be satisfied for authentication to be successful.
|
|
5
|
+
*/
|
|
6
|
+
export type SecuritySchemeCollection = Record<SecuritySchemeKey, AuthScope[]>;
|
|
7
|
+
export type AuthScope = string;
|
|
8
|
+
export type EndpointMetadata = {
|
|
9
|
+
/**
|
|
10
|
+
* An array of security scheme collections. Each collection represents an alternative way to authenticate.
|
|
11
|
+
*/
|
|
12
|
+
security?: SecuritySchemeCollection[];
|
|
13
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EndpointMetadata } from "./EndpointMetadata.js";
|
|
2
|
+
import { Supplier } from "./Supplier.js";
|
|
3
|
+
type EndpointSupplierFn<T> = (arg: {
|
|
4
|
+
endpointMetadata: EndpointMetadata;
|
|
5
|
+
}) => T | Promise<T>;
|
|
6
|
+
export type EndpointSupplier<T> = Supplier<T> | EndpointSupplierFn<T>;
|
|
7
|
+
export declare const EndpointSupplier: {
|
|
8
|
+
get: <T>(supplier: EndpointSupplier<T>, arg: {
|
|
9
|
+
endpointMetadata: EndpointMetadata;
|
|
10
|
+
}) => Promise<T>;
|
|
11
|
+
};
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.EndpointSupplier = void 0;
|
|
13
|
+
exports.EndpointSupplier = {
|
|
14
|
+
get: (supplier, arg) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
if (typeof supplier === "function") {
|
|
16
|
+
return supplier(arg);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return supplier;
|
|
20
|
+
}
|
|
21
|
+
}),
|
|
22
|
+
};
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { APIResponse } from "./APIResponse.js";
|
|
2
|
-
import {
|
|
2
|
+
import { EndpointMetadata } from "./EndpointMetadata.js";
|
|
3
|
+
import { EndpointSupplier } from "./EndpointSupplier.js";
|
|
3
4
|
export type FetchFunction = <R = unknown>(args: Fetcher.Args) => Promise<APIResponse<R, Fetcher.Error>>;
|
|
4
5
|
export declare namespace Fetcher {
|
|
5
6
|
interface Args {
|
|
6
7
|
url: string;
|
|
7
8
|
method: string;
|
|
8
9
|
contentType?: string;
|
|
9
|
-
headers?: Record<string, string |
|
|
10
|
+
headers?: Record<string, string | EndpointSupplier<string | null | undefined> | null | undefined>;
|
|
10
11
|
queryParameters?: Record<string, unknown>;
|
|
11
12
|
body?: unknown;
|
|
12
13
|
timeoutMs?: number;
|
|
@@ -16,6 +17,7 @@ export declare namespace Fetcher {
|
|
|
16
17
|
requestType?: "json" | "file" | "bytes";
|
|
17
18
|
responseType?: "json" | "blob" | "sse" | "streaming" | "text" | "arrayBuffer" | "binary-response";
|
|
18
19
|
duplex?: "half";
|
|
20
|
+
endpointMetadata?: EndpointMetadata;
|
|
19
21
|
}
|
|
20
22
|
type Error = FailedStatusCodeError | NonJsonError | TimeoutError | UnknownError;
|
|
21
23
|
interface FailedStatusCodeError {
|
|
@@ -13,6 +13,7 @@ exports.fetcher = void 0;
|
|
|
13
13
|
exports.fetcherImpl = fetcherImpl;
|
|
14
14
|
const json_js_1 = require("../json.js");
|
|
15
15
|
const createRequestUrl_js_1 = require("./createRequestUrl.js");
|
|
16
|
+
const EndpointSupplier_js_1 = require("./EndpointSupplier.js");
|
|
16
17
|
const getErrorResponseBody_js_1 = require("./getErrorResponseBody.js");
|
|
17
18
|
const getFetchFn_js_1 = require("./getFetchFn.js");
|
|
18
19
|
const getRequestBody_js_1 = require("./getRequestBody.js");
|
|
@@ -20,9 +21,9 @@ const getResponseBody_js_1 = require("./getResponseBody.js");
|
|
|
20
21
|
const makeRequest_js_1 = require("./makeRequest.js");
|
|
21
22
|
const RawResponse_js_1 = require("./RawResponse.js");
|
|
22
23
|
const requestWithRetries_js_1 = require("./requestWithRetries.js");
|
|
23
|
-
const Supplier_js_1 = require("./Supplier.js");
|
|
24
24
|
function getHeaders(args) {
|
|
25
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
var _a;
|
|
26
27
|
const newHeaders = {};
|
|
27
28
|
if (args.body !== undefined && args.contentType != null) {
|
|
28
29
|
newHeaders["Content-Type"] = args.contentType;
|
|
@@ -31,7 +32,7 @@ function getHeaders(args) {
|
|
|
31
32
|
return newHeaders;
|
|
32
33
|
}
|
|
33
34
|
for (const [key, value] of Object.entries(args.headers)) {
|
|
34
|
-
const result = yield
|
|
35
|
+
const result = yield EndpointSupplier_js_1.EndpointSupplier.get(value, { endpointMetadata: (_a = args.endpointMetadata) !== null && _a !== void 0 ? _a : {} });
|
|
35
36
|
if (typeof result === "string") {
|
|
36
37
|
newHeaders[key] = result;
|
|
37
38
|
continue;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export type { APIResponse } from "./APIResponse.js";
|
|
2
2
|
export type { BinaryResponse } from "./BinaryResponse.js";
|
|
3
|
+
export type { EndpointMetadata } from "./EndpointMetadata.js";
|
|
4
|
+
export { EndpointSupplier } from "./EndpointSupplier.js";
|
|
3
5
|
export type { Fetcher, FetchFunction } from "./Fetcher.js";
|
|
4
6
|
export { fetcher } from "./Fetcher.js";
|
|
5
7
|
export { getHeader } from "./getHeader.js";
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Supplier = exports.unknownRawResponse = exports.toRawResponse = exports.abortRawResponse = exports.HttpResponsePromise = exports.getHeader = exports.fetcher = void 0;
|
|
3
|
+
exports.Supplier = exports.unknownRawResponse = exports.toRawResponse = exports.abortRawResponse = exports.HttpResponsePromise = exports.getHeader = exports.fetcher = exports.EndpointSupplier = void 0;
|
|
4
|
+
var EndpointSupplier_js_1 = require("./EndpointSupplier.js");
|
|
5
|
+
Object.defineProperty(exports, "EndpointSupplier", { enumerable: true, get: function () { return EndpointSupplier_js_1.EndpointSupplier; } });
|
|
4
6
|
var Fetcher_js_1 = require("./Fetcher.js");
|
|
5
7
|
Object.defineProperty(exports, "fetcher", { enumerable: true, get: function () { return Fetcher_js_1.fetcher; } });
|
|
6
8
|
var getHeader_js_1 = require("./getHeader.js");
|
|
@@ -14,29 +14,35 @@ const INITIAL_RETRY_DELAY = 1000; // in milliseconds
|
|
|
14
14
|
const MAX_RETRY_DELAY = 60000; // in milliseconds
|
|
15
15
|
const DEFAULT_MAX_RETRIES = 2;
|
|
16
16
|
const JITTER_FACTOR = 0.2; // 20% random jitter
|
|
17
|
-
function
|
|
18
|
-
// Generate a random value between
|
|
19
|
-
const jitterMultiplier = 1 +
|
|
17
|
+
function addPositiveJitter(delay) {
|
|
18
|
+
// Generate a random value between 0 and +JITTER_FACTOR
|
|
19
|
+
const jitterMultiplier = 1 + Math.random() * JITTER_FACTOR;
|
|
20
|
+
return delay * jitterMultiplier;
|
|
21
|
+
}
|
|
22
|
+
function addSymmetricJitter(delay) {
|
|
23
|
+
// Generate a random value in a JITTER_FACTOR-sized percentage range around delay
|
|
24
|
+
const jitterMultiplier = 1 + (Math.random() - 0.5) * JITTER_FACTOR;
|
|
20
25
|
return delay * jitterMultiplier;
|
|
21
26
|
}
|
|
22
27
|
function getRetryDelayFromHeaders(response, retryAttempt) {
|
|
23
|
-
// Check for Retry-After header first (RFC 7231)
|
|
28
|
+
// Check for Retry-After header first (RFC 7231), with no jitter
|
|
24
29
|
const retryAfter = response.headers.get("Retry-After");
|
|
25
30
|
if (retryAfter) {
|
|
26
31
|
// Parse as number of seconds...
|
|
27
32
|
const retryAfterSeconds = parseInt(retryAfter, 10);
|
|
28
|
-
if (!isNaN(retryAfterSeconds)) {
|
|
29
|
-
// Convert seconds to milliseconds and cap at MAX_RETRY_DELAY
|
|
33
|
+
if (!isNaN(retryAfterSeconds) && retryAfterSeconds > 0) {
|
|
30
34
|
return Math.min(retryAfterSeconds * 1000, MAX_RETRY_DELAY);
|
|
31
35
|
}
|
|
32
36
|
// ...or as an HTTP date; both are valid
|
|
33
37
|
const retryAfterDate = new Date(retryAfter);
|
|
34
38
|
if (!isNaN(retryAfterDate.getTime())) {
|
|
35
39
|
const delay = retryAfterDate.getTime() - Date.now();
|
|
36
|
-
|
|
40
|
+
if (delay > 0) {
|
|
41
|
+
return Math.min(Math.max(delay, 0), MAX_RETRY_DELAY);
|
|
42
|
+
}
|
|
37
43
|
}
|
|
38
44
|
}
|
|
39
|
-
// Then check for industry-standard X-RateLimit-Reset header
|
|
45
|
+
// Then check for industry-standard X-RateLimit-Reset header, with positive jitter
|
|
40
46
|
const rateLimitReset = response.headers.get("X-RateLimit-Reset");
|
|
41
47
|
if (rateLimitReset) {
|
|
42
48
|
const resetTime = parseInt(rateLimitReset, 10);
|
|
@@ -44,23 +50,21 @@ function getRetryDelayFromHeaders(response, retryAttempt) {
|
|
|
44
50
|
// Assume Unix timestamp in epoch seconds
|
|
45
51
|
const delay = resetTime * 1000 - Date.now();
|
|
46
52
|
if (delay > 0) {
|
|
47
|
-
return Math.min(delay, MAX_RETRY_DELAY);
|
|
53
|
+
return addPositiveJitter(Math.min(delay, MAX_RETRY_DELAY));
|
|
48
54
|
}
|
|
49
55
|
}
|
|
50
56
|
}
|
|
51
|
-
// Fall back to exponential backoff
|
|
52
|
-
return Math.min(INITIAL_RETRY_DELAY * Math.pow(2, retryAttempt), MAX_RETRY_DELAY);
|
|
57
|
+
// Fall back to exponential backoff, with symmetric jitter
|
|
58
|
+
return addSymmetricJitter(Math.min(INITIAL_RETRY_DELAY * Math.pow(2, retryAttempt), MAX_RETRY_DELAY));
|
|
53
59
|
}
|
|
54
60
|
function requestWithRetries(requestFn_1) {
|
|
55
61
|
return __awaiter(this, arguments, void 0, function* (requestFn, maxRetries = DEFAULT_MAX_RETRIES) {
|
|
56
62
|
let response = yield requestFn();
|
|
57
63
|
for (let i = 0; i < maxRetries; ++i) {
|
|
58
64
|
if ([408, 429].includes(response.status) || response.status >= 500) {
|
|
59
|
-
// Get delay
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
const delayWithJitter = addJitter(baseDelay);
|
|
63
|
-
yield new Promise((resolve) => setTimeout(resolve, delayWithJitter));
|
|
65
|
+
// Get delay with appropriate jitter applied
|
|
66
|
+
const delay = getRetryDelayFromHeaders(response, i);
|
|
67
|
+
yield new Promise((resolve) => setTimeout(resolve, delay));
|
|
64
68
|
response = yield requestFn();
|
|
65
69
|
}
|
|
66
70
|
else {
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function mergeOnlyDefinedHeaders(...headersArray: (Record<string, string | core.Supplier<string | null | undefined> | null | undefined> | null | undefined)[]): Record<string, string | core.Supplier<string | null | undefined>>;
|
|
1
|
+
export declare function mergeHeaders<THeaderValue>(...headersArray: (Record<string, THeaderValue> | null | undefined)[]): Record<string, string | THeaderValue>;
|
|
2
|
+
export declare function mergeOnlyDefinedHeaders<THeaderValue>(...headersArray: (Record<string, THeaderValue> | null | undefined)[]): Record<string, THeaderValue>;
|
package/dist/cjs/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.1.
|
|
1
|
+
export declare const SDK_VERSION = "0.1.7";
|
package/dist/cjs/version.js
CHANGED
package/dist/esm/Client.mjs
CHANGED
|
@@ -13,8 +13,8 @@ export class CredalClient {
|
|
|
13
13
|
this._options = Object.assign(Object.assign({}, _options), { headers: mergeHeaders({
|
|
14
14
|
"X-Fern-Language": "JavaScript",
|
|
15
15
|
"X-Fern-SDK-Name": "@credal/sdk",
|
|
16
|
-
"X-Fern-SDK-Version": "0.1.
|
|
17
|
-
"User-Agent": "@credal/sdk/0.1.
|
|
16
|
+
"X-Fern-SDK-Version": "0.1.7",
|
|
17
|
+
"User-Agent": "@credal/sdk/0.1.7",
|
|
18
18
|
"X-Fern-Runtime": core.RUNTIME.type,
|
|
19
19
|
"X-Fern-Runtime-Version": core.RUNTIME.version,
|
|
20
20
|
}, _options === null || _options === void 0 ? void 0 : _options.headers) });
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { AuthProvider } from "./AuthProvider.mjs";
|
|
2
|
-
export {
|
|
1
|
+
export type { AuthProvider } from "./AuthProvider.mjs";
|
|
2
|
+
export type { AuthRequest } from "./AuthRequest.mjs";
|
|
3
3
|
export { BasicAuth } from "./BasicAuth.mjs";
|
|
4
4
|
export { BearerToken } from "./BearerToken.mjs";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type SecuritySchemeKey = string;
|
|
2
|
+
/**
|
|
3
|
+
* A collection of security schemes, where the key is the name of the security scheme and the value is the list of scopes required for that scheme.
|
|
4
|
+
* All schemes in the collection must be satisfied for authentication to be successful.
|
|
5
|
+
*/
|
|
6
|
+
export type SecuritySchemeCollection = Record<SecuritySchemeKey, AuthScope[]>;
|
|
7
|
+
export type AuthScope = string;
|
|
8
|
+
export type EndpointMetadata = {
|
|
9
|
+
/**
|
|
10
|
+
* An array of security scheme collections. Each collection represents an alternative way to authenticate.
|
|
11
|
+
*/
|
|
12
|
+
security?: SecuritySchemeCollection[];
|
|
13
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EndpointMetadata } from "./EndpointMetadata.mjs";
|
|
2
|
+
import { Supplier } from "./Supplier.mjs";
|
|
3
|
+
type EndpointSupplierFn<T> = (arg: {
|
|
4
|
+
endpointMetadata: EndpointMetadata;
|
|
5
|
+
}) => T | Promise<T>;
|
|
6
|
+
export type EndpointSupplier<T> = Supplier<T> | EndpointSupplierFn<T>;
|
|
7
|
+
export declare const EndpointSupplier: {
|
|
8
|
+
get: <T>(supplier: EndpointSupplier<T>, arg: {
|
|
9
|
+
endpointMetadata: EndpointMetadata;
|
|
10
|
+
}) => Promise<T>;
|
|
11
|
+
};
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export const EndpointSupplier = {
|
|
11
|
+
get: (supplier, arg) => __awaiter(void 0, void 0, void 0, function* () {
|
|
12
|
+
if (typeof supplier === "function") {
|
|
13
|
+
return supplier(arg);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
return supplier;
|
|
17
|
+
}
|
|
18
|
+
}),
|
|
19
|
+
};
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { APIResponse } from "./APIResponse.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { EndpointMetadata } from "./EndpointMetadata.mjs";
|
|
3
|
+
import { EndpointSupplier } from "./EndpointSupplier.mjs";
|
|
3
4
|
export type FetchFunction = <R = unknown>(args: Fetcher.Args) => Promise<APIResponse<R, Fetcher.Error>>;
|
|
4
5
|
export declare namespace Fetcher {
|
|
5
6
|
interface Args {
|
|
6
7
|
url: string;
|
|
7
8
|
method: string;
|
|
8
9
|
contentType?: string;
|
|
9
|
-
headers?: Record<string, string |
|
|
10
|
+
headers?: Record<string, string | EndpointSupplier<string | null | undefined> | null | undefined>;
|
|
10
11
|
queryParameters?: Record<string, unknown>;
|
|
11
12
|
body?: unknown;
|
|
12
13
|
timeoutMs?: number;
|
|
@@ -16,6 +17,7 @@ export declare namespace Fetcher {
|
|
|
16
17
|
requestType?: "json" | "file" | "bytes";
|
|
17
18
|
responseType?: "json" | "blob" | "sse" | "streaming" | "text" | "arrayBuffer" | "binary-response";
|
|
18
19
|
duplex?: "half";
|
|
20
|
+
endpointMetadata?: EndpointMetadata;
|
|
19
21
|
}
|
|
20
22
|
type Error = FailedStatusCodeError | NonJsonError | TimeoutError | UnknownError;
|
|
21
23
|
interface FailedStatusCodeError {
|
|
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { toJson } from "../json.mjs";
|
|
11
11
|
import { createRequestUrl } from "./createRequestUrl.mjs";
|
|
12
|
+
import { EndpointSupplier } from "./EndpointSupplier.mjs";
|
|
12
13
|
import { getErrorResponseBody } from "./getErrorResponseBody.mjs";
|
|
13
14
|
import { getFetchFn } from "./getFetchFn.mjs";
|
|
14
15
|
import { getRequestBody } from "./getRequestBody.mjs";
|
|
@@ -16,9 +17,9 @@ import { getResponseBody } from "./getResponseBody.mjs";
|
|
|
16
17
|
import { makeRequest } from "./makeRequest.mjs";
|
|
17
18
|
import { abortRawResponse, toRawResponse, unknownRawResponse } from "./RawResponse.mjs";
|
|
18
19
|
import { requestWithRetries } from "./requestWithRetries.mjs";
|
|
19
|
-
import { Supplier } from "./Supplier.mjs";
|
|
20
20
|
function getHeaders(args) {
|
|
21
21
|
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
var _a;
|
|
22
23
|
const newHeaders = {};
|
|
23
24
|
if (args.body !== undefined && args.contentType != null) {
|
|
24
25
|
newHeaders["Content-Type"] = args.contentType;
|
|
@@ -27,7 +28,7 @@ function getHeaders(args) {
|
|
|
27
28
|
return newHeaders;
|
|
28
29
|
}
|
|
29
30
|
for (const [key, value] of Object.entries(args.headers)) {
|
|
30
|
-
const result = yield
|
|
31
|
+
const result = yield EndpointSupplier.get(value, { endpointMetadata: (_a = args.endpointMetadata) !== null && _a !== void 0 ? _a : {} });
|
|
31
32
|
if (typeof result === "string") {
|
|
32
33
|
newHeaders[key] = result;
|
|
33
34
|
continue;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export type { APIResponse } from "./APIResponse.mjs";
|
|
2
2
|
export type { BinaryResponse } from "./BinaryResponse.mjs";
|
|
3
|
+
export type { EndpointMetadata } from "./EndpointMetadata.mjs";
|
|
4
|
+
export { EndpointSupplier } from "./EndpointSupplier.mjs";
|
|
3
5
|
export type { Fetcher, FetchFunction } from "./Fetcher.mjs";
|
|
4
6
|
export { fetcher } from "./Fetcher.mjs";
|
|
5
7
|
export { getHeader } from "./getHeader.mjs";
|
|
@@ -11,29 +11,35 @@ const INITIAL_RETRY_DELAY = 1000; // in milliseconds
|
|
|
11
11
|
const MAX_RETRY_DELAY = 60000; // in milliseconds
|
|
12
12
|
const DEFAULT_MAX_RETRIES = 2;
|
|
13
13
|
const JITTER_FACTOR = 0.2; // 20% random jitter
|
|
14
|
-
function
|
|
15
|
-
// Generate a random value between
|
|
16
|
-
const jitterMultiplier = 1 +
|
|
14
|
+
function addPositiveJitter(delay) {
|
|
15
|
+
// Generate a random value between 0 and +JITTER_FACTOR
|
|
16
|
+
const jitterMultiplier = 1 + Math.random() * JITTER_FACTOR;
|
|
17
|
+
return delay * jitterMultiplier;
|
|
18
|
+
}
|
|
19
|
+
function addSymmetricJitter(delay) {
|
|
20
|
+
// Generate a random value in a JITTER_FACTOR-sized percentage range around delay
|
|
21
|
+
const jitterMultiplier = 1 + (Math.random() - 0.5) * JITTER_FACTOR;
|
|
17
22
|
return delay * jitterMultiplier;
|
|
18
23
|
}
|
|
19
24
|
function getRetryDelayFromHeaders(response, retryAttempt) {
|
|
20
|
-
// Check for Retry-After header first (RFC 7231)
|
|
25
|
+
// Check for Retry-After header first (RFC 7231), with no jitter
|
|
21
26
|
const retryAfter = response.headers.get("Retry-After");
|
|
22
27
|
if (retryAfter) {
|
|
23
28
|
// Parse as number of seconds...
|
|
24
29
|
const retryAfterSeconds = parseInt(retryAfter, 10);
|
|
25
|
-
if (!isNaN(retryAfterSeconds)) {
|
|
26
|
-
// Convert seconds to milliseconds and cap at MAX_RETRY_DELAY
|
|
30
|
+
if (!isNaN(retryAfterSeconds) && retryAfterSeconds > 0) {
|
|
27
31
|
return Math.min(retryAfterSeconds * 1000, MAX_RETRY_DELAY);
|
|
28
32
|
}
|
|
29
33
|
// ...or as an HTTP date; both are valid
|
|
30
34
|
const retryAfterDate = new Date(retryAfter);
|
|
31
35
|
if (!isNaN(retryAfterDate.getTime())) {
|
|
32
36
|
const delay = retryAfterDate.getTime() - Date.now();
|
|
33
|
-
|
|
37
|
+
if (delay > 0) {
|
|
38
|
+
return Math.min(Math.max(delay, 0), MAX_RETRY_DELAY);
|
|
39
|
+
}
|
|
34
40
|
}
|
|
35
41
|
}
|
|
36
|
-
// Then check for industry-standard X-RateLimit-Reset header
|
|
42
|
+
// Then check for industry-standard X-RateLimit-Reset header, with positive jitter
|
|
37
43
|
const rateLimitReset = response.headers.get("X-RateLimit-Reset");
|
|
38
44
|
if (rateLimitReset) {
|
|
39
45
|
const resetTime = parseInt(rateLimitReset, 10);
|
|
@@ -41,23 +47,21 @@ function getRetryDelayFromHeaders(response, retryAttempt) {
|
|
|
41
47
|
// Assume Unix timestamp in epoch seconds
|
|
42
48
|
const delay = resetTime * 1000 - Date.now();
|
|
43
49
|
if (delay > 0) {
|
|
44
|
-
return Math.min(delay, MAX_RETRY_DELAY);
|
|
50
|
+
return addPositiveJitter(Math.min(delay, MAX_RETRY_DELAY));
|
|
45
51
|
}
|
|
46
52
|
}
|
|
47
53
|
}
|
|
48
|
-
// Fall back to exponential backoff
|
|
49
|
-
return Math.min(INITIAL_RETRY_DELAY * Math.pow(2, retryAttempt), MAX_RETRY_DELAY);
|
|
54
|
+
// Fall back to exponential backoff, with symmetric jitter
|
|
55
|
+
return addSymmetricJitter(Math.min(INITIAL_RETRY_DELAY * Math.pow(2, retryAttempt), MAX_RETRY_DELAY));
|
|
50
56
|
}
|
|
51
57
|
export function requestWithRetries(requestFn_1) {
|
|
52
58
|
return __awaiter(this, arguments, void 0, function* (requestFn, maxRetries = DEFAULT_MAX_RETRIES) {
|
|
53
59
|
let response = yield requestFn();
|
|
54
60
|
for (let i = 0; i < maxRetries; ++i) {
|
|
55
61
|
if ([408, 429].includes(response.status) || response.status >= 500) {
|
|
56
|
-
// Get delay
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
const delayWithJitter = addJitter(baseDelay);
|
|
60
|
-
yield new Promise((resolve) => setTimeout(resolve, delayWithJitter));
|
|
62
|
+
// Get delay with appropriate jitter applied
|
|
63
|
+
const delay = getRetryDelayFromHeaders(response, i);
|
|
64
|
+
yield new Promise((resolve) => setTimeout(resolve, delay));
|
|
61
65
|
response = yield requestFn();
|
|
62
66
|
}
|
|
63
67
|
else {
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function mergeOnlyDefinedHeaders(...headersArray: (Record<string, string | core.Supplier<string | null | undefined> | null | undefined> | null | undefined)[]): Record<string, string | core.Supplier<string | null | undefined>>;
|
|
1
|
+
export declare function mergeHeaders<THeaderValue>(...headersArray: (Record<string, THeaderValue> | null | undefined)[]): Record<string, string | THeaderValue>;
|
|
2
|
+
export declare function mergeOnlyDefinedHeaders<THeaderValue>(...headersArray: (Record<string, THeaderValue> | null | undefined)[]): Record<string, THeaderValue>;
|
package/dist/esm/version.d.mts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.1.
|
|
1
|
+
export declare const SDK_VERSION = "0.1.7";
|
package/dist/esm/version.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "0.1.
|
|
1
|
+
export const SDK_VERSION = "0.1.7";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@credal/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"private": false,
|
|
5
5
|
"repository": "github:credal-ai/credal-typescript-sdk",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -30,23 +30,18 @@
|
|
|
30
30
|
],
|
|
31
31
|
"scripts": {
|
|
32
32
|
"format": "prettier . --write --ignore-unknown",
|
|
33
|
-
"build": "
|
|
33
|
+
"build": "pnpm build:cjs && pnpm build:esm",
|
|
34
34
|
"build:cjs": "tsc --project ./tsconfig.cjs.json",
|
|
35
35
|
"build:esm": "tsc --project ./tsconfig.esm.json && node scripts/rename-to-esm-files.js dist/esm",
|
|
36
|
-
"test": "
|
|
37
|
-
"test:unit": "
|
|
38
|
-
"test:
|
|
39
|
-
"test:wire": "jest --selectProjects wire"
|
|
36
|
+
"test": "vitest",
|
|
37
|
+
"test:unit": "vitest --project unit",
|
|
38
|
+
"test:wire": "vitest --project wire"
|
|
40
39
|
},
|
|
41
40
|
"devDependencies": {
|
|
42
41
|
"webpack": "^5.97.1",
|
|
43
42
|
"ts-loader": "^9.5.1",
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"@types/jest": "^29.5.14",
|
|
47
|
-
"ts-jest": "^29.3.4",
|
|
48
|
-
"jest-environment-jsdom": "^29.7.0",
|
|
49
|
-
"msw": "^2.8.4",
|
|
43
|
+
"vitest": "^3.2.4",
|
|
44
|
+
"msw": "2.11.2",
|
|
50
45
|
"@types/node": "^18.19.70",
|
|
51
46
|
"prettier": "^3.4.2",
|
|
52
47
|
"typescript": "~5.7.2"
|
|
@@ -57,7 +52,7 @@
|
|
|
57
52
|
"path": false,
|
|
58
53
|
"stream": false
|
|
59
54
|
},
|
|
60
|
-
"packageManager": "
|
|
55
|
+
"packageManager": "pnpm@10.14.0",
|
|
61
56
|
"engines": {
|
|
62
57
|
"node": ">=18.0.0"
|
|
63
58
|
},
|