Skip to content

refactor: simplify syntax around URLSearchParams #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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