From 288635dce1c5dea31c1cd3c3e5e446855464199a Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Mon, 24 Jun 2024 14:09:20 -0400 Subject: [PATCH] Clean up constants, types, and TODOs --- src/constants-internal.ts | 4 ++-- src/constants.ts | 28 ++++++++++++++++++++++++---- src/models/model.ts | 6 +++--- src/sources/index.ts | 1 + src/sources/types.ts | 10 +++++----- src/sources/widget-base-source.ts | 6 +++--- src/sources/widget-query-source.ts | 4 ++-- src/sources/widget-table-source.ts | 4 ++-- src/types-internal.ts | 1 + src/types.ts | 28 +++++++++++----------------- test/constants.test.ts | 14 +++++++------- test/types.test.ts | 24 ++++++++++++------------ 12 files changed, 73 insertions(+), 57 deletions(-) diff --git a/src/constants-internal.ts b/src/constants-internal.ts index f16b422..94f95d6 100644 --- a/src/constants-internal.ts +++ b/src/constants-internal.ts @@ -1,7 +1,7 @@ -import {FilterTypes} from './types'; +import {FilterType} from './constants'; /** @internal */ -export const FILTER_TYPES = new Set(Object.values(FilterTypes)); +export const FILTER_TYPES = new Set(Object.values(FilterType)); /** * Threshold to use GET requests, vs POST diff --git a/src/constants.ts b/src/constants.ts index e227ba0..1903033 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,17 +1,37 @@ export const CLIENT_ID = 'carto-api-client'; -// TODO(cleanup): Lots of inconsistency in naming and usage of enums in the project. - /** @internalRemarks Source: @carto/constants */ -export enum MAP_TYPES { +export enum MapType { TABLE = 'table', QUERY = 'query', TILESET = 'tileset', } /** @internalRemarks Source: @carto/constants */ -export enum API_VERSIONS { +export enum ApiVersion { V1 = 'v1', V2 = 'v2', V3 = 'v3', } + +/** @internalRemarks Source: @carto/react-core */ +export enum GroupDateType { + YEARS = 'year', + MONTHS = 'month', + WEEKS = 'week', + DAYS = 'day', + HOURS = 'hour', + MINUTES = 'minute', + SECONDS = 'second', +} + +/** @internalRemarks Source: @carto/react-api, @deck.gl/carto */ +export enum FilterType { + IN = 'in', + /** [a, b] both are included. */ + BETWEEN = 'between', + /** [a, b) a is included, b is not. */ + CLOSED_OPEN = 'closed_open', + TIME = 'time', + STRING_SEARCH = 'stringSearch', +} diff --git a/src/models/model.ts b/src/models/model.ts index d28c151..5eb31d5 100644 --- a/src/models/model.ts +++ b/src/models/model.ts @@ -1,5 +1,5 @@ import {getClient} from '../client'; -import {API_VERSIONS, MAP_TYPES} from '../constants'; +import {ApiVersion, MapType} from '../constants'; import { DEFAULT_GEO_COLUMN, REQUEST_GET_MAX_URL_LENGTH, @@ -48,11 +48,11 @@ export function executeModel(props: { checkCredentials(source.credentials); assert( - source.credentials.apiVersion === API_VERSIONS.V3, + source.credentials.apiVersion === ApiVersion.V3, 'SQL Model API is a feature only available in CARTO 3.' ); assert( - source.type !== MAP_TYPES.TILESET, + source.type !== MapType.TILESET, 'executeModel: Tileset not supported' ); diff --git a/src/sources/index.ts b/src/sources/index.ts index e4c8c45..ffbf218 100644 --- a/src/sources/index.ts +++ b/src/sources/index.ts @@ -2,3 +2,4 @@ export type {WidgetSource} from './widget-base-source.js'; export * from './widget-query-source.js'; export * from './widget-table-source.js'; export * from './wrappers.js'; +export * from './types.js'; diff --git a/src/sources/types.ts b/src/sources/types.ts index 9b6b4ee..6771eee 100644 --- a/src/sources/types.ts +++ b/src/sources/types.ts @@ -45,14 +45,14 @@ export interface ScatterRequestOptions extends BaseRequestOptions { export interface TimeSeriesRequestOptions extends BaseRequestOptions { column: string; - stepSize?: $TODO; - stepMultiplier?: $TODO; + stepSize?: number; + stepMultiplier?: number; operation?: AggregationType; operationColumn?: string; joinOperation?: AggregationType; - splitByCategory?: $TODO; - splitByCategoryLimit?: $TODO; - splitByCategoryValues?: $TODO; + splitByCategory?: string; + splitByCategoryLimit?: number; + splitByCategoryValues?: string[]; } export interface HistogramRequestOptions extends BaseRequestOptions { diff --git a/src/sources/widget-base-source.ts b/src/sources/widget-base-source.ts index 5758512..1da6cfd 100644 --- a/src/sources/widget-base-source.ts +++ b/src/sources/widget-base-source.ts @@ -18,7 +18,7 @@ import { import {Source, Filter, FilterLogicalOperator, Credentials} from '../types.js'; import {SourceOptions} from '@deck.gl/carto'; import {getWidgetFilters, normalizeObjectKeys} from '../utils.js'; -import {API_VERSIONS, MAP_TYPES} from '../constants.js'; +import {ApiVersion, MapType} from '../constants.js'; import { DEFAULT_API_BASE_URL, DEFAULT_GEO_COLUMN, @@ -29,7 +29,7 @@ import {getClient} from '../client.js'; * TODO(cleanup): Consolidate {@link SourceOptions} and {@link Source}. */ export interface WidgetBaseSourceProps extends SourceOptions, Credentials { - type?: MAP_TYPES; + type?: MapType; filtersLogicalOperator?: FilterLogicalOperator; queryParameters?: unknown[]; provider?: string; @@ -52,7 +52,7 @@ export class WidgetBaseSource { this.props = {...WidgetBaseSource.defaultProps, ...props}; this.connectionName = props.connectionName; this.credentials = { - apiVersion: props.apiVersion || API_VERSIONS.V3, + apiVersion: props.apiVersion || ApiVersion.V3, apiBaseUrl: props.apiBaseUrl || DEFAULT_API_BASE_URL, clientId: props.clientId || getClient(), accessToken: props.accessToken, diff --git a/src/sources/widget-query-source.ts b/src/sources/widget-query-source.ts index c9f5064..cb86241 100644 --- a/src/sources/widget-query-source.ts +++ b/src/sources/widget-query-source.ts @@ -3,7 +3,7 @@ import { QuadbinQuerySourceOptions, VectorQuerySourceOptions, } from '@deck.gl/carto'; -import {MAP_TYPES} from '../constants.js'; +import {MapType} from '../constants.js'; import {WidgetBaseSource, WidgetBaseSourceProps} from './widget-base-source.js'; import {Source} from '../types.js'; @@ -18,7 +18,7 @@ export class WidgetQuerySource extends WidgetBaseSource< protected override getSource(owner: string): Source { return { ...super.getSource(owner), - type: MAP_TYPES.QUERY, + type: MapType.QUERY, data: this.props.sqlQuery, }; } diff --git a/src/sources/widget-table-source.ts b/src/sources/widget-table-source.ts index 512f240..04d8236 100644 --- a/src/sources/widget-table-source.ts +++ b/src/sources/widget-table-source.ts @@ -4,7 +4,7 @@ import { VectorTableSourceOptions, } from '@deck.gl/carto'; import {WidgetBaseSource, WidgetBaseSourceProps} from './widget-base-source.js'; -import {MAP_TYPES} from '../constants.js'; +import {MapType} from '../constants.js'; import {Source} from '../types.js'; type LayerTableSourceOptions = @@ -18,7 +18,7 @@ export class WidgetTableSource extends WidgetBaseSource< protected override getSource(owner: string): Source { return { ...super.getSource(owner), - type: MAP_TYPES.TABLE, + type: MapType.TABLE, data: this.props.tableName, }; } diff --git a/src/types-internal.ts b/src/types-internal.ts index c569c0e..97c9501 100644 --- a/src/types-internal.ts +++ b/src/types-internal.ts @@ -2,4 +2,5 @@ * INTERNAL */ +/** @internal */ export type $TODO = any; diff --git a/src/types.ts b/src/types.ts index b87c2a8..d410881 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -import type {API_VERSIONS, MAP_TYPES} from './constants'; +import type {ApiVersion, MapType, FilterType} from './constants'; /****************************************************************************** * AGGREGATION @@ -28,7 +28,7 @@ export type SpatialFilter = GeoJSON.Polygon | GeoJSON.MultiPolygon; /** @internalRemarks Source: @carto/react-api */ export type Credentials = { - apiVersion?: API_VERSIONS; + apiVersion?: ApiVersion; apiBaseUrl?: string; geoColumn?: string; accessToken: string; @@ -36,7 +36,7 @@ export type Credentials = { /** @internalRemarks Source: @carto/react-api */ export type Source = { - type: MAP_TYPES; + type: MapType; connection: string; credentials: Credentials; data: string; @@ -46,22 +46,16 @@ export type Source = { filtersLogicalOperator?: 'and' | 'or'; }; -/** @internalRemarks Source: @carto/react-api, @deck.gl/carto */ -export enum FilterTypes { - In = 'in', - Between = 'between', // [a, b] both are included - ClosedOpen = 'closed_open', // [a, b) a is included, b is not - Time = 'time', - StringSearch = 'stringSearch', -} - /** @internalRemarks Source: @carto/react-api, @deck.gl/carto */ export interface Filter { - [FilterTypes.In]?: number[]; - [FilterTypes.Between]?: number[][]; - [FilterTypes.ClosedOpen]?: number[][]; - [FilterTypes.Time]?: number[][]; - [FilterTypes.StringSearch]?: string[]; + [FilterType.IN]?: number[]; + /** [a, b] both are included. */ + [FilterType.BETWEEN]?: number[][]; + /** [a, b) a is included, b is not. */ + [FilterType.CLOSED_OPEN]?: number[][]; + [FilterType.TIME]?: number[][]; + [FilterType.STRING_SEARCH]?: string[]; } +/** @internalRemarks Source: @carto/react-core */ export type FilterLogicalOperator = 'and' | 'or'; diff --git a/test/constants.test.ts b/test/constants.test.ts index 3b9fbe1..5dbac4f 100644 --- a/test/constants.test.ts +++ b/test/constants.test.ts @@ -1,12 +1,12 @@ import {expect, test} from 'vitest'; -import {CLIENT_ID, MAP_TYPES, API_VERSIONS} from '@carto/api-client'; +import {CLIENT_ID, MapType, ApiVersion} from '@carto/api-client'; test('constants', () => { expect(CLIENT_ID).toBe('carto-api-client'); - expect(MAP_TYPES.TABLE).toBe('table'); - expect(MAP_TYPES.QUERY).toBe('query'); - expect(MAP_TYPES.TILESET).toBe('tileset'); - expect(API_VERSIONS.V1).toBe('v1'); - expect(API_VERSIONS.V2).toBe('v2'); - expect(API_VERSIONS.V3).toBe('v3'); + expect(MapType.TABLE).toBe('table'); + expect(MapType.QUERY).toBe('query'); + expect(MapType.TILESET).toBe('tileset'); + expect(ApiVersion.V1).toBe('v1'); + expect(ApiVersion.V2).toBe('v2'); + expect(ApiVersion.V3).toBe('v3'); }); diff --git a/test/types.test.ts b/test/types.test.ts index 0a2e3ae..70a2bf8 100644 --- a/test/types.test.ts +++ b/test/types.test.ts @@ -1,12 +1,12 @@ import {assertType, test} from 'vitest'; import { - API_VERSIONS, + ApiVersion, AggregationType, Credentials, Filter, FilterLogicalOperator, - FilterTypes, - MAP_TYPES, + FilterType, + MapType, Source, SpatialFilter, } from '@carto/api-client'; @@ -59,7 +59,7 @@ test('Credentials', () => { assertType({ accessToken: '••••', geoColumn: 'geometry', - apiVersion: API_VERSIONS.V3, + apiVersion: ApiVersion.V3, apiBaseUrl: 'https://example.com', }); @@ -71,7 +71,7 @@ test('Credentials', () => { test('Source', () => { assertType({ - type: MAP_TYPES.TABLE, + type: MapType.TABLE, data: 'my_data', connection: 'my_connection', credentials: {accessToken: '••••'}, @@ -81,15 +81,15 @@ test('Source', () => { assertType({}); }); -test('FilterTypes', () => { - assertType(FilterTypes.In); - assertType(FilterTypes.Between); - assertType(FilterTypes.ClosedOpen); - assertType(FilterTypes.Time); - assertType(FilterTypes.StringSearch); +test('FilterType', () => { + assertType(FilterType.IN); + assertType(FilterType.BETWEEN); + assertType(FilterType.CLOSED_OPEN); + assertType(FilterType.TIME); + assertType(FilterType.STRING_SEARCH); // @ts-expect-error - assertType('invalid'); + assertType('invalid'); }); test('Filter', () => {