import Prismic from '@prismicio/client' ; import { Query } from '@prismicio/types' ; import ResolvedApi from '@prismicio/client/types/ResolvedApi' ; import { CollectionVenuePrismicDocument , PopupDialogPrismicDocument , emptyPopupDialog , emptyPrismicQuery , GlobalAlertPrismicDocument , emptyGlobalAlert , } from '../services/prismic/documents' ; import { Handler } from './' ; export const defaultValue = { globalAlert : emptyGlobalAlert ( ) , popupDialog : emptyPopupDialog ( ) , collectionVenues : emptyPrismicQuery < CollectionVenuePrismicDocument > ( ) , } as const ; type Key = keyof typeof defaultValue ; export type PrismicData = { globalAlert : GlobalAlertPrismicDocument ; popupDialog : PopupDialogPrismicDocument ; collectionVenues : Query < CollectionVenuePrismicDocument > ; } ; export const handler = { defaultValue , fetch : fetchPrismicValues , } ; const fetchers = { globalAlert : async ( api ) => { const document = await api . getSingle ( 'global-alert' ) ; return document ; } , popupDialog : async ( api ) => { const document = await api . getSingle ( 'popup-dialog' ) ; return document ; } , collectionVenues : async ( api ) => { return api . query ( [ Prismic . Predicates . any ( 'document.type' , [ 'collection-venue' ] ) , ] ) ; } , } ; async function fetchPrismicValues ( ) { const api = await Prismic . getApi ( 'https://wellcomecollection.cdn.prismic.io/api/v2' ) ; const keys = Object . keys ( fetchers ) ; const values = await Promise . allSettled ( Object . values ( fetchers ) . map ( fetcher => fetcher ( api ) ) ) ; const zipped = keys . reduce ( ( acc , key , i ) => { const result = values [ i ] ; const value = result . status === 'fulfilled' ? result . value : null ; if ( result . status === 'rejected' ) { console . error ( `Failed to fetch ${ keys [ i ] } from Prismic` , result . reason ) ; } return { ... acc , [ key ] : value , } ; } , { } ) ; return zipped as PrismicData ; } export default handler ;