jobsuche-api-js
Version:
A JavaScript wrapper for the Arbeitsagentur jobs API, allowing developers to easily integrate job search functionality into their applications.
78 lines (77 loc) • 2.88 kB
JavaScript
import base64 from "base-64";
import axios from "axios";
import { headers, jobDetailV2Link, jobDetailV3Link } from "../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 = base64.encode(refnr);
const url = `${jobDetailV2Link}${encodedJobRef}`;
try {
const response = await axios.get(url, {
headers,
timeout: DEFAULT_TIMEOUT, // Add timeout here for consistency
});
return response.data;
}
catch (error) {
if (axios.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.CancelToken.source();
const encodedJobRef = base64.encode(refnr);
const headers = {
"X-Api-Key": "jobboerse-jobsuche",
};
const url = `${jobDetailV3Link}${encodedJobRef}`;
try {
const response = await axios.get(url, {
headers,
timeout: DEFAULT_TIMEOUT,
cancelToken: cancelTokenSource.token,
});
cancelTokenSource = null;
return response.data;
}
catch (error) {
if (axios.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;
}
}
export { fetchJobDetailsV2, fetchJobDetailsV3 };