From afd50caf42cbe69935a05a84434a956a2bdc1808 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Fri, 8 Nov 2024 16:41:22 +0100 Subject: [PATCH] Update Types --- client/fetch/index.js | 76 +++++++++++++++++++++++++++++++++ client/index.js | 15 +++++-- index.d.ts | 44 ++++++++++++++++++- index.js | 2 + package-shared/types/index.d.ts | 41 ++++++++++++++++-- package.json | 5 ++- tsconfig.json | 3 +- 7 files changed, 175 insertions(+), 11 deletions(-) create mode 100644 client/fetch/index.js diff --git a/client/fetch/index.js b/client/fetch/index.js new file mode 100644 index 0000000..948e787 --- /dev/null +++ b/client/fetch/index.js @@ -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; diff --git a/client/index.js b/client/index.js index 0aad182..1acf68d 100644 --- a/client/index.js +++ b/client/index.js @@ -9,6 +9,8 @@ const inputFileToBase64 = require("./media/inputFileToBase64"); const getAccessToken = require("./auth/google/getAccessToken"); const getGithubAccessToken = require("./auth/github/getAccessToken"); const logout = require("./auth/logout"); +const { fetchApi } = require("./fetch"); +const clientFetch = require("./fetch"); //////////////////////////////////////// //////////////////////////////////////// @@ -37,11 +39,16 @@ const auth = { }; /** - * Main Export + * Fetch */ -const datasquirelClient = { - media: media, - auth: auth, +const fetch = { + fetchApi, + clientFetch, }; +/** + * Main Export + */ +const datasquirelClient = { media, auth, fetch }; + module.exports = datasquirelClient; diff --git a/index.d.ts b/index.d.ts index a7b3857..967880a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,2 +1,42 @@ -import * as types from "./package-shared/types/index"; -export default types; +import get = require("./utils/get"); +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 }; diff --git a/index.js b/index.js index b7d6ff2..cf2f4ff 100644 --- a/index.js +++ b/index.js @@ -27,6 +27,7 @@ const getToken = require("./users/get-token"); const validateToken = require("./users/validate-token"); const sanitizeSql = require("./utils/functions/sanitizeSql"); +const datasquirelClient = require("./client"); //////////////////////////////////////// //////////////////////////////////////// @@ -71,6 +72,7 @@ const datasquirel = { user: user, getSchema: getSchema, sanitizeSql: sanitizeSql, + client: datasquirelClient, }; module.exports = datasquirel; diff --git a/package-shared/types/index.d.ts b/package-shared/types/index.d.ts index 383e425..ebcfa70 100644 --- a/package-shared/types/index.d.ts +++ b/package-shared/types/index.d.ts @@ -1,4 +1,4 @@ -import http from "http"; +import type { IncomingMessage, ServerResponse } from "http"; import { Editor } from "tinymce"; export type DSQL_DatabaseFullName = string; @@ -186,9 +186,9 @@ export interface PackageUserLoginLocalBody { dbSchema?: DSQL_DatabaseSchemaType; } -type Request = http.IncomingMessage; +type Request = IncomingMessage; -type Response = http.ServerResponse; +type Response = ServerResponse; type ImageInputFileToBase64FunctionReturn = { imageBase64: string; @@ -1096,3 +1096,38 @@ export type CheckApiCredentialsFnParam = { database?: string; table?: string; }; + +export type FetchApiFn = ( + url: string, + options?: FetchApiOptions, + csrf?: boolean +) => Promise; + +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; +}; diff --git a/package.json b/package.json index 74fea04..1a6d4cb 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,15 @@ { "name": "datasquirel", - "version": "2.4.3", + "version": "2.4.4", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": { "dsql-watch": "./engine/dsql.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": { "type": "git", "url": "git+https://github.com/BenjaminToby/dsql.git" diff --git a/tsconfig.json b/tsconfig.json index 377f664..04af5b7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -29,7 +29,8 @@ "types", "users", "utils", - "package-shared" + "package-shared", + "client" ], "exclude": ["node_modules", "dump"] }