Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api.ts
Original file line number Diff line number Diff line change
@@ -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";
1 change: 1 addition & 0 deletions api/commits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./commits/project.ts";
1 change: 1 addition & 0 deletions api/commits/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./project/page_id.ts";
86 changes: 86 additions & 0 deletions api/commits/project/page_id.ts
Original file line number Diff line number Diff line change
@@ -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<R extends Response | undefined>
extends BaseOptions<R> {
/** 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 = <R extends Response | undefined>(
project: string,
pageId: string,
options?: GetCommitsOption<R>,
): 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 = <R extends Response | undefined = Response>(
project: string,
pageId: string,
options?: GetCommitsOption<R>,
): 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>
>;
3 changes: 3 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down