|
| 1 | +import type { |
| 2 | + MemberProject, |
| 3 | + NotFoundError, |
| 4 | + NotLoggedInError, |
| 5 | + NotMemberError, |
| 6 | + NotMemberProject, |
| 7 | +} from "@cosense/types/rest"; |
| 8 | +import type { ResponseOfEndpoint } from "../../targeted_response.ts"; |
| 9 | +import { type BaseOptions, setDefaults } from "../../util.ts"; |
| 10 | +import { cookie } from "../../rest/auth.ts"; |
| 11 | + |
| 12 | +/** Create a request to `GET /api/projects/:project` |
| 13 | + * |
| 14 | + * @param project - Project name to retrieve information for |
| 15 | + * @param options - Additional configuration options |
| 16 | + * @returns A {@linkcode Request} object for fetching project data |
| 17 | + */ |
| 18 | +export const makeGetRequest = <R extends Response | undefined>( |
| 19 | + project: string, |
| 20 | + options?: BaseOptions<R>, |
| 21 | +): Request => { |
| 22 | + const { sid, hostName } = setDefaults(options ?? {}); |
| 23 | + |
| 24 | + return new Request( |
| 25 | + `https://${hostName}/api/projects/${project}`, |
| 26 | + sid ? { headers: { Cookie: cookie(sid) } } : undefined, |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +/** Get detailed information about a Scrapbox project |
| 31 | + * |
| 32 | + * This function retrieves detailed information about a project, including its |
| 33 | + * access level, settings, and metadata. The returned data type depends on |
| 34 | + * whether the user has member access to the project. |
| 35 | + * |
| 36 | + * @param project - Project name to retrieve information for |
| 37 | + * @param options Additional configuration options for the request |
| 38 | + * @returns A {@linkcode Response} object containing the project data |
| 39 | + */ |
| 40 | +export const get = <R extends Response | undefined = Response>( |
| 41 | + project: string, |
| 42 | + options?: BaseOptions<R>, |
| 43 | +): Promise< |
| 44 | + ResponseOfEndpoint<{ |
| 45 | + 200: MemberProject | NotMemberProject; |
| 46 | + 404: NotFoundError; |
| 47 | + 401: NotLoggedInError; |
| 48 | + 403: NotMemberError; |
| 49 | + }, R> |
| 50 | +> => |
| 51 | + setDefaults(options ?? {}).fetch( |
| 52 | + makeGetRequest(project, options), |
| 53 | + ) as Promise< |
| 54 | + ResponseOfEndpoint<{ |
| 55 | + 200: MemberProject | NotMemberProject; |
| 56 | + 404: NotFoundError; |
| 57 | + 401: NotLoggedInError; |
| 58 | + 403: NotMemberError; |
| 59 | + }, R> |
| 60 | + >; |
0 commit comments