|
| 1 | +import type { |
| 2 | + CommitsResponse, |
| 3 | + NotFoundError, |
| 4 | + NotLoggedInError, |
| 5 | + NotMemberError, |
| 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 | +/** |
| 12 | + * Represents an error when the commit HEAD is invalid. |
| 13 | + */ |
| 14 | +export interface InvalidHeadError { |
| 15 | + /** error message */ |
| 16 | + message: string; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Options for {@linkcode getCommits} |
| 21 | + * |
| 22 | + * @experimental **UNSTABLE**: New API, yet to be vetted. |
| 23 | + */ |
| 24 | +export interface GetCommitsOption<R extends Response | undefined> |
| 25 | + extends BaseOptions<R> { |
| 26 | + /** Returns only the commits before the specified commit id. |
| 27 | + * |
| 28 | + * If not specified, returned all commits. |
| 29 | + */ |
| 30 | + head?: string; |
| 31 | +} |
| 32 | + |
| 33 | +/** Constructs a request for the `/api/commits/:project/:pageId` endpoint |
| 34 | + * |
| 35 | + * @experimental **UNSTABLE**: New API, yet to be vetted. |
| 36 | + * |
| 37 | + * @param project The project name containing the desired page |
| 38 | + * @param pageId The page ID to retrieve |
| 39 | + * @param options - Additional configuration options |
| 40 | + * @returns A {@linkcode Request} object for fetching page data |
| 41 | + */ |
| 42 | +export const makeGetCommitsRequest = <R extends Response | undefined>( |
| 43 | + project: string, |
| 44 | + pageId: string, |
| 45 | + options?: GetCommitsOption<R>, |
| 46 | +): Request => { |
| 47 | + const { sid, baseURL, head } = setDefaults(options ?? {}); |
| 48 | + |
| 49 | + return new Request( |
| 50 | + `${baseURL}api/commits/${project}/${pageId}?head=${head ?? ""}`, |
| 51 | + sid ? { headers: { Cookie: cookie(sid) } } : undefined, |
| 52 | + ); |
| 53 | +}; |
| 54 | + |
| 55 | +/** Retrieves the commit history for a specified page |
| 56 | + * |
| 57 | + * @experimental **UNSTABLE**: New API, yet to be vetted. |
| 58 | + * |
| 59 | + * @param project The project name containing the desired page |
| 60 | + * @param pageId The page ID to retrieve |
| 61 | + * @param options Additional configuration options for the request |
| 62 | + */ |
| 63 | +export const getCommits = <R extends Response | undefined = Response>( |
| 64 | + project: string, |
| 65 | + pageId: string, |
| 66 | + options?: GetCommitsOption<R>, |
| 67 | +): Promise< |
| 68 | + ResponseOfEndpoint<{ |
| 69 | + 200: CommitsResponse; |
| 70 | + 400: InvalidHeadError; |
| 71 | + 401: NotLoggedInError; |
| 72 | + 403: NotMemberError; |
| 73 | + 404: NotFoundError; |
| 74 | + }, R> |
| 75 | +> => |
| 76 | + setDefaults(options ?? {}).fetch( |
| 77 | + makeGetCommitsRequest(project, pageId, options), |
| 78 | + ) as Promise< |
| 79 | + ResponseOfEndpoint<{ |
| 80 | + 200: CommitsResponse; |
| 81 | + 400: InvalidHeadError; |
| 82 | + 401: NotLoggedInError; |
| 83 | + 403: NotMemberError; |
| 84 | + 404: NotFoundError; |
| 85 | + }, R> |
| 86 | + >; |
0 commit comments