This commit is contained in:
2025-12-22 07:18:57 +01:00
parent 0d9f313dc0
commit cfcd08680f
46 changed files with 1338 additions and 98 deletions
+55
View File
@@ -0,0 +1,55 @@
import path from "path";
import {
APIResponseObject,
ClientCrudFetchParams,
PostInsertReturn,
} from "../../package-shared/types";
import serializeQuery from "../../package-shared/utils/serialize-query";
import fetchApi from "../fetch";
export default async function clientCrudFetch<
T extends { [k: string]: any } = { [k: string]: any },
P = string,
R extends { [k: string]: any } = { [k: string]: any }
>({
table,
basePath,
body,
query,
targetId,
method = "GET",
apiOrigin,
}: ClientCrudFetchParams<T, P>) {
try {
let pathname = basePath || ``;
pathname = path.join(pathname, String(table));
if (targetId) {
pathname = path.join(pathname, String(targetId));
}
if (query) {
pathname = `${pathname}${serializeQuery(query)}`;
}
pathname = apiOrigin
? `${apiOrigin}/${pathname}`.replace(/([^:]\/)\/+/g, "$1")
: pathname;
const res = await fetchApi<
any,
APIResponseObject<PostInsertReturn | R[]>
>(pathname, {
method,
body,
});
return res;
} catch (error: any) {
return {
success: false,
msg: `API ERROR => ${error.message}`,
};
}
}
+8 -1
View File
@@ -14,6 +14,7 @@ import slugify from "../package-shared/utils/slugify";
import postLogin from "./auth/post-login";
import deserializeQuery from "../package-shared/utils/deserialize-query";
import debugLog from "../package-shared/utils/logging/debug-log";
import clientCrudFetch from "./crud-fetch";
const media = {
imageInputToBase64,
@@ -56,6 +57,12 @@ const fetch = {
/**
* Main Export
*/
const datasquirelClient = { media, auth, fetch, utils };
const datasquirelClient = {
media,
auth,
fetch,
utils,
clientCrudFetch,
};
export default datasquirelClient;