Feat/SW-1555 jobylon integration * feat(SW-1555): Added jobylon feed query * feat(SW-1555): Added jobylon feed component Approved-by: Fredrik Thorsson Approved-by: Matilda Landström
95 lines
2.4 KiB
TypeScript
95 lines
2.4 KiB
TypeScript
import { publicProcedure, router } from "@/server/trpc"
|
|
|
|
import { jobylonFeedSchema } from "./output"
|
|
import {
|
|
getJobylonFeedCounter,
|
|
getJobylonFeedFailCounter,
|
|
getJobylonFeedSuccessCounter,
|
|
} from "./telemetry"
|
|
|
|
export const TWENTYFOUR_HOURS = 60 * 60 * 24
|
|
|
|
// The URL for the Jobylon feed including the hash for the specific feed.
|
|
// The URL and hash are generated by Jobylon. Documentation: https://developer.jobylon.com/feed-api
|
|
const feedUrl =
|
|
"https://feed.jobylon.com/feeds/cc04ba19-f0bd-4412-8b9b-d1d1fcbf0800"
|
|
|
|
export const jobylonQueryRouter = router({
|
|
feed: router({
|
|
get: publicProcedure.query(async function () {
|
|
const url = new URL(feedUrl)
|
|
url.search = new URLSearchParams({
|
|
format: "json",
|
|
}).toString()
|
|
const urlString = url.toString()
|
|
|
|
getJobylonFeedCounter.add(1, { url: urlString })
|
|
console.info(
|
|
"jobylon.feed start",
|
|
JSON.stringify({ query: { url: urlString } })
|
|
)
|
|
|
|
const response = await fetch(url, {
|
|
cache: "force-cache",
|
|
next: {
|
|
revalidate: TWENTYFOUR_HOURS,
|
|
},
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const text = await response.text()
|
|
const error = {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
text,
|
|
}
|
|
getJobylonFeedFailCounter.add(1, {
|
|
url: urlString,
|
|
error_type: "http_error",
|
|
error: JSON.stringify(error),
|
|
})
|
|
console.error(
|
|
"jobylon.feed error",
|
|
JSON.stringify({
|
|
query: { url: urlString },
|
|
error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
const responseJson = await response.json()
|
|
const validatedResponse = jobylonFeedSchema.safeParse(responseJson)
|
|
|
|
if (!validatedResponse.success) {
|
|
getJobylonFeedFailCounter.add(1, {
|
|
urlString,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedResponse.error),
|
|
})
|
|
|
|
console.error(
|
|
"jobylon.feed error",
|
|
JSON.stringify({
|
|
query: { url: urlString },
|
|
error: validatedResponse.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
getJobylonFeedSuccessCounter.add(1, {
|
|
url: urlString,
|
|
})
|
|
console.info(
|
|
"jobylon.feed success",
|
|
JSON.stringify({
|
|
query: { url: urlString },
|
|
})
|
|
)
|
|
|
|
return validatedResponse.data
|
|
}),
|
|
}),
|
|
})
|