From 5deffd3631ce4ef8f6adaa42e001c49784e8307f Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Thu, 26 Sep 2024 15:49:16 +0900 Subject: [PATCH] refactor: simplify syntax around `URLSearchParams` --- rest/pages.ts | 10 +++++----- rest/project.ts | 9 ++++----- rest/search.ts | 11 +++++------ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/rest/pages.ts b/rest/pages.ts index bed3211..bd757fd 100644 --- a/rest/pages.ts +++ b/rest/pages.ts @@ -39,11 +39,11 @@ const getPage_toRequest: GetPage["toRequest"] = ( options, ) => { const { sid, hostName, followRename, projects } = setDefaults(options ?? {}); - const params = new URLSearchParams(); - params.append("followRename", `${followRename ?? true}`); - for (const id of projects ?? []) { - params.append("projects", id); - } + + const params = new URLSearchParams([ + ["followRename", `${followRename ?? true}`], + ...(projects?.map?.((id) => ["projects", id]) ?? []), + ]); return new Request( `https://${hostName}/api/pages/${project}/${ diff --git a/rest/project.ts b/rest/project.ts index b749019..c352d02 100644 --- a/rest/project.ts +++ b/rest/project.ts @@ -130,13 +130,12 @@ export type ListProjectsError = NotLoggedInError | HTTPError; const ListProject_toRequest: ListProjects["toRequest"] = (projectIds, init) => { const { sid, hostName } = setDefaults(init ?? {}); - const param = new URLSearchParams(); - for (const id of projectIds) { - param.append("ids", id); - } + const params = new URLSearchParams( + projectIds.map((id) => ["ids", id]), + ); return new Request( - `https://${hostName}/api/projects?${param.toString()}`, + `https://${hostName}/api/projects?${params}`, sid ? { headers: { Cookie: cookie(sid) } } : undefined, ); }; diff --git a/rest/search.ts b/rest/search.ts index 45d8dbb..03c4ffc 100644 --- a/rest/search.ts +++ b/rest/search.ts @@ -132,14 +132,13 @@ export const searchForWatchList = async ( > => { const { sid, hostName, fetch } = setDefaults(init ?? {}); - const params = new URLSearchParams(); - params.append("q", encodeURIComponent(query)); - for (const projectId of projectIds) { - params.append("ids", projectId); - } + const params = new URLSearchParams([ + ["q", encodeURIComponent(query)], + ...projectIds.map((projectId) => ["ids", projectId]), + ]); const req = new Request( - `https://${hostName}/api/projects/search/watch-list?${params.toString()}`, + `https://${hostName}/api/projects/search/watch-list?${params}`, sid ? { headers: { Cookie: cookie(sid) } } : undefined, );