open-vault/ui/app/utils/api-path.js
Hamid Ghaf 27bb03bbc0
adding copyright header (#19555)
* adding copyright header

* fix fmt and a test
2023-03-15 09:00:52 -07:00

30 lines
807 B
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { assert } from '@ember/debug';
// This is a tagged template function that will
// replace placeholders in the form of 'id' with the value from the passed context
//
// usage:
// let fn = apiPath`foo/bar/${'id'}`;
// let output = fn({id: 'an-id'});
// output will result in 'foo/bar/an-id';
export default function apiPath(strings, ...keys) {
return function (data) {
const dict = data || {};
const result = [strings[0]];
assert(
`Expected ${keys.length} keys in apiPath context, only recieved ${Object.keys(data).join(',')}`,
Object.keys(data).length >= keys.length
);
keys.forEach((key, i) => {
result.push(dict[key], strings[i + 1]);
});
return result.join('');
};
}