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 -5
View File
@@ -1,6 +1,10 @@
type FetchApiOptions = {
type FetchApiOptions<T extends {
[k: string]: any;
} = {
[k: string]: any;
}> = {
method: "POST" | "GET" | "DELETE" | "PUT" | "PATCH" | "post" | "get" | "delete" | "put" | "patch";
body?: object | string;
body?: T | string;
headers?: FetchHeader;
};
type FetchHeader = HeadersInit & {
@@ -15,7 +19,18 @@ export type FetchApiReturn = {
/**
* # Fetch API
*/
export default function fetchApi(url: string, options?: FetchApiOptions, csrf?: boolean,
/** Key to use to grab local Storage csrf value. */
localStorageCSRFKey?: string): Promise<any>;
export default function fetchApi<T extends {
[k: string]: any;
} = {
[k: string]: any;
}, R extends any = any>(url: string, options?: FetchApiOptions<T>, csrf?: boolean,
/**
* 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>;
export {};
+70 -80
View File
@@ -1,90 +1,80 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = fetchApi;
const lodash_1 = __importDefault(require("lodash"));
const get_csrf_header_name_1 = __importDefault(require("../../package-shared/actions/get-csrf-header-name"));
import _ from "lodash";
/**
* # Fetch API
*/
function fetchApi(url, options, csrf,
/** Key to use to grab local Storage csrf value. */
localStorageCSRFKey) {
return __awaiter(this, void 0, void 0, function* () {
let data;
const csrfValue = localStorage.getItem(localStorageCSRFKey || (0, get_csrf_header_name_1.default)());
let finalHeaders = {
"Content-Type": "application/json",
};
if (csrf && csrfValue) {
finalHeaders[(0, get_csrf_header_name_1.default)()] = csrfValue;
}
if (typeof options === "string") {
try {
let fetchData;
switch (options) {
case "post":
fetchData = yield fetch(url, {
method: options,
headers: finalHeaders,
});
data = fetchData.json();
break;
default:
fetchData = yield fetch(url);
data = fetchData.json();
break;
}
}
catch (error) {
console.log("FetchAPI error #1:", error.message);
data = null;
export default async function fetchApi(url, options, csrf,
/**
* Key to use to grab local Storage csrf value.
*/
localStorageCSRFKey,
/**
* Key with which to set the request header csrf
* value
*/
csrfHeaderKey) {
let data;
const csrfKey = "x-dsql-csrf-key";
const csrfValue = localStorage.getItem(localStorageCSRFKey || csrfKey);
let finalHeaders = {
"Content-Type": "application/json",
};
if (csrf && csrfValue) {
finalHeaders[localStorageCSRFKey || csrfKey] = csrfValue;
}
if (typeof options === "string") {
try {
let fetchData;
switch (options) {
case "post":
fetchData = await fetch(url, {
method: options,
headers: finalHeaders,
});
data = fetchData.json();
break;
default:
fetchData = await fetch(url);
data = fetchData.json();
break;
}
}
else if (typeof options === "object") {
try {
let fetchData;
if (options.body && typeof options.body === "object") {
let oldOptionsBody = lodash_1.default.cloneDeep(options.body);
options.body = JSON.stringify(oldOptionsBody);
}
if (options.headers) {
options.headers = lodash_1.default.merge(options.headers, finalHeaders);
const finalOptions = Object.assign({}, options);
fetchData = yield fetch(url, finalOptions);
}
else {
const finalOptions = Object.assign(Object.assign({}, options), { headers: finalHeaders });
fetchData = yield fetch(url, finalOptions);
}
data = fetchData.json();
}
catch (error) {
console.log("FetchAPI error #2:", error.message);
data = null;
}
catch (error) {
console.log("FetchAPI error #1:", error.message);
data = null;
}
else {
try {
let fetchData = yield fetch(url);
data = yield fetchData.json();
}
else if (typeof options === "object") {
try {
let fetchData;
if (options.body && typeof options.body === "object") {
let oldOptionsBody = _.cloneDeep(options.body);
options.body = JSON.stringify(oldOptionsBody);
}
catch (error) {
console.log("FetchAPI error #3:", error.message);
data = null;
if (options.headers) {
options.headers = _.merge(options.headers, finalHeaders);
const finalOptions = Object.assign({}, options);
fetchData = await fetch(url, finalOptions);
}
else {
const finalOptions = Object.assign(Object.assign({}, options), { headers: finalHeaders });
fetchData = await fetch(url, finalOptions);
}
data = fetchData.json();
}
return data;
});
catch (error) {
console.log("FetchAPI error #2:", error.message);
data = null;
}
}
else {
try {
let fetchData = await fetch(url);
data = await fetchData.json();
}
catch (error) {
console.log("FetchAPI error #3:", error.message);
data = null;
}
}
return data;
}