Skip to content

Commit

Permalink
[kbnClient] fix basePath handling and export reponse type (elastic#97277
Browse files Browse the repository at this point in the history
)

Co-authored-by: spalger <spalger@users.noreply.github.com>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
3 people authored and madirey committed May 11, 2021
1 parent 852785a commit 55ed68d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class KbnClientImportExport {
excludeExportDetails: true,
includeReferencesDeep: true,
},
responseType: 'text',
});

if (typeof resp.data !== 'string') {
Expand Down
15 changes: 12 additions & 3 deletions packages/kbn-test/src/kbn_client/kbn_client_requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Url from 'url';
import Https from 'https';
import Qs from 'querystring';

import Axios, { AxiosResponse } from 'axios';
import Axios, { AxiosResponse, ResponseType } from 'axios';
import { ToolingLog, isAxiosRequestError, isAxiosResponseError } from '@kbn/dev-utils';

const isConcliftOnGetError = (error: any) => {
Expand Down Expand Up @@ -53,6 +53,7 @@ export interface ReqOptions {
body?: any;
retries?: number;
headers?: Record<string, string>;
responseType?: ResponseType;
}

const delay = (ms: number) =>
Expand Down Expand Up @@ -84,11 +85,16 @@ export class KbnClientRequester {
}

public resolveUrl(relativeUrl: string = '/') {
return Url.resolve(this.pickUrl(), relativeUrl);
let baseUrl = this.pickUrl();
if (!baseUrl.endsWith('/')) {
baseUrl += '/';
}
const relative = relativeUrl.startsWith('/') ? relativeUrl.slice(1) : relativeUrl;
return Url.resolve(baseUrl, relative);
}

async request<T>(options: ReqOptions): Promise<AxiosResponse<T>> {
const url = Url.resolve(this.pickUrl(), options.path);
const url = this.resolveUrl(options.path);
const description = options.description || `${options.method} ${url}`;
let attempt = 0;
const maxAttempts = options.retries ?? DEFAULT_MAX_ATTEMPTS;
Expand All @@ -107,6 +113,9 @@ export class KbnClientRequester {
'kbn-xsrf': 'kbn-client',
},
httpsAgent: this.httpsAgent,
responseType: options.responseType,
// work around https://github.com/axios/axios/issues/2791
transformResponse: options.responseType === 'text' ? [(x) => x] : undefined,
paramsSerializer: (params) => Qs.stringify(params),
});

Expand Down

0 comments on commit 55ed68d

Please sign in to comment.