Update Types

This commit is contained in:
Benjamin Toby 2024-11-08 16:41:22 +01:00
parent 2ee812fef5
commit afd50caf42
7 changed files with 175 additions and 11 deletions

76
client/fetch/index.js Normal file
View File

@ -0,0 +1,76 @@
/** @type {import("../../package-shared/types").FetchApiFn} */
async function clientFetch() {
let data;
if (typeof options === "string") {
try {
let fetchData;
const csrfValue = localStorage.getItem("csrf");
switch (options) {
case "post":
fetchData = await fetch(url, {
method: options,
headers: {
"Content-Type": "application/json",
"x-csrf-auth": csrf ? csrfValue : "",
},
});
data = fetchData.json();
break;
default:
fetchData = await fetch(url);
data = fetchData.json();
break;
}
} catch (/** @type {any} */ error) {
console.log("FetchAPI error #1:", error.message);
data = null;
}
} else if (typeof options === "object") {
try {
let fetchData;
const csrfValue = localStorage.getItem("csrf");
if (options.body && typeof options.body === "object") {
let oldOptionsBody = _.cloneDeep(options.body);
options.body = JSON.stringify(oldOptionsBody);
}
if (options.headers) {
options.headers["x-csrf-auth"] = csrf ? csrfValue : "";
const finalOptions = { ...options };
fetchData = await fetch(url, finalOptions);
} else {
fetchData = await fetch(url, {
...options,
headers: {
"Content-Type": "application/json",
"x-csrf-auth": csrf ? csrfValue : "",
},
});
}
data = fetchData.json();
} catch (/** @type {any} */ error) {
console.log("FetchAPI error #2:", error.message);
data = null;
}
} else {
try {
let fetchData = await fetch(url);
data = fetchData.json();
} catch (/** @type {any} */ error) {
console.log("FetchAPI error #3:", error.message);
data = null;
}
}
return data;
}
module.exports = clientFetch;
exports.fetchApi = clientFetch;

View File

@ -9,6 +9,8 @@ const inputFileToBase64 = require("./media/inputFileToBase64");
const getAccessToken = require("./auth/google/getAccessToken"); const getAccessToken = require("./auth/google/getAccessToken");
const getGithubAccessToken = require("./auth/github/getAccessToken"); const getGithubAccessToken = require("./auth/github/getAccessToken");
const logout = require("./auth/logout"); const logout = require("./auth/logout");
const { fetchApi } = require("./fetch");
const clientFetch = require("./fetch");
//////////////////////////////////////// ////////////////////////////////////////
//////////////////////////////////////// ////////////////////////////////////////
@ -37,11 +39,16 @@ const auth = {
}; };
/** /**
* Main Export * Fetch
*/ */
const datasquirelClient = { const fetch = {
media: media, fetchApi,
auth: auth, clientFetch,
}; };
/**
* Main Export
*/
const datasquirelClient = { media, auth, fetch };
module.exports = datasquirelClient; module.exports = datasquirelClient;

44
index.d.ts vendored
View File

@ -1,2 +1,42 @@
import * as types from "./package-shared/types/index"; import get = require("./utils/get");
export default types; import post = require("./utils/post");
export namespace media {
export { uploadImage };
export { uploadFile };
export { deleteFile };
}
export namespace user {
export { createUser };
export { loginUser };
export { sendEmailCode };
export { logoutUser };
export { userAuth };
export { reAuthUser };
export { updateUser };
export { getUser };
export { getToken };
export { validateToken };
export namespace social {
export { loginWithGoogle };
export { loginWithGithub };
}
}
import getSchema = require("./utils/get-schema");
import sanitizeSql = require("./utils/functions/sanitizeSql");
import datasquirelClient = require("./client");
import uploadImage = require("./utils/upload-image");
import uploadFile = require("./utils/upload-file");
import deleteFile = require("./utils/delete-file");
import createUser = require("./users/add-user");
import loginUser = require("./users/login-user");
import sendEmailCode = require("./users/send-email-code");
import logoutUser = require("./users/logout-user");
import userAuth = require("./users/user-auth");
import reAuthUser = require("./users/reauth-user");
import updateUser = require("./users/update-user");
import getUser = require("./users/get-user");
import getToken = require("./users/get-token");
import validateToken = require("./users/validate-token");
import loginWithGoogle = require("./users/social/google-auth");
import loginWithGithub = require("./users/social/github-auth");
export { get, post, getSchema, sanitizeSql, datasquirelClient as client };

