96 lines
2.6 KiB
TypeScript
96 lines
2.6 KiB
TypeScript
import grabHostNames from "../../utils/grab-host-names";
|
|
import apiDeleteUser from "../../functions/api/users/api-delete-user";
|
|
import { UpdateUserFunctionReturn } from "../../types";
|
|
import grabUserDSQLAPIPath from "../../utils/backend/users/grab-api-path";
|
|
|
|
type Param = {
|
|
apiKey?: string;
|
|
database: string;
|
|
deletedUserId: string | number;
|
|
useLocal?: boolean;
|
|
apiVersion?: string;
|
|
};
|
|
|
|
/**
|
|
* # Update User
|
|
*/
|
|
export default async function deleteUser({
|
|
apiKey,
|
|
database,
|
|
deletedUserId,
|
|
useLocal,
|
|
apiVersion = "v1",
|
|
}: Param): Promise<UpdateUserFunctionReturn> {
|
|
const grabedHostNames = grabHostNames();
|
|
const { host, port, scheme } = grabedHostNames;
|
|
|
|
if (useLocal) {
|
|
return await apiDeleteUser({
|
|
dbFullName: database,
|
|
deletedUserId,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make https request
|
|
*
|
|
* @description make a request to datasquirel.com
|
|
*/
|
|
const httpResponse = (await new Promise((resolve, reject) => {
|
|
const reqPayload = JSON.stringify({
|
|
database,
|
|
deletedUserId,
|
|
});
|
|
|
|
const finalAPIKey =
|
|
apiKey ||
|
|
process.env.DSQL_API_KEY ||
|
|
process.env.DSQL_FULL_ACCESS_API_KEY;
|
|
|
|
const httpsRequest = scheme.request(
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Content-Length": Buffer.from(reqPayload).length,
|
|
Authorization: finalAPIKey,
|
|
},
|
|
port,
|
|
hostname: host,
|
|
path: grabUserDSQLAPIPath({
|
|
paradigm: "auth",
|
|
action: "delete",
|
|
database,
|
|
apiVersion,
|
|
userID: deletedUserId,
|
|
}),
|
|
},
|
|
|
|
/**
|
|
* 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();
|
|
})) as UpdateUserFunctionReturn;
|
|
|
|
return httpResponse;
|
|
}
|