|
| 1 | +import type { |
| 2 | + BasePage, |
| 3 | + NotFoundError, |
| 4 | + NotLoggedInError, |
| 5 | + NotMemberError, |
| 6 | + PageList, |
| 7 | +} from "@cosense/types/rest"; |
| 8 | +import { type BaseOptions, setDefaults } from "../../util.ts"; |
| 9 | +import { cookie } from "../../rest/auth.ts"; |
| 10 | +import type { |
| 11 | + ResponseOfEndpoint, |
| 12 | + TargetedResponse, |
| 13 | +} from "../../targeted_response.ts"; |
| 14 | +import { |
| 15 | + type HTTPError, |
| 16 | + makeError, |
| 17 | + makeHTTPError, |
| 18 | + type TypedError, |
| 19 | +} from "../../error.ts"; |
| 20 | +import { pooledMap } from "@std/async/pool"; |
| 21 | +import { range } from "@core/iterutil/range"; |
| 22 | +import { flatten } from "@core/iterutil/async/flatten"; |
| 23 | + |
| 24 | +/** Options for {@linkcode get} */ |
| 25 | +export interface ListPagesOption<R extends Response | undefined> |
| 26 | + extends BaseOptions<R> { |
| 27 | + /** the sort of page list to return |
| 28 | + * |
| 29 | + * @default {"updated"} |
| 30 | + */ |
| 31 | + sort?: |
| 32 | + | "updatedWithMe" |
| 33 | + | "updated" |
| 34 | + | "created" |
| 35 | + | "accessed" |
| 36 | + | "pageRank" |
| 37 | + | "linked" |
| 38 | + | "views" |
| 39 | + | "title"; |
| 40 | + /** the index getting page list from |
| 41 | + * |
| 42 | + * @default {0} |
| 43 | + */ |
| 44 | + skip?: number; |
| 45 | + /** threshold of the length of page list |
| 46 | + * |
| 47 | + * @default {100} |
| 48 | + */ |
| 49 | + limit?: number; |
| 50 | +} |
| 51 | + |
| 52 | +/** Constructs a request for the `/api/pages/:project` endpoint |
| 53 | + * |
| 54 | + * @param project The project name to list pages from |
| 55 | + * @param options - Additional configuration options (sorting, pagination, etc.) |
| 56 | + * @returns A {@linkcode Request} object for fetching pages data |
| 57 | + */ |
| 58 | +export const makeGetRequest = <R extends Response | undefined>( |
| 59 | + project: string, |
| 60 | + options?: ListPagesOption<R>, |
| 61 | +): Request => { |
| 62 | + const { sid, baseURL, sort, limit, skip } = setDefaults( |
| 63 | + options ?? {}, |
| 64 | + ); |
| 65 | + const params = new URLSearchParams(); |
| 66 | + if (sort !== undefined) params.append("sort", sort); |
| 67 | + if (limit !== undefined) params.append("limit", `${limit}`); |
| 68 | + if (skip !== undefined) params.append("skip", `${skip}`); |
| 69 | + |
| 70 | + return new Request( |
| 71 | + `${baseURL}api/pages/${project}?${params}`, |
| 72 | + sid ? { headers: { Cookie: cookie(sid) } } : undefined, |
| 73 | + ); |
| 74 | +}; |
| 75 | + |
| 76 | +/** Lists pages from a specified project |
| 77 | + * |
| 78 | + * @param project The project name to list pages from |
| 79 | + * @param options Configuration options for pagination and sorting |
| 80 | + * @returns A {@linkcode Result}<{@linkcode unknown}, {@linkcode Error}> containing: |
| 81 | + * - Success: The page data in JSON format |
| 82 | + * - Error: One of several possible errors: |
| 83 | + * - {@linkcode NotFoundError}: Page not found |
| 84 | + * - {@linkcode NotLoggedInError}: Authentication required |
| 85 | + * - {@linkcode NotMemberError}: User lacks access |
| 86 | + */ |
| 87 | +export const get = <R extends Response | undefined = Response>( |
| 88 | + project: string, |
| 89 | + options?: ListPagesOption<R>, |
| 90 | +): Promise< |
| 91 | + ResponseOfEndpoint<{ |
| 92 | + 200: PageList; |
| 93 | + 404: NotFoundError; |
| 94 | + 401: NotLoggedInError; |
| 95 | + 403: NotMemberError; |
| 96 | + }, R> |
| 97 | +> => |
| 98 | + setDefaults(options ?? {}).fetch( |
| 99 | + makeGetRequest(project, options), |
| 100 | + ) as Promise< |
| 101 | + ResponseOfEndpoint<{ |
| 102 | + 200: PageList; |
| 103 | + 404: NotFoundError; |
| 104 | + 401: NotLoggedInError; |
| 105 | + 403: NotMemberError; |
| 106 | + }, R> |
| 107 | + >; |
| 108 | + |
| 109 | +/** Options for {@linkcode list} */ |
| 110 | +export interface ListPagesStreamOption<R extends Response | undefined> |
| 111 | + extends ListPagesOption<R> { |
| 112 | + /** The number of requests to make concurrently |
| 113 | + * |
| 114 | + * @default {3} |
| 115 | + */ |
| 116 | + poolLimit?: number; |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Lists pages from a given `project` with pagination |
| 121 | + * |
| 122 | + * @param project The project name to list pages from |
| 123 | + * @param options Configuration options for pagination and sorting |
| 124 | + * @throws {HTTPError | TypedError<"NotLoggedInError" | "NotMemberError" | "NotFoundError">} If any requests in the pagination sequence fail |
| 125 | + */ |
| 126 | +export async function* list( |
| 127 | + project: string, |
| 128 | + options?: ListPagesStreamOption<Response>, |
| 129 | +): AsyncGenerator<BasePage, void, unknown> { |
| 130 | + const props = { |
| 131 | + ...(options ?? {}), |
| 132 | + skip: options?.skip ?? 0, |
| 133 | + limit: options?.limit ?? 100, |
| 134 | + }; |
| 135 | + const response = await ensureResponse(await get(project, props)); |
| 136 | + const list = await response.json(); |
| 137 | + yield* list.pages; |
| 138 | + |
| 139 | + const limit = list.limit; |
| 140 | + const skip = list.skip + limit; |
| 141 | + const times = Math.ceil((list.count - skip) / limit); |
| 142 | + |
| 143 | + yield* flatten( |
| 144 | + pooledMap( |
| 145 | + options?.poolLimit ?? 3, |
| 146 | + range(0, times - 1), |
| 147 | + async (i) => { |
| 148 | + const response = await ensureResponse( |
| 149 | + await get(project, { ...props, skip: skip + i * limit, limit }), |
| 150 | + ); |
| 151 | + const list = await response.json(); |
| 152 | + return list.pages; |
| 153 | + }, |
| 154 | + ), |
| 155 | + ); |
| 156 | +} |
| 157 | + |
| 158 | +const ensureResponse = async ( |
| 159 | + response: ResponseOfEndpoint<{ |
| 160 | + 200: PageList; |
| 161 | + 404: NotFoundError; |
| 162 | + 401: NotLoggedInError; |
| 163 | + 403: NotMemberError; |
| 164 | + }, Response>, |
| 165 | +): Promise<TargetedResponse<200, PageList>> => { |
| 166 | + switch (response.status) { |
| 167 | + case 200: |
| 168 | + return response; |
| 169 | + case 401: |
| 170 | + case 403: |
| 171 | + case 404: { |
| 172 | + const error = await response.json(); |
| 173 | + throw makeError(error.name, error.message) satisfies TypedError< |
| 174 | + "NotLoggedInError" | "NotMemberError" | "NotFoundError" |
| 175 | + >; |
| 176 | + } |
| 177 | + default: |
| 178 | + throw makeHTTPError(response) satisfies HTTPError; |
| 179 | + } |
| 180 | +}; |
| 181 | + |
| 182 | +export * as replace from "./project/replace.ts"; |
| 183 | +export * as search from "./project/search.ts"; |
| 184 | +export * as title from "./project/title.ts"; |
0 commit comments