Skip to content

Commit

Permalink
add url params to fetch (Aylur#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
kotontrion authored and gorsbart committed Feb 28, 2024
1 parent d57097b commit 7988cac
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,28 @@ export type FetchOptions = {
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
body?: string;
headers?: Record<string, string>;
params?: Record<string, any>;
};

export async function fetch(url: string, options: FetchOptions = {}) {
const session = new Soup.Session();

if (options.params) {
url += '?' + Object.entries(options.params)
.map(([key, value]) => {
if (Array.isArray(value)) {
return value.map(val =>
`${encodeURIComponent(key)}=${encodeURIComponent(val)}`).join('&');
} else if (typeof value === 'object') {
return `${encodeURIComponent(key)}=${encodeURIComponent(
JSON.stringify(value))}`;
} else {
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
}
})
.join('&');
}

const message = new Soup.Message({
method: options.method || 'GET',
uri: GLib.Uri.parse(url, GLib.UriFlags.NONE),
Expand Down

0 comments on commit 7988cac

Please sign in to comment.