This commit is contained in:
Benjamin Toby
2025-07-05 14:59:30 +01:00
parent 6e334c2525
commit 7e8bb37c09
526 changed files with 17560 additions and 11386 deletions
+20 -12
View File
@@ -1,7 +1,6 @@
import _ from "lodash";
import getCsrfHeaderName from "../../package-shared/actions/get-csrf-header-name";
type FetchApiOptions = {
type FetchApiOptions<T extends { [k: string]: any } = { [k: string]: any }> = {
method:
| "POST"
| "GET"
@@ -13,7 +12,7 @@ type FetchApiOptions = {
| "delete"
| "put"
| "patch";
body?: object | string;
body?: T | string;
headers?: FetchHeader;
};
@@ -31,25 +30,34 @@ 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 || getCsrfHeaderName()
);
const csrfKey = "x-dsql-csrf-key";
const csrfValue = localStorage.getItem(localStorageCSRFKey || csrfKey);
let finalHeaders = {
"Content-Type": "application/json",
} as FetchHeader;
if (csrf && csrfValue) {
finalHeaders[getCsrfHeaderName()] = csrfValue;
finalHeaders[localStorageCSRFKey || csrfKey] = csrfValue;
}
if (typeof options === "string") {