jobsuche-api-js
Version:
A JavaScript wrapper for the Arbeitsagentur jobs API, allowing developers to easily integrate job search functionality into their applications.
84 lines (83 loc) • 3.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchJobDetailsV2 = fetchJobDetailsV2;
exports.fetchJobDetailsV3 = fetchJobDetailsV3;
const base_64_1 = __importDefault(require("base-64"));
const axios_1 = __importDefault(require("axios"));
const urls_1 = require("../constants/urls");
const DEFAULT_TIMEOUT = 10000; // 10 seconds
let cancelTokenSource = null;
/**
* Fetches job details (V2) based on the provided reference number.
* @param {string} refnr from the response of the jobSearch function.
* @typeParam string
* Note: Please add the refnr as it is no need to encode it with Base64.
* @returns {(Promise<JobDetails | null>)} - The job details result.
*/
async function fetchJobDetailsV2(refnr) {
const encodedJobRef = base_64_1.default.encode(refnr);
const url = `${urls_1.jobDetailV2Link}${encodedJobRef}`;
try {
const response = await axios_1.default.get(url, {
headers: urls_1.headers,
timeout: DEFAULT_TIMEOUT, // Add timeout here for consistency
});
return response.data;
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
console.warn("====error GET JOB DETAILS V2 FUNCTION====", error.message);
}
else {
console.error("Unexpected error:", error);
}
return null;
}
}
/**
* Fetches job details (V3) based on the provided reference number.
* Note: No OAuth Access Token Required! Only Add 'X-Api-Key': 'jobboerse-jobsuche' to the header.
* @param {string} refnr - The reference number of the job.
* @returns {Promise<IJobDetailsV3ResponseProps | null>} - The job details.
*/
async function fetchJobDetailsV3(refnr) {
var _a;
if (cancelTokenSource) {
cancelTokenSource.cancel("Operation canceled due to new request.");
}
cancelTokenSource = axios_1.default.CancelToken.source();
const encodedJobRef = base_64_1.default.encode(refnr);
const headers = {
"X-Api-Key": "jobboerse-jobsuche",
};
const url = `${urls_1.jobDetailV3Link}${encodedJobRef}`;
try {
const response = await axios_1.default.get(url, {
headers,
timeout: DEFAULT_TIMEOUT,
cancelToken: cancelTokenSource.token,
});
cancelTokenSource = null;
return response.data;
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
console.warn("====error GET JOB DETAILS V3 FUNCTION====", error.message);
// Retry on timeout
if (error.code === "ECONNABORTED" || ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
console.log("Retrying to fetch job details...");
// Optional: Add a small delay before retrying
await new Promise((resolve) => setTimeout(resolve, 1000));
return await fetchJobDetailsV3(refnr); // Retry the function
}
}
else {
console.error("Unexpected error:", error);
return null;
}
return null;
}
}