Skip to content

Commit 9547855

Browse files
committed
✨ Implement the wrapper for /api/projects
1 parent 373d3a9 commit 9547855

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

rest/project.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type {
44
NotLoggedInError,
55
NotMemberError,
66
NotMemberProject,
7+
ProjectId,
8+
ProjectResponse,
79
} from "../deps/scrapbox-rest.ts";
810
import { cookie } from "./auth.ts";
911
import { UnexpectedResponseError } from "./error.ts";
@@ -50,3 +52,43 @@ export const getProject = async (
5052
const value = (await res.json()) as MemberProject | NotMemberProject;
5153
return { ok: true, value };
5254
};
55+
56+
/** list the projects' information
57+
*
58+
* @param projectIds project ids. This must have more than 1 id
59+
* @param init connect.sid etc.
60+
*/
61+
export const listProjects = async (
62+
projectIds: ProjectId[],
63+
init?: BaseOptions,
64+
): Promise<Result<ProjectResponse, NotLoggedInError>> => {
65+
const { sid, hostName, fetch } = setDefaults(init ?? {});
66+
const param = new URLSearchParams();
67+
for (const id of projectIds) {
68+
param.append("ids", id);
69+
}
70+
const path = `https://${hostName}/api/projects?${param.toString()}`;
71+
const res = await fetch(
72+
path,
73+
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
74+
);
75+
76+
if (!res.ok) {
77+
const text = await res.text();
78+
const value = tryToErrorLike(text);
79+
if (!value) {
80+
throw new UnexpectedResponseError({
81+
path: new URL(path),
82+
...res,
83+
body: text,
84+
});
85+
}
86+
return {
87+
ok: false,
88+
value: value as NotLoggedInError,
89+
};
90+
}
91+
92+
const value = (await res.json()) as ProjectResponse;
93+
return { ok: true, value };
94+
};

0 commit comments

Comments
 (0)