datasquirel/package-shared/utils/grab-host-names.ts
Benjamin Toby d78e44c98d Updates
2025-04-23 13:45:48 +01:00

51 lines
1.4 KiB
TypeScript

// @ts-check
import https from "https";
import http from "http";
type GrabHostNamesReturn = {
host: string;
port: number | string;
scheme: typeof http | typeof https;
user_id: string | number;
};
type Param = {
userId?: string | number;
env?: { [k: string]: string };
remoteHost?: string;
remoteHostPort?: string;
useDefault?: boolean;
};
/**
* # Grab Names For Query
*/
export default function grabHostNames(param?: Param): GrabHostNamesReturn {
const finalEnv = param?.env
? { ...process.env, ...param.env }
: process.env;
const scheme = finalEnv["DSQL_HTTP_SCHEME"];
const localHost = finalEnv["DSQL_LOCAL_HOST"];
const localHostPort = finalEnv["DSQL_LOCAL_HOST_PORT"];
const remoteHost = param?.useDefault
? undefined
: finalEnv["DSQL_API_REMOTE_HOST"]?.match(/.*\..*/)
? finalEnv["DSQL_API_REMOTE_HOST"]
: undefined;
const remoteHostPort = param?.useDefault
? undefined
: finalEnv["DSQL_API_REMOTE_HOST_PORT"]?.match(/./)
? finalEnv["DSQL_API_REMOTE_HOST_PORT"]
: undefined;
return {
host: remoteHost || localHost || "datasquirel.com",
port: remoteHostPort || localHostPort || 443,
scheme: scheme?.match(/^http$/i) ? http : https,
user_id: param?.userId || String(finalEnv["DSQL_API_USER_ID"] || 0),
};
}