Skip to content

Commit 5d03fe6

Browse files
authored
Merge pull request #206 from takker99:refactor-params
refactor: simplify syntax around `URLSearchParams`
2 parents 33acc9f + 5deffd3 commit 5d03fe6

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

rest/pages.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ const getPage_toRequest: GetPage["toRequest"] = (
3939
options,
4040
) => {
4141
const { sid, hostName, followRename, projects } = setDefaults(options ?? {});
42-
const params = new URLSearchParams();
43-
params.append("followRename", `${followRename ?? true}`);
44-
for (const id of projects ?? []) {
45-
params.append("projects", id);
46-
}
42+
43+
const params = new URLSearchParams([
44+
["followRename", `${followRename ?? true}`],
45+
...(projects?.map?.((id) => ["projects", id]) ?? []),
46+
]);
4747

4848
return new Request(
4949
`https://${hostName}/api/pages/${project}/${

rest/project.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,12 @@ export type ListProjectsError = NotLoggedInError | HTTPError;
130130

131131
const ListProject_toRequest: ListProjects["toRequest"] = (projectIds, init) => {
132132
const { sid, hostName } = setDefaults(init ?? {});
133-
const param = new URLSearchParams();
134-
for (const id of projectIds) {
135-
param.append("ids", id);
136-
}
133+
const params = new URLSearchParams(
134+
projectIds.map((id) => ["ids", id]),
135+
);
137136

138137
return new Request(
139-
`https://${hostName}/api/projects?${param.toString()}`,
138+
`https://${hostName}/api/projects?${params}`,
140139
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
141140
);
142141
};

rest/search.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,13 @@ export const searchForWatchList = async (
132132
> => {
133133
const { sid, hostName, fetch } = setDefaults(init ?? {});
134134

135-
const params = new URLSearchParams();
136-
params.append("q", encodeURIComponent(query));
137-
for (const projectId of projectIds) {
138-
params.append("ids", projectId);
139-
}
135+
const params = new URLSearchParams([
136+
["q", encodeURIComponent(query)],
137+
...projectIds.map((projectId) => ["ids", projectId]),
138+
]);
140139

141140
const req = new Request(
142-
`https://${hostName}/api/projects/search/watch-list?${params.toString()}`,
141+
`https://${hostName}/api/projects/search/watch-list?${params}`,
143142
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
144143
);
145144

0 commit comments

Comments
 (0)