94 lines
2.4 KiB
TypeScript
94 lines
2.4 KiB
TypeScript
import path from "path";
|
|
import fs from "fs";
|
|
import grabHostNames from "../../utils/grab-host-names";
|
|
import apiCreateUser from "../../functions/api/users/api-create-user";
|
|
import { AddUserFunctionReturn, UserDataPayload } from "../../types";
|
|
|
|
type Param = {
|
|
key?: string;
|
|
database: string;
|
|
payload: UserDataPayload;
|
|
encryptionKey?: string;
|
|
useLocal?: boolean;
|
|
verify?: boolean;
|
|
};
|
|
|
|
/**
|
|
* # Add User to Database
|
|
*/
|
|
export default async function addUser({
|
|
key,
|
|
payload,
|
|
database,
|
|
encryptionKey,
|
|
useLocal,
|
|
verify,
|
|
}: Param): Promise<AddUserFunctionReturn> {
|
|
const grabedHostNames = grabHostNames();
|
|
const { host, port, scheme } = grabedHostNames;
|
|
|
|
if (useLocal) {
|
|
return await apiCreateUser({
|
|
database,
|
|
encryptionKey,
|
|
payload,
|
|
verify,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make https request
|
|
*
|
|
* @description make a request to datasquirel.com
|
|
*/
|
|
const httpResponse = await new Promise((resolve, reject) => {
|
|
const reqPayload = JSON.stringify({
|
|
payload,
|
|
database,
|
|
encryptionKey,
|
|
});
|
|
|
|
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: ``,
|
|
},
|
|
|
|
/**
|
|
* 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 as AddUserFunctionReturn;
|
|
}
|