Skip to content

Commit

Permalink
add url params to fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
kotontrion committed Jan 1, 2024
1 parent eb9ffd9 commit b767586
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,30 @@ export type FetchOptions = {
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
body?: string;
headers?: Record<string, string>;
params?: 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(([k, v]) =>
`${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&');
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('&');
}

print(url)

Check failure on line 36 in src/utils/fetch.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

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

0 comments on commit b767586

Please sign in to comment.