datasquirel/package-shared/api/crud/post.ts
Benjamin Toby fcc668d3a1 Updates
2025-07-05 17:39:29 +01:00

52 lines
1.3 KiB
TypeScript

import path from "path";
import queryDSQLAPI from "../../functions/api/query-dsql-api";
import { APIResponseObject, DsqlCrudQueryObject } from "../../types";
import grabAPIBasePath from "../../utils/grab-api-base-path";
export type APICrudPostParams<
T extends { [key: string]: any } = { [key: string]: any }
> = {
dbName: string;
tableName: string;
body: T;
update?: boolean;
apiKey?: string;
/**
* # Query datasquirel.com
*/
useDefault?: boolean;
};
export default async function apiCrudPOST<
T extends { [key: string]: any } = { [key: string]: any }
>({
dbName,
tableName,
body,
update,
apiKey,
useDefault,
}: APICrudPostParams<T>): Promise<APIResponseObject> {
const basePath = grabAPIBasePath({ paradigm: "crud" });
const passedID = body.id as string | number | undefined;
const finalID = update
? typeof passedID === "number"
? String(passedID)
: passedID
: undefined;
const finalPath = path.join(basePath, dbName, tableName, finalID || "");
const GET_RES = await queryDSQLAPI<DsqlCrudQueryObject<T>>({
method: update ? "PUT" : "POST",
path: finalPath,
body,
apiKey,
useDefault,
});
return GET_RES;
}