@gandalan/weblibs 1.1.82 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@
6
6
  * @property {function(string) : FluentRESTClient} useToken - Function to set the JWT token and return the FluentApi object.
7
7
  * @property {function(string) : object|Array<any>} get - Async function to perform GET requests.
8
8
  * @property {function(string, object) : object|Array<any>} put - Async function to perform PUT requests with a payload.
9
- * @property {function(string, object) : object|Array<any>} post - Async function to perform POST requests with a payload.
9
+ * @property {function(string, object|FormData) : object|Array<any>} post - Async function to perform POST requests with a payload.
10
10
  * @property {function(string) : object|Array<any>} delete - Async function to perform DELETE requests.
11
11
  */
12
12
 
@@ -21,7 +21,7 @@ export function restClient() {
21
21
 
22
22
  /**
23
23
  * set the base URL for all requests
24
- *
24
+ *
25
25
  * @param {string} url to use as base URL
26
26
  * @returns {FluentRESTClient}
27
27
  */
@@ -78,13 +78,23 @@ export function restClient() {
78
78
  *
79
79
  * @async
80
80
  * @param {string} [url=""]
81
- * @param {Object} [payload={}]
81
+ * @param {Object|FormData} [payload={}]
82
82
  * @returns {Promise<any>}
83
83
  */
84
84
  async post(url = "", payload = {}) {
85
85
  const finalUrl = `${this.baseUrl}${url}`;
86
- const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type": "application/json" } : {};
87
- const res = await fetch(finalUrl, { method: "POST", body: JSON.stringify(payload), headers });
86
+ const headers = this.token ? { "Authorization": `Bearer ${this.token}` } : {};
87
+
88
+ // Determine the body and headers based on the payload type
89
+ let body;
90
+ if (payload instanceof FormData) {
91
+ body = payload;
92
+ } else {
93
+ body = JSON.stringify(payload);
94
+ headers["Content-Type"] = "application/json";
95
+ }
96
+
97
+ const res = await fetch(finalUrl, { method: "POST", body, headers });
88
98
  if (res.ok)
89
99
  return await this._parseReponse(res);
90
100
  throw new Error(`POST ${finalUrl} failed: ${res.status} ${res.statusText}`);
@@ -106,8 +116,7 @@ export function restClient() {
106
116
  throw new Error(`DELETE ${finalUrl} failed: ${res.status} ${res.statusText}`);
107
117
  },
108
118
 
109
- async _parseReponse(res)
110
- {
119
+ async _parseReponse(res) {
111
120
  // check if repsonse is JSON, then return parsed JSON, otherwise return text
112
121
  const contentType = res.headers.get("content-type");
113
122
  if (contentType && contentType.includes("application/json"))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "1.1.82",
3
+ "version": "1.2.0",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "keywords": [
6
6
  "gandalan"
@@ -27,9 +27,9 @@
27
27
  "validator": "^13.12.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@babel/eslint-parser": "^7.25.8",
30
+ "@babel/eslint-parser": "^7.25.9",
31
31
  "eslint": "^8.57.1",
32
- "eslint-plugin-svelte": "^2.44.1",
32
+ "eslint-plugin-svelte": "^2.46.0",
33
33
  "svelte": "^4.2.19",
34
34
  "svelte-check": "^3.8.6"
35
35
  },