-
Notifications
You must be signed in to change notification settings - Fork 1
/
pages.ts
46 lines (41 loc) · 1.22 KB
/
pages.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
import type {
NotFoundError,
NotMemberError,
Page,
} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.0.2/scrapbox.d.ts";
import { isJSON } from "https://deno.land/x/is_json@v1.0.2/mod.ts";
import { createCookieHeaders, Options } from "./options.ts";
import { toLc } from "./lc.ts";
type PageProps = {
project: string;
title: string;
followRename?: boolean;
} & Options;
export const pages = {
async get(
{ project, title, sid, fetch = globalThis.fetch, followRename = false }:
PageProps,
) {
const response = await fetch(
`https://scrapbox.io/api/pages/${project}/${toLc(title)}${
followRename ? `?followRename=true` : ""
}`,
sid !== undefined ? createCookieHeaders(sid) : undefined,
);
if (!response.ok) {
const text = await response.text();
if (!isJSON(text)) throw Error(response.statusText);
const json = await response.json();
switch (json.name) {
case "NotMemberError":
return json as NotMemberError;
case "NotFoundError":
return json as NotFoundError;
default:
throw Error(response.statusText);
}
}
const json = (await response.json()) as Page;
return json;
},
};