Skip to content

Commit

Permalink
feat: lift cms client helper methods to their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
mcky committed Dec 1, 2022
1 parent 2743954 commit 6e2cf93
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 125 deletions.
129 changes: 129 additions & 0 deletions src/node-lib/cms/sanity-client/cmsMethods.ts
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 };
126 changes: 1 addition & 125 deletions src/node-lib/cms/sanity-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,7 @@ import {
} from "../../../common-lib/cms-types";
import { webinarsListingPageSchema } from "../../../common-lib/cms-types/webinarsListingPage";

import { resolveSanityReferences } from "./resolveSanityReferences";
import { parseResults } from "./parseResults";
import { resolveHubspotFromReferences } from "./resolveHubspotFromReferences";

export type Params = {
previewMode?: boolean;
};

export type ListParams = Params & {
limit?: number;
};

/**
* Search for references to other documents or hubspot forms within
* sanity documents and "resolve" them to their actual values
*/
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;
};
import { getSingleton, getBySlug, getList } from "./cmsMethods";

const getSanityClient = () => ({
webinarsListingPage: getSingleton(
Expand Down Expand Up @@ -206,101 +179,4 @@ const getSanityClient = () => ({
),
});

type GQLMethod = typeof sanityGraphqlApi[keyof typeof sanityGraphqlApi];

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);
};
};

/**
* 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
*/
const getDraftFilterParam = (
isPreviewMode: boolean | undefined
): { is_draft: boolean | undefined } =>
isPreviewMode ? { is_draft: undefined } : { is_draft: false };

export default getSanityClient;

0 comments on commit 6e2cf93

Please sign in to comment.