Skip to content

Commit c0f41ac

Browse files
authored
Merge pull request #107 from Dataport/refactor/#33-lib-get-features
Refactor/#33 lib get features
2 parents 86b1667 + b7a8fde commit c0f41ac

File tree

8 files changed

+62
-51
lines changed

8 files changed

+62
-51
lines changed

packages/clients/dish/src/utils/navigateToDenkmal.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export function navigateToDenkmal(instance, objektId: string) {
1111
if (!wfsConfig) {
1212
throw new Error('Client is missing wfsConfig on DISH search method.')
1313
}
14+
if (!wfsConfig.queryParameters) {
15+
throw new Error(
16+
'Client is missing wfsConfig.queryParameters on DISH search method.'
17+
)
18+
}
1419

1520
getWfsFeatures(null, denkmaelerWfsService.url, objektId, {
1621
...wfsConfig.queryParameters.wfsConfiguration,

packages/lib/getFeatures/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## unpublished
4+
5+
- Breaking: `getWfsFeatures` now throws errors if required parameters on the wfs configuration are missing instead of only printing error messages on the console.
6+
37
## 1.0.1
48

59
- Fix: Increase type precision of EPSG codes from `string` to `EPSG:${string}`.

packages/lib/getFeatures/gazetteer/parse.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export function parseGazetteerResponse(
2424
const gmlFeatures = new DOMParser()
2525
.parseFromString(text, 'application/xml')
2626
.getElementsByTagName(`wfs:${memberSuffix}`)
27-
// @ts-expect-error | This is needed as this already is a workaround because the GeoJSON reader can't read the XMLResponse from a WFS-G
2827
const featureCollection: FeatureCollection = {
2928
type: 'FeatureCollection',
3029
features,

packages/lib/getFeatures/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ export interface AdditionalSearchOptions {
1212
typeNames?: string | string[]
1313
}
1414

15+
/*
16+
* Explanation by dimension:
17+
* First: each child resembles a query
18+
* Second: children will be ANDed on multiple children
19+
* Third: [key, value] where key is a property name
20+
* Explanation by example:
21+
* [[['a', 'b'], ['a', 'c']], [['a', 'b']]]
22+
* becomes
23+
* QUERY(a=b && c=d), QUERY(a=b)
24+
* where the second query is only executed if the first doesn't fill
25+
* maxFeatures to its limit.
26+
*/
27+
export type KeyValueSetArray = Array<Array<[string, string]>>
28+
1529
/** Adds the possibility to have a 'title' attribute in a GeoJSON Feature */
1630
export interface PolarGeoJsonFeature extends GeoJsonFeature {
1731
/** The projection of the coordinates of the features */

packages/lib/getFeatures/wfs/buildWfsFilter.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WfsParameters } from '../types'
1+
import { KeyValueSetArray, WfsParameters } from '../types'
22

33
const removeLinebreaks = (s) => s.replace(/\r?\n|\r/g, '')
44

@@ -58,21 +58,10 @@ const buildWfsFilterQuery = (
5858
* Builds filter of multiple queries from possible interpretations of inputs.
5959
* Multiple queries are sent so that service may stop computing after
6060
* maxFeatures has been fulfilled.
61-
* @param inputs - Explanation by dimension.
62-
* First: each child resembles a query
63-
* Second: children will be ANDed on multiple children
64-
* Third: [key, value] where key is a property name
65-
* Explanation by example.
66-
* [[['a', 'b'], ['a', 'c']], [['a', 'b']]]
67-
* becomes
68-
* QUERY(a=b && c=d), QUERY(a=b)
69-
* where the second query is only executed if the first doesn't fill
70-
* maxFeatures to its limit.
71-
* @param parameters - @see WfsParameters
7261
* @returns request xml
7362
*/
7463
export const buildWfsFilter = (
75-
inputs: Array<Array<[string, string]>>,
64+
inputs: KeyValueSetArray,
7665
parameters: WfsParameters
7766
) =>
7867
removeLinebreaks(

packages/lib/getFeatures/wfs/getFeatureTitleFromPattern.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export const getFeatureTitleFromPattern = (
2828
const keys = blocks.reduce(
2929
(keyAccumulator, block) =>
3030
Array.isArray(block) ? [...keyAccumulator, block[1]] : keyAccumulator,
31-
[]
32-
) as string[]
31+
[] as string[]
32+
)
3333
const foundKeys = keys.reduce(
3434
(sum, key) =>
3535
typeof properties[key] !== 'undefined' && properties[key] !== ''

packages/lib/getFeatures/wfs/index.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
1-
import { WfsParameters } from '../types'
1+
import { KeyValueSetArray, WfsParameters } from '../types'
22
import { errorCheck } from '../utils/errorCheck'
33
import { parseWfsResponse } from './parse'
44
import { buildWfsFilter } from './buildWfsFilter'
55
import { match } from './match'
66

7-
export function getWfsFeatures(
7+
export async function getWfsFeatures(
88
signal: AbortSignal | null,
99
url: string,
1010
inputValue: string,
1111
parameters: WfsParameters
1212
) {
1313
const { fieldName, patterns, patternKeys } = parameters
14-
// arrays OF sets OF key-value-pairs
15-
let inputs: string[][][] = [[[]]]
16-
17-
if (fieldName && patterns) {
18-
console.error(
19-
'@polar/lib-get-features: Using both fieldName and patterns for WFS search. These are mutually exclusive. Patterns will be ignored.'
14+
if (!fieldName && (!patterns || !patternKeys)) {
15+
throw new Error(
16+
'Incomplete WFS search configuration. Either "fieldName" or "patterns" and "patternKeys" are required.'
2017
)
2118
}
22-
23-
if (fieldName) {
24-
inputs = [[[fieldName, inputValue]]]
25-
} else if (patterns && patternKeys) {
26-
inputs = match(patterns, patternKeys, inputValue)
27-
} else {
19+
if (fieldName && patterns) {
2820
console.error(
29-
'@polar/lib-get-features: Incomplete WFS search configuration. Either "fieldName" or "patterns" and "patternKeys" are required.'
21+
'@polar/lib-get-features: Using both fieldName and patterns for WFS search. These are mutually exclusive. Patterns will be ignored.'
3022
)
3123
}
24+
// arrays of sets of key-value-pairs
25+
const inputs: KeyValueSetArray = fieldName
26+
? [[[fieldName, inputValue]]]
27+
: match(patterns, patternKeys, inputValue)
3228

3329
const body = buildWfsFilter(inputs, parameters)
3430

35-
return fetch(encodeURI(url), { signal, method: 'POST', body }).then(
36-
(response: Response) => {
37-
errorCheck(response)
38-
return parseWfsResponse(response, fieldName || patterns, !fieldName)
39-
}
40-
)
31+
const response = await fetch(encodeURI(url), { signal, method: 'POST', body })
32+
errorCheck(response)
33+
return parseWfsResponse(response, fieldName || patterns, !fieldName)
4134
}

packages/lib/getFeatures/wfs/match.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// code doesn't produce RegExpMatchArray where index is not set ... :|
22
/* eslint-disable @typescript-eslint/no-non-null-assertion */
33

4+
import { KeyValueSetArray } from '../types'
5+
46
export type Separator = string
57
export type Slot = RegExpMatchArray
68
export type Block = Slot | Separator
@@ -55,10 +57,10 @@ const sortComparableMatches = (comparableA, comparableB) => {
5557
* 2. pattern fulfillment
5658
*/
5759
const sortMatches = (
58-
matches: string[][][],
60+
matches: KeyValueSetArray,
5961
patterns: string[],
6062
uninterpretedCharacters: number[]
61-
): string[][][] => {
63+
): KeyValueSetArray => {
6264
const comparableMatches = matches.map((match, index) => ({
6365
match,
6466
uninterpreted: uninterpretedCharacters[index],
@@ -72,7 +74,7 @@ const sortMatches = (
7274

7375
// remove duplicates and empty matches
7476
const known: string[] = []
75-
const sortedFilteredMatches = sortedMatches.filter((match) => {
77+
return sortedMatches.filter((match) => {
7678
if (match.length === 0) {
7779
return false
7880
}
@@ -83,23 +85,31 @@ const sortMatches = (
8385
known.push(asString)
8486
return true
8587
})
86-
87-
return sortedFilteredMatches
8888
}
8989

9090
/**
9191
* matches an input string to patterns
9292
*/
9393
export const match = (
94-
patterns: string[],
95-
patternKeys: Record<string, string>,
94+
patterns: string[] | undefined,
95+
patternKeys: Record<string, string> | undefined,
9696
inputValue: string
97-
): string[][][] => {
98-
const matches: string[][][] = []
97+
): KeyValueSetArray => {
98+
if (!patterns) {
99+
throw new Error(
100+
'@polar/lib-get-features: Parameter "patterns" is missing on wfs configuration for pattern-based search.'
101+
)
102+
}
103+
if (!patternKeys) {
104+
throw new Error(
105+
'@polar/lib-get-features: Parameter "patternKeys" is missing on wfs configuration for pattern-based search.'
106+
)
107+
}
108+
const matches: KeyValueSetArray = []
99109
const uninterpretedCharacters: number[] = []
100110
patterns.forEach((pattern) => {
101111
const patternBlocks = getBlocks(pattern)
102-
const patternMapping: string[][] = []
112+
const patternMapping: KeyValueSetArray[number] = []
103113
let traverseInput = inputValue
104114

105115
patternBlocks.forEach((block) => {
@@ -126,8 +136,5 @@ export const match = (
126136
uninterpretedCharacters.push(traverseInput.length)
127137
matches.push(patternMapping)
128138
})
129-
130-
const sortedMatches = sortMatches(matches, patterns, uninterpretedCharacters)
131-
132-
return sortedMatches
139+
return sortMatches(matches, patterns, uninterpretedCharacters)
133140
}

0 commit comments

Comments
 (0)