Skip to content

Commit 4ab7543

Browse files
authored
Merge pull request #49 from takker99/add-projects
✨ Implement the wrapper for /api/projects
2 parents 0b2ce1b + 9547855 commit 4ab7543

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

deps/scrapbox-rest.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ export type {
1616
NotPrivilegeError,
1717
Page,
1818
PageList,
19+
ProjectId,
20+
ProjectResponse,
1921
ProjectSearchResult,
2022
SearchedTitle,
2123
SearchResult,
2224
SessionError,
2325
TweetInfo,
24-
} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.3/rest.ts";
26+
} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.4/rest.ts";

deps/scrapbox.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export type {
22
Line,
33
Scrapbox,
4-
} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.3/userscript.ts";
4+
} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.4/userscript.ts";
55
export type {
66
BaseStore,
7-
} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.3/baseStore.ts";
7+
} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.4/baseStore.ts";
88
export * from "https://esm.sh/@progfay/[email protected]";

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)