vault/ui/app/utils/api-path.js
Matthew Irish 27e595e352
UI - dynamic related capabilities (#7013)
* lay groundwork for application serializer to setup capabilities relationships

* add api path util and tests, and attach-capabilites fn

* make attach-capabilities work with array responses, add tests
2019-06-28 16:07:45 -05:00

25 lines
722 B
JavaScript

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) {
let dict = data || {};
let result = [strings[0]];
assert(
`Expected ${keys.length} keys in apiPath context, only recieved ${Object.keys(data).length}`,
keys.length === Object.keys(data).length
);
keys.forEach((key, i) => {
result.push(dict[key], strings[i + 1]);
});
return result.join('');
};
}