@datastax/astra-db-ts
Version:
Data API TypeScript client
55 lines (54 loc) • 3.4 kB
JavaScript
;
// Copyright Datastax, Inc
// SPDX-License-Identifier: Apache-2.0
// noinspection JSDeprecatedSymbols
var _DataAPIClient_options;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataAPIClient = void 0;
const tslib_1 = require("tslib");
const index_js_1 = require("../lib/index.js");
const index_js_2 = require("../db/index.js");
const index_js_3 = require("../administration/index.js");
const constants_js_1 = require("../lib/constants.js");
const admin_opts_handler_js_1 = require("../client/opts-handlers/admin-opts-handler.js");
const db_opts_handler_js_1 = require("../client/opts-handlers/db-opts-handler.js");
const root_opts_handler_js_1 = require("../client/opts-handlers/root-opts-handler.js");
const hierarchical_logger_js_1 = require("../lib/logging/hierarchical-logger.js");
const internal_logger_js_1 = require("../lib/logging/internal-logger.js");
class DataAPIClient extends hierarchical_logger_js_1.HierarchicalLogger {
constructor(tokenOrOptions, maybeOptions) {
const tokenPassed = (typeof tokenOrOptions === 'string' || tokenOrOptions instanceof index_js_1.TokenProvider || arguments.length > 1);
const token = (tokenPassed)
? tokenOrOptions
: undefined;
const rawOptions = (tokenPassed)
? maybeOptions
: tokenOrOptions;
const loggingConfig = internal_logger_js_1.InternalLogger.cfg.parse(rawOptions?.logging);
super(null, loggingConfig);
_DataAPIClient_options.set(this, void 0);
const parsedToken = index_js_1.TokenProvider.opts.parse(token, 'token');
tslib_1.__classPrivateFieldSet(this, _DataAPIClient_options, (0, root_opts_handler_js_1.RootOptsHandler)(parsedToken, this).parse(rawOptions ?? {}, 'options'), "f");
Object.defineProperty(this, constants_js_1.$CustomInspect, {
value: () => `DataAPIClient(env="${tslib_1.__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 index_js_2.Db(tslib_1.__classPrivateFieldGet(this, _DataAPIClient_options, "f"), endpoint, db_opts_handler_js_1.DbOptsHandler.parse(options));
}
admin(options) {
if (tslib_1.__classPrivateFieldGet(this, _DataAPIClient_options, "f").environment !== 'astra') {
throw new index_js_2.InvalidEnvironmentError('admin', tslib_1.__classPrivateFieldGet(this, _DataAPIClient_options, "f").environment, ['astra'], 'AstraAdmin is only available for Astra databases');
}
return new index_js_3.AstraAdmin(tslib_1.__classPrivateFieldGet(this, _DataAPIClient_options, "f"), admin_opts_handler_js_1.AdminOptsHandler.parse(options, 'options'));
}
async close() {
await tslib_1.__classPrivateFieldGet(this, _DataAPIClient_options, "f").fetchCtx.ctx.close?.();
tslib_1.__classPrivateFieldGet(this, _DataAPIClient_options, "f").fetchCtx.closed.ref = true;
}
}
exports.DataAPIClient = DataAPIClient;
_DataAPIClient_options = new WeakMap();