isbndb-client
Version:
A TypeScript client library for the ISBNdb API
52 lines (51 loc) • 1.92 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createIsbndbClient = createIsbndbClient;
exports.formatIsbndbError = formatIsbndbError;
const axios_1 = __importDefault(require("axios"));
/**
* Maps a plan type to the correct ISBNdb API base URL.
*/
const planToBaseUrl = {
BASIC: "https://api2.isbndb.com",
PRO: "https://api.pro.isbndb.com",
PREMIUM: "https://api.premium.isbndb.com",
};
/**
* Creates an Axios instance preconfigured for the ISBNdb API.
*
* @param apiKey Your personal API key from isbndb.com
* @param plan The plan level to determine the correct base URL. Defaults to 'BASIC'
* @returns AxiosInstance preconfigured with baseURL and headers
*/
function createIsbndbClient(apiKey, plan = "BASIC") {
var _a;
const baseURL = (_a = planToBaseUrl[plan]) !== null && _a !== void 0 ? _a : planToBaseUrl["BASIC"];
const client = axios_1.default.create({
baseURL,
headers: {
Authorization: apiKey,
},
});
// Attach an error formatter for consistent error messages
client.interceptors.response.use(response => response, (error) => Promise.reject(formatIsbndbError(error)));
return client;
}
/**
* Formats an AxiosError into a more readable IsbndbError.
*
* @param error AxiosError from the request
* @returns IsbndbError object
*/
function formatIsbndbError(error) {
var _a, _b, _c, _d;
return {
status: (_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 0,
statusText: (_c = error.response) === null || _c === void 0 ? void 0 : _c.statusText,
message: error.message,
url: (_d = error.config) === null || _d === void 0 ? void 0 : _d.url,
};
}