Skip to content

Commit 43ae4a4

Browse files
authored
Merge pull request #109 from takker99/get-table
✨ Get a table from REST API
2 parents eed7dfe + d0c21e2 commit 43ae4a4

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

rest/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./pages.ts";
2+
export * from "./table.ts";
23
export * from "./project.ts";
34
export * from "./profile.ts";
45
export * from "./replaceLinks.ts";

rest/table.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 getTable_toRequest: GetTable["toRequest"] = (
12+
project,
13+
title,
14+
filename,
15+
options,
16+
) => {
17+
const { sid, hostName } = setDefaults(options ?? {});
18+
const path = `https://${hostName}/api/pages/${project}/${
19+
encodeTitleURI(title)
20+
}/${encodeURIComponent(filename)}.csv`;
21+
22+
return new Request(
23+
path,
24+
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
25+
);
26+
};
27+
28+
const getTable_fromResponse: GetTable["fromResponse"] = async (res) => {
29+
if (!res.ok) {
30+
if (res.status === 404) {
31+
// responseが空文字の時があるので、自前で組み立てる
32+
return {
33+
ok: false,
34+
value: {
35+
name: "NotFoundError",
36+
message: "Table not found.",
37+
},
38+
};
39+
}
40+
return makeError<NotLoggedInError | NotMemberError>(res);
41+
}
42+
return { ok: true, value: await res.text() };
43+
};
44+
45+
export interface GetTable {
46+
/** /api/table/:project/:title/:filename.csv の要求を組み立てる
47+
*
48+
* @param project 取得したいページのproject名
49+
* @param title 取得したいページのtitle 大文字小文字は問わない
50+
* @param filename テーブルの名前
51+
* @param options オプション
52+
* @return request
53+
*/
54+
toRequest: (
55+
project: string,
56+
title: string,
57+
filename: string,
58+
options?: BaseOptions,
59+
) => Request;
60+
61+
/** 帰ってきた応答からページのJSONデータを取得する
62+
*
63+
* @param res 応答
64+
* @return ページのJSONデータ
65+
*/
66+
fromResponse: (res: Response) => Promise<
67+
Result<
68+
string,
69+
NotFoundError | NotLoggedInError | NotMemberError
70+
>
71+
>;
72+
73+
(
74+
project: string,
75+
title: string,
76+
filename: string,
77+
options?: BaseOptions,
78+
): Promise<
79+
Result<
80+
string,
81+
NotFoundError | NotLoggedInError | NotMemberError
82+
>
83+
>;
84+
}
85+
86+
/** 指定したテーブルをCSV形式で得る
87+
*
88+
* @param project 取得したいページのproject名
89+
* @param title 取得したいページのtitle 大文字小文字は問わない
90+
* @param filename テーブルの名前
91+
* @param options オプション
92+
*/
93+
export const getTable: GetTable = async (
94+
project,
95+
title,
96+
filename,
97+
options,
98+
) => {
99+
const { fetch } = setDefaults(options ?? {});
100+
const req = getTable_toRequest(project, title, filename, options);
101+
const res = await fetch(req);
102+
return await getTable_fromResponse(res);
103+
};
104+
105+
getTable.toRequest = getTable_toRequest;
106+
getTable.fromResponse = getTable_fromResponse;

0 commit comments

Comments
 (0)