Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[http] Expose isKibanaReponse helper in public API #182392

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,4 @@ export type {
export { isKibanaRequest, isRealRequest, ensureRawRequest, CoreKibanaRequest } from './src/request';
export { isSafeMethod } from './src/route';
export { HapiResponseAdapter } from './src/response_adapter';
export {
kibanaResponseFactory,
lifecycleResponseFactory,
isKibanaResponse,
KibanaResponse,
} from './src/response';
export { kibanaResponseFactory, lifecycleResponseFactory, KibanaResponse } from './src/response';
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ import type {
} from '@kbn/core-http-server';
import mime from 'mime';

export function isKibanaResponse(response: Record<string, any>): response is IKibanaResponse {
return typeof response.status === 'number' && typeof response.options === 'object';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the most thorough check and would be nice to add unit tests for before making public but overall happy with the idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jloleysens I added some tests in d1a8d60 and made some small changes to the helper to make it a bit more strict, let me know what you think!

}

/**
* A response data object, expected to returned as a result of {@link RequestHandler} execution
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import type {
AuthResultRedirected,
AuthToolkit,
} from '@kbn/core-http-server';
import { isKibanaResponse } from '@kbn/core-http-server';
import { AuthResultType } from '@kbn/core-http-server';
import {
HapiResponseAdapter,
CoreKibanaRequest,
lifecycleResponseFactory,
isKibanaResponse,
} from '@kbn/core-http-router-server-internal';

const authResult = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import type {
OnPostAuthResult,
OnPostAuthHandler,
} from '@kbn/core-http-server';
import { isKibanaResponse } from '@kbn/core-http-server';
import { OnPostAuthResultType } from '@kbn/core-http-server';
import {
HapiResponseAdapter,
CoreKibanaRequest,
lifecycleResponseFactory,
isKibanaResponse,
} from '@kbn/core-http-router-server-internal';

const postAuthResult = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import type {
OnPreAuthHandler,
OnPreAuthToolkit,
} from '@kbn/core-http-server';
import { isKibanaResponse } from '@kbn/core-http-server';
import { OnPreAuthResultType } from '@kbn/core-http-server';
import {
HapiResponseAdapter,
CoreKibanaRequest,
isKibanaResponse,
lifecycleResponseFactory,
} from '@kbn/core-http-router-server-internal';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import type {
OnPreRoutingResult,
OnPreRoutingHandler,
} from '@kbn/core-http-server';
import { isKibanaResponse } from '@kbn/core-http-server';
import { OnPreRoutingResultType } from '@kbn/core-http-server';
import {
HapiResponseAdapter,
CoreKibanaRequest,
isKibanaResponse,
lifecycleResponseFactory,
} from '@kbn/core-http-router-server-internal';

Expand Down
1 change: 1 addition & 0 deletions packages/core/http/core-http-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export {
getRequestValidation,
getResponseValidation,
isFullValidatorContainer,
isKibanaResponse,
} from './src/router';

export type { ICspConfig } from './src/csp';
Expand Down
1 change: 1 addition & 0 deletions packages/core/http/core-http-server/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type {
ErrorHttpResponseOptions,
FileHttpResponseOptions,
} from './response';
export { isKibanaResponse } from './response';
export type {
RouteConfigOptions,
RouteMethod,
Expand Down
85 changes: 85 additions & 0 deletions packages/core/http/core-http-server/src/router/response.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { isKibanaResponse } from './response';

describe('isKibanaResponse', () => {
it('expects the status to be a number', () => {
expect(
isKibanaResponse({
status: 200,
options: {},
Comment on lines +11 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue having this exposed from the public type package is that we can't even test it against our concrete implementation 😅.

However it's probably fine for now, I don't have a better suggestion / place to put it that wouldn't imply create yet another package, so if @jloleysens is fine with the approach, I'm too.

})
).toEqual(true);

expect(
isKibanaResponse({
status: '200',
options: {},
})
).toEqual(false);
});

it('expects the options to be an object', () => {
expect(
isKibanaResponse({
status: 200,
options: {},
})
).toEqual(true);

expect(
isKibanaResponse({
status: 200,
options: [],
})
).toEqual(false);
expect(
isKibanaResponse({
status: 200,
options: null,
})
).toEqual(false);
expect(
isKibanaResponse({
status: 200,
options: 'a string',
})
).toEqual(false);
expect(
isKibanaResponse({
status: 200,
options: new Set(),
})
).toEqual(false);
expect(
isKibanaResponse({
status: 200,
options: () => {},
})
).toEqual(false);
});

it('allows a payload but no other properties', () => {
expect(
isKibanaResponse({
status: 200,
options: {},
payload: 'My stuff',
})
).toEqual(true);

expect(
isKibanaResponse({
status: 200,
options: {},
data: 'Not allowed',
})
).toEqual(false);
});
});
16 changes: 16 additions & 0 deletions packages/core/http/core-http-server/src/router/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ export interface IKibanaResponse<T extends HttpResponsePayload | ResponseError =
readonly options: HttpResponseOptions;
}

export function isKibanaResponse(response: Record<string, any>): response is IKibanaResponse {
const { status, options, payload, ...rest } = response;

if (Object.keys(rest).length !== 0) {
return false;
}

return (
typeof status === 'number' &&
typeof options === 'object' &&
!Array.isArray(options) &&
options !== null &&
!(options instanceof Set)
);
}

/**
* HTTP response parameters for a response with adjustable status code.
* @public
Expand Down