124 lines
3.2 KiB
TypeScript
124 lines
3.2 KiB
TypeScript
import path from "path";
|
|
import fs from "fs";
|
|
import grabHostNames from "../../utils/grab-host-names";
|
|
import apiGetUser from "../../functions/api/users/api-get-user";
|
|
import {
|
|
APIGetUserFunctionParams,
|
|
GetUserFunctionReturn,
|
|
GetUserParams,
|
|
} from "../../types";
|
|
import grabUserDSQLAPIPath from "../../utils/backend/users/grab-api-path";
|
|
|
|
/**
|
|
* # Get User
|
|
*/
|
|
export default async function getUser({
|
|
apiKey,
|
|
userId,
|
|
database,
|
|
fields,
|
|
useLocal,
|
|
apiVersion = "v1",
|
|
dbUserId,
|
|
selectAll,
|
|
}: GetUserParams): Promise<GetUserFunctionReturn> {
|
|
/**
|
|
* Initialize
|
|
*/
|
|
const defaultFields = [
|
|
"id",
|
|
"first_name",
|
|
"last_name",
|
|
"email",
|
|
"username",
|
|
"image",
|
|
"image_thumbnail",
|
|
"verification_status",
|
|
"date_created",
|
|
"date_created_code",
|
|
"date_created_timestamp",
|
|
"date_updated",
|
|
"date_updated_code",
|
|
"date_updated_timestamp",
|
|
];
|
|
|
|
const updatedFields =
|
|
fields && fields[0] ? [...defaultFields, ...fields] : defaultFields;
|
|
|
|
const grabedHostNames = grabHostNames();
|
|
const { host, port, scheme } = grabedHostNames;
|
|
|
|
const getUserParams: APIGetUserFunctionParams = {
|
|
userId,
|
|
fields: [...new Set(updatedFields)],
|
|
database,
|
|
dbUserId,
|
|
selectAll,
|
|
};
|
|
|
|
if (useLocal) {
|
|
return await apiGetUser(getUserParams);
|
|
}
|
|
|
|
/**
|
|
* Make https request
|
|
*
|
|
* @description make a request to datasquirel.com
|
|
*/
|
|
const httpResponse = await new Promise((resolve, reject) => {
|
|
const reqPayload = JSON.stringify(getUserParams);
|
|
|
|
const finalAPIKey =
|
|
apiKey ||
|
|
process.env.DSQL_API_KEY ||
|
|
process.env.DSQL_FULL_ACCESS_API_KEY ||
|
|
process.env.DSQL_READ_ONLY_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: "get",
|
|
database,
|
|
apiVersion,
|
|
userID: userId,
|
|
}),
|
|
},
|
|
|
|
/**
|
|
* 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 GetUserFunctionReturn;
|
|
}
|