mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
callWithRequest should pass the context that the API method would get when called normally instead of assuming client is the context for everything
Fixes #6483
This commit is contained in:
parent
273ff94535
commit
335a276f7a
1 changed files with 8 additions and 2 deletions
|
@ -2,15 +2,21 @@ const _ = require('lodash');
|
|||
const Promise = require('bluebird');
|
||||
const Boom = require('boom');
|
||||
const getBasicAuthRealm = require('./get_basic_auth_realm');
|
||||
const toPath = require('lodash/internal/toPath');
|
||||
|
||||
module.exports = (client) => {
|
||||
return (req, endpoint, params = {}) => {
|
||||
if (req.headers.authorization) {
|
||||
_.set(params, 'headers.authorization', req.headers.authorization);
|
||||
}
|
||||
const api = _.get(client, endpoint);
|
||||
const path = toPath(endpoint);
|
||||
const api = _.get(client, path);
|
||||
let apiContext = _.get(client, path.slice(0, -1));
|
||||
if (_.isEmpty(apiContext)) {
|
||||
apiContext = client;
|
||||
}
|
||||
if (!api) throw new Error(`callWithRequest called with an invalid endpoint: ${endpoint}`);
|
||||
return api.call(client, params)
|
||||
return api.call(apiContext, params)
|
||||
.catch((err) => {
|
||||
if (err.status === 401) {
|
||||
// TODO: The err.message is temporary until we have support for getting headers in the client.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue