|
| 1 | +import type { |
| 2 | + BadRequestError, |
| 3 | + NotFoundError, |
| 4 | + NotLoggedInError, |
| 5 | + NotMemberError, |
| 6 | +} from "@cosense/types/rest"; |
| 7 | +import { type BaseOptions, setDefaults } from "../../../util.ts"; |
| 8 | +import { cookie } from "../../../rest/auth.ts"; |
| 9 | +import type { ResponseOfEndpoint } from "../../../targeted_response.ts"; |
| 10 | + |
| 11 | +/** Options for {@linkcode export1HopLinks} |
| 12 | + * |
| 13 | + * @experimental **UNSTABLE**: New API, yet to be vetted. |
| 14 | + */ |
| 15 | +export type Export1HopLinksOptions<R extends Response | undefined> = |
| 16 | + BaseOptions<R>; |
| 17 | + |
| 18 | +/** Constructs a request for the `/api/smart-context/export-1hop-links/:project.txt` endpoint |
| 19 | + * |
| 20 | + * @experimental **UNSTABLE**: New API, yet to be vetted. |
| 21 | + * |
| 22 | + * @param project The project name to export 1-hop links for |
| 23 | + * @param title The title of the page to export 1-hop links for |
| 24 | + * @param options - Configuration options |
| 25 | + * @returns A {@linkcode Request} object for the API endpoint |
| 26 | + */ |
| 27 | +export const makeExport1HopLinksRequest = <R extends Response | undefined>( |
| 28 | + project: string, |
| 29 | + title: string, |
| 30 | + options?: Export1HopLinksOptions<R>, |
| 31 | +): Request => { |
| 32 | + const { sid, baseURL } = setDefaults(options ?? {}); |
| 33 | + const params = new URLSearchParams({ title: title }); |
| 34 | + |
| 35 | + return new Request( |
| 36 | + `${baseURL}api/smart-context/export-1hop-links/${project}.txt?${params}`, |
| 37 | + sid ? { headers: { Cookie: cookie(sid) } } : undefined, |
| 38 | + ); |
| 39 | +}; |
| 40 | + |
| 41 | +/** Exports 1-hop links for a given page in a project as AI-readable text format. |
| 42 | + * |
| 43 | + * @experimental **UNSTABLE**: New API, yet to be vetted. |
| 44 | + * |
| 45 | + * @param project The project name to export 1-hop links for |
| 46 | + * @param title The title of the page to export 1-hop links for |
| 47 | + * @param options - Configuration options. **Make sure to set `sid` or you will never get the 200 OK response.** |
| 48 | + */ |
| 49 | +export const export1HopLinks = <R extends Response | undefined = Response>( |
| 50 | + project: string, |
| 51 | + title: string, |
| 52 | + options?: Export1HopLinksOptions<R>, |
| 53 | +): Promise< |
| 54 | + ResponseOfEndpoint<{ |
| 55 | + 200: string; |
| 56 | + 404: NotFoundError; |
| 57 | + 400: BadRequestError; |
| 58 | + 401: NotLoggedInError; |
| 59 | + 403: NotMemberError; |
| 60 | + }, R> |
| 61 | +> => |
| 62 | + setDefaults(options ?? {}).fetch( |
| 63 | + makeExport1HopLinksRequest(project, title, options), |
| 64 | + ) as Promise< |
| 65 | + ResponseOfEndpoint<{ |
| 66 | + 200: string; |
| 67 | + 400: BadRequestError; |
| 68 | + 404: NotFoundError; |
| 69 | + 401: NotLoggedInError; |
| 70 | + 403: NotMemberError; |
| 71 | + }, R> |
| 72 | + >; |
0 commit comments