-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: lift cms client helper methods to their own file
- Loading branch information
Showing
2 changed files
with
130 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { z } from "zod"; | ||
|
||
import sanityGraphqlApi from "../../sanity-graphql"; | ||
|
||
import { parseResults } from "./parseResults"; | ||
import { resolveHubspotFromReferences } from "./resolveHubspotFromReferences"; | ||
import { resolveSanityReferences } from "./resolveSanityReferences"; | ||
|
||
type GQLMethod = typeof sanityGraphqlApi[keyof typeof sanityGraphqlApi]; | ||
|
||
export type Params = { | ||
previewMode?: boolean; | ||
}; | ||
|
||
export type ListParams = Params & { | ||
limit?: number; | ||
}; | ||
|
||
export const getBySlug = < | ||
Method extends GQLMethod, | ||
Response extends Awaited<ReturnType<Method>>, | ||
Data extends Record<string, unknown> | undefined, | ||
Schema extends z.ZodTypeAny | ||
>( | ||
graphqlMethod: Method, | ||
schema: Schema, | ||
getResultValue: (res: Response) => Data | ||
) => { | ||
return async (slug: string, { previewMode, ...params }: Params = {}) => { | ||
const result = await graphqlMethod({ | ||
isDraftFilter: getDraftFilterParam(previewMode), | ||
slug, | ||
...params, | ||
}); | ||
|
||
const pageData = getResultValue(result); | ||
|
||
if (!pageData) { | ||
return null; | ||
} | ||
|
||
const withResolvedReferences = await resolveEmbeddedReferences(pageData); | ||
|
||
return parseResults(schema, withResolvedReferences, previewMode); | ||
}; | ||
}; | ||
|
||
export const getSingleton = < | ||
Method extends GQLMethod, | ||
Response extends Awaited<ReturnType<Method>>, | ||
Data extends Record<string, unknown> | undefined, | ||
Schema extends z.ZodTypeAny | ||
>( | ||
graphqlMethod: Method, | ||
schema: Schema, | ||
getResultValue: (res: Response) => Data | ||
) => { | ||
return async ({ previewMode, ...params }: Params = {}) => { | ||
const result = await graphqlMethod({ | ||
isDraftFilter: getDraftFilterParam(previewMode), | ||
...params, | ||
}); | ||
|
||
const pageData = getResultValue(result); | ||
|
||
if (!pageData) { | ||
return null; | ||
} | ||
|
||
const withResolvedReferences = await resolveEmbeddedReferences(pageData); | ||
|
||
return parseResults(schema, withResolvedReferences, previewMode); | ||
}; | ||
}; | ||
|
||
export const getList = < | ||
Method extends GQLMethod, | ||
Resp extends Awaited<ReturnType<Method>>, | ||
Data extends Array<Record<string, unknown>>, | ||
Schema extends z.ZodTypeAny | ||
>( | ||
graphqlMethod: Method, | ||
schema: Schema, | ||
getPageData: (res: Resp) => Data | ||
) => { | ||
return async ({ previewMode, ...params }: ListParams = {}) => { | ||
const results = await graphqlMethod({ | ||
isDraftFilter: getDraftFilterParam(previewMode), | ||
...params, | ||
}); | ||
|
||
const pageData = getPageData(results); | ||
|
||
if (!pageData) { | ||
return []; | ||
} | ||
|
||
const withResolvedReferences = await resolveEmbeddedReferences(pageData); | ||
|
||
return parseResults(schema, withResolvedReferences, previewMode); | ||
}; | ||
}; | ||
|
||
/** | ||
* Search for references to other documents or hubspot forms within | ||
* sanity documents and "resolve" them to their actual values | ||
*/ | ||
export const resolveEmbeddedReferences = async < | ||
T extends Record<string, unknown> | Record<string, unknown>[] | ||
>( | ||
document: T | ||
): Promise<T> => { | ||
const withPortableTextReferences = await resolveSanityReferences(document); | ||
const withForms = await resolveHubspotFromReferences( | ||
withPortableTextReferences | ||
); | ||
|
||
return withForms; | ||
}; | ||
|
||
/** | ||
* When in preview mode we want to fetch draft and non-draft | ||
* content and filter client side, but for production we | ||
* never want draft content | ||
*/ | ||
export const getDraftFilterParam = ( | ||
isPreviewMode: boolean | undefined | ||
): { is_draft: boolean | undefined } => | ||
isPreviewMode ? { is_draft: undefined } : { is_draft: false }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters