|
| 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 ExtendedOptions, setDefaults } from "../../../../util.ts"; |
| 8 | +import { cookie } from "../../../../rest/auth.ts"; |
| 9 | +import { get } from "../../../users/me.ts"; |
| 10 | + |
| 11 | +/** Constructs a request for the `/api/pages/:project/replace/links` endpoint |
| 12 | + * |
| 13 | + * @param project - The project name where all links will be replaced |
| 14 | + * @param from - The original link text to be replaced |
| 15 | + * @param to - The new link text to replace with |
| 16 | + * @param init - Additional configuration options |
| 17 | + * @returns A {@linkcode Request} object for replacing links in `project` |
| 18 | + */ |
| 19 | +export const makePostRequest = <R extends Response | undefined>( |
| 20 | + project: string, |
| 21 | + from: string, |
| 22 | + to: string, |
| 23 | + init?: ExtendedOptions<R>, |
| 24 | +): Request => { |
| 25 | + const { sid, hostName, csrf } = setDefaults(init ?? {}); |
| 26 | + |
| 27 | + return new Request( |
| 28 | + `https://${hostName}/api/pages/${project}/replace/links`, |
| 29 | + { |
| 30 | + method: "POST", |
| 31 | + headers: { |
| 32 | + "Content-Type": "application/json;charset=utf-8", |
| 33 | + "X-CSRF-TOKEN": csrf ?? "", |
| 34 | + ...(sid ? { Cookie: cookie(sid) } : {}), |
| 35 | + }, |
| 36 | + body: JSON.stringify({ from, to }), |
| 37 | + }, |
| 38 | + ); |
| 39 | +}; |
| 40 | + |
| 41 | +/** Retrieves JSON data for a specified page |
| 42 | + * |
| 43 | + * @param project - The project name where all links will be replaced |
| 44 | + * @param from - The original link text to be replaced |
| 45 | + * @param to - The new link text to replace with |
| 46 | + * @param init - Additional configuration options |
| 47 | + * @returns A {@linkcode Result}<{@linkcode unknown}, {@linkcode Error}> containing: |
| 48 | + * - Success: The page data in JSON format |
| 49 | + * - Error: One of several possible errors: |
| 50 | + * - {@linkcode NotFoundError}: Page not found |
| 51 | + * - {@linkcode NotLoggedInError}: Authentication required |
| 52 | + * - {@linkcode NotMemberError}: User lacks access |
| 53 | + */ |
| 54 | +export const post = async <R extends Response | undefined = Response>( |
| 55 | + project: string, |
| 56 | + from: string, |
| 57 | + to: string, |
| 58 | + init?: ExtendedOptions<R>, |
| 59 | +): Promise< |
| 60 | + ResponseOfEndpoint<{ |
| 61 | + 200: string; |
| 62 | + 404: NotFoundError; |
| 63 | + 401: NotLoggedInError; |
| 64 | + 403: NotMemberError; |
| 65 | + }, R> |
| 66 | +> => { |
| 67 | + let { csrf, fetch, ...init2 } = setDefaults(init ?? {}); |
| 68 | + |
| 69 | + if (!csrf) { |
| 70 | + const res = await get(init2); |
| 71 | + if (!res.ok) return res; |
| 72 | + csrf = (await res.json()).csrfToken; |
| 73 | + } |
| 74 | + |
| 75 | + return fetch( |
| 76 | + makePostRequest(project, from, to, { csrf, ...init2 }), |
| 77 | + ) as Promise< |
| 78 | + ResponseOfEndpoint<{ |
| 79 | + 200: string; |
| 80 | + 404: NotFoundError; |
| 81 | + 401: NotLoggedInError; |
| 82 | + 403: NotMemberError; |
| 83 | + }, R> |
| 84 | + >; |
| 85 | +}; |
0 commit comments