-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.ts
More file actions
132 lines (121 loc) · 3.17 KB
/
core.ts
File metadata and controls
132 lines (121 loc) · 3.17 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import * as z from "zod/mini";
import { NotFoundRequestError, request } from "../lib/request";
const PreviewSchema = z.object({
id: z.string(),
label: z.string(),
url: z.string(),
});
const GetPreviewsResponseSchema = z.object({
results: z.array(PreviewSchema),
});
export type Preview = z.infer<typeof PreviewSchema>;
export async function getPreviews(config: {
repo: string;
token: string | undefined;
host: string;
}): Promise<Preview[]> {
const { repo, token, host } = config;
const url = new URL(
"core/repository/preview_configs",
getCoreBaseUrl(repo, host),
);
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(
previewConfig: {
name: string;
websiteURL: string;
resolverPath: string | undefined;
},
config: { repo: string; token: string | undefined; host: string },
): Promise<void> {
const { repo, token, host } = config;
const url = new URL("previews/new", getCoreBaseUrl(repo, host));
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(
id: string,
config: { repo: string; token: string | undefined; host: string },
): Promise<void> {
const { repo, token, host } = config;
const url = new URL(
`previews/delete/${id}`,
getCoreBaseUrl(repo, host),
);
await request(url, {
method: "POST",
body: {},
credentials: { "prismic-auth": token },
});
}
const RepositoryResponseSchema = z.object({
simulator_url: z.optional(z.string()),
});
export async function getSimulatorUrl(config: {
repo: string;
token: string | undefined;
host: string;
}): Promise<string | undefined> {
const { repo, token, host } = config;
const url = new URL("core/repository", getCoreBaseUrl(repo, host));
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(
simulatorUrl: string,
config: { repo: string; token: string | undefined; host: string },
): Promise<void> {
const { repo, token, host } = config;
const url = new URL("core/repository", getCoreBaseUrl(repo, host));
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 {
return new URL(`https://${repo}.${host}/`);
}