Skip to content

Commit 872dd54

Browse files
committed
feat(api): Implement the /api/users/me endpoint
1 parent ed57c55 commit 872dd54

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * as pages from "./api/pages.ts";
2+
export * as users from "./api/users.ts";

api/users.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as me from "./users/me.ts";

api/users/me.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { GuestUser, MemberUser } from "@cosense/types/rest";
2+
import type { ResponseOfEndpoint } from "../../targeted_response.ts";
3+
import { type BaseOptions, setDefaults } from "../../util.ts";
4+
import { cookie } from "../../rest/auth.ts";
5+
6+
/** Constructs a request for the `/api/users/me endpoint`
7+
*
8+
* This endpoint retrieves the current user's profile information,
9+
* which can be either a {@linkcode MemberUser} or {@linkcode GuestUser} profile.
10+
*
11+
* @param init - Options including `connect.sid` (session ID) and other configuration
12+
* @returns A {@linkcode Request} object for fetching user profile data
13+
*/
14+
export const makeGetRequest = <R extends Response | undefined>(
15+
init?: BaseOptions<R>,
16+
): Request => {
17+
const { sid, hostName } = setDefaults(init ?? {});
18+
return new Request(
19+
`https://${hostName}/api/users/me`,
20+
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
21+
);
22+
};
23+
24+
/** get the user profile
25+
*
26+
* @param init - Options including `connect.sid` (session ID) and other configuration
27+
* @returns A {@linkcode Response} object containing the user profile data
28+
*/
29+
export const get = <R extends Response | undefined = Response>(
30+
init?: BaseOptions<R>,
31+
): Promise<
32+
ResponseOfEndpoint<{ 200: MemberUser | GuestUser }, R>
33+
> =>
34+
setDefaults(init ?? {}).fetch(
35+
makeGetRequest(init),
36+
) as Promise<ResponseOfEndpoint<{ 200: MemberUser | GuestUser }, R>>;

deno.jsonc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
"./unstable-api": "./api.ts",
2525
"./unstable-api/pages": "./api/pages.ts",
2626
"./unstable-api/pages/project": "./api/pages/project.ts",
27-
"./unstable-api/pages/project/title": "./api/pages/project/title.ts"
27+
"./unstable-api/pages/project/title": "./api/pages/project/title.ts",
28+
"./unstable-api/users": "./api/users.ts",
29+
"./unstable-api/users/me": "./api/users/me.ts"
2830
},
2931
"imports": {
3032
"@core/unknownutil": "jsr:@core/unknownutil@^4.0.0",

0 commit comments

Comments
 (0)