-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.mjs
More file actions
61 lines (56 loc) · 2.1 KB
/
Copy pathapi.mjs
File metadata and controls
61 lines (56 loc) · 2.1 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
import { RemoteError, UserError, mapApiError, mapNetworkError, missingTokenMessage } from "./errors.mjs";
function baseUrl(value) {
return value.replace(/\/+$/, "");
}
export function requireToken(env = process.env) {
const token = env.INVOLUTIONHELL_SATOKEN;
if (!token) throw new UserError(missingTokenMessage());
return token;
}
async function request(url, options, fetchImpl, operation) {
let response;
try {
response = await fetchImpl(url, { ...options, signal: AbortSignal.timeout(10_000) });
} catch (error) {
throw new RemoteError(mapNetworkError(error, operation));
}
if (!response.ok) throw new RemoteError(mapApiError(response.status, operation));
try {
return await response.json();
} catch {
throw new RemoteError("InvolutionHell returned an invalid JSON response.");
}
}
export async function publishPost(payload, {
env = process.env,
fetchImpl = fetch,
apiUrl = env.INVOLUTIONHELL_API_URL ?? "https://api.involutionhell.com",
siteUrl = env.INVOLUTIONHELL_SITE_URL ?? "https://involutionhell.com",
} = {}) {
const token = requireToken(env);
const body = await request(`${baseUrl(apiUrl)}/api/posts`, {
method: "POST",
headers: { "content-type": "application/json", satoken: token },
body: JSON.stringify(payload),
}, fetchImpl, "publish");
const post = body?.data;
if (!post?.slug || !post.authorUsername) {
throw new RemoteError("InvolutionHell returned an invalid publish response.");
}
const path = `/u/${encodeURIComponent(post.authorUsername)}/posts/${encodeURIComponent(post.slug)}`;
return new URL(path, `${baseUrl(siteUrl)}/`).toString();
}
export async function whoAmI({
env = process.env,
fetchImpl = fetch,
apiUrl = env.INVOLUTIONHELL_API_URL ?? "https://api.involutionhell.com",
} = {}) {
const token = requireToken(env);
const body = await request(`${baseUrl(apiUrl)}/auth/me`, {
headers: { satoken: token },
}, fetchImpl, "whoami");
if (typeof body?.data?.username !== "string" || !body.data.username) {
throw new RemoteError("InvolutionHell returned an invalid user response.");
}
return body.data.username;
}