import axios, { AxiosRequestConfig } from "axios";
import { Buffer } from "buffer";
export const _DATABASE = import.meta.env.VITE_DATABASE;
export const _BASE_URL = import.meta.env.VITE_BASE_URL;
const local_host = `http://localhost:${import.meta.env.VITE_FQ_LOCAL_SERVER_PORT}`;
import tokens from "./tokens.json";
const typedTokens: Tokens = tokens as Tokens;
type HttpMethod = "get" | "post" | "put" | "delete" | "sql";
params: [{ [key: string]: string | number }];
key?: Record<string, string | any>;
page?: Record<string, string | number>;
sort?: Record<string, string | number>;
joins?: Record<string, string | number>;
filter?: Record<string, string | number>;
search?: Record<string, string | number>;
nearby?: Record<string, string | number>;
hidden?: Record<string, string | number>;
fields?: Record<string, string | number>;
session?: Record<string, string | number>;
validation?: Record<string, string | number>;
permissions?: Record<string, string | number>;
function uniqueKey(input: string) {
let code = input.charCodeAt(0);
for (let i = 0; i < input.length; i++) {
const char = input.charCodeAt(i);
code = (code << 5) - code + char;
return Buffer.from(code.toString()).toString("base64").substring(0, 8);
function isSQLQuery(method: HttpMethod | "SQL", body: any) {
if (Array.isArray(body)) {
return body.map(b => b.sql).every(b => typeof b === "string");
return typeof body.sql === "string";
function getSQLQuery(body: any) {
if (Array.isArray(body)) {
return JSON.stringify(body[0].sql);
return JSON.stringify(body.map(b => b.sql));
return JSON.stringify(body.sql);
function getKey(method: HttpMethod, url: string, options: RequestOptions): string {
if (!local_host) throw new Error("local_host is not defined");
const _url = local_host + url;
const parsed_url = new URL(_url);
const pathname = "/" + parsed_url.pathname.split("/")[1];
collections: options.joins,
permissions: options.permissions,
validation: options.validation,
body_is_array: !isSQLQuery(method, options.body) ? Array.isArray(options.body || {}) : "",
sql_query: isSQLQuery(method, options.body) ? getSQLQuery(options.body) : "",
for (let key in request) {
if (request[key as keyof typeof request]) {
tokenStr += key + ":" + request[key as keyof typeof request];
return method + ":" + pathname + ">" + uniqueKey(tokenStr);
const makeRequest = async (method: HttpMethod, endpoint: string, options: RequestOptions = {}): Promise<any> => {
if (hidden) headers.hidden = hidden;
if (filter) headers.filter = filter;
if (fields) headers.fields = fields;
if (session) headers.session = session;
if (joins) headers.collections = joins;
if (validation) headers.validation = validation;
if (permissions) headers.permissions = permissions;
if (nearby) headers.nearby = nearby;
const key = getKey(method, endpoint, options);
const token = typedTokens[key] || false;
console.log("Loading started...");
const axiosInstance = axios.create({
baseURL: token ? _BASE_URL : local_host,
headers: { app: _DATABASE },
const requestConfig: AxiosRequestConfig = {
const response = await axiosInstance(requestConfig);
console.error(`${method.toUpperCase()} Error:`, error.message);
console.log("Loading completed.");
get: async (endpoint: string, options?: RequestOptions): Promise<any> => makeRequest("get", endpoint, options),
put: async (endpoint: string, options?: RequestOptions): Promise<any> => makeRequest("put", endpoint, options),
post: async (endpoint: string, options?: RequestOptions): Promise<any> => makeRequest("post", endpoint, options),
delete: async (endpoint: string, options?: RequestOptions): Promise<any> => makeRequest("delete", endpoint, options),
sql: async (endpoint: string, options?: RequestOptions): Promise<any> =>
makeRequest("post", `/sql-${endpoint.replace("/", "")}`, options),