|
| 1 | +import type { |
| 2 | + NotFoundError, |
| 3 | + NotLoggedInError, |
| 4 | + NotMemberError, |
| 5 | +} from "@cosense/types/rest"; |
| 6 | +import type { ResponseOfEndpoint } from "../../../../targeted_response.ts"; |
| 7 | +import { type BaseOptions, setDefaults } from "../../../../util.ts"; |
| 8 | +import { encodeTitleURI } from "../../../../title.ts"; |
| 9 | +import { cookie } from "../../../../rest/auth.ts"; |
| 10 | + |
| 11 | +/** Options for {@linkcode get} */ |
| 12 | +export interface GetIconOption<R extends Response | undefined> |
| 13 | + extends BaseOptions<R> { |
| 14 | + /** use `followRename` */ |
| 15 | + followRename?: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +/** Constructs a request for the `/api/pages/:project/:title/icon` endpoint |
| 19 | + * |
| 20 | + * @param project The project name containing the desired page |
| 21 | + * @param title The page title to retrieve (case insensitive) |
| 22 | + * @param options - Additional configuration options |
| 23 | + * @returns A {@linkcode Request} object for fetching page data |
| 24 | + */ |
| 25 | +export const makeGetRequest = <R extends Response | undefined>( |
| 26 | + project: string, |
| 27 | + title: string, |
| 28 | + options?: GetIconOption<R>, |
| 29 | +): Request => { |
| 30 | + const { sid, hostName, followRename } = setDefaults(options ?? {}); |
| 31 | + |
| 32 | + return new Request( |
| 33 | + `https://${hostName}/api/pages/${project}/${ |
| 34 | + encodeTitleURI(title) |
| 35 | + }/icon?followRename=${followRename ?? true}`, |
| 36 | + sid ? { headers: { Cookie: cookie(sid) } } : undefined, |
| 37 | + ); |
| 38 | +}; |
| 39 | + |
| 40 | +/** Retrieves a specified page image |
| 41 | + * |
| 42 | + * @param project The project name containing the desired page |
| 43 | + * @param title The page title to retrieve (case insensitive) |
| 44 | + * @param options Additional configuration options for the request |
| 45 | + * @returns A {@linkcode Response} object containing the page image |
| 46 | + */ |
| 47 | +export const get = <R extends Response | undefined = Response>( |
| 48 | + project: string, |
| 49 | + title: string, |
| 50 | + options?: GetIconOption<R>, |
| 51 | +): Promise< |
| 52 | + ResponseOfEndpoint<{ |
| 53 | + 200: Blob; |
| 54 | + 404: NotFoundError; |
| 55 | + 401: NotLoggedInError; |
| 56 | + 403: NotMemberError; |
| 57 | + }, R> |
| 58 | +> => |
| 59 | + setDefaults(options ?? {}).fetch( |
| 60 | + makeGetRequest(project, title, options), |
| 61 | + ) as Promise< |
| 62 | + ResponseOfEndpoint<{ |
| 63 | + 200: Blob; |
| 64 | + 404: NotFoundError; |
| 65 | + 401: NotLoggedInError; |
| 66 | + 403: NotMemberError; |
| 67 | + }, R> |
| 68 | + >; |
0 commit comments