UNPKG

mindee

Version:

Mindee Client Library for Node.js

41 lines (40 loc) 1.59 kB
import { logger } from "../../logger.js"; import { BaseSettings } from "../../http/baseSettings.js"; import { MindeeConfigurationError } from "../../errors/index.js"; const API_V2_KEY_ENVVAR_NAME = "MINDEE_V2_API_KEY"; const API_V2_HOST_ENVVAR_NAME = "MINDEE_V2_API_HOST"; const DEFAULT_MINDEE_API_HOST = "api-v2.mindee.net"; /** * Settings for the V2 API. */ export class ApiSettings extends BaseSettings { constructor({ apiKey, dispatcher, }) { super(apiKey, dispatcher); if (!this.apiKey || this.apiKey.length === 0) { throw new MindeeConfigurationError("Your V2 API key could not be set, check your Client Configuration\n." + `You can set this using the ${API_V2_KEY_ENVVAR_NAME} environment variable.`); } /* eslint-disable @typescript-eslint/naming-convention */ this.baseHeaders = { "User-Agent": this.getUserAgent(), Authorization: `${this.apiKey}`, }; /* eslint-enable @typescript-eslint/naming-convention */ } apiKeyFromEnv() { const envVarValue = process.env[API_V2_KEY_ENVVAR_NAME]; if (envVarValue) { logger.debug("Set the V2 API key from the environment"); return envVarValue; } return ""; } hostnameFromEnv() { const envVarValue = process.env[API_V2_HOST_ENVVAR_NAME]; if (envVarValue) { logger.debug(`Set the V2 API hostname from the environment to: ${envVarValue}`); return envVarValue; } return DEFAULT_MINDEE_API_HOST; } }