-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sources): Move sources from @deck.gl/carto #28
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
baaac48
feat: migrate sources and requests from @deck.gl/carto
donmccurdy 7419311
fix TS issues
donmccurdy c83bb75
Fix failure if deck not defined
donmccurdy 23173e6
chore(release): v0.4.0-alpha.0
donmccurdy 5348811
type fixes
donmccurdy 4669cc8
iterate on required exports
donmccurdy 0b88783
fixes
donmccurdy cb91da7
fixes for @deck.gl/carto
donmccurdy 33a3a80
chore(release): v0.3.2-0
donmccurdy bee725d
chore(release): v0.4.0-alpha.1
donmccurdy bb7e5ad
remove wrappers.ts, add widget sources directly in sources
donmccurdy b6ac088
migrate sources tests
donmccurdy c7a7c7e
migrate requestWithParameters tests
donmccurdy dc718da
migrate query tests
donmccurdy ab72151
migrate CartoAPIError tests
donmccurdy 2db5780
chore(release): v0.4.0-alpha.2
donmccurdy 271f59f
fix duplicate client/clientID params
donmccurdy 9ce9987
fix coverage
donmccurdy a40056f
chore(release): v0.4.0-alpha.3
donmccurdy ec67f5a
fix(metrics): Consistent 'deck-gl-carto' client ID
donmccurdy 0853d09
revert coverage change
donmccurdy dedfdb4
reorganize constants (internal & public)
donmccurdy 946a3d5
stricter types
donmccurdy ea9f615
chore(release): v0.4.0-alpha.4
donmccurdy 0fa0d9c
feat(constants): Export DEFAULT_API_BASE_URL
donmccurdy 101650a
chore(release): v0.4.0-alpha.5
donmccurdy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
"repository": "github:CartoDB/carto-api-client", | ||
"author": "Don McCurdy <[email protected]>", | ||
"packageManager": "[email protected]", | ||
"version": "0.3.1", | ||
"version": "0.4.0-alpha.5", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public", | ||
|
@@ -52,7 +52,6 @@ | |
"LICENSE.md" | ||
], | ||
"dependencies": { | ||
"@deck.gl/carto": "^9.0.30", | ||
"@turf/bbox-clip": "^7.1.0", | ||
"@turf/bbox-polygon": "^7.1.0", | ||
"@turf/helpers": "^7.1.0", | ||
|
@@ -62,6 +61,7 @@ | |
}, | ||
"devDependencies": { | ||
"@deck.gl/aggregation-layers": "^9.0.30", | ||
"@deck.gl/carto": "^9.0.30", | ||
"@deck.gl/core": "^9.0.30", | ||
"@deck.gl/extensions": "^9.0.30", | ||
"@deck.gl/geo-layers": "^9.0.30", | ||
|
@@ -98,5 +98,6 @@ | |
"vite": "^5.2.10", | ||
"vitest": "1.6.0", | ||
"vue": "^3.4.27" | ||
} | ||
}, | ||
"stableVersion": "0.3.1" | ||
donmccurdy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// deck.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
|
||
import {MapType} from '../types'; | ||
|
||
export type APIRequestType = | ||
| 'Map data' | ||
| 'Map instantiation' | ||
| 'Public map' | ||
| 'Tile stats' | ||
| 'SQL' | ||
| 'Basemap style'; | ||
|
||
export type APIErrorContext = { | ||
requestType: APIRequestType; | ||
mapId?: string; | ||
connection?: string; | ||
source?: string; | ||
type?: MapType; | ||
}; | ||
|
||
/** | ||
* | ||
* Custom error for reported errors in CARTO Maps API. | ||
* Provides useful debugging information in console and context for applications. | ||
* | ||
*/ | ||
export class CartoAPIError extends Error { | ||
/** Source error from server */ | ||
error: Error; | ||
|
||
/** Context (API call & parameters) in which error occured */ | ||
errorContext: APIErrorContext; | ||
|
||
/** Response from server */ | ||
response?: Response; | ||
|
||
/** JSON Response from server */ | ||
responseJson?: any; | ||
|
||
constructor( | ||
error: Error, | ||
errorContext: APIErrorContext, | ||
response?: Response, | ||
responseJson?: any | ||
) { | ||
let responseString = 'Failed to connect'; | ||
if (response) { | ||
responseString = 'Server returned: '; | ||
if (response.status === 400) { | ||
responseString += 'Bad request'; | ||
} else if (response.status === 401 || response.status === 403) { | ||
responseString += 'Unauthorized access'; | ||
} else if (response.status === 404) { | ||
responseString += 'Not found'; | ||
} else { | ||
responseString += 'Error'; | ||
} | ||
|
||
responseString += ` (${response.status}):`; | ||
} | ||
responseString += ` ${error.message || error}`; | ||
|
||
let message = `${errorContext.requestType} API request failed`; | ||
message += `\n${responseString}`; | ||
for (const key of Object.keys(errorContext)) { | ||
if (key === 'requestType') continue; | ||
message += `\n${formatErrorKey(key)}: ${(errorContext as any)[key]}`; | ||
} | ||
message += '\n'; | ||
|
||
super(message); | ||
|
||
this.name = 'CartoAPIError'; | ||
this.response = response; | ||
this.responseJson = responseJson; | ||
this.error = error; | ||
this.errorContext = errorContext; | ||
} | ||
} | ||
|
||
/** | ||
* Converts camelCase to Camel Case | ||
*/ | ||
function formatErrorKey(key: string) { | ||
return key.replace(/([A-Z])/g, ' $1').replace(/^./, (s) => s.toUpperCase()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// deck.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
|
||
import {MapType} from '../types.js'; | ||
|
||
export type V3Endpoint = 'maps' | 'stats' | 'sql'; | ||
|
||
function joinPath(...args: string[]): string { | ||
return args | ||
.map((part) => (part.endsWith('/') ? part.slice(0, -1) : part)) | ||
.join('/'); | ||
} | ||
|
||
function buildV3Path( | ||
apiBaseUrl: string, | ||
version: 'v3', | ||
endpoint: V3Endpoint, | ||
...rest: string[] | ||
): string { | ||
return joinPath(apiBaseUrl, version, endpoint, ...rest); | ||
} | ||
|
||
/** @internal Required by fetchMap(). */ | ||
export function buildPublicMapUrl({ | ||
apiBaseUrl, | ||
cartoMapId, | ||
}: { | ||
apiBaseUrl: string; | ||
cartoMapId: string; | ||
}): string { | ||
return buildV3Path(apiBaseUrl, 'v3', 'maps', 'public', cartoMapId); | ||
} | ||
|
||
/** @internal Required by fetchMap(). */ | ||
export function buildStatsUrl({ | ||
attribute, | ||
apiBaseUrl, | ||
connectionName, | ||
source, | ||
type, | ||
}: { | ||
attribute: string; | ||
apiBaseUrl: string; | ||
connectionName: string; | ||
source: string; | ||
type: MapType; | ||
}): string { | ||
if (type === 'query') { | ||
return buildV3Path(apiBaseUrl, 'v3', 'stats', connectionName, attribute); | ||
} | ||
|
||
// type === 'table' | ||
return buildV3Path( | ||
apiBaseUrl, | ||
'v3', | ||
'stats', | ||
connectionName, | ||
source, | ||
attribute | ||
); | ||
} | ||
|
||
export function buildSourceUrl({ | ||
apiBaseUrl, | ||
connectionName, | ||
endpoint, | ||
}: { | ||
apiBaseUrl: string; | ||
connectionName: string; | ||
endpoint: MapType; | ||
}): string { | ||
return buildV3Path(apiBaseUrl, 'v3', 'maps', connectionName, endpoint); | ||
} | ||
|
||
export function buildQueryUrl({ | ||
apiBaseUrl, | ||
connectionName, | ||
}: { | ||
apiBaseUrl: string; | ||
connectionName: string; | ||
}): string { | ||
return buildV3Path(apiBaseUrl, 'v3', 'sql', connectionName, 'query'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// deck.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
|
||
export { | ||
CartoAPIError, | ||
APIErrorContext, | ||
APIRequestType, | ||
} from './carto-api-error.js'; | ||
// Internal, but required for fetchMap(). | ||
export {buildPublicMapUrl, buildStatsUrl} from './endpoints.js'; | ||
export {query} from './query.js'; | ||
export type {QueryOptions} from './query.js'; | ||
export {requestWithParameters} from './request-with-parameters.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// deck.gl | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) vis.gl contributors | ||
|
||
import {SOURCE_DEFAULTS} from '../sources/index'; | ||
import type { | ||
SourceOptions, | ||
QuerySourceOptions, | ||
QueryResult, | ||
} from '../sources/types'; | ||
import {buildQueryUrl} from './endpoints'; | ||
import {requestWithParameters} from './request-with-parameters'; | ||
import {APIErrorContext} from './carto-api-error'; | ||
|
||
export type QueryOptions = SourceOptions & | ||
Omit<QuerySourceOptions, 'spatialDataColumn'>; | ||
type UrlParameters = {q: string; queryParameters?: string}; | ||
|
||
export const query = async function ( | ||
options: QueryOptions | ||
): Promise<QueryResult> { | ||
const { | ||
apiBaseUrl = SOURCE_DEFAULTS.apiBaseUrl, | ||
clientId = SOURCE_DEFAULTS.clientId, | ||
maxLengthURL = SOURCE_DEFAULTS.maxLengthURL, | ||
connectionName, | ||
sqlQuery, | ||
queryParameters, | ||
} = options; | ||
const urlParameters: UrlParameters = {q: sqlQuery}; | ||
|
||
if (queryParameters) { | ||
urlParameters.queryParameters = JSON.stringify(queryParameters); | ||
} | ||
|
||
const baseUrl = buildQueryUrl({apiBaseUrl, connectionName}); | ||
const headers = { | ||
Authorization: `Bearer ${options.accessToken}`, | ||
...options.headers, | ||
}; | ||
const parameters = {client: clientId, ...urlParameters}; | ||
|
||
const errorContext: APIErrorContext = { | ||
requestType: 'SQL', | ||
connection: options.connectionName, | ||
type: 'query', | ||
source: JSON.stringify(parameters, undefined, 2), | ||
}; | ||
return await requestWithParameters<QueryResult>({ | ||
baseUrl, | ||
parameters, | ||
headers, | ||
errorContext, | ||
maxLengthURL, | ||
}); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, i'm wondering.
Shouldn't we remove this dependency?
It seems like now we would have circular dep ... even if in devDepencies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dev dependency is only here (after this PR) because we need it in examples...
https://github.com/CartoDB/carto-api-client/blob/main/examples/01-pure-js/pure-js.ts
... and it's a bit hard to test the layer sources without that. So it won't be a "circular dependency" for users of either package, I think that should be OK?