@afriex/core
Version:
HTTP client, configuration and typed errors for the Afriex Business API SDK
65 lines • 2.74 kB
JavaScript
import { Environment, DEFAULT_CONFIG, } from "./Environment.js";
import { LogLevel } from "../utils/logger.js";
/**
* The API version this SDK was written for, sent as the `x-api-version`
* header. It is also what the API serves when the header is left out.
*/
export const DEFAULT_API_VERSION = "2026-05-18";
/** The header the API version is sent in. */
export const API_VERSION_HEADER = "x-api-version";
/** The header a request signature is sent in. */
export const API_SIGNATURE_HEADER = "x-api-signature";
/**
* The `User-Agent` sent when none is configured. `@afriex/sdk` adds its own
* version to it; a client built from `@afriex/core` alone sends it as it is.
*/
export const DEFAULT_USER_AGENT = "Afriex-TypeScript-SDK";
/**
* The methods retried by default: those that are safe to send twice. POST and
* PATCH are left out, because repeating one can repeat its effect.
*/
export const DEFAULT_RETRYABLE_METHODS = [
"GET",
"PUT",
"HEAD",
"DELETE",
"OPTIONS",
"TRACE",
];
export class Config {
constructor(config) {
this.validateConfig(config);
this.apiKey = config.apiKey;
this.environment = config.environment || Environment.PRODUCTION;
this.logLevel = config.logLevel || LogLevel.ERROR;
this.enableLogging = config.enableLogging ?? true;
this.apiVersion = config.apiVersion || DEFAULT_API_VERSION;
this.signRequest = config.signRequest;
this.userAgent = config.userAgent || DEFAULT_USER_AGENT;
const envConfig = DEFAULT_CONFIG[this.environment];
const customConfig = config.customConfig || {};
this.baseUrl = customConfig.baseUrl || envConfig.baseUrl;
this.timeout = customConfig.timeout || envConfig.timeout;
this.maxRetries = config.retryConfig?.maxRetries ?? envConfig.maxRetries;
this.retryDelay = config.retryConfig?.retryDelay || envConfig.retryDelay;
this.retryableStatusCodes = config.retryConfig?.retryableStatusCodes || [
408, 429, 500, 502, 503, 504,
];
this.retryableMethods = config.retryConfig?.retryableMethods
? [...config.retryConfig.retryableMethods]
: [...DEFAULT_RETRYABLE_METHODS];
}
validateConfig(config) {
if (!config.apiKey || typeof config.apiKey !== "string") {
throw new Error("API key is required and must be a string");
}
if (config.apiKey.trim().length === 0) {
throw new Error("API key cannot be empty");
}
if (config.signRequest !== undefined &&
typeof config.signRequest !== "function") {
throw new Error("signRequest must be a function");
}
}
}
//# sourceMappingURL=Config.js.map