Skip to content

Commit ecc74fd

Browse files
committed
feat(api): Implement the /api/pages/:project/:title/icon endpoint
1 parent 9efcc4a commit ecc74fd

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

api/pages/project/title.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,4 @@ export const get = <R extends Response | undefined = Response>(
8282
>;
8383

8484
export * as text from "./title/text.ts";
85+
export * as icon from "./title/icon.ts";

api/pages/project/title/icon.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 BaseOptions, setDefaults } from "../../../../util.ts";
8+
import { encodeTitleURI } from "../../../../title.ts";
9+
import { cookie } from "../../../../rest/auth.ts";
10+
11+
/** Options for {@linkcode get} */
12+
export interface GetIconOption<R extends Response | undefined>
13+
extends BaseOptions<R> {
14+
/** use `followRename` */
15+
followRename?: boolean;
16+
}
17+
18+
/** Constructs a request for the `/api/pages/:project/:title/icon` endpoint
19+
*
20+
* @param project The project name containing the desired page
21+
* @param title The page title to retrieve (case insensitive)
22+
* @param options - Additional configuration options
23+
* @returns A {@linkcode Request} object for fetching page data
24+
*/
25+
export const makeGetRequest = <R extends Response | undefined>(
26+
project: string,
27+
title: string,
28+
options?: GetIconOption<R>,
29+
): Request => {
30+
const { sid, hostName, followRename } = setDefaults(options ?? {});
31+
32+
return new Request(
33+
`https://${hostName}/api/pages/${project}/${
34+
encodeTitleURI(title)
35+
}/icon?followRename=${followRename ?? true}`,
36+
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
37+
);
38+
};
39+
40+
/** Retrieves a specified page image
41+
*
42+
* @param project The project name containing the desired page
43+
* @param title The page title to retrieve (case insensitive)
44+
* @param options Additional configuration options for the request
45+
* @returns A {@linkcode Response} object containing the page image
46+
*/
47+
export const get = <R extends Response | undefined = Response>(
48+
project: string,
49+
title: string,
50+
options?: GetIconOption<R>,
51+
): Promise<
52+
ResponseOfEndpoint<{
53+
200: Blob;
54+
404: NotFoundError;
55+
401: NotLoggedInError;
56+
403: NotMemberError;
57+
}, R>
58+
> =>
59+
setDefaults(options ?? {}).fetch(
60+
makeGetRequest(project, title, options),
61+
) as Promise<
62+
ResponseOfEndpoint<{
63+
200: Blob;
64+
404: NotFoundError;
65+
401: NotLoggedInError;
66+
403: NotMemberError;
67+
}, R>
68+
>;

deno.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"./unstable-api/pages/project": "./api/pages/project.ts",
2727
"./unstable-api/pages/project/title": "./api/pages/project/title.ts",
2828
"./unstable-api/pages/project/title/text": "./api/pages/project/title/text.ts",
29+
"./unstable-api/pages/project/title/icon": "./api/pages/project/title/icon.ts",
2930
"./unstable-api/users": "./api/users.ts",
3031
"./unstable-api/users/me": "./api/users/me.ts"
3132
},

0 commit comments

Comments
 (0)