From cc4e19daf3ee4567dc6cb89789f1fc164184e0b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Meier?= Date: Sun, 17 Mar 2024 17:59:46 +0100 Subject: [PATCH] Publish v0.3.0-alpha.10 - Api::request now passes the data from a get request as searchParams - expose responseStatus and responseHeaders in the opts object --- package.json | 2 +- src/api/Api.ts | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index e13427d..74c8a0b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fire-lib-js", - "version": "0.3.0-alpha.9", + "version": "0.3.0-alpha.10", "author": "Sören Meier ", "type": "module", "scripts": { diff --git a/src/api/Api.ts b/src/api/Api.ts index 15a9a30..d7a072c 100644 --- a/src/api/Api.ts +++ b/src/api/Api.ts @@ -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 * @@ -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, @@ -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