Skip to content

Commit

Permalink
fix: optional Link Resolver for asLink and asHTML (#22)
Browse files Browse the repository at this point in the history
Closes #21
  • Loading branch information
angeloashmore authored Jul 2, 2021
1 parent 6dec3ab commit b007ff1
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 68 deletions.
4 changes: 2 additions & 2 deletions src/asHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from "./types";

function defaultHTMLSerializer(
linkResolver: LinkResolverFunction<string>,
linkResolver: LinkResolverFunction<string> | undefined,
_type: Parameters<HTMLFunctionSerializer>[0],
node: Parameters<HTMLFunctionSerializer>[1],
content: Parameters<HTMLFunctionSerializer>[2],
Expand Down Expand Up @@ -74,7 +74,7 @@ function defaultHTMLSerializer(

export function asHTML(
richTextField: RichTextField,
linkResolver: LinkResolverFunction<string>,
linkResolver?: LinkResolverFunction<string>,
htmlSerializer?: HTMLFunctionSerializer | HTMLMapSerializer,
): string {
let serializer: RichTextFunctionSerializer<string>;
Expand Down
14 changes: 3 additions & 11 deletions src/asLink.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { LinkField, LinkType } from "@prismicio/types";
import { ArgumentError } from "./lib/ArgumentError";
import { LinkResolverFunction } from "./types";

/**
Expand All @@ -17,15 +16,8 @@ import { LinkResolverFunction } from "./types";
*/
export const asLink = <LinkResolverFunctionReturnType = string>(
linkField: LinkField,
linkResolver: LinkResolverFunction<LinkResolverFunctionReturnType>,
):
| ReturnType<LinkResolverFunction<LinkResolverFunctionReturnType>>
| string
| null => {
if (typeof linkResolver !== "function") {
throw new ArgumentError("linkResolver", "function", typeof linkResolver);
}

linkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType>,
): LinkResolverFunctionReturnType | string | null => {
if (!linkField) {
return null;
}
Expand All @@ -41,7 +33,7 @@ export const asLink = <LinkResolverFunctionReturnType = string>(
return linkField.url;
} else if ("id" in linkField) {
// ...when not...
return linkResolver(linkField);
return linkResolver ? linkResolver(linkField) : null;
} else {
// ...when empty
return null;
Expand Down
9 changes: 2 additions & 7 deletions src/graphql/asLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
LinkField,
LinkType,
} from "@prismicio/types/dist/graphql";
import { ArgumentError } from "../lib/ArgumentError";
import { LinkResolverFunction } from "./types";

/**
Expand All @@ -26,7 +25,7 @@ export const asLink = <
LinkResolverFunctionReturnType = string,
>(
linkField: LinkField<LinkResolverLinkToDocumentField>,
linkResolver: LinkResolverFunction<
linkResolver?: LinkResolverFunction<
LinkResolverLinkToDocumentField,
LinkResolverFunctionReturnType
>,
Expand All @@ -39,18 +38,14 @@ export const asLink = <
>
| string
| null => {
if (typeof linkResolver !== "function") {
throw new ArgumentError("linkResolver", "function", typeof linkResolver);
}

if (!linkField) {
return null;
}

if ("url" in linkField) {
return linkField.url;
} else if (linkField._linkType === LinkType.Document) {
return linkResolver(linkField);
return linkResolver ? linkResolver(linkField) : null;
} else {
return null;
}
Expand Down
14 changes: 0 additions & 14 deletions src/lib/ArgumentError.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib/serializerHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const serializePreFormatted = (node: RTPreformattedNode): string => {

// TODO: Check link behavior with image + maybe rewrap with paragraph
export const serializeImage = (
_linkResolver: LinkResolverFunction<string>,
_linkResolver: LinkResolverFunction<string> | undefined,
node: RTImageNode,
): string => {
return `<img src="${node.url}" alt="${node.alt}"${
Expand All @@ -49,7 +49,7 @@ export const serializeEmbed = (node: RTEmbedNode): string => {
};

export const serializeHyperlink = (
linkResolver: LinkResolverFunction,
linkResolver: LinkResolverFunction | undefined,
node: RTLinkNode,
children: string[],
): string => {
Expand Down
25 changes: 9 additions & 16 deletions test/asLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@ import test from "ava";
import { linkResolver } from "./__testutils__/linkResolver";

import { asLink } from "../src";
import { ArgumentError } from "../src/lib/ArgumentError";

test("throws when no linkResolver is provided", (t) => {
const error = t.throws(
() => {
// @ts-expect-error testing JavaScript failsafe on purpose
asLink();
},
{ instanceOf: ArgumentError },
);

t.is(
error.message,
"Expected argument `linkResolver` to be of type `function` but received type `undefined`",
);
});

test("returns null when link field is falsy", (t) => {
const field = undefined;
Expand Down Expand Up @@ -82,6 +66,15 @@ test("resolves a link to document field with `apiOptions.routes`", (t) => {
t.is(asLink(field, linkResolver), "/test");
});

test("returns null when given a document field and linkResolver is not provided ", (t) => {
const field = {
id: "XvoFFREAAM0WGBng",
link_type: "Document",
};

t.is(asLink(field), null);
});

test("resolves a link to web field", (t) => {
const field = {
link_type: "Web",
Expand Down
27 changes: 11 additions & 16 deletions test/graphql-asLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { FilledMinimalLinkToDocumentField } from "@prismicio/types/dist/graphql"
import test from "ava";

import { asLink, LinkResolverFunction } from "../src/graphql";
import { ArgumentError } from "../src/lib/ArgumentError";

interface MyLinkToDocumentField extends FilledMinimalLinkToDocumentField {
_meta: {
Expand All @@ -13,21 +12,6 @@ interface MyLinkToDocumentField extends FilledMinimalLinkToDocumentField {
const linkResolver: LinkResolverFunction<MyLinkToDocumentField> = (doc) =>
`/${doc._meta.uid}`;

test("throws when no linkResolver is provided", (t) => {
const error = t.throws(
() => {
// @ts-expect-error testing JavaScript failsafe
asLink();
},
{ instanceOf: ArgumentError },
);

t.is(
error.message,
"Expected argument `linkResolver` to be of type `function` but received type `undefined`",
);
});

test("returns null when link field is falsy", (t) => {
const field = undefined;

Expand Down Expand Up @@ -81,3 +65,14 @@ test("resolves a link to document field", (t) => {

t.is(asLink(field, linkResolver), "/test");
});

test("returns null when given a document field and linkResolver is not provided ", (t) => {
const field = {
_linkType: "Link.document",
_meta: {
uid: "test",
},
} as const;

t.is(asLink(field), null);
});

0 comments on commit b007ff1

Please sign in to comment.