|
| 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