View File

@ -27,6 +27,7 @@ const getToken = require("./users/get-token");
const validateToken = require("./users/validate-token"); const validateToken = require("./users/validate-token");
const sanitizeSql = require("./utils/functions/sanitizeSql"); const sanitizeSql = require("./utils/functions/sanitizeSql");
const datasquirelClient = require("./client");
//////////////////////////////////////// ////////////////////////////////////////
//////////////////////////////////////// ////////////////////////////////////////
@ -71,6 +72,7 @@ const datasquirel = {
user: user, user: user,
getSchema: getSchema, getSchema: getSchema,
sanitizeSql: sanitizeSql, sanitizeSql: sanitizeSql,
client: datasquirelClient,
}; };
module.exports = datasquirel; module.exports = datasquirel;

View File

@ -1,4 +1,4 @@
import http from "http"; import type { IncomingMessage, ServerResponse } from "http";
import { Editor } from "tinymce"; import { Editor } from "tinymce";
export type DSQL_DatabaseFullName = string; export type DSQL_DatabaseFullName = string;
@ -186,9 +186,9 @@ export interface PackageUserLoginLocalBody {
dbSchema?: DSQL_DatabaseSchemaType; dbSchema?: DSQL_DatabaseSchemaType;
} }
type Request = http.IncomingMessage; type Request = IncomingMessage;
type Response = http.ServerResponse; type Response = ServerResponse;
type ImageInputFileToBase64FunctionReturn = { type ImageInputFileToBase64FunctionReturn = {
imageBase64: string; imageBase64: string;
@ -1096,3 +1096,38 @@ export type CheckApiCredentialsFnParam = {
database?: string; database?: string;
table?: string; table?: string;
}; };
export type FetchApiFn = (
url: string,
options?: FetchApiOptions,
csrf?: boolean
) => Promise<any>;
type FetchApiOptions = {
method:
| "POST"
| "GET"
| "DELETE"
| "PUT"
| "PATCH"
| "post"
| "get"
| "delete"
| "put"
| "patch";
body?: object | string;
headers?: FetchHeader;
};
export type AuthCsrfHeaderName = "x-csrf-auth";
type FetchHeader = HeadersInit & {
[key in AuthCsrfHeaderName]?: string | null;
};
export type FetchApiReturn = {
success: boolean;
payload: any;
msg?: string;
[key: string]: any;
};

View File

@ -1,12 +1,15 @@
{ {
"name": "datasquirel", "name": "datasquirel",
"version": "2.4.3", "version": "2.4.4",
"description": "Cloud-based SQL data management tool", "description": "Cloud-based SQL data management tool",
"main": "index.js", "main": "index.js",
"bin": { "bin": {
"dsql-watch": "./engine/dsql.js", "dsql-watch": "./engine/dsql.js",
"dsql-dump": "./engine/dump.js" "dsql-dump": "./engine/dump.js"
}, },
"scripts": {
"compile-tsc": "rm -rf dist && tsc --declaration --allowJs --outDir dist --emitDeclarationOnly --resolveJsonModule index.js && cat ./dist/index.d.ts > ./index.d.ts"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/BenjaminToby/dsql.git" "url": "git+https://github.com/BenjaminToby/dsql.git"

View File

@ -29,7 +29,8 @@
"types", "types",
"users", "users",
"utils", "utils",
"package-shared" "package-shared",
"client"
], ],
"exclude": ["node_modules", "dump"] "exclude": ["node_modules", "dump"]
} }