-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.ts
43 lines (40 loc) · 1.33 KB
/
init.ts
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
import { AdapterType, CLIENT_BY_ADAPTER_NAME } from './adapters';
import { ClientConfig } from './config';
import { ObjectStorageError } from './errors';
import { ObjectStoreClient } from './ObjectStoreClient';
type clientConstructor = (config: ClientConfig) => ObjectStoreClient;
const CONSTRUCTORS: { readonly [key: string]: clientConstructor } = Object.entries(
CLIENT_BY_ADAPTER_NAME,
).reduce((acc, [k, v]) => ({ ...acc, [k]: (config: ClientConfig) => new v(config) }), {});
/**
* Return object store client for the specified `type`.
*
* @param type
* @param endpoint
* @param accessKeyId
* @param secretAccessKey
* @param tlsEnabled
*/
export function initObjectStoreClient(
type: AdapterType,
endpoint?: string,
accessKeyId?: string,
secretAccessKey?: string,
tlsEnabled = true,
): ObjectStoreClient {
const constructor = CONSTRUCTORS[type];
if (!constructor) {
throw new ObjectStorageError(`Unknown client type "${type}"`);
}
if ((accessKeyId && !secretAccessKey) || (!accessKeyId && secretAccessKey)) {
throw new ObjectStorageError(
'Both access key id and secret access key must be set, or neither must be set',
);
}
const credentials = accessKeyId && secretAccessKey ? { accessKeyId, secretAccessKey } : undefined;
return constructor({
credentials,
endpoint,
tlsEnabled,
});
}