Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Publish v0.3.5 fix(api): get requests not having data appended as sea…
Browse files Browse the repository at this point in the history
…rch params

- this bug was introduced in b24916f v0.3.2
  • Loading branch information
soerenmeier committed Jun 18, 2024
1 parent efd78e4 commit 42231f1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fire-lib-js",
"version": "0.3.4",
"version": "0.3.5",
"author": "Sören Meier <[email protected]>",
"type": "module",
"scripts": {
Expand Down
17 changes: 14 additions & 3 deletions src/api/Api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import ApiError from './ApiError.js';

const DEF_ORIGIN = 'http://fir_def';

/**
* Api class to handle requests to a server
*/
Expand Down Expand Up @@ -48,7 +50,13 @@ export default class Api {

if (!this.addr) throw ApiError.newOther('Server addr not defined');

const url = this.addr + path;
// since URL does not accept an url with out a host we need to add
// a default host, which we will remove later
// the URL is used in the first place to allow to modify the
// search params
// todo: there might still be an issue with relative and absolute
// paths, but i'm not sure fetch supports relative paths?
const url = new URL(this.addr + path, DEF_ORIGIN);

try {
let fetchParams = {
Expand All @@ -60,8 +68,7 @@ export default class Api {

// don't send a body if the method is get
if (method.toLowerCase() === 'get') {
const parseUrl = new URL(url, 'http://placeholder');
const searchParams = parseUrl.searchParams;
const searchParams = url.searchParams;

for (const [key, value] of Object.entries(data ?? {})) {
if (value !== undefined && value !== null)
Expand All @@ -71,6 +78,10 @@ export default class Api {
fetchParams.body = JSON.stringify(data);
}

let urlStr = url.toString();
if (urlStr.startsWith(DEF_ORIGIN))
urlStr = urlStr.substring(DEF_ORIGIN.length);

const resp = await fetch(url, fetchParams);

opts.responseStatus = resp.status;
Expand Down

0 comments on commit 42231f1

Please sign in to comment.