Skip to content

Commit

Permalink
Clean up constants, types, and TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Jun 24, 2024
1 parent e93577f commit 288635d
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/constants-internal.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
28 changes: 24 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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',
}
6 changes: 3 additions & 3 deletions src/models/model.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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'
);

Expand Down
1 change: 1 addition & 0 deletions src/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
10 changes: 5 additions & 5 deletions src/sources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/sources/widget-base-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -52,7 +52,7 @@ export class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
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,
Expand Down
4 changes: 2 additions & 2 deletions src/sources/widget-query-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/sources/widget-table-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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,
};
}
Expand Down
1 change: 1 addition & 0 deletions src/types-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
* INTERNAL
*/

/** @internal */
export type $TODO = any;
28 changes: 11 additions & 17 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {API_VERSIONS, MAP_TYPES} from './constants';
import type {ApiVersion, MapType, FilterType} from './constants';

/******************************************************************************
* AGGREGATION
Expand Down Expand Up @@ -28,15 +28,15 @@ 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;
};

/** @internalRemarks Source: @carto/react-api */
export type Source = {
type: MAP_TYPES;
type: MapType;
connection: string;
credentials: Credentials;
data: string;
Expand All @@ -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';
14 changes: 7 additions & 7 deletions test/constants.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
24 changes: 12 additions & 12 deletions test/types.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -59,7 +59,7 @@ test('Credentials', () => {
assertType<Credentials>({
accessToken: '••••',
geoColumn: 'geometry',
apiVersion: API_VERSIONS.V3,
apiVersion: ApiVersion.V3,
apiBaseUrl: 'https://example.com',
});

Expand All @@ -71,7 +71,7 @@ test('Credentials', () => {

test('Source', () => {
assertType<Source>({
type: MAP_TYPES.TABLE,
type: MapType.TABLE,
data: 'my_data',
connection: 'my_connection',
credentials: {accessToken: '••••'},
Expand All @@ -81,15 +81,15 @@ test('Source', () => {
assertType<Source>({});
});

test('FilterTypes', () => {
assertType<FilterTypes>(FilterTypes.In);
assertType<FilterTypes>(FilterTypes.Between);
assertType<FilterTypes>(FilterTypes.ClosedOpen);
assertType<FilterTypes>(FilterTypes.Time);
assertType<FilterTypes>(FilterTypes.StringSearch);
test('FilterType', () => {
assertType<FilterType>(FilterType.IN);
assertType<FilterType>(FilterType.BETWEEN);
assertType<FilterType>(FilterType.CLOSED_OPEN);
assertType<FilterType>(FilterType.TIME);
assertType<FilterType>(FilterType.STRING_SEARCH);

// @ts-expect-error
assertType<FilterTypes>('invalid');
assertType<FilterType>('invalid');
});

test('Filter', () => {
Expand Down

0 comments on commit 288635d

Please sign in to comment.