-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapiSettings.ts
More file actions
54 lines (48 loc) · 1.62 KB
/
apiSettings.ts
File metadata and controls
54 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { logger } from "@/logger.js";
import { BaseSettings, MindeeApiConstructorProps } from "@/http/baseSettings.js";
import { MindeeConfigurationError } from "@/errors/index.js";
const API_V2_KEY_ENVVAR_NAME: string = "MINDEE_V2_API_KEY";
const API_V2_HOST_ENVVAR_NAME: string = "MINDEE_V2_API_HOST";
const DEFAULT_MINDEE_API_HOST: string = "api-v2.mindee.net";
/**
* Settings for the V2 API.
*/
export class ApiSettings extends BaseSettings {
baseHeaders: Record<string, string>;
constructor({
apiKey,
dispatcher,
}: MindeeApiConstructorProps) {
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 */
}
protected apiKeyFromEnv(): string {
const envVarValue = process.env[API_V2_KEY_ENVVAR_NAME];
if (envVarValue) {
logger.debug(
"Set the V2 API key from the environment"
);
return envVarValue;
}
return "";
}
protected hostnameFromEnv(): string {
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;
}
}