From 23e291560723f10b387c60e1406a9e1cca919655 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Wed, 16 Oct 2024 12:36:46 +0200 Subject: [PATCH] refactor(pageData): improve type narrowing for query response result checking `response.data !== null` was not enough to narrow the type of `response` from `QueryResponseInitial` to just `QueryResponseInitial`, so a custom type guard has been defined --- src/app/(main)/[lang]/page.tsx | 10 ++----- src/utils/pageData.ts | 53 +++++++++++----------------------- src/utils/queryResponse.ts | 7 +++++ 3 files changed, 27 insertions(+), 43 deletions(-) create mode 100644 src/utils/queryResponse.ts diff --git a/src/app/(main)/[lang]/page.tsx b/src/app/(main)/[lang]/page.tsx index 58e067bca..b8644b5c7 100644 --- a/src/app/(main)/[lang]/page.tsx +++ b/src/app/(main)/[lang]/page.tsx @@ -2,6 +2,7 @@ import { Metadata } from "next"; import InformationSection from "src/components/informationSection/InformationSection"; import { getDraftModeInfo } from "src/utils/draftmode"; +import { isNonNullQueryResponse } from "src/utils/queryResponse"; import SectionRenderer from "src/utils/renderSection"; import { generateMetadataFromSeo } from "src/utils/seo"; import { LinkType } from "studio/lib/interfaces/navigation"; @@ -40,7 +41,7 @@ const Home = async ({ params }: Props) => { { perspective }, ); - if (initialLandingPage.data === null) { + if (!isNonNullQueryResponse(initialLandingPage)) { return ( { ); } - const initialData = { - ...initialLandingPage, - data: initialLandingPage.data, - }; - return initialLandingPage.data.sections.map((section, index) => ( diff --git a/src/utils/pageData.ts b/src/utils/pageData.ts index 3573ed02d..d071fbe7e 100644 --- a/src/utils/pageData.ts +++ b/src/utils/pageData.ts @@ -27,6 +27,8 @@ import { CUSTOMER_CASE_QUERY } from "studioShared/lib/queries/customerCases"; import { loadSharedQuery } from "studioShared/lib/store"; import { customerCaseID } from "studioShared/schemas/documents/customerCase"; +import { isNonNullQueryResponse } from "./queryResponse"; + type PageFromParams = { queryResponse: D; docType: T; @@ -43,7 +45,7 @@ async function fetchDynamicPage({ if (path.length === 0) { return null; } - const pageResult = await loadStudioQuery( + const queryResponse = await loadStudioQuery( PAGE_BY_SLUG_QUERY, { slug: path[0], @@ -51,14 +53,11 @@ async function fetchDynamicPage({ }, { perspective }, ); - if (pageResult.data === null) { + if (!isNonNullQueryResponse(queryResponse)) { return null; } return { - queryResponse: { - ...pageResult, - data: pageResult.data, - }, + queryResponse, docType: pageBuilderID, }; } @@ -89,7 +88,7 @@ async function fetchCompensationsPage({ perspective, }, ); - if (compensationsPageResult.data === null) { + if (!isNonNullQueryResponse(compensationsPageResult)) { return null; } const companyLocationsResult = await loadStudioQuery( @@ -97,7 +96,7 @@ async function fetchCompensationsPage({ {}, { perspective }, ); - if (companyLocationsResult.data === null) { + if (!isNonNullQueryResponse(companyLocationsResult)) { return null; } const localeDocumentResult = await loadStudioQuery( @@ -105,23 +104,14 @@ async function fetchCompensationsPage({ {}, { perspective }, ); - if (localeDocumentResult.data === null) { + if (!isNonNullQueryResponse(localeDocumentResult)) { return null; } return { queryResponse: { - compensationsPage: { - ...compensationsPageResult, - data: compensationsPageResult.data, - }, - companyLocations: { - ...companyLocationsResult, - data: companyLocationsResult.data, - }, - locale: { - ...localeDocumentResult, - data: localeDocumentResult.data, - }, + compensationsPage: compensationsPageResult, + companyLocations: companyLocationsResult, + locale: localeDocumentResult, }, docType: compensationsId, }; @@ -148,15 +138,12 @@ async function fetchCustomerCase({ }, { perspective }, ); - if (customerCasesPageResult.data === null) { + if (!isNonNullQueryResponse(customerCasesPageResult)) { return null; } if (path.length === 1) { return { - queryResponse: { - ...customerCasesPageResult, - data: customerCasesPageResult.data, - }, + queryResponse: customerCasesPageResult, docType: customerCasesPageID, }; } @@ -170,14 +157,11 @@ async function fetchCustomerCase({ perspective, }, ); - if (customerCaseResult.data === null) { + if (!isNonNullQueryResponse(customerCaseResult)) { return null; } return { - queryResponse: { - ...customerCaseResult, - data: customerCaseResult.data, - }, + queryResponse: customerCaseResult, docType: customerCaseID, }; } @@ -203,14 +187,11 @@ async function fetchLegalDocument({ perspective, }, ); - if (queryResponse.data === null) { + if (!isNonNullQueryResponse(queryResponse)) { return null; } return { - queryResponse: { - ...queryResponse, - data: queryResponse.data, - }, + queryResponse, docType: legalDocumentID, }; } diff --git a/src/utils/queryResponse.ts b/src/utils/queryResponse.ts new file mode 100644 index 000000000..6862ca393 --- /dev/null +++ b/src/utils/queryResponse.ts @@ -0,0 +1,7 @@ +import { QueryResponseInitial } from "@sanity/react-loader"; + +export function isNonNullQueryResponse( + value: QueryResponseInitial, +): value is QueryResponseInitial { + return value.data !== null; +}