Skip to content

Commit 2473a00

Browse files
committed
✨ add a wrapper of /api/code/:project/:title/:filename
1 parent 697844a commit 2473a00

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

rest/getCodeBlock.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import type {
2+
NotFoundError,
3+
NotLoggedInError,
4+
NotMemberError,
5+
} from "../deps/scrapbox-rest.ts";
6+
import { cookie } from "./auth.ts";
7+
import { makeError } from "./error.ts";
8+
import { encodeTitleURI } from "../title.ts";
9+
import { BaseOptions, Result, setDefaults } from "./util.ts";
10+
11+
const getCodeBlock_toRequest: GetCodeBlock["toRequest"] = (
12+
project,
13+
title,
14+
filename,
15+
options,
16+
) => {
17+
const { sid, hostName } = setDefaults(options ?? {});
18+
const path = `https://${hostName}/api/code/${project}/${
19+
encodeTitleURI(title)
20+
}/${encodeTitleURI(filename)}`;
21+
22+
return new Request(
23+
path,
24+
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
25+
);
26+
};
27+
28+
const getCodeBlock_fromResponse: GetCodeBlock["fromResponse"] = async (res) => {
29+
if (!res.ok) {
30+
return makeError<NotFoundError | NotLoggedInError | NotMemberError>(res);
31+
}
32+
return { ok: true, value: await res.text() };
33+
};
34+
35+
export interface GetCodeBlock {
36+
/** /api/code/:project/:title/:filename の要求を組み立てる
37+
*
38+
* @param project 取得したいページのproject名
39+
* @param title 取得したいページのtitle 大文字小文字は問わない
40+
* @param filename 取得したいコードブロックのファイル名
41+
* @param options オプション
42+
* @return request
43+
*/
44+
toRequest: (
45+
project: string,
46+
title: string,
47+
filename: string,
48+
options?: BaseOptions,
49+
) => Request;
50+
51+
/** 帰ってきた応答からコードを取得する
52+
*
53+
* @param res 応答
54+
* @return コード
55+
*/
56+
fromResponse: (res: Response) => Promise<
57+
Result<
58+
string,
59+
NotFoundError | NotLoggedInError | NotMemberError
60+
>
61+
>;
62+
63+
(
64+
project: string,
65+
title: string,
66+
filename: string,
67+
options?: BaseOptions,
68+
): Promise<
69+
Result<
70+
string,
71+
NotFoundError | NotLoggedInError | NotMemberError
72+
>
73+
>;
74+
}
75+
76+
/** 指定したコードブロック中のテキストを取得する
77+
*
78+
* @param project 取得したいページのproject名
79+
* @param title 取得したいページのtitle 大文字小文字は問わない
80+
* @param filename 取得したいコードブロックのファイル名
81+
* @param options オプション
82+
*/
83+
export const getCodeBlock: GetCodeBlock = async (
84+
project,
85+
title,
86+
filename,
87+
options,
88+
) => {
89+
const { fetch } = setDefaults(options ?? {});
90+
const req = getCodeBlock_toRequest(project, title, filename, options);
91+
const res = await fetch(req);
92+
return await getCodeBlock_fromResponse(res);
93+
};
94+
95+
getCodeBlock.toRequest = getCodeBlock_toRequest;
96+
getCodeBlock.fromResponse = getCodeBlock_fromResponse;

rest/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export * from "./auth.ts";
1414
export * from "./util.ts";
1515
export * from "./error.ts";
1616
export * from "./getCodeBlocks.ts";
17+
export * from "./getCodeBlock.ts";
1718
export * from "./uploadToGCS.ts";

0 commit comments

Comments
 (0)