diff --git a/api.ts b/api.ts index 6c9ab85..e58400a 100644 --- a/api.ts +++ b/api.ts @@ -1,6 +1,7 @@ +export * from "./api/commits.ts"; export * from "./api/pages.ts"; export * from "./api/projects.ts"; -export * from "./api/users.ts"; export * from "./api/smart-context.ts"; +export * from "./api/users.ts"; export type { HTTPError, TypedError } from "./error.ts"; export type { BaseOptions, ExtendedOptions, OAuthOptions } from "./util.ts"; diff --git a/api/commits.ts b/api/commits.ts new file mode 100644 index 0000000..3d4a1bb --- /dev/null +++ b/api/commits.ts @@ -0,0 +1 @@ +export * from "./commits/project.ts"; diff --git a/api/commits/project.ts b/api/commits/project.ts new file mode 100644 index 0000000..8665fda --- /dev/null +++ b/api/commits/project.ts @@ -0,0 +1 @@ +export * from "./project/page_id.ts"; diff --git a/api/commits/project/page_id.ts b/api/commits/project/page_id.ts new file mode 100644 index 0000000..13d90d0 --- /dev/null +++ b/api/commits/project/page_id.ts @@ -0,0 +1,86 @@ +import type { + CommitsResponse, + NotFoundError, + NotLoggedInError, + NotMemberError, +} from "@cosense/types/rest"; +import type { ResponseOfEndpoint } from "../../../targeted_response.ts"; +import { type BaseOptions, setDefaults } from "../../../util.ts"; +import { cookie } from "../../../rest/auth.ts"; + +/** + * Represents an error when the commit HEAD is invalid. + */ +export interface InvalidHeadError { + /** error message */ + message: string; +} + +/** + * Options for {@linkcode getCommits} + * + * @experimental **UNSTABLE**: New API, yet to be vetted. + */ +export interface GetCommitsOption + extends BaseOptions { + /** Returns only the commits before the specified commit id. + * + * If not specified, returned all commits. + */ + head?: string; +} + +/** Constructs a request for the `/api/commits/:project/:pageId` endpoint + * + * @experimental **UNSTABLE**: New API, yet to be vetted. + * + * @param project The project name containing the desired page + * @param pageId The page ID to retrieve + * @param options - Additional configuration options + * @returns A {@linkcode Request} object for fetching page data + */ +export const makeGetCommitsRequest = ( + project: string, + pageId: string, + options?: GetCommitsOption, +): Request => { + const { sid, baseURL, head } = setDefaults(options ?? {}); + + return new Request( + `${baseURL}api/commits/${project}/${pageId}?head=${head ?? ""}`, + sid ? { headers: { Cookie: cookie(sid) } } : undefined, + ); +}; + +/** Retrieves the commit history for a specified page + * + * @experimental **UNSTABLE**: New API, yet to be vetted. + * + * @param project The project name containing the desired page + * @param pageId The page ID to retrieve + * @param options Additional configuration options for the request + */ +export const getCommits = ( + project: string, + pageId: string, + options?: GetCommitsOption, +): Promise< + ResponseOfEndpoint<{ + 200: CommitsResponse; + 400: InvalidHeadError; + 401: NotLoggedInError; + 403: NotMemberError; + 404: NotFoundError; + }, R> +> => + setDefaults(options ?? {}).fetch( + makeGetCommitsRequest(project, pageId, options), + ) as Promise< + ResponseOfEndpoint<{ + 200: CommitsResponse; + 400: InvalidHeadError; + 401: NotLoggedInError; + 403: NotMemberError; + 404: NotFoundError; + }, R> + >; diff --git a/deno.jsonc b/deno.jsonc index dae0fb1..094cfe5 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -23,6 +23,9 @@ "./title": "./title.ts", "./unstable-api": "./api.ts", "./unstable-api/pages": "./api/pages.ts", + "./unstable-api/commits": "./api/commits.ts", + "./unstable-api/commits/project": "./api/commits/project.ts", + "./unstable-api/commits/project/page-id": "./api/commits/project/page_id.ts", "./unstable-api/pages/project": "./api/pages/project.ts", "./unstable-api/pages/project/replace": "./api/pages/project/replace.ts", "./unstable-api/pages/project/replace/links": "./api/pages/project/replace/links.ts",