Skip to content

Commit

Permalink
use URLSearchParams to encode url params (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
ximex authored Feb 11, 2025
1 parent 73cfc27 commit 08dfe26
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
21 changes: 16 additions & 5 deletions src/api/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ export const createConfiguration = (params: CreateConfigParams) =>
body: JSON.stringify(params),
});

export const getConfiguration = (configuration: string) =>
fetchApiJson<Configuration>(`./info?configuration=${configuration}`);
export const getConfiguration = (configuration: string) => {
const urlSearchParams = new URLSearchParams({
configuration: configuration,
});
return fetchApiJson<Configuration>(`./info?${urlSearchParams.toString()}`);
};

export const deleteConfiguration = (configuration: string) =>
fetchApiText(`./delete?configuration=${configuration}`, {
export const deleteConfiguration = (configuration: string) => {
const urlSearchParams = new URLSearchParams({
configuration: configuration,
});
return fetchApiText(`./delete?${urlSearchParams.toString()}`, {
method: "post",
});
};

export const compileConfiguration = (
configuration: string,
Expand All @@ -62,7 +70,10 @@ export const getJsonConfig = async (
filename: string,
): Promise<Record<string, any> | null> => {
try {
return fetchApiJson(`./json-config?configuration=${filename}`);
const urlSearchParams = new URLSearchParams({
configuration: filename,
});
return fetchApiJson(`./json-config?${urlSearchParams.toString()}`);
} catch (err) {
if (err instanceof APIError && err.status === 404) {
return null;
Expand Down
35 changes: 22 additions & 13 deletions src/api/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@ export type DownloadType = {
download: string;
};

export const getDownloadTypes = (configuration: string) =>
fetchApiJson<DownloadType[]>(
`./downloads?configuration=${encodeURIComponent(configuration)}`,
export const getDownloadTypes = (configuration: string) => {
const urlSearchParams = new URLSearchParams({
configuration: configuration,
});
return fetchApiJson<DownloadType[]>(
`./downloads?${urlSearchParams.toString()}`,
);
};

export const getDownloadUrl = (configuration: string, type: DownloadType) =>
`./download.bin?configuration=${encodeURIComponent(
configuration,
)}&file=${encodeURIComponent(type.file)}&download=${encodeURIComponent(
type.download,
)}`;
export const getDownloadUrl = (configuration: string, type: DownloadType) => {
const urlSearchParams = new URLSearchParams({
configuration: configuration,
file: type.file,
download: type.download,
});
return `./download.bin?${urlSearchParams.toString()}`;
};

export const getFactoryDownloadUrl = (configuration: string) =>
`./download.bin?configuration=${encodeURIComponent(
configuration,
)}&file=firmware.factory.bin`;
export const getFactoryDownloadUrl = (configuration: string) => {
const urlSearchParams = new URLSearchParams({
configuration: configuration,
file: "firmware.factory.bin",
});
return `./download.bin?${urlSearchParams.toString()}`;
};
13 changes: 10 additions & 3 deletions src/api/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { APIError, fetchApiText } from ".";
// null if file not found.
export const getFile = async (filename: string): Promise<string | null> => {
try {
return fetchApiText(`./edit?configuration=${filename}`);
const urlSearchParams = new URLSearchParams({
configuration: filename,
});
return fetchApiText(`./edit?${urlSearchParams.toString()}`);
} catch (err) {
if (err instanceof APIError && err.status === 404) {
return null;
Expand All @@ -15,8 +18,12 @@ export const getFile = async (filename: string): Promise<string | null> => {
export const writeFile = async (
filename: string,
content: string,
): Promise<string> =>
fetchApiText(`./edit?configuration=${filename}`, {
): Promise<string> => {
const urlSearchParams = new URLSearchParams({
configuration: filename,
});
return fetchApiText(`./edit?${urlSearchParams.toString()}`, {
method: "POST",
body: content,
});
};

0 comments on commit 08dfe26

Please sign in to comment.