Skip to content

Commit

Permalink
docs: document linkResolverFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed May 11, 2021
1 parent be75fb5 commit 3172393
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 2 additions & 4 deletions src/asLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import { LinkField, LinkType } from "@prismicio/types";
import { ArgumentError } from "./ArgumentError";
import { LinkResolverFunction } from "./types";

// TODO: provide a way to handle v1 fields

/**
* Resolves any type of link field to a URL
*
* @param linkField - Any kind of link field to resolve
* @param linkResolver - A link resolver function, without it you're expected to use the `routes` from the API
*
* @returns Resolved URL, an empty string if provided link is empty
* @returns Resolved URL, null if provided link is empty
*
* @typeParam LinkResolverFunctionReturnType - Link resolver function return type
*
Expand All @@ -29,8 +27,8 @@ export function asLink<LinkResolverFunctionReturnType = string>(
}

switch (linkField.link_type) {
case LinkType.Web:
case LinkType.Media:
case LinkType.Web:
return linkField.url;

case LinkType.Document:
Expand Down
15 changes: 13 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { FilledLinkToDocumentField } from "@prismicio/types";
import type { FilledLinkToDocumentField } from "@prismicio/types";

/**
* Resolves a link to a Prismic document to a URL
*
* @param linkToDocumentField - A document link field to resolve
*
* @returns Resolved URL
*
* @typeParam ReturnType - Return type of your link resolver function, useful if you prefer to return a complex object
*
* @see Prismic link resolver documentation: {@link https://prismic.io/docs/technologies/link-resolver-javascript}
*/
export type LinkResolverFunction<ReturnType = string> = (
doc: Omit<FilledLinkToDocumentField, "url">
linkToDocumentField: Omit<FilledLinkToDocumentField, "url">
) => ReturnType;

0 comments on commit 3172393

Please sign in to comment.