Skip to content

Commit

Permalink
Expose an HTTP-request browser client
Browse files Browse the repository at this point in the history
  • Loading branch information
eliperelman committed Apr 24, 2019
1 parent 73c3492 commit 32af0ba
Show file tree
Hide file tree
Showing 13 changed files with 478 additions and 183 deletions.
2 changes: 1 addition & 1 deletion src/core/public/core_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export class CoreSystem {
const i18n = this.i18n.setup();
const injectedMetadata = this.injectedMetadata.setup();
const fatalErrors = this.fatalErrors.setup({ i18n });
const http = this.http.setup({ fatalErrors });
const basePath = this.basePath.setup({ injectedMetadata });
const http = this.http.setup({ basePath, injectedMetadata, fatalErrors });
const uiSettings = this.uiSettings.setup({
http,
injectedMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@
* under the License.
*/

jest.mock('../chrome', () => ({
addBasePath: (path: string) => `http://localhost/myBase/${path}`,
}));
import { HttpHandler, HttpFetchOptions, AbortablePromise } from './types';

jest.mock('../metadata', () => ({
metadata: {
version: 'my-version',
},
}));
export function abortable<T>(handler: HttpHandler<T>) {
return (path: string, options: HttpFetchOptions = {}): AbortablePromise<T> => {
const controller = options.signal
? new AbortController()
: { signal: options.signal, abort: Function.prototype };
const promise = new Promise<T>((resolve, reject) => {
handler(path, { ...options, signal: controller.signal }).then(resolve, reject);
});

import { kfetchAbortable } from './kfetch_abortable';

describe('kfetchAbortable', () => {
it('should return an object with a fetching promise and an abort callback', () => {
const { fetching, abort } = kfetchAbortable({ pathname: 'my/path' });
expect(typeof fetching.then).toBe('function');
expect(typeof fetching.catch).toBe('function');
expect(typeof abort).toBe('function');
});
});
return Object.assign(promise, {
abort() {
controller.abort();
return promise;
},
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,14 @@
* under the License.
*/

import { kfetch, KFetchKibanaOptions, KFetchOptions } from './kfetch';
export class HttpFetchError extends Error {
constructor(message: string, public readonly response?: Response, public readonly body?: any) {
super(message);

type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;

function createAbortable() {
const abortController = new AbortController();
const { signal, abort } = abortController;

return {
signal,
abort: abort.bind(abortController),
};
}

export function kfetchAbortable(
fetchOptions?: Omit<KFetchOptions, 'signal'>,
kibanaOptions?: KFetchKibanaOptions
) {
const { signal, abort } = createAbortable();
const fetching = kfetch({ ...fetchOptions, signal }, kibanaOptions);

return {
fetching,
abort,
};
// captureStackTrace is only available in the V8 engine, so any browser using
// a different JS engine won't have access to this method.
if (Error.captureStackTrace) {
Error.captureStackTrace(this, HttpFetchError);
}
}
}
31 changes: 14 additions & 17 deletions src/core/public/http/http_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@
* specific language governing permissions and limitations
* under the License.
*/
import { HttpService, HttpSetup } from './http_service';

const createSetupContractMock = () => {
const setupContract: jest.Mocked<HttpSetup> = {
addLoadingCount: jest.fn(),
getLoadingCount$: jest.fn(),
};
return setupContract;
};
import { HttpService, HttpSetup } from './http_service';

type HttpServiceContract = PublicMethodsOf<HttpService>;
const createMock = () => {
const mocked: jest.Mocked<HttpServiceContract> = {
setup: jest.fn(),
stop: jest.fn(),
};
mocked.setup.mockReturnValue(createSetupContractMock());
return mocked;
};
const createSetupContractMock = (): jest.Mocked<HttpSetup> => ({
fetch: jest.fn(),
get: jest.fn(),
post: jest.fn(),
put: jest.fn(),
del: jest.fn(),
addLoadingCount: jest.fn(),
getLoadingCount$: jest.fn(),
});
const createMock = (): jest.Mocked<PublicMethodsOf<HttpService>> => ({
setup: jest.fn().mockReturnValue(createSetupContractMock()),
stop: jest.fn(),
});

export const httpServiceMock = {
create: createMock,
Expand Down
Loading

0 comments on commit 32af0ba

Please sign in to comment.