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