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.0-alpha.10
Browse files Browse the repository at this point in the history
- Api::request now passes the data from a get request as searchParams
- expose responseStatus and responseHeaders in the opts object
  • Loading branch information
soerenmeier committed Mar 17, 2024
1 parent 00927ba commit cc4e19d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 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.0-alpha.9",
"version": "0.3.0-alpha.10",
"author": "Sören Meier <[email protected]>",
"type": "module",
"scripts": {
Expand Down
21 changes: 17 additions & 4 deletions src/api/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default class Api {
*
* @param method - The method of the request
* @param path - The path of the request
* @param data - The data to be sent
* @param data - The data to be sent if the method is get the data will
* be sent as query params
* @param headers - The headers to be sent
* @param opts - The additional options to be sent to fetch
*
Expand All @@ -47,6 +48,8 @@ export default class Api {

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

const url = new URL(this.addr + path);

try {
let fetchParams = {
headers,
Expand All @@ -56,12 +59,22 @@ export default class Api {
fetchParams.headers['content-type'] = 'application/json';

// don't send a body if the method is get
if (method.toLowerCase() !== 'get')
if (method.toLowerCase() === 'get') {
const searchParams = url.searchParams;

for (const [key, value] of Object.entries(data ?? {})) {
searchParams.set(key, value);
}
} else {
fetchParams.body = JSON.stringify(data);
}

const resp = await fetch(url, fetchParams);

const resp = await fetch(this.addr + path, fetchParams);
opts.responseStatus = resp.status;
opts.responseHeaders = resp.headers;

if (resp.status === 200) {
if (resp.ok) {
return await resp.json();
} else {
// we've got and error
Expand Down

0 comments on commit cc4e19d

Please sign in to comment.