-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (66 loc) · 2.68 KB
/
index.js
File metadata and controls
71 lines (66 loc) · 2.68 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
import { getAPI } from '@/api'
/**
* Generic helper to check if an API has no items (useful for advisory conditions)
* @param {Object} store - Vuex store instance
* @param {string} apiName - Name of the API to call (e.g., 'listNetworks')
* @param {Object} params - Optional parameters to merge with defaults
* @param {Function} filterFunc - Optional function to filter items. If provided, returns true if no items match the filter.
* @param {string} itemsKey - Optional key for items array in response. If not provided, will be deduced from apiName
* @returns {Promise<boolean>} - Returns true if no items exist (advisory should be shown), false otherwise
*/
export async function hasNoItems (store, apiName, params = {}, filterFunc = null, responseKey = null, itemsKey = null) {
if (!(apiName in store.getters.apis)) {
return false
}
// If itemsKey not provided, deduce it from apiName
if (!itemsKey) {
// Remove 'list' prefix: listNetworks -> Networks
let key = apiName.replace(/^list/i, '')
// Convert to lowercase
key = key.toLowerCase()
// Handle plural forms: remove trailing 's' or convert 'ies' to 'y'
if (key.endsWith('ies')) {
key = key.slice(0, -3) + 'y'
} else if (key.endsWith('s')) {
key = key.slice(0, -1)
}
itemsKey = key
}
const allParams = {
listall: true,
...params
}
if (filterFunc == null) {
allParams.page = 1
allParams.pageSize = 1
}
try {
const json = await getAPI(apiName, allParams)
// Auto-derive response key: listNetworks -> listnetworksresponse
const apiResponseKey = responseKey || `${apiName.toLowerCase()}response`
const items = json?.[apiResponseKey]?.[itemsKey] || []
if (filterFunc) {
return !items.some(filterFunc)
}
return items.length === 0
} catch (error) {
console.error(`Failed to fetch items for advisory check via API ${apiName}`, error)
return false
}
}