Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 53 additions & 25 deletions src/clients/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from "zod/mini";

import { request } from "../lib/request";
import { NotFoundRequestError, request } from "../lib/request";

const PreviewSchema = z.object({
id: z.string(),
Expand All @@ -24,11 +24,18 @@ export async function getPreviews(config: {
"core/repository/preview_configs",
getCoreBaseUrl(repo, host),
);
const response = await request(url, {
credentials: { "prismic-auth": token },
schema: GetPreviewsResponseSchema,
});
return response.results;
try {
const response = await request(url, {
credentials: { "prismic-auth": token },
schema: GetPreviewsResponseSchema,
});
return response.results;
} catch (error) {
if (error instanceof NotFoundRequestError) {
error.message = `Repository not found: ${repo}`;
}
throw error;
}
}

export async function addPreview(
Expand All @@ -41,15 +48,22 @@ export async function addPreview(
): Promise<void> {
const { repo, token, host } = config;
const url = new URL("previews/new", getCoreBaseUrl(repo, host));
await request(url, {
method: "POST",
body: {
name: previewConfig.name,
websiteURL: previewConfig.websiteURL,
resolverPath: previewConfig.resolverPath,
},
credentials: { "prismic-auth": token },
});
try {
await request(url, {
method: "POST",
body: {
name: previewConfig.name,
websiteURL: previewConfig.websiteURL,
resolverPath: previewConfig.resolverPath,
},
credentials: { "prismic-auth": token },
});
} catch (error) {
if (error instanceof NotFoundRequestError) {
error.message = `Repository not found: ${repo}`;
}
throw error;
}
}

export async function removePreview(
Expand Down Expand Up @@ -79,11 +93,18 @@ export async function getSimulatorUrl(config: {
}): Promise<string | undefined> {
const { repo, token, host } = config;
const url = new URL("core/repository", getCoreBaseUrl(repo, host));
const response = await request(url, {
credentials: { "prismic-auth": token },
schema: RepositoryResponseSchema,
});
return response.simulator_url;
try {
const response = await request(url, {
credentials: { "prismic-auth": token },
schema: RepositoryResponseSchema,
});
return response.simulator_url;
} catch (error) {
if (error instanceof NotFoundRequestError) {
error.message = `Repository not found: ${repo}`;
}
throw error;
}
}

export async function setSimulatorUrl(
Expand All @@ -92,11 +113,18 @@ export async function setSimulatorUrl(
): Promise<void> {
const { repo, token, host } = config;
const url = new URL("core/repository", getCoreBaseUrl(repo, host));
await request(url, {
method: "PATCH",
body: { simulator_url: simulatorUrl },
credentials: { "prismic-auth": token },
});
try {
await request(url, {
method: "PATCH",
body: { simulator_url: simulatorUrl },
credentials: { "prismic-auth": token },
});
} catch (error) {
if (error instanceof NotFoundRequestError) {
error.message = `Repository not found: ${repo}`;
}
throw error;
}
}

function getCoreBaseUrl(repo: string, host: string): URL {
Expand Down
32 changes: 23 additions & 9 deletions src/clients/custom-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CustomType, SharedSlice } from "@prismicio/types-internal/lib/customtypes";

import { request } from "../lib/request";
import { NotFoundRequestError, request } from "../lib/request";

export async function getCustomTypes(config: {
repo: string;
Expand All @@ -10,10 +10,17 @@ export async function getCustomTypes(config: {
const { repo, token, host } = config;
const customTypesServiceUrl = getCustomTypesServiceUrl(host);
const url = new URL("customtypes", customTypesServiceUrl);
const response = await request<CustomType[]>(url, {
headers: { repository: repo, Authorization: `Bearer ${token}` },
});
return response;
try {
const response = await request<CustomType[]>(url, {
headers: { repository: repo, Authorization: `Bearer ${token}` },
});
return response;
} catch (error) {
if (error instanceof NotFoundRequestError) {
error.message = `Repository not found: ${repo}`;
}
throw error;
}
}

export async function getSlices(config: {
Expand All @@ -24,10 +31,17 @@ export async function getSlices(config: {
const { repo, token, host } = config;
const customTypesServiceUrl = getCustomTypesServiceUrl(host);
const url = new URL("slices", customTypesServiceUrl);
const response = await request<SharedSlice[]>(url, {
headers: { repository: repo, Authorization: `Bearer ${token}` },
});
return response;
try {
const response = await request<SharedSlice[]>(url, {
headers: { repository: repo, Authorization: `Bearer ${token}` },
});
return response;
} catch (error) {
if (error instanceof NotFoundRequestError) {
error.message = `Repository not found: ${repo}`;
}
throw error;
}
}

function getCustomTypesServiceUrl(host: string): URL {
Expand Down
19 changes: 13 additions & 6 deletions src/clients/locale.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from "zod/mini";

import { request } from "../lib/request";
import { NotFoundRequestError, request } from "../lib/request";

const LocaleSchema = z.object({
id: z.string(),
Expand All @@ -19,11 +19,18 @@ export async function getLocales(config: {
const { repo, token, host } = config;
const url = new URL("repository/locales", getLocaleServiceUrl(host));
url.searchParams.set("repository", repo);
const response = await request(url, {
headers: { Authorization: `Bearer ${token}` },
schema: z.object({ results: z.array(LocaleSchema) }),
});
return response.results;
try {
const response = await request(url, {
headers: { Authorization: `Bearer ${token}` },
schema: z.object({ results: z.array(LocaleSchema) }),
});
return response.results;
} catch (error) {
if (error instanceof NotFoundRequestError) {
error.message = `Repository not found: ${repo}`;
}
throw error;
}
}

export async function upsertLocale(
Expand Down
25 changes: 16 additions & 9 deletions src/clients/repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from "zod/mini";

import { request } from "../lib/request";
import { NotFoundRequestError, request } from "../lib/request";

const RepositorySchema = z.object({
quotas: z.optional(
Expand All @@ -20,14 +20,21 @@ export async function getRepository(config: {
const { repo, token, host } = config;
const url = getRepositoryServiceUrl(host);
url.searchParams.set("repository", repo);
const response = await request(url, {
headers: {
Authorization: `Bearer ${token}`,
repository: repo,
},
schema: RepositorySchema,
});
return response;
try {
const response = await request(url, {
headers: {
Authorization: `Bearer ${token}`,
repository: repo,
},
schema: RepositorySchema,
});
return response;
} catch (error) {
if (error instanceof NotFoundRequestError) {
error.message = `Repository not found: ${repo}`;
}
throw error;
}
}

function getRepositoryServiceUrl(host: string): URL {
Expand Down
Loading
Loading