Updates
This commit is contained in:
+12
-5
@@ -3,13 +3,14 @@ import {
|
||||
DSQLClientFetchHeader,
|
||||
DSQLFetchApiOptions,
|
||||
} from "../../package-shared/types";
|
||||
import serializeQuery from "../../package-shared/utils/serialize-query";
|
||||
|
||||
/**
|
||||
* # Fetch API
|
||||
*/
|
||||
export default async function fetchApi<
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
R extends any = any
|
||||
R extends any = any,
|
||||
>(url: string, options?: DSQLFetchApiOptions<T>): Promise<R> {
|
||||
let data;
|
||||
|
||||
@@ -21,13 +22,19 @@ export default async function fetchApi<
|
||||
finalHeaders[options.csrfKey] = options.csrfValue;
|
||||
}
|
||||
|
||||
let finalURL = url;
|
||||
|
||||
if (options?.query) {
|
||||
finalURL += serializeQuery(options.query);
|
||||
}
|
||||
|
||||
if (typeof options === "string") {
|
||||
try {
|
||||
let fetchData;
|
||||
|
||||
switch (options) {
|
||||
case "post":
|
||||
fetchData = await fetch(url, {
|
||||
fetchData = await fetch(finalURL, {
|
||||
method: options,
|
||||
headers: finalHeaders,
|
||||
} as RequestInit);
|
||||
@@ -35,7 +42,7 @@ export default async function fetchApi<
|
||||
break;
|
||||
|
||||
default:
|
||||
fetchData = await fetch(url);
|
||||
fetchData = await fetch(finalURL);
|
||||
data = fetchData.json();
|
||||
break;
|
||||
}
|
||||
@@ -60,7 +67,7 @@ export default async function fetchApi<
|
||||
|
||||
fetchOptions = _.merge(fetchOptions, options.fetchOptions);
|
||||
|
||||
fetchData = await fetch(url, fetchOptions);
|
||||
fetchData = await fetch(finalURL, fetchOptions);
|
||||
data = fetchData.json();
|
||||
} catch (error: any) {
|
||||
console.log("FetchAPI error #2:", error.message);
|
||||
@@ -68,7 +75,7 @@ export default async function fetchApi<
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
let fetchData = await fetch(url);
|
||||
let fetchData = await fetch(finalURL);
|
||||
data = await fetchData.json();
|
||||
} catch (error: any) {
|
||||
console.log("FetchAPI error #3:", error.message);
|
||||
|
||||
Reference in New Issue
Block a user