/
fetchNgwLayerItems.ts
59 lines (56 loc) · 1.83 KB
/
fetchNgwLayerItems.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { propertiesFilter } from '@nextgis/properties-filter';
import CancelablePromise from '@nextgis/cancelable-promise';
import {
createFeatureFieldFilterQueries,
fetchNgwLayerItemsRequest,
} from './featureLayerUtils';
import { prepareNgwFieldsToPropertiesFilter } from './prepareNgwFieldsToPropertiesFilter';
import type { Geometry } from 'geojson';
import type { FeatureItem } from '@nextgis/ngw-connector';
import { FeatureProperties, isArray } from '@nextgis/utils';
import type { PropertiesFilter } from '@nextgis/properties-filter';
import type { FetchNgwItemsOptions } from '../interfaces';
export function fetchNgwLayerItems<
G extends Geometry = Geometry,
P extends FeatureProperties = FeatureProperties,
>(options: FetchNgwItemsOptions<P>): CancelablePromise<FeatureItem<P, G>[]> {
const filters = options.filters;
if (filters) {
return createFeatureFieldFilterQueries({
...options,
filters,
}).then((data) => {
const clientFilterValidate =
options.clientFilterValidate ?? isFilterWithAnyCase(filters);
// Additional client-side filter check
if (clientFilterValidate) {
data = data.filter((y) => {
const fields = prepareNgwFieldsToPropertiesFilter({
...y.fields,
id: y.id,
});
const result = propertiesFilter(fields, filters);
return result;
});
}
return data;
}) as CancelablePromise<FeatureItem<P, G>[]>;
} else {
return fetchNgwLayerItemsRequest<G, P>(options);
}
}
function isFilterWithAnyCase(filters: PropertiesFilter): boolean {
if (filters[0] === 'any') {
return true;
}
for (let i = 1; i < filters.length; i++) {
const p = filters[i];
if (isArray(p)) {
const deep = isFilterWithAnyCase(p);
if (deep) {
return true;
}
}
}
return false;
}