@datastax/astra-db-ts
Version:
Data API TypeScript client
51 lines (50 loc) • 3.02 kB
JavaScript
// Copyright Datastax, Inc
// SPDX-License-Identifier: Apache-2.0
// noinspection JSDeprecatedSymbols
var _DataAPIClient_options;
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
import { TokenProvider } from '../lib/index.js';
import { Db, InvalidEnvironmentError } from '../db/index.js';
import { AstraAdmin } from '../administration/index.js';
import { $CustomInspect } from '../lib/constants.js';
import { AdminOptsHandler } from '../client/opts-handlers/admin-opts-handler.js';
import { DbOptsHandler } from '../client/opts-handlers/db-opts-handler.js';
import { RootOptsHandler } from '../client/opts-handlers/root-opts-handler.js';
import { HierarchicalLogger } from '../lib/logging/hierarchical-logger.js';
import { InternalLogger } from '../lib/logging/internal-logger.js';
export class DataAPIClient extends HierarchicalLogger {
constructor(tokenOrOptions, maybeOptions) {
const tokenPassed = (typeof tokenOrOptions === 'string' || tokenOrOptions instanceof TokenProvider || arguments.length > 1);
const token = (tokenPassed)
? tokenOrOptions
: undefined;
const rawOptions = (tokenPassed)
? maybeOptions
: tokenOrOptions;
const loggingConfig = InternalLogger.cfg.parse(rawOptions?.logging);
super(null, loggingConfig);
_DataAPIClient_options.set(this, void 0);
const parsedToken = TokenProvider.opts.parse(token, 'token');
__classPrivateFieldSet(this, _DataAPIClient_options, RootOptsHandler(parsedToken, this).parse(rawOptions ?? {}, 'options'), "f");
Object.defineProperty(this, $CustomInspect, {
value: () => `DataAPIClient(env="${__classPrivateFieldGet(this, _DataAPIClient_options, "f").environment}")`,
});
}
db(endpoint, options) {
if (typeof options === 'string') {
throw new Error(`.db() no longer allows the .db('<id>', '<region>') overload; please pass in the full endpoint url (e.g. .db('<endpoint>')). You may use the exported \`buildAstraEndpoint\` utility function if you need to create an endpoint from just an ID and a region.`);
}
return new Db(__classPrivateFieldGet(this, _DataAPIClient_options, "f"), endpoint, DbOptsHandler.parse(options));
}
admin(options) {
if (__classPrivateFieldGet(this, _DataAPIClient_options, "f").environment !== 'astra') {
throw new InvalidEnvironmentError('admin', __classPrivateFieldGet(this, _DataAPIClient_options, "f").environment, ['astra'], 'AstraAdmin is only available for Astra databases');
}
return new AstraAdmin(__classPrivateFieldGet(this, _DataAPIClient_options, "f"), AdminOptsHandler.parse(options, 'options'));
}
async close() {
await __classPrivateFieldGet(this, _DataAPIClient_options, "f").fetchCtx.ctx.close?.();
__classPrivateFieldGet(this, _DataAPIClient_options, "f").fetchCtx.closed.ref = true;
}
}
_DataAPIClient_options = new WeakMap();