-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-types.ts
More file actions
49 lines (45 loc) · 1.4 KB
/
custom-types.ts
File metadata and controls
49 lines (45 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type { CustomType, SharedSlice } from "@prismicio/types-internal/lib/customtypes";
import { NotFoundRequestError, request } from "../lib/request";
export async function getCustomTypes(config: {
repo: string;
token: string | undefined;
host: string;
}): Promise<CustomType[]> {
const { repo, token, host } = config;
const customTypesServiceUrl = getCustomTypesServiceUrl(host);
const url = new URL("customtypes", customTypesServiceUrl);
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: {
repo: string;
token: string | undefined;
host: string;
}): Promise<SharedSlice[]> {
const { repo, token, host } = config;
const customTypesServiceUrl = getCustomTypesServiceUrl(host);
const url = new URL("slices", customTypesServiceUrl);
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 {
return new URL(`https://customtypes.${host}/`);
}