Skip to content

Commit

Permalink
refactor: @prismicio/client now supports repository name
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Mar 2, 2022
1 parent 87e9526 commit d4ac98b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
16 changes: 5 additions & 11 deletions src/createClientFromOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import nodeFetch from "node-fetch";
import { Client, createClient, getEndpoint } from "@prismicio/client";
import { Client, createClient } from "@prismicio/client";

import {
PrismicPluginOptionsWithClient,
Expand Down Expand Up @@ -28,14 +28,8 @@ export const createClientFromOptions = (
): Client => {
return "client" in options
? options.client
: createClient(
/** @see Regex101 expression: {@link https://regex101.com/r/GT2cl7/1} */
/^(https?:)?\/\//gim.test(options.endpoint)
? options.endpoint
: getEndpoint(options.endpoint),
{
fetch: nodeFetch,
...options.clientConfig,
},
);
: createClient(options.endpoint, {
fetch: nodeFetch,
...options.clientConfig,
});
};
14 changes: 13 additions & 1 deletion test/createClientFromOptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import test from "ava";

import { Client } from "@prismicio/client";
import { Client, createClient } from "@prismicio/client";

import { createClientFromOptions } from "../src";

const repositoryName = "createClientFromOptions-test-ts";

test("returns a Prismic client instance", (t) => {
t.true(
createClientFromOptions({
client: createClient(repositoryName, {
fetch: async (url) => ({ status: 200, json: async () => url }),
}),
}) instanceof Client,
);
t.true(
createClientFromOptions({ endpoint: repositoryName }) instanceof Client,
);
t.true(
createClientFromOptions({
endpoint: `https://${repositoryName}.cdn.prismic.io/api/v2`,
}) instanceof Client,
);
});

0 comments on commit d4ac98b

Please sign in to comment.