Skip to content

Commit

Permalink
bit more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
toptobes committed Jan 31, 2025
1 parent ad305dc commit a00596a
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/administration/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import type { DevOpsAPIRequestInfo } from '@/src/lib/api/clients/devops-api-http-client';
import type { DataAPIErrorDescriptor } from '@/src/documents';
import { DataAPIClientEvent } from '@/src/lib/logging/events';
import { BaseDataAPIClientEvent } from '@/src/lib/logging/events';
import { TimeoutDescriptor } from '@/src/lib/api/timeouts/timeouts';

/**
Expand Down Expand Up @@ -52,7 +52,7 @@ export type AdminCommandEventMap = {
*
* @public
*/
export abstract class AdminCommandEvent extends DataAPIClientEvent {
export abstract class AdminCommandEvent extends BaseDataAPIClientEvent {
/**
* The path for the request, not including the Base URL.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/client/opts-handlers/admin-opts-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { DecoderType, MonoidalOptionsHandler, OptionsHandlerTypes, Parsed, Unparse } from '@/src/lib/opts-handler';
import { MonoidalOptionsHandler, OptionsHandlerTypes, Parsed, Unparse } from '@/src/lib/opts-handler';
import { AdminOptions } from '@/src/client';
import { TokenProvider } from '@/src/lib';
import { object, oneOf, optional, record, string } from 'decoders';
import { DecoderType, object, oneOf, optional, record, string } from 'decoders';
import { Timeouts } from '@/src/lib/api/timeouts/timeouts';
import { Logger } from '@/src/lib/logging/logger';

Expand Down
4 changes: 2 additions & 2 deletions src/client/opts-handlers/db-opts-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { DecoderType, MonoidalOptionsHandler, OptionsHandlerTypes, Parsed, Unparse } from '@/src/lib/opts-handler';
import { MonoidalOptionsHandler, OptionsHandlerTypes, Parsed, Unparse } from '@/src/lib/opts-handler';
import { DbOptions } from '@/src/client';
import { TokenProvider } from '@/src/lib';
import { Decoder, nullish, object, oneOf, optional, record, regex, string, unknown } from 'decoders';
import { Decoder, DecoderType, nullish, object, oneOf, optional, record, regex, string, unknown } from 'decoders';
import { Timeouts } from '@/src/lib/api/timeouts/timeouts';
import { Logger } from '@/src/lib/logging/logger';
import { TableSerDes } from '@/src/documents/tables/ser-des/ser-des';
Expand Down
14 changes: 12 additions & 2 deletions src/client/opts-handlers/http-opts-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { boolean, constant, either, inexact, object, optional, positiveInteger, taggedUnion } from 'decoders';
import {
boolean,
constant,
DecoderType,
either,
inexact,
object,
optional,
positiveInteger,
taggedUnion,
} from 'decoders';
import { function_, isNullish } from '@/src/lib/utils';
import { DecoderType, OptionsHandler, OptionsHandlerTypes, Parsed } from '@/src/lib/opts-handler';
import { OptionsHandler, OptionsHandlerTypes, Parsed } from '@/src/lib/opts-handler';
import { DataAPIHttpOptions } from '@/src/client';
import type { FetchCtx, Fetcher } from '@/src/lib/api/fetch/types';
import { FetchH2, FetchNative } from '@/src/lib';
Expand Down
6 changes: 3 additions & 3 deletions src/client/opts-handlers/root-opts-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { DecoderType, OptionsHandler, OptionsHandlerTypes, Parsed } from '@/src/lib/opts-handler';
import { DataAPIClientOptions, DataAPIClient } from '@/src/client';
import { OptionsHandler, OptionsHandlerTypes, Parsed } from '@/src/lib/opts-handler';
import { DataAPIClient, DataAPIClientOptions } from '@/src/client';
import { type DataAPIClientEventMap, TokenProvider } from '@/src/lib';
import { object, optional } from 'decoders';
import { DecoderType, object, optional } from 'decoders';
import { Timeouts } from '@/src/lib/api/timeouts/timeouts';
import { Logger } from '@/src/lib/logging/logger';
import { EnvironmentCfgHandler } from '@/src/client/opts-handlers/environment-cfg-handler';
Expand Down
9 changes: 5 additions & 4 deletions src/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { CreateCollectionOptions } from '@/src/db/types/collections/create-colle
import { TokenProvider } from '@/src/lib';
import { DataAPIHttpClient, EmissionStrategy } from '@/src/lib/api/clients/data-api-http-client';
import { KeyspaceRef } from '@/src/lib/api/clients/types';
import { validateDataAPIEnv } from '@/src/lib/utils';
import { EmbeddingHeadersProvider, FoundRow, SomeRow, Table, TableDropIndexOptions } from '@/src/documents';
import { DEFAULT_DATA_API_PATHS } from '@/src/lib/api/constants';
import { CollectionOptions } from '@/src/db/types/collections/collection-options';
Expand All @@ -44,6 +43,7 @@ import { TableSerDes } from '@/src/documents/tables/ser-des/ser-des';
import { AdminOptsHandler } from '@/src/client/opts-handlers/admin-opts-handler';
import { DbOptsHandler, ParsedDbOptions } from '@/src/client/opts-handlers/db-opts-handler';
import { ParsedRootClientOpts } from '@/src/client/opts-handlers/root-opts-handler';
import { EnvironmentCfgHandler } from '@/src/client/opts-handlers/environment-cfg-handler';

/**
* #### Overview
Expand Down Expand Up @@ -369,9 +369,7 @@ export class Db {
public admin(options: AdminOptions & { environment: Exclude<DataAPIEnvironment, 'astra'> }): DataAPIDbAdmin

public admin(options?: AdminOptions & { environment?: DataAPIEnvironment }): DbAdmin {
const environment = options?.environment ?? 'astra';

validateDataAPIEnv(environment);
const environment = EnvironmentCfgHandler.parseWithin(options, 'environment');

if (this.#defaultOpts.environment !== environment) {
throw new InvalidEnvironmentError('db.admin()', environment, [this.#defaultOpts.environment], 'environment option is not the same as set in the DataAPIClient');
Expand Down Expand Up @@ -1200,6 +1198,9 @@ export class Db {
});
}

/**
* Backdoor to the HTTP client for if it's absolutely necessary. Which it almost never (if even ever) is.
*/
public get _httpClient(): OpaqueHttpClient {
return this.#httpClient;
}
Expand Down
4 changes: 2 additions & 2 deletions src/documents/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import { DEFAULT_KEYSPACE, type RawDataAPIResponse } from '@/src/lib';
// import { DataAPIClientEvent } from '@/src/lib/logging/events'; needs to be like this or it errors
import { DataAPIClientEvent } from '@/src/lib/logging/events';
import { BaseDataAPIClientEvent } from '@/src/lib/logging/events';
import type { DataAPIRequestInfo } from '@/src/lib/api/clients/data-api-http-client';
import type { DataAPIErrorDescriptor } from '@/src/documents/errors';
import { TimeoutDescriptor } from '@/src/lib/api/timeouts/timeouts';
Expand Down Expand Up @@ -55,7 +55,7 @@ export type CommandEventMap = {
*
* @public
*/
export abstract class CommandEvent extends DataAPIClientEvent {
export abstract class CommandEvent extends BaseDataAPIClientEvent {
/**
* The command object. Equal to the response body of the HTTP request.
*
Expand Down
4 changes: 2 additions & 2 deletions src/lib/logging/cfg-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { DecoderType, MonoidalOptionsHandler, OptionsHandlerTypes, Parsed } from '@/src/lib/opts-handler';
import { MonoidalOptionsHandler, OptionsHandlerTypes, Parsed } from '@/src/lib/opts-handler';
import { DataAPILoggingConfig, DataAPILoggingEvent, DataAPILoggingOutput } from '@/src/lib';
import { array, either, nonEmptyArray, object, oneOf, optional } from 'decoders';
import { array, DecoderType, either, nonEmptyArray, object, oneOf, optional } from 'decoders';
import {
LoggingDefaultOutputs,
LoggingDefaults,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/logging/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @public
*/
export abstract class DataAPIClientEvent {
export abstract class BaseDataAPIClientEvent {
/**
* The name of the event.
*/
Expand All @@ -39,7 +39,7 @@ export abstract class DataAPIClientEvent {
* Returns the event in a formatted string, as it would be logged to stdout/stderr (if enabled).
*/
public formatted(): string {
return `${DataAPIClientEvent.formattedPrefix()}[${this.name}]`;
return `${BaseDataAPIClientEvent.formattedPrefix()}[${this.name}]`;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/lib/logging/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type {
import { EmptyInternalLoggingConfig, EventConstructors } from '@/src/lib/logging/constants';
import { buildOutputsMap } from '@/src/lib/logging/util';
import type TypedEventEmitter from 'typed-emitter';
import type { DataAPIClientEvent } from '@/src/lib';
import type { BaseDataAPIClientEvent } from '@/src/lib';
import { LoggingCfgHandler, ParsedLoggingConfig } from '@/src/lib/logging/cfg-handler';

/**
Expand Down Expand Up @@ -69,7 +69,7 @@ export class Logger implements Partial<Record<keyof DataAPIClientEventMap, unkno
const event = _event as keyof DataAPIClientEventMap;

this[event] = (...args: any[]) => {
const eventClass = new (<any>EventConstructors[event])(...args) as DataAPIClientEvent;
const eventClass = new (<any>EventConstructors[event])(...args) as BaseDataAPIClientEvent;

if (outputs.event) {
this.emitter.emit(event, <any>eventClass);
Expand Down
5 changes: 0 additions & 5 deletions src/lib/opts-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ export type Parsed<Brand> = { [__parsed]: Brand };
*/
export type Unparse<T> = Omit<T, typeof __parsed>;

/**
* @internal
*/
export type DecoderType<T> = T extends Decoder<infer U> ? U : never;

/**
* @internal
*/
Expand Down
11 changes: 0 additions & 11 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { DataAPIEnvironment, nullish } from '@/src/lib/types';
import { DataAPIEnvironments } from '@/src/lib/constants';
import JBI from 'json-bigint';
import { SomeDoc } from '@/src/documents';
import BigNumber from 'bignumber.js';
Expand All @@ -26,15 +24,6 @@ export function isNullish(t: unknown): t is null | undefined {
return t === null || t === undefined;
}

/**
* @internal
*/
export function validateDataAPIEnv(env: unknown): asserts env is DataAPIEnvironment | nullish {
if (!isNullish(env) && !(<readonly unknown[]>DataAPIEnvironments).includes(env)) {
throw new Error(`Given environment is invalid (must be ${DataAPIEnvironments.map(e => `"${e}"`).join(', ')}, or nullish to default to "astra" (got: ${env}).`);
}
}

/**
* @internal
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/testlib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import * as process from 'node:process';
import dotenv from 'dotenv';
import { DataAPIEnvironments } from '@/src/lib/constants';
import { DataAPIClientEvent, DataAPIEnvironment } from '@/src/lib';
import { BaseDataAPIClientEvent, DataAPIEnvironment } from '@/src/lib';

dotenv.config();

Expand Down Expand Up @@ -57,7 +57,7 @@ export const DEMO_APPLICATION_URI = 'https://12341234-1234-1234-1234-12341234123

export const DEFAULT_TEST_TIMEOUT = +process.env.CLIENT_TESTS_TIMEOUT! || 90000;

export const LOGGING_PRED: (e: DataAPIClientEvent, isGlobal: boolean) => boolean = process.env.LOGGING_PRED
export const LOGGING_PRED: (e: BaseDataAPIClientEvent, isGlobal: boolean) => boolean = process.env.LOGGING_PRED
? new Function("e", "isGlobal", "return " + process.env.LOGGING_PRED) as any
: () => false;

Expand Down
4 changes: 2 additions & 2 deletions tests/testlib/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
TEST_APPLICATION_URI,
TEST_HTTP_CLIENT,
} from '@/tests/testlib/config';
import { DataAPIClientEvent, DataAPIClientEventMap, DataAPILoggingConfig } from '@/src/lib';
import { BaseDataAPIClientEvent, DataAPIClientEventMap, DataAPILoggingConfig } from '@/src/lib';
import { CreateTableDefinition, InferTableSchema } from '@/src/db';
import * as util from 'node:util';

Expand Down Expand Up @@ -126,7 +126,7 @@ export const initTestObjects = (opts?: TestObjectsOptions) => {
});

for (const event of ['commandSucceeded', 'adminCommandSucceeded', 'commandFailed', 'adminCommandFailed'] as (keyof DataAPIClientEventMap)[]) {
client.on(event, (e: DataAPIClientEvent) => LOGGING_PRED(e, isGlobal) && console.log((isGlobal ? '[Global] ' : '') + util.inspect(e, { depth: null, colors: true })));
client.on(event, (e: BaseDataAPIClientEvent) => LOGGING_PRED(e, isGlobal) && console.log((isGlobal ? '[Global] ' : '') + util.inspect(e, { depth: null, colors: true })));
}

const db = client.db(TEST_APPLICATION_URI);
Expand Down
20 changes: 5 additions & 15 deletions tests/unit/lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ import assert from 'assert';
import { describe, it } from '@/tests/testlib';
import * as bn from 'bignumber.js';
import {
buildAstraEndpoint, forJSEnv,
buildAstraEndpoint,
forJSEnv,
isBigNumber,
isNullish,
jsonTryParse, pathMatches, pathArraysEqual,
jsonTryParse,
pathArraysEqual,
pathMatches,
toArray,
validateDataAPIEnv,
withJbiNullProtoFix,
} from '@/src/lib/utils';
import { DataAPIEnvironments } from '@/src/lib';
import JBI from 'json-bigint';

describe('unit.lib.utils', () => {
Expand All @@ -41,17 +42,6 @@ describe('unit.lib.utils', () => {
});
});

describe('validateDataAPIEnv', () => {
it('works', () => {
for (const env of [null, undefined, ...DataAPIEnvironments]) {
assert.doesNotThrow(() => validateDataAPIEnv(env));
}
for (const env of [0, '', 'hi!', {}, [], 'Astra', 'ASTRA']) {
assert.throws(() => validateDataAPIEnv(env));
}
});
});

describe('jsonTryParse', () => {
it('works', () => {
assert.deepStrictEqual(jsonTryParse('{}', 'else'), {});
Expand Down

0 comments on commit a00596a

Please sign in to comment.