-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathHeaderAuthProvider.ts
More file actions
49 lines (40 loc) · 1.8 KB
/
Copy pathHeaderAuthProvider.ts
File metadata and controls
49 lines (40 loc) · 1.8 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
// This file was auto-generated by Fern from our API Definition.
import * as core from "../core/index.js";
import * as errors from "../errors/index.js";
const PARAM_KEY = "apiKey" as const;
const ENV_HEADER_KEY = "DEEPGRAM_API_KEY" as const;
const HEADER_NAME = "Authorization" as const;
export class HeaderAuthProvider implements core.AuthProvider {
private readonly options: HeaderAuthProvider.Options;
constructor(options: HeaderAuthProvider.Options) {
this.options = options;
}
public static canCreate(options: Partial<HeaderAuthProvider.Options>): boolean {
return options?.[PARAM_KEY] != null || (typeof process !== "undefined" && process.env?.[ENV_HEADER_KEY] != null);
}
public async getAuthRequest({
endpointMetadata,
}: {
endpointMetadata?: core.EndpointMetadata;
} = {}): Promise<core.AuthRequest> {
const headerValue = (await core.Supplier.get(this.options[PARAM_KEY])) ?? (typeof process !== "undefined" ? process.env?.[ENV_HEADER_KEY] : undefined);
if (headerValue == null) {
throw new errors.DeepgramError({
message: HeaderAuthProvider.AUTH_CONFIG_ERROR_MESSAGE,
});
}
return {
headers: { [HEADER_NAME]: headerValue },
};
}
}
export namespace HeaderAuthProvider {
export const AUTH_SCHEME = "ApiKeyAuth" as const;
export const AUTH_CONFIG_ERROR_MESSAGE: string =
`Please provide '${PARAM_KEY}' when initializing the client, or set the '${ENV_HEADER_KEY}' environment variable` as const;
export type Options = AuthOptions;
export type AuthOptions = { [PARAM_KEY]?: core.Supplier<string> | undefined };
export function createInstance(options: Options): core.AuthProvider {
return new HeaderAuthProvider(options);
}
}