@carto/api-client 0.0.1-1 → 0.0.1-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,6 +14,119 @@ npm install --save @carto/api-client
14
14
 
15
15
  WORK IN PROGRESS.
16
16
 
17
+ ### Fetching data
18
+
19
+ Import `vectorTableSource`, `vectorQuerySource`, and other data source functions
20
+ from the `@carto/api-client` package. These are drop-in replacements for the equivalent functions from the `@deck.gl/carto` package, and the same data source may be used with any number of layers or widgets. Tileset sources are not yet supported.
21
+
22
+ ```javascript
23
+ import { vectorTableSource } from '@carto/api-client';
24
+
25
+ const data = vectorTableSource({
26
+ accessToken: '••••',
27
+ connectionName: 'carto_dw',
28
+ tableName: 'carto-demo-data.demo_tables.retail_stores'
29
+ });
30
+
31
+ const { widgetSource } = await data;
32
+
33
+ // → {name: string; value: number}[]
34
+ const categories = await widgetSource.getCategories({
35
+ column: 'store_type',
36
+ operation: 'count',
37
+ });
38
+
39
+ // → {value: number}
40
+ const formula = await widgetSource.getFormula({operation: 'count'});
41
+
42
+ // → {totalCount: number; rows: Record<string, number | string>[]}
43
+ const table = await widgetSource.getTable({
44
+ columns: ['a', 'b', 'c'],
45
+ sortBy: ['a'],
46
+ rowsPerPage: 20
47
+ });
48
+
49
+ ...
50
+ ```
51
+
52
+ ### Column filter
53
+
54
+ To filter the widget source by a non-geospatial column, pass a `filters`
55
+ property to the source factory function.
56
+
57
+ ```javascript
58
+ import {vectorTableSource} from '@carto/api-client';
59
+
60
+ const data = vectorTableSource({
61
+ accessToken: '••••',
62
+ connectionName: 'carto_dw',
63
+ tableName: 'carto-demo-data.demo_tables.retail_stores',
64
+ filters: {
65
+ store_type: {owner: 'widget-id', values: ['retail']},
66
+ },
67
+ });
68
+ ```
69
+
70
+ By default, filters affect all layers and widgets using a given data source. To
71
+ exclude a particular widget from the filter, pass a `filterOwner` parameter
72
+ matching the filters from which it should be excluded. In some cases, a widget's
73
+ results should not be affected by a filter that the widget itself created.
74
+
75
+ ```javascript
76
+ // → {name: string; value: number}[]
77
+ const categories = await widgetSource.getCategories({
78
+ filterOwner: 'widget-id',
79
+ column: 'store_type',
80
+ operation: 'count',
81
+ });
82
+ ```
83
+
84
+ ### Spatial filter
85
+
86
+ To filter the widget source to a spatial region, pass a `spatialFilter` parameter (GeoJSON Polygon or MultiPolygon geometry) to any data fetching function.
87
+
88
+ ```javascript
89
+ // → {name: string; value: number}[]
90
+ const categories = await widgetSource.getCategories({
91
+ column: 'store_type',
92
+ operation: 'count',
93
+ spatialFilter: {
94
+ type: "Polygon"
95
+ coordinates: [
96
+ [
97
+ [-74.0562, 40.8331],
98
+ [-74.0562, 40.6933],
99
+ [-73.8734, 40.6933],
100
+ [-73.8734, 40.8331],
101
+ [-74.0562, 40.8331]
102
+ ]
103
+ ],
104
+ }
105
+ });
106
+ ```
107
+
108
+ To create a spatial filter from the current [deck.gl `viewState`](https://deck.gl/docs/developer-guide/views#using-a-view-with-view-state):
109
+
110
+ ```javascript
111
+ import {WebMercatorViewport} from '@deck.gl/core';
112
+
113
+ function createViewStatePolygon(viewState) {
114
+ const viewport = new WebMercatorViewport(viewState);
115
+ return {
116
+ type: 'Polygon',
117
+ coordinates: [
118
+ [
119
+ viewport.unproject([0, 0]),
120
+ viewport.unproject([viewport.width, 0]),
121
+ viewport.unproject([viewport.width, viewport.height]),
122
+ viewport.unproject([0, viewport.height]),
123
+ viewport.unproject([0, 0]),
124
+ ],
125
+ ],
126
+ };
127
+ }
128
+ ```
129
+
17
130
  ## Versioning
18
131
 
19
132
  Package versioning follows [Semantic Versioning 2.0.0](https://semver.org/).