This commit is contained in:
Benjamin Toby 2025-12-26 13:16:11 +01:00
parent f1e0aa9fc4
commit 27887ded41
6 changed files with 44 additions and 70 deletions

View File

@ -1,28 +1,8 @@
import _ from "lodash"; import _ from "lodash";
import { DSQLClientFetchHeader } from "../../package-shared/types"; import {
DSQLClientFetchHeader,
type FetchApiOptions<T extends { [k: string]: any } = { [k: string]: any }> = { DSQLFetchApiOptions,
method: } from "../../package-shared/types";
| "POST"
| "GET"
| "DELETE"
| "PUT"
| "PATCH"
| "post"
| "get"
| "delete"
| "put"
| "patch";
body?: T | string;
headers?: DSQLClientFetchHeader;
};
export type FetchApiReturn = {
success: boolean;
payload: any;
msg?: string;
[key: string]: any;
};
/** /**
* # Fetch API * # Fetch API
@ -30,26 +10,15 @@ export type FetchApiReturn = {
export default async function fetchApi< export default async function fetchApi<
T extends { [k: string]: any } = { [k: string]: any }, T extends { [k: string]: any } = { [k: string]: any },
R extends any = any R extends any = any
>( >(url: string, options?: DSQLFetchApiOptions<T>): Promise<R> {
url: string,
options?: FetchApiOptions<T>,
csrf?: boolean,
/**
* Key to use to grab local Storage csrf value.
*/
localStorageCSRFKey?: string
): Promise<R> {
let data; let data;
const csrfKey = "x-dsql-csrf-key";
const csrfValue = localStorage.getItem(localStorageCSRFKey || csrfKey);
let finalHeaders = { let finalHeaders = {
"Content-Type": "application/json", "Content-Type": "application/json",
} as DSQLClientFetchHeader; } as DSQLClientFetchHeader;
if (csrf && csrfValue) { if (options?.csrfKey && options.csrfValue) {
finalHeaders[localStorageCSRFKey || csrfKey] = csrfValue; finalHeaders[options.csrfKey] = options.csrfValue;
} }
if (typeof options === "string") { if (typeof options === "string") {

View File

@ -1,19 +1,4 @@
import { DSQLClientFetchHeader } from "../../package-shared/types"; import { DSQLFetchApiOptions } from "../../package-shared/types";
type FetchApiOptions<T extends {
[k: string]: any;
} = {
[k: string]: any;
}> = {
method: "POST" | "GET" | "DELETE" | "PUT" | "PATCH" | "post" | "get" | "delete" | "put" | "patch";
body?: T | string;
headers?: DSQLClientFetchHeader;
};
export type FetchApiReturn = {
success: boolean;
payload: any;
msg?: string;
[key: string]: any;
};
/** /**
* # Fetch API * # Fetch API
*/ */
@ -21,9 +6,4 @@ export default function fetchApi<T extends {
[k: string]: any; [k: string]: any;
} = { } = {
[k: string]: any; [k: string]: any;
}, R extends any = any>(url: string, options?: FetchApiOptions<T>, csrf?: boolean, }, R extends any = any>(url: string, options?: DSQLFetchApiOptions<T>): Promise<R>;
/**
* Key to use to grab local Storage csrf value.
*/
localStorageCSRFKey?: string): Promise<R>;
export {};

View File

@ -17,20 +17,14 @@ const lodash_1 = __importDefault(require("lodash"));
/** /**
* # Fetch API * # Fetch API
*/ */
function fetchApi(url, options, csrf, function fetchApi(url, options) {
/**
* Key to use to grab local Storage csrf value.
*/
localStorageCSRFKey) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let data; let data;
const csrfKey = "x-dsql-csrf-key";
const csrfValue = localStorage.getItem(localStorageCSRFKey || csrfKey);
let finalHeaders = { let finalHeaders = {
"Content-Type": "application/json", "Content-Type": "application/json",
}; };
if (csrf && csrfValue) { if ((options === null || options === void 0 ? void 0 : options.csrfKey) && options.csrfValue) {
finalHeaders[localStorageCSRFKey || csrfKey] = csrfValue; finalHeaders[options.csrfKey] = options.csrfValue;
} }
if (typeof options === "string") { if (typeof options === "string") {
try { try {

View File

@ -2300,3 +2300,14 @@ export type ClientCrudFetchParams<T extends {
apiOrigin?: string; apiOrigin?: string;
headers?: DSQLClientFetchHeader; headers?: DSQLClientFetchHeader;
}; };
export type DSQLFetchApiOptions<T extends {
[k: string]: any;
} = {
[k: string]: any;
}> = {
method: "POST" | "GET" | "DELETE" | "PUT" | "PATCH" | "post" | "get" | "delete" | "put" | "patch";
body?: T | string;
headers?: DSQLClientFetchHeader;
csrfValue?: string;
csrfKey?: string;
};

View File

@ -2961,3 +2961,23 @@ export type ClientCrudFetchParams<
apiOrigin?: string; apiOrigin?: string;
headers?: DSQLClientFetchHeader; headers?: DSQLClientFetchHeader;
}; };
export type DSQLFetchApiOptions<
T extends { [k: string]: any } = { [k: string]: any }
> = {
method:
| "POST"
| "GET"
| "DELETE"
| "PUT"
| "PATCH"
| "post"
| "get"
| "delete"
| "put"
| "patch";
body?: T | string;
headers?: DSQLClientFetchHeader;
csrfValue?: string;
csrfKey?: string;
};

View File

@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/datasquirel", "name": "@moduletrace/datasquirel",
"version": "5.6.3", "version": "5.6.4",
"description": "Cloud-based SQL data management tool", "description": "Cloud-based SQL data management tool",
"main": "dist/index.js", "main": "dist/index.js",
"bin": { "bin": {