This commit is contained in:
Benjamin Toby
2025-02-04 13:21:44 +01:00
parent f0850146be
commit 442ad5aa32
17 changed files with 207 additions and 79 deletions
+18 -8
View File
@@ -1,6 +1,6 @@
import _ from "lodash";
type FetchApiOptions = {
type FetchApiOptions<T extends { [k: string]: any } = { [k: string]: any }> = {
method:
| "POST"
| "GET"
@@ -12,7 +12,7 @@ type FetchApiOptions = {
| "delete"
| "put"
| "patch";
body?: object | string;
body?: T | string;
headers?: FetchHeader;
};
@@ -30,13 +30,23 @@ export type FetchApiReturn = {
/**
* # Fetch API
*/
export default async function fetchApi(
export default async function fetchApi<
T extends { [k: string]: any } = { [k: string]: any },
R extends any = any
>(
url: string,
options?: FetchApiOptions,
options?: FetchApiOptions<T>,
csrf?: boolean,
/** Key to use to grab local Storage csrf value. */
localStorageCSRFKey?: string
): Promise<any> {
/**
* Key to use to grab local Storage csrf value.
*/
localStorageCSRFKey?: string,
/**
* Key with which to set the request header csrf
* value
*/
csrfHeaderKey?: string
): Promise<R> {
let data;
const csrfValue = localStorage.getItem(localStorageCSRFKey || "csrf");
@@ -46,7 +56,7 @@ export default async function fetchApi(
} as FetchHeader;
if (csrf && csrfValue) {
finalHeaders[`'${csrfValue.replace(/\"/g, "")}'`] = "true";
finalHeaders[csrfHeaderKey || "x-csrf-key"] = csrfValue;
}
if (typeof options === "string") {