Skip to content

Commit

Permalink
fix: no longer require /customtypes in the endpoint option (#6)
Browse files Browse the repository at this point in the history
* fix: no longer require `/customtypes` in the `endpoint` option

* chore(deps): update dependencies

* refactor: use relative URLs with `./`
  • Loading branch information
angeloashmore committed Dec 19, 2022
1 parent 37bbad2 commit 62f1950
Show file tree
Hide file tree
Showing 16 changed files with 787 additions and 184 deletions.
704 changes: 639 additions & 65 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
"size-limit": "^8.1.0",
"standard-version": "^9.5.0",
"typescript": "^4.9.4",
"vite": "^3.2.5",
"vite-plugin-sdk": "^0.0.6",
"vite": "^4.0.1",
"vite-plugin-sdk": "^0.1.0",
"vitest": "^0.25.8"
},
"engines": {
Expand Down
42 changes: 28 additions & 14 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {
/**
* The default endpoint for the Prismic Custom Types API.
*/
const DEFAULT_CUSTOM_TYPES_API_ENDPOINT =
"https://customtypes.prismic.io/customtypes";
const DEFAULT_CUSTOM_TYPES_API_ENDPOINT = "https://customtypes.prismic.io";

/**
* Configuration for creating a `CustomTypesClient`.
Expand Down Expand Up @@ -103,6 +102,8 @@ export class CustomTypesClient {
/**
* The Prismic Custom Types API endpoint for the repository. The standard
* Custom Types API endpoint will be used if no value is provided.
*
* @defaultValue `https://customtypes.prismic.io`
*/
endpoint: string;

Expand All @@ -127,6 +128,19 @@ export class CustomTypesClient {
this.endpoint = config.endpoint || DEFAULT_CUSTOM_TYPES_API_ENDPOINT;
this.token = config.token;

// TODO: Remove the following `if` statement in v2.
//
// v1 erroneously assumed `/customtypes` would be part of
// `this.endpoint`, forcing all custom endpoints to include
// `/customtypes`.
//
// The client no longer assumes `/customtypes`. This `if`
// statement ensures backwards compatibility with existing
// custom endpoints that includes `/customtypes`.
if (/\/customtypes\/?$/.test(this.endpoint)) {
this.endpoint = this.endpoint.replace(/\/customtypes\/?$/, "");
}

if (typeof config.fetch === "function") {
this.fetchFn = config.fetch;
} else if (typeof globalThis.fetch === "function") {
Expand Down Expand Up @@ -156,7 +170,7 @@ export class CustomTypesClient {
async getAllCustomTypes<TCustomType extends prismicT.CustomTypeModel>(
params?: CustomTypesClientMethodParams & FetchParams,
): Promise<TCustomType[]> {
return await this.fetch<TCustomType[]>("", params);
return await this.fetch<TCustomType[]>("./customtypes", params);
}

/**
Expand All @@ -176,7 +190,7 @@ export class CustomTypesClient {
id: string,
params?: CustomTypesClientMethodParams & FetchParams,
): Promise<TCustomType> {
return await this.fetch<TCustomType>(id, params);
return await this.fetch<TCustomType>(`./customtypes/${id}`, params);
}

/**
Expand All @@ -199,7 +213,7 @@ export class CustomTypesClient {
params?: CustomTypesClientMethodParams & FetchParams,
): Promise<TCustomType> {
await this.fetch<undefined>(
"insert",
"./customtypes/insert",
params,
createPostFetchRequestInit(customType),
);
Expand Down Expand Up @@ -227,7 +241,7 @@ export class CustomTypesClient {
params?: CustomTypesClientMethodParams & FetchParams,
): Promise<TCustomType> {
await this.fetch<undefined>(
"update",
"./customtypes/update",
params,
createPostFetchRequestInit(customType),
);
Expand All @@ -250,7 +264,7 @@ export class CustomTypesClient {
id: TCustomTypeID,
params?: CustomTypesClientMethodParams & FetchParams,
): Promise<TCustomTypeID> {
await this.fetch<undefined>(id, params, {
await this.fetch<undefined>(`./customtypes/${id}`, params, {
method: "DELETE",
});

Expand All @@ -271,7 +285,7 @@ export class CustomTypesClient {
async getAllSharedSlices<TSharedSliceModel extends prismicT.SharedSliceModel>(
params?: CustomTypesClientMethodParams & FetchParams,
): Promise<TSharedSliceModel[]> {
return await this.fetch<TSharedSliceModel[]>("/slices", params);
return await this.fetch<TSharedSliceModel[]>("./slices", params);
}

/**
Expand All @@ -292,7 +306,7 @@ export class CustomTypesClient {
id: string,
params?: CustomTypesClientMethodParams & FetchParams,
): Promise<TSharedSliceModel> {
return await this.fetch<TSharedSliceModel>(`/slices/${id}`, params);
return await this.fetch<TSharedSliceModel>(`./slices/${id}`, params);
}

/**
Expand All @@ -315,7 +329,7 @@ export class CustomTypesClient {
params?: CustomTypesClientMethodParams & FetchParams,
): Promise<TSharedSliceModel> {
await this.fetch(
"/slices/insert",
"./slices/insert",
params,
createPostFetchRequestInit(slice),
);
Expand Down Expand Up @@ -343,7 +357,7 @@ export class CustomTypesClient {
params?: CustomTypesClientMethodParams & FetchParams,
): Promise<TSharedSliceModel> {
await this.fetch(
"/slices/update",
"./slices/update",
params,
createPostFetchRequestInit(slice),
);
Expand All @@ -366,7 +380,7 @@ export class CustomTypesClient {
id: TSharedSliceID,
params?: CustomTypesClientMethodParams & FetchParams,
): Promise<TSharedSliceID> {
await this.fetch(`/slices/${id}`, params, {
await this.fetch(`./slices/${id}`, params, {
method: "DELETE",
});

Expand Down Expand Up @@ -397,10 +411,10 @@ export class CustomTypesClient {
params: Partial<CustomTypesClientMethodParams> & FetchParams = {},
requestInit: RequestInitLike = {},
): Promise<T> {
const base = params.endpoint || this.endpoint;
const endpoint = params.endpoint || this.endpoint;
const url = new URL(
path,
base.endsWith("/") ? base : `${base}/`,
endpoint.endsWith("/") ? endpoint : `${endpoint}/`,
).toString();

const res = await this.fetchFn(url, {
Expand Down
2 changes: 1 addition & 1 deletion test/__testutils__/createClientConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const createClientConfig = (
.update(ctx.meta.name)
.digest("hex");
const token = crypto.createHash("md5").update(repositoryName).digest("hex");
const endpoint = `https://${repositoryName}.example.com/customtypes`;
const endpoint = `https://${repositoryName}.example.com`;

return {
repositoryName,
Expand Down
12 changes: 0 additions & 12 deletions test/__testutils__/resolveURL.ts

This file was deleted.

42 changes: 24 additions & 18 deletions test/client-getAllCustomTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ test("returns all Custom Types", async (ctx) => {
const client = createClient(ctx);

ctx.server.use(
msw.rest.get(client.endpoint, (req, res, ctx) => {
if (!isAuthorizedRequest(client, req)) {
return res(
ctx.status(403),
ctx.json({ message: "[MOCK FORBIDDEN ERROR]" }),
);
}
msw.rest.get(
new URL("./customtypes", client.endpoint).toString(),
(req, res, ctx) => {
if (!isAuthorizedRequest(client, req)) {
return res(
ctx.status(403),
ctx.json({ message: "[MOCK FORBIDDEN ERROR]" }),
);
}

return res(ctx.json(queryResponse));
}),
return res(ctx.json(queryResponse));
},
),
);

const res = await client.getAllCustomTypes();
Expand All @@ -38,16 +41,19 @@ test("uses params if provided", async (ctx) => {
};

ctx.server.use(
msw.rest.get(params.endpoint, (req, res, ctx) => {
if (!isAuthorizedRequest(params, req)) {
return res(
ctx.status(403),
ctx.json({ message: "[MOCK FORBIDDEN ERROR]" }),
);
}
msw.rest.get(
new URL("./customtypes", params.endpoint).toString(),
(req, res, ctx) => {
if (!isAuthorizedRequest(params, req)) {
return res(
ctx.status(403),
ctx.json({ message: "[MOCK FORBIDDEN ERROR]" }),
);
}

return res(ctx.json(queryResponse));
}),
return res(ctx.json(queryResponse));
},
),
);

const res = await client.getAllCustomTypes(params);
Expand Down
43 changes: 24 additions & 19 deletions test/client-getAllSharedSlices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as msw from "msw";

import { createClient } from "./__testutils__/createClient";
import { isAuthorizedRequest } from "./__testutils__/isAuthorizedRequest";
import { resolveURL } from "./__testutils__/resolveURL";

import * as prismicCustomTypes from "../src";

Expand All @@ -12,16 +11,19 @@ test("returns all Shared Slices", async (ctx) => {
const client = createClient(ctx);

ctx.server.use(
msw.rest.get(resolveURL(client.endpoint, "/slices"), (req, res, ctx) => {
if (!isAuthorizedRequest(client, req)) {
return res(
ctx.status(403),
ctx.json({ message: "[MOCK FORBIDDEN ERROR]" }),
);
}
msw.rest.get(
new URL("./slices", client.endpoint).toString(),
(req, res, ctx) => {
if (!isAuthorizedRequest(client, req)) {
return res(
ctx.status(403),
ctx.json({ message: "[MOCK FORBIDDEN ERROR]" }),
);
}

return res(ctx.json(queryResponse));
}),
return res(ctx.json(queryResponse));
},
),
);

const res = await client.getAllSharedSlices();
Expand All @@ -39,16 +41,19 @@ test("uses params if provided", async (ctx) => {
};

ctx.server.use(
msw.rest.get(resolveURL(params.endpoint, "/slices"), (req, res, ctx) => {
if (!isAuthorizedRequest(params, req)) {
return res(
ctx.status(403),
ctx.json({ message: "[MOCK FORBIDDEN ERROR]" }),
);
}
msw.rest.get(
new URL("./slices", params.endpoint).toString(),
(req, res, ctx) => {
if (!isAuthorizedRequest(params, req)) {
return res(
ctx.status(403),
ctx.json({ message: "[MOCK FORBIDDEN ERROR]" }),
);
}

return res(ctx.json(queryResponse));
}),
return res(ctx.json(queryResponse));
},
),
);

const res = await client.getAllSharedSlices(params);
Expand Down
7 changes: 3 additions & 4 deletions test/client-getCustomTypeByID.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import { createClient } from "./__testutils__/createClient";
import { isAuthorizedRequest } from "./__testutils__/isAuthorizedRequest";

import * as prismicCustomTypes from "../src";
import { resolveURL } from "./__testutils__/resolveURL";

test("returns a Custom Type by ID", async (ctx) => {
const customType = ctx.mock.model.customType();
const client = createClient(ctx);

ctx.server.use(
msw.rest.get(
resolveURL(client.endpoint, customType.id),
new URL(`./customtypes/${customType.id}`, client.endpoint).toString(),
(req, res, ctx) => {
if (!isAuthorizedRequest(client, req)) {
return res(
Expand Down Expand Up @@ -43,7 +42,7 @@ test("uses params if provided", async (ctx) => {

ctx.server.use(
msw.rest.get(
resolveURL(params.endpoint, customType.id),
new URL(`./customtypes/${customType.id}`, params.endpoint).toString(),
(req, res, ctx) => {
if (!isAuthorizedRequest(params, req)) {
return res(
Expand All @@ -68,7 +67,7 @@ test("throws NotFoundError if a matching Custom Type was not found", async (ctx)

ctx.server.use(
msw.rest.get(
resolveURL(client.endpoint, customType.id),
new URL(`./customtypes/${customType.id}`, client.endpoint).toString(),
(req, res, ctx) => {
if (!isAuthorizedRequest(client, req)) {
return res(
Expand Down
7 changes: 3 additions & 4 deletions test/client-getSharedSliceByID.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import { createClient } from "./__testutils__/createClient";
import { isAuthorizedRequest } from "./__testutils__/isAuthorizedRequest";

import * as prismicCustomTypes from "../src";
import { resolveURL } from "./__testutils__/resolveURL";

test("returns a Shared Slice by ID", async (ctx) => {
const sharedSlice = ctx.mock.model.sharedSlice();
const client = createClient(ctx);

ctx.server.use(
msw.rest.get(
resolveURL(client.endpoint, `/slices/${sharedSlice.id}`),
new URL(`./slices/${sharedSlice.id}`, client.endpoint).toString(),
(req, res, ctx) => {
if (!isAuthorizedRequest(client, req)) {
return res(
Expand Down Expand Up @@ -43,7 +42,7 @@ test("uses params if provided", async (ctx) => {

ctx.server.use(
msw.rest.get(
resolveURL(params.endpoint, `/slices/${sharedSlice.id}`),
new URL(`./slices/${sharedSlice.id}`, params.endpoint).toString(),
(req, res, ctx) => {
if (!isAuthorizedRequest(params, req)) {
return res(
Expand All @@ -68,7 +67,7 @@ test("throws NotFoundError if a matching Custom Type was not found", async (ctx)

ctx.server.use(
msw.rest.get(
resolveURL(client.endpoint, `/slices/${sharedSlice.id}`),
new URL(`./slices/${sharedSlice.id}`, client.endpoint).toString(),
(req, res, ctx) => {
if (!isAuthorizedRequest(client, req)) {
return res(
Expand Down

0 comments on commit 62f1950

Please sign in to comment.