|
| 1 | +import type { |
| 2 | + ErrorLike, |
| 3 | + NotFoundError, |
| 4 | + NotLoggedInError, |
| 5 | + NotMemberError, |
| 6 | + PageSnapshotList, |
| 7 | + PageSnapshotResult, |
| 8 | +} from "../deps/scrapbox-rest.ts"; |
| 9 | +import { cookie } from "./auth.ts"; |
| 10 | +import { BaseOptions, Result, setDefaults } from "./util.ts"; |
| 11 | +import { makeError } from "./error.ts"; |
| 12 | + |
| 13 | +/** 不正な`timestampId`を渡されたときに発生するエラー */ |
| 14 | +export interface InvalidPageSnapshotIdError extends ErrorLike { |
| 15 | + name: "InvalidPageSnapshotIdError"; |
| 16 | +} |
| 17 | + |
| 18 | +/** get a page snapshot |
| 19 | + * |
| 20 | + * @param options connect.sid etc. |
| 21 | + */ |
| 22 | +export const getSnapshot = async ( |
| 23 | + project: string, |
| 24 | + pageId: string, |
| 25 | + timestampId: string, |
| 26 | + options?: BaseOptions, |
| 27 | +): Promise< |
| 28 | + Result< |
| 29 | + PageSnapshotResult, |
| 30 | + | NotFoundError |
| 31 | + | NotLoggedInError |
| 32 | + | NotMemberError |
| 33 | + | InvalidPageSnapshotIdError |
| 34 | + > |
| 35 | +> => { |
| 36 | + const { sid, hostName, fetch } = setDefaults(options ?? {}); |
| 37 | + |
| 38 | + const req = new Request( |
| 39 | + `https://${hostName}/api/page-snapshots/${project}/${pageId}/${timestampId}`, |
| 40 | + sid ? { headers: { Cookie: cookie(sid) } } : undefined, |
| 41 | + ); |
| 42 | + |
| 43 | + const res = await fetch(req); |
| 44 | + |
| 45 | + if (!res.ok) { |
| 46 | + if (res.status === 422) { |
| 47 | + return { |
| 48 | + ok: false, |
| 49 | + value: { |
| 50 | + name: "InvalidPageSnapshotIdError", |
| 51 | + message: await res.text(), |
| 52 | + }, |
| 53 | + }; |
| 54 | + } |
| 55 | + return makeError<NotFoundError | NotLoggedInError | NotMemberError>(res); |
| 56 | + } |
| 57 | + |
| 58 | + const value = (await res.json()) as PageSnapshotResult; |
| 59 | + return { ok: true, value }; |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * Retrieves the timestamp IDs for a specific page in a project. |
| 64 | + * |
| 65 | + * @param project - The name of the project. |
| 66 | + * @param pageId - The ID of the page. |
| 67 | + * @param options - Optional configuration options. |
| 68 | + * @returns A promise that resolves to a {@link Result} object containing the page snapshot list if successful, |
| 69 | + * or an error if the request fails. |
| 70 | + */ |
| 71 | +export const getTimestampIds = async ( |
| 72 | + project: string, |
| 73 | + pageId: string, |
| 74 | + options?: BaseOptions, |
| 75 | +): Promise< |
| 76 | + Result<PageSnapshotList, NotFoundError | NotLoggedInError | NotMemberError> |
| 77 | +> => { |
| 78 | + const { sid, hostName, fetch } = setDefaults(options ?? {}); |
| 79 | + |
| 80 | + const req = new Request( |
| 81 | + `https://${hostName}/api/page-snapshots/${project}/${pageId}`, |
| 82 | + sid ? { headers: { Cookie: cookie(sid) } } : undefined, |
| 83 | + ); |
| 84 | + |
| 85 | + const res = await fetch(req); |
| 86 | + |
| 87 | + if (!res.ok) { |
| 88 | + return makeError<NotFoundError | NotLoggedInError | NotMemberError>(res); |
| 89 | + } |
| 90 | + |
| 91 | + const value = (await res.json()) as PageSnapshotList; |
| 92 | + return { ok: true, value }; |
| 93 | +}; |
0 commit comments