getAllPages()
実装
owner or admin
others
https://code2svg.vercel.app/svg/https://raw.githubusercontent.com/takker99/Scrapbubble/0.7.3/throttle.ts#.svg
入れるなら、もっと汎用化したいところ
とりあえずこのページに置いておこう
code:mod.ts
import {
BaseOptions,
Fetch,
exportPages,
listPages,
getPage,
readLinksBulk,
InvalidFollowingIdError,
Result,
TooLongURIError,
} from "../scrapbox-userscript-std/rest.ts";
import {
Page,
ExportedPage,
NotFoundError,
NotLoggedInError,
NotMemberError,
} from "../scrapbox-jp%2Ftypes/rest.ts";
export interface PageLoader {
count: number;
pages: ExportedPage<true>[] | AsyncGenerator<
Result<Page, NotFoundError | NotLoggedInError | NotMemberError | TooLongURIError>,
void,
unknown
;
}
export const getAllPages = async (project: string, options?: BaseOptions): Promise<Result<PageLoader, NotFoundError | NotLoggedInError | NotMemberError | InvalidFollowingIdError>> => {
{
const result = await exportPages(project, { metadata: true, ...options });
if (result.ok) return {
ok: true,
value: {
count: result.value.pages.length,
pages: result.value.pages,
},
};
const error = result.value;
if (error.name === "NotFoundError") return { ok: false, value: error };
}
const result = await listPages(project, { ...options, limit: 1 });
if (!result.ok) return result;
const reader = await readLinksBulk(project, options);
if ("name" in reader) return { ok: false, value: reader };
return {
ok: true,
value: {
count: result.value.count,
pages: (async function* () {
const promises: Promise<
Result<Page, NotFoundError | NotLoggedInError | NotMemberError | TooLongURIError>
[] = [];
const throttle = makeThrottle<Response>(3);
const { fetch: fetch_ = globalThis.fetch, ...rest } = options ?? {};
const fetch: Fetch = (args) => throttle(() => fetch_(args));
for await (const links of reader) {
promises.push(...links.map(
(link) => getPage(project, link.title, { ...rest, fetch })
));
}
for await (const promise of promises) {
yield await promise;
}
})(),
},
};
};
code:js
await (async() => {
const res = await getAllPages("customize");
if(!res.ok){
console.error(res.value);
return;
}
let i = 0;
for await (const result of res.value.pages){
i++;
if(!result.ok){
console.error(result.value);
continue;
}
console.log(%c${i}/${res.value.count}, "color:gray", result.value.title);
}
})();