Skip to content

Commit

Permalink
feat(sources): Infer clientId where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy authored Jul 9, 2024
2 parents 283d013 + 301f498 commit fbf762c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/models/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export function executeModel(props: {
);

const {model, source, params, opts} = props;
const {type, apiVersion, apiBaseUrl, accessToken, connectionName} = source;
const {type, apiVersion, apiBaseUrl, accessToken, connectionName, clientId} =
source;

assert(apiBaseUrl, 'executeModel: missing apiBaseUrl');
assert(accessToken, 'executeModel: missing accessToken');
Expand All @@ -83,7 +84,7 @@ export function executeModel(props: {

const queryParams: Record<string, string> = {
type,
client: getClient(),
client: clientId,
source: data,
params: JSON.stringify(params),
queryParameters,
Expand Down
32 changes: 19 additions & 13 deletions src/sources/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
H3QuerySourceOptions as _H3QuerySourceOptions,
QuadbinTableSourceOptions as _QuadbinTableSourceOptions,
QuadbinQuerySourceOptions as _QuadbinQuerySourceOptions,
SourceOptions,
} from '@deck.gl/carto';
import {WidgetBaseSourceProps} from './widget-base-source.js';
import {WidgetQuerySource} from './widget-query-source.js';
Expand Down Expand Up @@ -54,6 +55,7 @@ export type VectorQuerySourceOptions =
export async function vectorTableSource(
props: VectorTableSourceOptions
): Promise<VectorTableSourceResponse> {
assignDefaultProps(props);
const response = await _vectorTableSource(props as _VectorTableSourceOptions);
return {...response, widgetSource: new WidgetTableSource(props)};
}
Expand All @@ -62,15 +64,11 @@ export async function vectorTableSource(
export async function vectorQuerySource(
props: VectorQuerySourceOptions
): Promise<VectorQuerySourceResponse> {
assignDefaultProps(props);
const response = await _vectorQuerySource(props as _VectorQuerySourceOptions);
return {...response, widgetSource: new WidgetQuerySource(props)};
}

/** Wrapper adding Widget API support to [vectorTilesetSource](https://deck.gl/docs/api-reference/carto/data-sources). */
export async function vectorTilesetSource() {
throw new Error('not implemented');
}

/******************************************************************************
* H3 SOURCES
*/
Expand All @@ -82,6 +80,7 @@ export type H3QuerySourceOptions = WrappedSourceOptions<_H3QuerySourceOptions>;
export async function h3TableSource(
props: H3TableSourceOptions
): Promise<H3TableSourceResponse> {
assignDefaultProps(props);
const response = await _h3TableSource(props);
return {...response, widgetSource: new WidgetTableSource(props)};
}
Expand All @@ -90,15 +89,11 @@ export async function h3TableSource(
export async function h3QuerySource(
props: H3QuerySourceOptions
): Promise<H3QuerySourceResponse> {
assignDefaultProps(props);
const response = await _h3QuerySource(props);
return {...response, widgetSource: new WidgetQuerySource(props)};
}

/** Wrapper adding Widget API support to [h3TilesetSource](https://deck.gl/docs/api-reference/carto/data-sources). */
export async function h3TilesetSource() {
throw new Error('not implemented');
}

/******************************************************************************
* QUADBIN SOURCES
*/
Expand All @@ -113,6 +108,7 @@ export type QuadbinQuerySourceOptions =
export async function quadbinTableSource(
props: QuadbinTableSourceOptions & WidgetBaseSourceProps
): Promise<QuadbinTableSourceResponse> {
assignDefaultProps(props);
const response = await _quadbinTableSource(props);
return {...response, widgetSource: new WidgetTableSource(props)};
}
Expand All @@ -121,11 +117,21 @@ export async function quadbinTableSource(
export async function quadbinQuerySource(
props: QuadbinQuerySourceOptions & WidgetBaseSourceProps
): Promise<QuadbinQuerySourceResponse> {
assignDefaultProps(props);
const response = await _quadbinQuerySource(props);
return {...response, widgetSource: new WidgetQuerySource(props)};
}

/** Wrapper adding Widget API support to [quadbinTilesetSource](https://deck.gl/docs/api-reference/carto/data-sources). */
export async function quadbinTilesetSource() {
throw new Error('not implemented');
/******************************************************************************
* DEFAULT PROPS
*/

declare const deck: {VERSION?: string} | undefined;
function assignDefaultProps<T extends SourceOptions>(props: T): void {
if (typeof deck !== 'undefined' && deck && deck.VERSION) {
props.clientId ||= 'deck-gl-carto';
// TODO: Uncomment if/when `@deck.gl/carto` devDependency is removed,
// and source functions are moved here rather than wrapped.
// props.deckglVersion ||= deck.VERSION;
}
}
18 changes: 0 additions & 18 deletions test/sources/wrappers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import {afterEach, expect, test, vi} from 'vitest';
import {
vectorQuerySource,
vectorTableSource,
vectorTilesetSource,
h3QuerySource,
h3TableSource,
h3TilesetSource,
quadbinQuerySource,
quadbinTableSource,
quadbinTilesetSource,
WidgetQuerySource,
WidgetTableSource,
} from '@carto/api-client';
Expand Down Expand Up @@ -60,11 +57,6 @@ test('vectorTableSource', async () => {
expect(widgetSource).toBeInstanceOf(WidgetTableSource);
});

test('vectorTilesetSource', async () => {
expect(vectorTilesetSource).toBeDefined();
expect(() => vectorTilesetSource()).rejects.toThrowError(/not implemented/i);
});

/******************************************************************************
* H3 SOURCES
*/
Expand Down Expand Up @@ -95,11 +87,6 @@ test('h3TableSource', async () => {
expect(widgetSource).toBeInstanceOf(WidgetTableSource);
});

test('h3TilesetSource', async () => {
expect(h3TilesetSource).toBeDefined();
expect(() => h3TilesetSource()).rejects.toThrowError(/not implemented/i);
});

/******************************************************************************
* QUADBIN SOURCES
*/
Expand Down Expand Up @@ -129,8 +116,3 @@ test('quadbinTableSource', async () => {

expect(widgetSource).toBeInstanceOf(WidgetTableSource);
});

test('quadbinTilesetSource', async () => {
expect(quadbinTilesetSource).toBeDefined();
expect(() => quadbinTilesetSource()).rejects.toThrowError(/not implemented/i);
});

0 comments on commit fbf762c

Please sign in to comment.