Skip to content

Commit

Permalink
feat: support Route Resolver uid and brokenRoute options
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Aug 20, 2022
1 parent 10c2c5a commit b89044b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ export type {
RequestInitLike,
ResponseLike,
Route,
RouteDefinition,
RouteBrokenDefinition,
} from "./types";
62 changes: 60 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,75 @@ export interface Ordering {
*
* {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
*/
export interface Route {
export type Route = RouteDefinition | RouteBrokenDefinition;

/**
* A `routes` parameter that determines how a document's URL field is resolved.
*
* @example With a document's UID field.
*
* ```ts
* {
* "type": "page",
* "path": "/:uid"
* }
* ```
*
* @example With a Content Relationship `parent` field.
*
* ```ts
* {
* "type": "page",
* "path": "/:parent?/:uid",
* "resolvers": {
* "parent": "parent"
* }
* }
* ```
*
* {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
*/
export type RouteDefinition = {
/**
* The Custom Type of the document.
*/
type: string;

/**
* A specific UID to which this route definition is scoped. The route is only
* defined for the given UID.
*/
uid?: string;

/**
* The resolved path of the document with optional placeholders.
*/
path: string;

/**
* An object that lists the API IDs of the Content Relationships in the route.
*/
resolvers?: Record<string, string>;
}
};

/**
* A `routes` parameter that determines how a broken Link or Content
* Relationship field's URL is resolved. A broken link is a link whose linked
* document has been unpublished or deleted.
*
* @example
*
* ```ts
* {
* "brokenRoute": "/404"
* }
* ```
*
* {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
*/
export type RouteBrokenDefinition = {
/**
* The path to use when a document link is broken (i.e. the document no longer exists).
*/
brokenRoute: string;
};
10 changes: 9 additions & 1 deletion test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,19 @@ it("ignores the integration fields ref if the repository provides a null value",
it("uses client-provided routes in queries", async (ctx) => {
const queryResponse = prismicM.api.query({ seed: ctx.meta.name });

const routes = [
const routes: prismic.Route[] = [
{
type: "page",
path: "/:uid",
},
{
type: "page",
uid: "home",
path: "/",
},
{
brokenRoute: "/404",
},
];

mockPrismicRestAPIV2({
Expand Down

0 comments on commit b89044b

Please sign in to comment.