-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocale.ts
More file actions
71 lines (64 loc) · 2.01 KB
/
locale.ts
File metadata and controls
71 lines (64 loc) · 2.01 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
import * as z from "zod/mini";
import { NotFoundRequestError, request } from "../lib/request";
const LocaleSchema = z.object({
id: z.string(),
label: z.string(),
customName: z.nullable(z.string()),
isMaster: z.boolean(),
});
export type Locale = z.infer<typeof LocaleSchema>;
export async function getLocales(config: {
repo: string;
token: string | undefined;
host: string;
}): Promise<Locale[]> {
const { repo, token, host } = config;
const url = new URL("repository/locales", getLocaleServiceUrl(host));
url.searchParams.set("repository", repo);
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(
locale: { id: string; isMaster?: boolean; customName?: string },
config: { repo: string; token: string | undefined; host: string },
): Promise<Locale> {
const { repo, token, host } = config;
const url = new URL("repository/locales", getLocaleServiceUrl(host));
url.searchParams.set("repository", repo);
const response = await request(url, {
method: "POST",
body: {
id: locale.id,
isMaster: locale.isMaster ?? false,
...(locale.customName ? { customName: locale.customName } : {}),
},
headers: { Authorization: `Bearer ${token}` },
schema: LocaleSchema,
});
return response;
}
export async function removeLocale(
code: string,
config: { repo: string; token: string | undefined; host: string },
): Promise<void> {
const { repo, token, host } = config;
const url = new URL(`repository/locales/${encodeURIComponent(code)}`, getLocaleServiceUrl(host));
url.searchParams.set("repository", repo);
await request(url, {
method: "DELETE",
headers: { Authorization: `Bearer ${token}` },
});
}
function getLocaleServiceUrl(host: string): URL {
return new URL(`https://api.internal.${host}/locale/`);
}