Updates
This commit is contained in:
+7
-38
@@ -1,28 +1,8 @@
|
||||
import _ from "lodash";
|
||||
import { DSQLClientFetchHeader } 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;
|
||||
};
|
||||
import {
|
||||
DSQLClientFetchHeader,
|
||||
DSQLFetchApiOptions,
|
||||
} from "../../package-shared/types";
|
||||
|
||||
/**
|
||||
* # Fetch API
|
||||
@@ -30,26 +10,15 @@ export type FetchApiReturn = {
|
||||
export default async 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
|
||||
): Promise<R> {
|
||||
>(url: string, options?: DSQLFetchApiOptions<T>): Promise<R> {
|
||||
let data;
|
||||
|
||||
const csrfKey = "x-dsql-csrf-key";
|
||||
const csrfValue = localStorage.getItem(localStorageCSRFKey || csrfKey);
|
||||
|
||||
let finalHeaders = {
|
||||
"Content-Type": "application/json",
|
||||
} as DSQLClientFetchHeader;
|
||||
|
||||
if (csrf && csrfValue) {
|
||||
finalHeaders[localStorageCSRFKey || csrfKey] = csrfValue;
|
||||
if (options?.csrfKey && options.csrfValue) {
|
||||
finalHeaders[options.csrfKey] = options.csrfValue;
|
||||
}
|
||||
|
||||
if (typeof options === "string") {
|
||||
|
||||
Reference in New Issue
Block a user