-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.ts
53 lines (46 loc) · 1.11 KB
/
lib.ts
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
import {
parse,
type Parse,
type Remap,
type RemapAPIResponse,
} from "./lib/parser";
export * from "./lib/parser";
export * from "./lib/format";
export async function remap(
key: string,
parse: Parse,
signal?: AbortSignal,
): Promise<Remap> {
const response = await fetch("https://bun.report/remap", {
method: "POST",
body: key,
headers: {
"Content-Type": "application/json",
},
signal,
});
if (response.status !== 200) {
throw new Error(
`${response.status} ${response.statusText}\nPlease try again later.`,
);
}
const remap = (await response.json()) as RemapAPIResponse | { error: string };
if ("error" in remap) {
throw new Error(`${remap.error}`);
}
return {
version: remap.version,
message: parse.message,
os: parse.os,
arch: parse.arch,
commit: remap.commit,
addresses: remap.addresses,
command: remap.command,
features: remap.features,
};
}
export async function parseAndRemap(str: string): Promise<Remap | null> {
const parsed = await parse(str);
if (!parsed) return null;
return remap(str, parsed);
}