2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2018-08-16 17:48:24 +00:00
|
|
|
// usage:
|
|
|
|
//
|
|
|
|
// import lazyCapabilities, { apiPath } from 'vault/macros/lazy-capabilities';
|
|
|
|
//
|
|
|
|
// export default DS.Model.extend({
|
|
|
|
// //pass the template string as the first arg, and be sure to use '' around the
|
2020-02-14 17:20:44 +00:00
|
|
|
// //parameters that get interpolated in the string - that's how the template function
|
2018-08-16 17:48:24 +00:00
|
|
|
// //knows where to put each value
|
|
|
|
// zeroAddressPath: lazyCapabilities(apiPath`${'id'}/config/zeroaddress`, 'id'),
|
|
|
|
//
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
|
2018-10-18 04:04:19 +00:00
|
|
|
import { maybeQueryRecord } from 'vault/macros/maybe-query-record';
|
2018-05-24 03:10:21 +00:00
|
|
|
|
|
|
|
export function apiPath(strings, ...keys) {
|
2021-12-17 03:44:29 +00:00
|
|
|
return function (data) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const dict = data || {};
|
|
|
|
const result = [strings[0]];
|
2018-05-24 03:10:21 +00:00
|
|
|
keys.forEach((key, i) => {
|
|
|
|
result.push(dict[key], strings[i + 1]);
|
|
|
|
});
|
|
|
|
return result.join('');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
export default function () {
|
2022-11-09 23:15:31 +00:00
|
|
|
const [templateFn, ...keys] = arguments;
|
2018-10-18 04:04:19 +00:00
|
|
|
return maybeQueryRecord(
|
2018-05-24 03:10:21 +00:00
|
|
|
'capabilities',
|
2021-12-17 03:44:29 +00:00
|
|
|
(context) => {
|
2018-10-18 04:04:19 +00:00
|
|
|
// pull all context attrs
|
2022-11-09 23:15:31 +00:00
|
|
|
const contextObject = context.getProperties(...keys);
|
2018-10-18 04:04:19 +00:00
|
|
|
// remove empty ones
|
2022-11-09 23:15:31 +00:00
|
|
|
const nonEmptyContexts = Object.keys(contextObject).reduce((ret, key) => {
|
2018-10-18 04:04:19 +00:00
|
|
|
if (contextObject[key] != null) {
|
|
|
|
ret[key] = contextObject[key];
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}, {});
|
|
|
|
// if all of them aren't present, cancel the fetch
|
|
|
|
if (Object.keys(nonEmptyContexts).length !== keys.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// otherwise proceed with the capabilities check
|
2018-05-24 03:10:21 +00:00
|
|
|
return {
|
2018-10-18 04:04:19 +00:00
|
|
|
id: templateFn(nonEmptyContexts),
|
2018-05-24 03:10:21 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
...keys
|
|
|
|
);
|
|
|
|
}
|