Skip to content

Commit

Permalink
feat(carto): Support filters parameter in rasterSource (#8928)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Don McCurdy <[email protected]>
  • Loading branch information
thedae and donmccurdy committed May 29, 2024
1 parent 5e247c6 commit 2d6dde3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
10 changes: 5 additions & 5 deletions modules/carto/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ export interface Filters {
}

interface Filter {
[FilterTypes.In]: number[];
[FilterTypes.Between]: number[][];
[FilterTypes.ClosedOpen]: number[][];
[FilterTypes.Time]: number[][];
[FilterTypes.StringSearch]: string[];
[FilterTypes.In]?: number[];
[FilterTypes.Between]?: number[][];
[FilterTypes.ClosedOpen]?: number[][];
[FilterTypes.Time]?: number[][];
[FilterTypes.StringSearch]?: string[];
}

export enum FilterTypes {
Expand Down
15 changes: 10 additions & 5 deletions modules/carto/src/sources/raster-source.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {baseSource} from './base-source';
import type {SourceOptions, TilejsonResult, TilesetSourceOptions} from './types';
import type {FilterOptions, SourceOptions, TilejsonResult, TilesetSourceOptions} from './types';

export type RasterSourceOptions = SourceOptions & TilesetSourceOptions;
type UrlParameters = {name: string};
export type RasterSourceOptions = SourceOptions & TilesetSourceOptions & FilterOptions;
type UrlParameters = {
name: string;
filters?: Record<string, unknown>;
};

export const rasterSource = async function (options: RasterSourceOptions): Promise<TilejsonResult> {
const {tableName} = options;
const {tableName, filters} = options;
const urlParameters: UrlParameters = {name: tableName};

if (filters) {
urlParameters.filters = filters;
}
return baseSource<UrlParameters>('raster', options, urlParameters) as Promise<TilejsonResult>;
};
14 changes: 14 additions & 0 deletions test/modules/carto/sources/raster-source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,17 @@ test('rasterSource', async t => {
}).catch(t.fail);
t.end();
});

test('rasterSource - filters', async t => {
await withMockFetchMapsV3(async calls => {
const tilejson = await rasterSource({
connectionName: 'carto_dw',
accessToken: '<token>',
tableName: 'a.b.raster_table',
filters: {type: {in: [1, 2, 3]}}
});
const url = new URL(calls[0].url);
t.is(url.searchParams.get('filters'), '{"type":{"in":[1,2,3]}}');
}).catch(t.fail);
t.end();
});

0 comments on commit 2d6dde3

Please sign in to comment.