datasquirel/dist/package-shared/actions/upload-file.js
Benjamin Toby 7e8bb37c09 Updates
2025-07-05 14:59:30 +01:00

62 lines
2.0 KiB
JavaScript

import grabHostNames from "../utils/grab-host-names";
/**
* # Upload File via API
*/
export default async function uploadImage({ key, payload, user_id, useDefault, }) {
var _a;
const grabedHostNames = grabHostNames({ useDefault });
const { host, port, scheme } = grabedHostNames;
try {
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify(payload);
const httpsRequest = scheme.request({
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key ||
process.env.DSQL_FULL_ACCESS_API_KEY ||
process.env.DSQL_API_KEY,
},
port,
hostname: host,
path: `/api/query/${user_id || grabedHostNames.user_id}/add-file`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
});
httpsRequest.write(reqPayload);
httpsRequest.end();
});
return httpResponse;
}
catch (error) {
console.log("Error in uploading file: ", error.message);
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Error Uploading File`, error);
return {
success: false,
payload: null,
msg: error.message,
};
}
}