From 373d3a954ad433f1744e19767ae0e522e502c485 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Sun, 1 May 2022 20:40:59 +0900 Subject: [PATCH 1/2] :arrow_up: --- deps/scrapbox-rest.ts | 4 +++- deps/scrapbox.ts | 4 ++-- deps/testing.ts | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/deps/scrapbox-rest.ts b/deps/scrapbox-rest.ts index e5cfbd2..e27c63f 100644 --- a/deps/scrapbox-rest.ts +++ b/deps/scrapbox-rest.ts @@ -16,9 +16,11 @@ export type { NotPrivilegeError, Page, PageList, + ProjectId, + ProjectResponse, ProjectSearchResult, SearchedTitle, SearchResult, SessionError, TweetInfo, -} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.3/rest.ts"; +} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.4/rest.ts"; diff --git a/deps/scrapbox.ts b/deps/scrapbox.ts index fdd09be..4760063 100644 --- a/deps/scrapbox.ts +++ b/deps/scrapbox.ts @@ -1,8 +1,8 @@ export type { Line, Scrapbox, -} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.3/userscript.ts"; +} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.4/userscript.ts"; export type { BaseStore, -} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.3/baseStore.ts"; +} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.4/baseStore.ts"; export * from "https://esm.sh/@progfay/scrapbox-parser@7.2.0"; diff --git a/deps/testing.ts b/deps/testing.ts index c44228c..4f340e0 100644 --- a/deps/testing.ts +++ b/deps/testing.ts @@ -1 +1 @@ -export * from "https://deno.land/std@0.136.0/testing/asserts.ts"; +export * from "https://deno.land/std@0.137.0/testing/asserts.ts"; From 95478552313da9953242a3c53e1f5f5c2fc4a9ce Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Sun, 1 May 2022 20:41:15 +0900 Subject: [PATCH 2/2] :sparkles: Implement the wrapper for /api/projects --- rest/project.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/rest/project.ts b/rest/project.ts index 70cc96f..2410b47 100644 --- a/rest/project.ts +++ b/rest/project.ts @@ -4,6 +4,8 @@ import type { NotLoggedInError, NotMemberError, NotMemberProject, + ProjectId, + ProjectResponse, } from "../deps/scrapbox-rest.ts"; import { cookie } from "./auth.ts"; import { UnexpectedResponseError } from "./error.ts"; @@ -50,3 +52,43 @@ export const getProject = async ( const value = (await res.json()) as MemberProject | NotMemberProject; return { ok: true, value }; }; + +/** list the projects' information + * + * @param projectIds project ids. This must have more than 1 id + * @param init connect.sid etc. + */ +export const listProjects = async ( + projectIds: ProjectId[], + init?: BaseOptions, +): Promise> => { + const { sid, hostName, fetch } = setDefaults(init ?? {}); + const param = new URLSearchParams(); + for (const id of projectIds) { + param.append("ids", id); + } + const path = `https://${hostName}/api/projects?${param.toString()}`; + const res = await fetch( + path, + sid ? { headers: { Cookie: cookie(sid) } } : undefined, + ); + + if (!res.ok) { + const text = await res.text(); + const value = tryToErrorLike(text); + if (!value) { + throw new UnexpectedResponseError({ + path: new URL(path), + ...res, + body: text, + }); + } + return { + ok: false, + value: value as NotLoggedInError, + }; + } + + const value = (await res.json()) as ProjectResponse; + return { ok: true, value }; +};