2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2019-06-28 21:07:45 +00:00
|
|
|
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) {
|
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]];
|
2019-06-28 21:07:45 +00:00
|
|
|
assert(
|
2019-07-02 21:23:07 +00:00
|
|
|
`Expected ${keys.length} keys in apiPath context, only recieved ${Object.keys(data).join(',')}`,
|
|
|
|
Object.keys(data).length >= keys.length
|
2019-06-28 21:07:45 +00:00
|
|
|
);
|
|
|
|
keys.forEach((key, i) => {
|
|
|
|
result.push(dict[key], strings[i + 1]);
|
|
|
|
});
|
|
|
|
return result.join('');
|
|
|
|
};
|
|
|
|
}
|