Utilizing memoized requests
This commit is contained in:
@@ -4,7 +4,7 @@ import { z } from "zod"
|
|||||||
|
|
||||||
import { ApiLang } from "@/constants/languages"
|
import { ApiLang } from "@/constants/languages"
|
||||||
import * as api from "@/lib/api"
|
import * as api from "@/lib/api"
|
||||||
import { serverClient } from "@/lib/trpc/server"
|
import { getProfile } from "@/lib/trpc/memoizedRequests"
|
||||||
import { protectedServerActionProcedure } from "@/server/trpc"
|
import { protectedServerActionProcedure } from "@/server/trpc"
|
||||||
|
|
||||||
import { editProfileSchema } from "@/components/Forms/Edit/Profile/schema"
|
import { editProfileSchema } from "@/components/Forms/Edit/Profile/schema"
|
||||||
@@ -68,7 +68,7 @@ export const editProfile = protectedServerActionProcedure
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const profile = await serverClient().user.get()
|
const profile = await getProfile()
|
||||||
if (!profile || "error" in profile) {
|
if (!profile || "error" in profile) {
|
||||||
console.error(
|
console.error(
|
||||||
"editProfile profile fetch error",
|
"editProfile profile fetch error",
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { serverClient } from "@/lib/trpc/server"
|
import { getMembershipCards } from "@/lib/trpc/memoizedRequests"
|
||||||
|
|
||||||
import { PlusCircleIcon } from "@/components/Icons"
|
import { PlusCircleIcon } from "@/components/Icons"
|
||||||
import Link from "@/components/TempDesignSystem/Link"
|
import Link from "@/components/TempDesignSystem/Link"
|
||||||
@@ -16,7 +16,7 @@ export default async function MembershipCardSlot({
|
|||||||
}: PageArgs<LangParams>) {
|
}: PageArgs<LangParams>) {
|
||||||
setLang(params.lang)
|
setLang(params.lang)
|
||||||
const { formatMessage } = await getIntl()
|
const { formatMessage } = await getIntl()
|
||||||
const membershipCards = await serverClient().user.membershipCards()
|
const membershipCards = await getMembershipCards()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className={styles.container}>
|
<section className={styles.container}>
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { getMembershipLevelSafely } from "@/lib/trpc/memoizedRequests"
|
||||||
import { serverClient } from "@/lib/trpc/server"
|
import { serverClient } from "@/lib/trpc/server"
|
||||||
|
|
||||||
import SectionWrapper from "../SectionWrapper"
|
import SectionWrapper from "../SectionWrapper"
|
||||||
@@ -11,7 +12,7 @@ export default async function OverviewTable({
|
|||||||
}: OverviewTableProps) {
|
}: OverviewTableProps) {
|
||||||
const [levels, membershipLevel] = await Promise.all([
|
const [levels, membershipLevel] = await Promise.all([
|
||||||
serverClient().contentstack.rewards.all(),
|
serverClient().contentstack.rewards.all(),
|
||||||
serverClient().user.safeMembershipLevel(),
|
getMembershipLevelSafely(),
|
||||||
])
|
])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { serverClient } from "@/lib/trpc/server"
|
import { getMembershipLevel } from "@/lib/trpc/memoizedRequests"
|
||||||
|
|
||||||
import SectionContainer from "@/components/Section/Container"
|
import SectionContainer from "@/components/Section/Container"
|
||||||
import SectionHeader from "@/components/Section/Header"
|
import SectionHeader from "@/components/Section/Header"
|
||||||
@@ -12,7 +12,7 @@ export default async function ExpiringPoints({
|
|||||||
subtitle,
|
subtitle,
|
||||||
title,
|
title,
|
||||||
}: AccountPageComponentProps) {
|
}: AccountPageComponentProps) {
|
||||||
const membershipLevel = await serverClient().user.membershipLevel()
|
const membershipLevel = await getMembershipLevel()
|
||||||
|
|
||||||
if (!membershipLevel?.pointsToExpire || !membershipLevel?.pointsExpiryDate) {
|
if (!membershipLevel?.pointsToExpire || !membershipLevel?.pointsExpiryDate) {
|
||||||
return null
|
return null
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Lock } from "react-feather"
|
import { Lock } from "react-feather"
|
||||||
|
|
||||||
import { MembershipLevelEnum } from "@/constants/membershipLevels"
|
import { MembershipLevelEnum } from "@/constants/membershipLevels"
|
||||||
|
import { getMembershipLevel } from "@/lib/trpc/memoizedRequests"
|
||||||
import { serverClient } from "@/lib/trpc/server"
|
import { serverClient } from "@/lib/trpc/server"
|
||||||
|
|
||||||
import SectionContainer from "@/components/Section/Container"
|
import SectionContainer from "@/components/Section/Container"
|
||||||
@@ -22,7 +23,7 @@ export default async function NextLevelRewardsBlock({
|
|||||||
link,
|
link,
|
||||||
}: AccountPageComponentProps) {
|
}: AccountPageComponentProps) {
|
||||||
const intl = await getIntl()
|
const intl = await getIntl()
|
||||||
const membershipLevel = await serverClient().user.membershipLevel()
|
const membershipLevel = await getMembershipLevel()
|
||||||
|
|
||||||
if (!membershipLevel || !membershipLevel?.nextLevel) {
|
if (!membershipLevel || !membershipLevel?.nextLevel) {
|
||||||
return null
|
return null
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { logout } from "@/constants/routes/handleAuth"
|
import { logout } from "@/constants/routes/handleAuth"
|
||||||
import { overview } from "@/constants/routes/myPages"
|
import { overview } from "@/constants/routes/myPages"
|
||||||
import { serverClient } from "@/lib/trpc/server"
|
import { getName } from "@/lib/trpc/memoizedRequests"
|
||||||
|
|
||||||
import Link from "@/components/TempDesignSystem/Link"
|
import Link from "@/components/TempDesignSystem/Link"
|
||||||
import { getIntl } from "@/i18n"
|
import { getIntl } from "@/i18n"
|
||||||
@@ -23,7 +23,7 @@ export default async function TopMenu({
|
|||||||
languageSwitcher,
|
languageSwitcher,
|
||||||
}: TopMenuProps) {
|
}: TopMenuProps) {
|
||||||
const { formatMessage } = await getIntl()
|
const { formatMessage } = await getIntl()
|
||||||
const user = await serverClient().user.name()
|
const user = await getName()
|
||||||
return (
|
return (
|
||||||
<div className={styles.topMenu}>
|
<div className={styles.topMenu}>
|
||||||
<div className={styles.container}>
|
<div className={styles.container}>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { homeHrefs } from "@/constants/homeHrefs"
|
import { homeHrefs } from "@/constants/homeHrefs"
|
||||||
import { env } from "@/env/server"
|
import { env } from "@/env/server"
|
||||||
import { getLanguageSwitcher } from "@/lib/trpc/memoizedRequests"
|
import { getLanguageSwitcher, getName } from "@/lib/trpc/memoizedRequests"
|
||||||
import { serverClient } from "@/lib/trpc/server"
|
import { serverClient } from "@/lib/trpc/server"
|
||||||
|
|
||||||
import { getLang } from "@/i18n/serverContext"
|
import { getLang } from "@/i18n/serverContext"
|
||||||
@@ -18,7 +18,7 @@ export default async function Header() {
|
|||||||
serverClient().contentstack.base.currentHeader({
|
serverClient().contentstack.base.currentHeader({
|
||||||
lang: getLang(),
|
lang: getLang(),
|
||||||
}),
|
}),
|
||||||
serverClient().user.name(),
|
getName(),
|
||||||
getLanguageSwitcher(),
|
getLanguageSwitcher(),
|
||||||
serverClient().contentstack.myPages.navigation.get(),
|
serverClient().contentstack.myPages.navigation.get(),
|
||||||
])
|
])
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { MembershipLevelEnum } from "@/constants/membershipLevels"
|
import { MembershipLevelEnum } from "@/constants/membershipLevels"
|
||||||
import { myPages } from "@/constants/routes/myPages"
|
import { myPages } from "@/constants/routes/myPages"
|
||||||
|
import { getMembershipLevelSafely, getName } from "@/lib/trpc/memoizedRequests"
|
||||||
import { serverClient } from "@/lib/trpc/server"
|
import { serverClient } from "@/lib/trpc/server"
|
||||||
|
|
||||||
import Link from "@/components/TempDesignSystem/Link"
|
import Link from "@/components/TempDesignSystem/Link"
|
||||||
@@ -17,8 +18,8 @@ export default async function MyPagesMenuWrapper() {
|
|||||||
const [intl, myPagesNavigation, user, membership] = await Promise.all([
|
const [intl, myPagesNavigation, user, membership] = await Promise.all([
|
||||||
getIntl(),
|
getIntl(),
|
||||||
serverClient().contentstack.myPages.navigation.get(),
|
serverClient().contentstack.myPages.navigation.get(),
|
||||||
serverClient().user.name(),
|
getName(),
|
||||||
serverClient().user.safeMembershipLevel(),
|
getMembershipLevelSafely(),
|
||||||
])
|
])
|
||||||
|
|
||||||
const membershipLevel = membership?.membershipLevel
|
const membershipLevel = membership?.membershipLevel
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { serverClient } from "@/lib/trpc/server"
|
import { getName } from "@/lib/trpc/memoizedRequests"
|
||||||
|
|
||||||
import LoginButton from "@/components/Current/Header/LoginButton"
|
import LoginButton from "@/components/Current/Header/LoginButton"
|
||||||
import ArrowRight from "@/components/Icons/ArrowRight"
|
import ArrowRight from "@/components/Icons/ArrowRight"
|
||||||
@@ -19,10 +19,10 @@ export default async function JoinLoyaltyContact({
|
|||||||
block,
|
block,
|
||||||
}: JoinLoyaltyContactProps) {
|
}: JoinLoyaltyContactProps) {
|
||||||
const intl = await getIntl()
|
const intl = await getIntl()
|
||||||
const user = await serverClient().user.name()
|
const username = await getName()
|
||||||
|
|
||||||
// Check if we have user, that means we are logged in.
|
// Check if we have user, that means we are logged in.
|
||||||
if (user) {
|
if (username) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import { serverClient } from "@/lib/trpc/server"
|
import { getName } from "@/lib/trpc/memoizedRequests"
|
||||||
|
|
||||||
import MyPagesSidebar from "@/components/MyPages/Sidebar"
|
import MyPagesSidebar from "@/components/MyPages/Sidebar"
|
||||||
|
|
||||||
export default async function MyPagesNavigation() {
|
export default async function MyPagesNavigation() {
|
||||||
const user = await serverClient().user.name()
|
const username = await getName()
|
||||||
|
|
||||||
// Check if we have user, that means we are logged in andt the My Pages menu can show.
|
// Check if we have user, that means we are logged in andt the My Pages menu can show.
|
||||||
if (!user) {
|
if (!username) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
return <MyPagesSidebar />
|
return <MyPagesSidebar />
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { getUserTracking } from "@/lib/trpc/memoizedRequests"
|
||||||
import { serverClient } from "@/lib/trpc/server"
|
import { serverClient } from "@/lib/trpc/server"
|
||||||
|
|
||||||
import RouterTransition from "@/components/TrackingSDK/RouterTransition"
|
import RouterTransition from "@/components/TrackingSDK/RouterTransition"
|
||||||
@@ -7,7 +8,7 @@ import TrackingSDKClient from "./Client"
|
|||||||
import { TrackingSDKPageData } from "@/types/components/tracking"
|
import { TrackingSDKPageData } from "@/types/components/tracking"
|
||||||
|
|
||||||
export const preloadUserTracking = () => {
|
export const preloadUserTracking = () => {
|
||||||
void serverClient().user.tracking()
|
void getUserTracking()
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function TrackingSDK({
|
export default async function TrackingSDK({
|
||||||
@@ -15,7 +16,7 @@ export default async function TrackingSDK({
|
|||||||
}: {
|
}: {
|
||||||
pageData: TrackingSDKPageData
|
pageData: TrackingSDKPageData
|
||||||
}) {
|
}) {
|
||||||
const userTrackingData = await serverClient().user.tracking()
|
const userTrackingData = await getUserTracking()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@@ -22,6 +22,32 @@ export const getCreditCardsSafely = cache(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
export const getMembershipLevel = cache(
|
||||||
|
async function getMemoizedMembershipLevel() {
|
||||||
|
return serverClient().user.membershipLevel()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
export const getMembershipLevelSafely = cache(
|
||||||
|
async function getMemoizedMembershipLevelSafely() {
|
||||||
|
return serverClient().user.safeMembershipLevel()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
export const getMembershipCards = cache(
|
||||||
|
async function getMemoizedMembershipCards() {
|
||||||
|
return serverClient().user.membershipCards()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
export const getName = cache(async function getMemoizedName() {
|
||||||
|
return serverClient().user.name()
|
||||||
|
})
|
||||||
|
|
||||||
|
export const getUserTracking = cache(async function getMemoizedUserTracking() {
|
||||||
|
return serverClient().user.tracking()
|
||||||
|
})
|
||||||
|
|
||||||
export const getHotelData = cache(async function getMemoizedHotelData(
|
export const getHotelData = cache(async function getMemoizedHotelData(
|
||||||
hotelId: string,
|
hotelId: string,
|
||||||
language: string
|
language: string
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { metrics } from "@opentelemetry/api"
|
import { metrics } from "@opentelemetry/api"
|
||||||
|
import { cache } from "react"
|
||||||
|
|
||||||
import * as api from "@/lib/api"
|
import * as api from "@/lib/api"
|
||||||
import {
|
import {
|
||||||
@@ -80,86 +81,87 @@ const getCreditCardsFailCounter = meter.createCounter(
|
|||||||
"trpc.user.creditCards-fail"
|
"trpc.user.creditCards-fail"
|
||||||
)
|
)
|
||||||
|
|
||||||
export async function getVerifiedUser({ session }: { session: Session }) {
|
export const getVerifiedUser = cache(
|
||||||
const now = Date.now()
|
async ({ session }: { session: Session }) => {
|
||||||
|
const now = Date.now()
|
||||||
if (session.token.expires_at && session.token.expires_at < now) {
|
if (session.token.expires_at && session.token.expires_at < now) {
|
||||||
return { error: true, cause: "token_expired" } as const
|
return { error: true, cause: "token_expired" } as const
|
||||||
}
|
}
|
||||||
getVerifiedUserCounter.add(1)
|
getVerifiedUserCounter.add(1)
|
||||||
console.info("api.user.profile getVerifiedUser start", JSON.stringify({}))
|
console.info("api.user.profile getVerifiedUser start", JSON.stringify({}))
|
||||||
const apiResponse = await api.get(api.endpoints.v1.profile, {
|
const apiResponse = await api.get(api.endpoints.v1.profile, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${session.token.access_token}`,
|
Authorization: `Bearer ${session.token.access_token}`,
|
||||||
},
|
},
|
||||||
})
|
|
||||||
|
|
||||||
if (!apiResponse.ok) {
|
|
||||||
const text = await apiResponse.text()
|
|
||||||
getVerifiedUserFailCounter.add(1, {
|
|
||||||
error_type: "http_error",
|
|
||||||
error: JSON.stringify({
|
|
||||||
status: apiResponse.status,
|
|
||||||
statusText: apiResponse.statusText,
|
|
||||||
text,
|
|
||||||
}),
|
|
||||||
})
|
})
|
||||||
console.error(
|
|
||||||
"api.user.profile getVerifiedUser error",
|
if (!apiResponse.ok) {
|
||||||
JSON.stringify({
|
const text = await apiResponse.text()
|
||||||
error: {
|
getVerifiedUserFailCounter.add(1, {
|
||||||
|
error_type: "http_error",
|
||||||
|
error: JSON.stringify({
|
||||||
status: apiResponse.status,
|
status: apiResponse.status,
|
||||||
statusText: apiResponse.statusText,
|
statusText: apiResponse.statusText,
|
||||||
text,
|
text,
|
||||||
},
|
}),
|
||||||
})
|
})
|
||||||
)
|
console.error(
|
||||||
if (apiResponse.status === 401) {
|
"api.user.profile getVerifiedUser error",
|
||||||
return { error: true, cause: "unauthorized" } as const
|
JSON.stringify({
|
||||||
} else if (apiResponse.status === 403) {
|
error: {
|
||||||
return { error: true, cause: "forbidden" } as const
|
status: apiResponse.status,
|
||||||
} else if (apiResponse.status === 404) {
|
statusText: apiResponse.statusText,
|
||||||
return { error: true, cause: "notfound" } as const
|
text,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
if (apiResponse.status === 401) {
|
||||||
|
return { error: true, cause: "unauthorized" } as const
|
||||||
|
} else if (apiResponse.status === 403) {
|
||||||
|
return { error: true, cause: "forbidden" } as const
|
||||||
|
} else if (apiResponse.status === 404) {
|
||||||
|
return { error: true, cause: "notfound" } as const
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
error: true,
|
||||||
|
cause: "unknown",
|
||||||
|
status: apiResponse.status,
|
||||||
|
} as const
|
||||||
}
|
}
|
||||||
return {
|
|
||||||
error: true,
|
|
||||||
cause: "unknown",
|
|
||||||
status: apiResponse.status,
|
|
||||||
} as const
|
|
||||||
}
|
|
||||||
|
|
||||||
const apiJson = await apiResponse.json()
|
const apiJson = await apiResponse.json()
|
||||||
if (!apiJson.data?.attributes) {
|
if (!apiJson.data?.attributes) {
|
||||||
getVerifiedUserFailCounter.add(1, {
|
getVerifiedUserFailCounter.add(1, {
|
||||||
error_type: "data_error",
|
error_type: "data_error",
|
||||||
})
|
|
||||||
console.error(
|
|
||||||
"api.user.profile getVerifiedUser data error",
|
|
||||||
JSON.stringify({
|
|
||||||
apiResponse: apiJson,
|
|
||||||
})
|
})
|
||||||
)
|
console.error(
|
||||||
return null
|
"api.user.profile getVerifiedUser data error",
|
||||||
}
|
JSON.stringify({
|
||||||
|
apiResponse: apiJson,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
const verifiedData = getUserSchema.safeParse(apiJson)
|
const verifiedData = getUserSchema.safeParse(apiJson)
|
||||||
if (!verifiedData.success) {
|
if (!verifiedData.success) {
|
||||||
getVerifiedUserFailCounter.add(1, {
|
getVerifiedUserFailCounter.add(1, {
|
||||||
error_type: "validation_error",
|
error_type: "validation_error",
|
||||||
error: JSON.stringify(verifiedData.error),
|
error: JSON.stringify(verifiedData.error),
|
||||||
})
|
|
||||||
console.error(
|
|
||||||
"api.user.profile validation error",
|
|
||||||
JSON.stringify({
|
|
||||||
errors: verifiedData.error,
|
|
||||||
})
|
})
|
||||||
)
|
console.error(
|
||||||
return null
|
"api.user.profile validation error",
|
||||||
|
JSON.stringify({
|
||||||
|
errors: verifiedData.error,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
getVerifiedUserSuccessCounter.add(1)
|
||||||
|
console.info("api.user.profile getVerifiedUser success", JSON.stringify({}))
|
||||||
|
return verifiedData
|
||||||
}
|
}
|
||||||
getVerifiedUserSuccessCounter.add(1)
|
)
|
||||||
console.info("api.user.profile getVerifiedUser success", JSON.stringify({}))
|
|
||||||
return verifiedData
|
|
||||||
}
|
|
||||||
|
|
||||||
function parsedUser(data: User, isMFA: boolean) {
|
function parsedUser(data: User, isMFA: boolean) {
|
||||||
const country = countries.find((c) => c.code === data.address.countryCode)
|
const country = countries.find((c) => c.code === data.address.countryCode)
|
||||||
|
|||||||
Reference in New Issue
Block a user