Skip to content

Commit

Permalink
fix: support documents without slugs in documentToLinkField
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Dec 6, 2021
1 parent 4c7c8bc commit a60f4be
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/documentToLinkField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
PrismicDocument,
} from "@prismicio/types";

type SetOptional<T, Keys extends keyof T> = Omit<T, Keys> &
Partial<Pick<T, Keys>>;

/**
* Converts a document into a link field, this is useful when crawling the API
* for document links
Expand All @@ -14,7 +17,9 @@ import {
* @returns The equivalent link field to use with `asLink()`
* @internal
*/
export const documentToLinkField = <TDocument extends PrismicDocument>(
export const documentToLinkField = <
TDocument extends SetOptional<PrismicDocument, "slugs">,
>(
prismicDocument: TDocument,
): FilledLinkToDocumentField<
TDocument["type"],
Expand Down
22 changes: 22 additions & 0 deletions test/documentToLinkField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,25 @@ test("returns equivalent link field from given document with non-empty data", (t
data: { foo: "bar" },
});
});

// This test checks support for Gatsby users. The `slugs` field is not
// queriable in Gatsby since it is deprecated.
// Deprecation info: https://community.prismic.io/t/what-are-slugs/6493
test("supports documents without slugs field", (t) => {
const document = {
...documentFixture.empty,
url: null,
slugs: undefined,
};

t.deepEqual(documentToLinkField(document), {
link_type: LinkType.Document,
id: "XvoFFREAAM0WGBng",
uid: "test",
type: "page",
tags: [],
lang: "en-us",
url: undefined,
slug: undefined,
});
});

0 comments on commit a60f4be

Please sign in to comment.