Skip to content

Commit

Permalink
refactor: simplify syntax around URLSearchParams
Browse files Browse the repository at this point in the history
  • Loading branch information
takker99 committed Sep 26, 2024
1 parent 33acc9f commit 5deffd3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
10 changes: 5 additions & 5 deletions rest/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}/${
Expand Down
9 changes: 4 additions & 5 deletions rest/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
};
Expand Down
11 changes: 5 additions & 6 deletions rest/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down

0 comments on commit 5deffd3

Please sign in to comment.