mirror of
https://github.com/MadsLorentzen/ai-job-search.git
synced 2026-09-17 08:36:25 +00:00
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
export const BASE_URL = "https://jobnet.dk/bff"
|
|
export const USER_AGENT = "Mozilla/5.0 (compatible; jobnet-cli/1.0)"
|
|
|
|
export async function apiFetch<T>(path: string, params?: Record<string, string>): Promise<T> {
|
|
let url = `${BASE_URL}${path}`
|
|
if (params && Object.keys(params).length > 0) {
|
|
const qs = new URLSearchParams(params)
|
|
url += `?${qs.toString()}`
|
|
}
|
|
|
|
const maxRetries = 6
|
|
let delay = 500
|
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
"User-Agent": USER_AGENT,
|
|
"x-csrf": "1",
|
|
},
|
|
signal: AbortSignal.timeout(15000),
|
|
})
|
|
|
|
if (response.status === 429 || response.status >= 500) {
|
|
if (attempt === maxRetries) {
|
|
throw new Error(`API request failed: ${response.status} ${response.statusText}`)
|
|
}
|
|
// Add jitter to spread out retries: base delay + random 0-500ms
|
|
const jitter = Math.floor(Math.random() * 500)
|
|
await new Promise((resolve) => setTimeout(resolve, delay + jitter))
|
|
delay = Math.min(delay * 2, 5000)
|
|
continue
|
|
}
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`API request failed: ${response.status} ${response.statusText}`)
|
|
}
|
|
|
|
return response.json() as Promise<T>
|
|
}
|
|
throw new Error("API request failed after max retries")
|
|
}
|
|
|
|
export function writeError(error: string, code: string): void {
|
|
process.stderr.write(JSON.stringify({ error, code }) + "\n")
|
|
}
|
|
|
|
export function stripHtml(html: string): string {
|
|
return html
|
|
.replace(/<[^>]*>/g, " ")
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, "'")
|
|
.replace(/ /g, " ")
|
|
.replace(/\s+/g, " ")
|
|
.trim()
|
|
}
|
|
|
|
export function normalizeJobId(input: string): string | null {
|
|
const trimmed = input.trim()
|
|
if (!trimmed) return null
|
|
if (/^[a-zA-Z0-9_-]+$/.test(trimmed)) return trimmed
|
|
const match = trimmed.match(/(?:\/find-job\/|\/JobAdDetails\/|\/Details\/)(?:detaljer\/)?([a-zA-Z0-9_-]+)(?:\/|$|\?|#)/i)
|
|
if (match) return match[1]
|
|
return null
|
|
}
|
|
|