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