Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | 31x 951x 913x 38x 31x 27x 6x 6x 6x 6x 8x 5x 21x 3x 3x 6x 2x 4x 3x 18x 17x 14x 14x 3x 1x 31x 31x 644x 644x 644x 644x 123x 79x 644x 16x 14x 14x 13x 644x 71x 71x 74x 74x 74x 74x 71x 72x 72x 72x 3x 3x 3x 3x 3x 3x 3x 69x 2x 67x 67x 644x 9x 24x 1x 23x 23x 23x 644x 12x 14x 14x 14x 3x 14x 14x 14x 14x 14x 14x 14x 14x 644x 643x 644x 1231x 619x 644x | const conditionalEncodeURIComponent = (str, encode = true) => { if (encode) { return encodeURIComponent(str); } return str; }; // TODO might be nice to allow for filterOptions to be of the shape of parsedKiwtQueryFilters, // so we can make use of the deparser there optionally for built in query grouping const buildFilterOptionBlock = (opf, isNested = false, encode = true) => { if (opf?.groupValues) { const groupValues = opf.groupValues; // Small utility function to add negation and brackets to the options block where necessary const negationAndNesting = (str) => `${groupValues?.NOT ? '!' : ''}${(isNested || groupValues?.NOT) ? '(' : ''}${str}${(isNested || groupValues?.NOT) ? ')' : ''}`; // First check whether groupValues is ANDed or ORed together Eif ( (groupValues?.AND && Array.isArray(groupValues.AND)) || (groupValues?.OR && Array.isArray(groupValues.OR)) ) { // AND takes precedence if (groupValues.AND) { return negationAndNesting(groupValues?.AND?.map(gvo => buildFilterOptionBlock(gvo, true, encode))?.join('&&')); } return negationAndNesting(groupValues?.OR?.map(gvo => buildFilterOptionBlock(gvo, true, encode))?.join('||')); } // If neither valid OR nor AND exist, ignore the block } else if (opf?.values) { // Build the values filter block const innerFilters = []; opf.values.forEach(opfv => { if (opf.path) { innerFilters.push(`${opf.path}${opf.comparator ?? '=='}${opfv}`); } else { innerFilters.push(opfv); } }); return conditionalEncodeURIComponent(innerFilters.join('||'), encode); } else if (opf?.value) { // If no value OR values, then ignore if (opf.path) { const filterString = `${opf.path}${opf.comparator ?? '=='}${opf.value}`; return conditionalEncodeURIComponent(filterString, encode); } return conditionalEncodeURIComponent(opf.value, encode); } return null; }; /* * GenerateKiwtQueryParams accepts * SASQ_MAP shape "options" -- TODO write a TS shape for this * SASQ "nsValues" shape "nsValues" -- TODO write a TS shape for this * boolean "encode" * It then uses these props to generate a query string for sending to KIWT type doTheLookupEndpoints */ // For now we can store the "safe to ignore" keys from the normal SASQ_MAP shape here. // These will be ignored rather than appended directly to the query as per normal. const keysToIgnore = ['totalRecords']; const generateKiwtQueryParams = (options, nsValues, encode = true) => { const { qindex, query, filters, sort } = nsValues; const { searchKey = '', /* * Array of objects to configure how filters coming from `nsValues.filters` are mapped into KIWT filter strings. * * Example structure: * [ * { * name: 'status', // Matches filter key in URL (e.g., filters=status.active) * filterPrefix: 'filters=', // Optional, defaults to 'filters=' * // Optional function to entirely customize how the array of values is mapped to a string. * // (e.g., ['active', 'cancelled'] => 'is:active and is:cancelled') * // Defaults to constructing from `values` map below * // valuesMapping: (filterValues) => string, * values: [ // Optional: Specific configuration for individual filter values * { * name: 'active', // Matches filter value in URL * value: 'activeValue', // Actual value to use in the KIWT query (defaults to name) * comparator: '==' // Optional comparator override (defaults to '==') * }, * // ... other values * ] * } * ] */ filterConfig = [], /* Assumtion made that if no filterKey is provided then the given filterValues for that key are standalaone, ie require no comparator or key */ filterKeys = {}, sortKeys = {}, stats = true, /* Of the form [{ path: 'this.is.some.path', direction: 'asc'/'desc', value: 'someOverrideValue'}, ...] * If only path is passed then assume asc. * If value is passed then we ignore path/direction and append "sort=${value}" directly */ sort: optionsSort, /* Of the form [ { path: 'this.is.some.path' comparator: '==' value: 'this is a value' //OR values: ['value1', 'value2'] //OR groupValues: { // This is an object containing objects either in groups of AND or OR. AND takes precedence AND: [ // Objects of the same shape as an individual filters object, recursively. ], NOT: true // When this is set to true, the entire group is negated } }, ... ] * This (with value instead of values) will construct a query param: "filters=this.is.some.path==this is a value" * If only value is passed, then it will construct directly: "filters=value" * If no comparator is passed, it assumes '==' * Values overwrites value and will construct "filters=this.is.some.path==value1||this.is.some.path==value2" * Values WITHOUT path will construct "filters=value1||value2" * * GroupValues will override everything above, and group into brackets. * * If more complex query building is desired, this should be done externally and passed in as a standalone 'value' */ filters: optionsFilters, ...rest } = options; const paramsArray = []; if (query) { paramsArray.push(...((qindex || searchKey)?.split(',') ?? []).map(m => `match=${conditionalEncodeURIComponent(m, encode)}`)); paramsArray.push(`term=${conditionalEncodeURIComponent(query, encode)}`); } // Actually build the optionsFilters block (Moved logic to its own function to allow recursion) if (optionsFilters) { optionsFilters.forEach(opf => { const optionsBlock = buildFilterOptionBlock(opf, false, encode); if (optionsBlock) { paramsArray.push(`filters=${optionsBlock}`); } }); } if (filters) { const filterMap = {}; filters.split(',').forEach(filter => { const [filterName, ...filterRest] = filter.trim()?.split('.') ?? []; const filterValue = filterRest.join('.'); if (filterMap[filterName] === undefined) filterMap[filterName] = []; filterMap[filterName].push(filterValue); }); // We now have a filterMap of shape { status: ['active', 'cancelled'], type: ['local'] } Object.entries(filterMap).forEach(([filterName, filterValues]) => { const filterConfigEntry = filterConfig.find(conf => conf.name === filterName); const filterKey = filterKeys[filterName]; if (filterConfigEntry) { // We have a direct mapping instruction, use it // Do we have a specific way to map values to a string? const filterString = filterConfigEntry.valuesMapping ? filterConfigEntry.valuesMapping(filterValues) : filterValues.map(v => { const fcValueEntry = filterConfigEntry?.values?.find(fce => fce.name === v) ?? {}; const fceValue = fcValueEntry.value ?? v; // This is especially useful where comparator acts strangely for a single value, such as `filters=foo isNotSet` const fceComparator = fcValueEntry.comparator ?? '=='; return `${filterKey ?? filterName}${fceComparator}${fceValue ?? v}`; }).join('||'); // This will NOT be url encoded by this component const filterPrefix = filterConfigEntry.filterPrefix ?? 'filters='; paramsArray.push(`${filterPrefix}${conditionalEncodeURIComponent(filterString, encode)}`); } else if (!filterKey) { // These filters have no key mapping so we just pass the values to the backend as-is. paramsArray.push(...(filterValues ?? []).map(f => `filters=${conditionalEncodeURIComponent(f, encode)}`)); } else { const filterString = filterValues.map(v => `${filterKey}==${v}`).join('||'); paramsArray.push(`filters=${conditionalEncodeURIComponent(filterString, encode)}`); } }); } if (optionsSort && optionsSort.length > 0) { optionsSort.forEach(os => { if (os.value) { paramsArray.push(`sort=${conditionalEncodeURIComponent(os.value, encode)}`); } else Eif (os.path) { // If no path then ignore const sortString = `${os.path};${os.direction ?? 'asc'}`; paramsArray.push(`sort=${conditionalEncodeURIComponent(sortString, encode)}`); } }); } if (sort) { paramsArray.push(...(sort.trim()?.split(',') ?? []).map(sortKey => { const reverse = sortKey.startsWith('-'); let term = sortKey.replace('-', ''); if (term in sortKeys) { term = term.replace(term, sortKeys[term]); } // Split the term by commas to handle multiple fields if specified const fields = term.split(','); const sortStrings = fields.map(field => { const descending = field.startsWith('-'); const sortField = field.replace('-', ''); const sortOrder = (reverse && !descending) || (!reverse && descending) ? 'desc' : 'asc'; const sortString = `${sortField};${sortOrder}`; return `sort=${conditionalEncodeURIComponent(sortString, encode)}`; }); return sortStrings.join('&'); })); } if (stats) { paramsArray.push('stats=true'); } for (const [key, value] of Object.entries(rest)) { if (!keysToIgnore.includes(key)) { paramsArray.push(`${key}=${conditionalEncodeURIComponent(value, encode)}`); } } return paramsArray; }; export default generateKiwtQueryParams; |