Skip to content

Commit 8f50d7f

Browse files
authored
Merge pull request #31 from takker99/add-embed
Add embed
2 parents d906158 + d3b50a5 commit 8f50d7f

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed

deps/scrapbox.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
export type {
2+
BadRequestError,
23
ErrorLike,
34
ExportedData,
45
GuestUser,
56
ImportedData,
7+
InvalidURLError,
68
MemberProject,
79
MemberUser,
810
NotFoundError,
@@ -14,7 +16,9 @@ export type {
1416
PageList,
1517
Scrapbox,
1618
SearchedTitle,
17-
} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.0.8/mod.ts";
18-
import type { Page } from "https://raw.githubusercontent.com/scrapbox-jp/types/0.0.8/mod.ts";
19+
SessionError,
20+
TweetInfo,
21+
} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.1.2/mod.ts";
22+
import type { Page } from "https://raw.githubusercontent.com/scrapbox-jp/types/0.1.2/mod.ts";
1923
export * from "https://esm.sh/@progfay/[email protected]";
2024
export type Line = Page["lines"][0];

rest/getTweetInfo.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import type {
2+
BadRequestError,
3+
InvalidURLError,
4+
SessionError,
5+
TweetInfo,
6+
} from "../deps/scrapbox.ts";
7+
import { cookie, getCSRFToken } from "./auth.ts";
8+
import { UnexpectedResponseError } from "./error.ts";
9+
import { tryToErrorLike } from "../is.ts";
10+
import { ExtendedOptions, Result, setDefaults } from "./util.ts";
11+
12+
/** 指定したTweetの情報を取得する
13+
*
14+
* @param url 取得したいTweetのURL
15+
* @param init connect.sidなど
16+
* @return tweetの中身とか
17+
*/
18+
export const getTweetInfo = async (
19+
url: string | URL,
20+
init?: ExtendedOptions,
21+
): Promise<
22+
Result<
23+
TweetInfo,
24+
| SessionError
25+
| InvalidURLError
26+
| BadRequestError
27+
>
28+
> => {
29+
const { sid, hostName, fetch, csrf } = setDefaults(init ?? {});
30+
const path = `https://${hostName}/api/embed-text/twitter?url=${
31+
encodeURIComponent(url.toString())
32+
}`;
33+
34+
const res = await fetch(
35+
path,
36+
{
37+
method: "POST",
38+
headers: {
39+
"Content-Type": "application/json;charset=utf-8",
40+
"X-CSRF-TOKEN": csrf ?? await getCSRFToken(init),
41+
...(sid ? { Cookie: cookie(sid) } : {}),
42+
},
43+
body: JSON.stringify({ timeout: 3000 }),
44+
},
45+
);
46+
47+
if (!res.ok) {
48+
if (res.status === 422) {
49+
return {
50+
ok: false,
51+
value: {
52+
name: "InvalidURLError",
53+
message: (await res.json()).message as string,
54+
},
55+
};
56+
}
57+
const text = await res.json();
58+
const value = tryToErrorLike(text);
59+
if (!value) {
60+
throw new UnexpectedResponseError({
61+
path: new URL(path),
62+
...res,
63+
body: await res.text(),
64+
});
65+
}
66+
return {
67+
ok: false,
68+
value: value as
69+
| SessionError
70+
| BadRequestError,
71+
};
72+
}
73+
74+
const tweet = (await res.json()) as TweetInfo;
75+
return { ok: true, value: tweet };
76+
};

rest/getWebPageTitle.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import type {
2+
BadRequestError,
3+
InvalidURLError,
4+
SessionError,
5+
} from "../deps/scrapbox.ts";
6+
import { cookie, getCSRFToken } from "./auth.ts";
7+
import { UnexpectedResponseError } from "./error.ts";
8+
import { tryToErrorLike } from "../is.ts";
9+
import { ExtendedOptions, Result, setDefaults } from "./util.ts";
10+
11+
/** 指定したURLのweb pageのtitleをscrapboxのserver経由で取得する
12+
*
13+
* @param url 取得したいURL
14+
* @param init connect.sidなど
15+
* @return web pageのtilte
16+
*/
17+
export const getWebPageTitle = async (
18+
url: string | URL,
19+
init?: ExtendedOptions,
20+
): Promise<
21+
Result<
22+
string,
23+
| SessionError
24+
| InvalidURLError
25+
| BadRequestError
26+
>
27+
> => {
28+
const { sid, hostName, fetch, csrf } = setDefaults(init ?? {});
29+
const path = `https://${hostName}/api/embed-text/url?url=${
30+
encodeURIComponent(url.toString())
31+
}`;
32+
33+
const res = await fetch(
34+
path,
35+
{
36+
method: "POST",
37+
headers: {
38+
"Content-Type": "application/json;charset=utf-8",
39+
"X-CSRF-TOKEN": csrf ?? await getCSRFToken(init),
40+
...(sid ? { Cookie: cookie(sid) } : {}),
41+
},
42+
body: JSON.stringify({ timeout: 3000 }),
43+
},
44+
);
45+
46+
if (!res.ok) {
47+
if (res.status === 422) {
48+
return {
49+
ok: false,
50+
value: {
51+
name: "InvalidURLError",
52+
message: (await res.json()).message as string,
53+
},
54+
};
55+
}
56+
const text = await res.json();
57+
const value = tryToErrorLike(text);
58+
if (!value) {
59+
throw new UnexpectedResponseError({
60+
path: new URL(path),
61+
...res,
62+
body: await res.text(),
63+
});
64+
}
65+
return {
66+
ok: false,
67+
value: value as
68+
| SessionError
69+
| BadRequestError,
70+
};
71+
}
72+
73+
const { title } = (await res.json()) as { title: string };
74+
return { ok: true, value: title };
75+
};

0 commit comments

Comments
 (0)