Counter name is now searchable and add counter for redirects * refactor: createCounter() only takes one argument, the name of the counter. Makes it easier to search for * feat: add counter when we do a redirect from redirect-service Approved-by: Linus Flood
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import * as api from "../../../api"
|
|
import { cache } from "../../../DUPLICATED/cache"
|
|
import { serverErrorByStatus, sessionExpiredError } from "../../../errors"
|
|
import { getBasicUserSchema } from "../output"
|
|
|
|
export const getBasicUser = cache(
|
|
async ({
|
|
token,
|
|
}: {
|
|
token: { expires_at?: number; access_token: string }
|
|
}) => {
|
|
const getBasicUserCounter = createCounter("user.getBasicUser")
|
|
const metricsGetBasicUser = getBasicUserCounter.init()
|
|
|
|
metricsGetBasicUser.start()
|
|
|
|
const now = Date.now()
|
|
if (token.expires_at && token.expires_at < now) {
|
|
throw sessionExpiredError()
|
|
}
|
|
|
|
const apiResponse = await api.get(api.endpoints.v2.Profile.basicProfile, {
|
|
headers: {
|
|
Authorization: `Bearer ${token.access_token}`,
|
|
},
|
|
})
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsGetBasicUser.httpError(apiResponse)
|
|
|
|
throw serverErrorByStatus(apiResponse.status, apiResponse)
|
|
}
|
|
const apiJson = await apiResponse.json()
|
|
|
|
const verifiedData = getBasicUserSchema.safeParse(apiJson)
|
|
if (!verifiedData.success) {
|
|
metricsGetBasicUser.validationError(verifiedData.error)
|
|
throw verifiedData.error
|
|
}
|
|
|
|
metricsGetBasicUser.success()
|
|
|
|
return verifiedData.data
|
|
}
|
|
)
|