|
1 |
| -import { WfsParameters } from '../types' |
| 1 | +import { KeyValueSetArray, WfsParameters } from '../types' |
2 | 2 | import { errorCheck } from '../utils/errorCheck'
|
3 | 3 | import { parseWfsResponse } from './parse'
|
4 | 4 | import { buildWfsFilter } from './buildWfsFilter'
|
5 | 5 | import { match } from './match'
|
6 | 6 |
|
7 |
| -export function getWfsFeatures( |
| 7 | +export async function getWfsFeatures( |
8 | 8 | signal: AbortSignal | null,
|
9 | 9 | url: string,
|
10 | 10 | inputValue: string,
|
11 | 11 | parameters: WfsParameters
|
12 | 12 | ) {
|
13 | 13 | 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.' |
20 | 17 | )
|
21 | 18 | }
|
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) { |
28 | 20 | 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.' |
30 | 22 | )
|
31 | 23 | }
|
| 24 | + // arrays of sets of key-value-pairs |
| 25 | + const inputs: KeyValueSetArray = fieldName |
| 26 | + ? [[[fieldName, inputValue]]] |
| 27 | + : match(patterns, patternKeys, inputValue) |
32 | 28 |
|
33 | 29 | const body = buildWfsFilter(inputs, parameters)
|
34 | 30 |
|
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) |
41 | 34 | }
|
0 commit comments