Skip to content

Commit

Permalink
[http] Expose isKibanaReponse helper in public API (#182392)
Browse files Browse the repository at this point in the history
For some other changes I'm working on, I need to be able to distinguish
if a route handler returned a `IKibanaResponse` / `KibanaResponse`
object or some other object that needs to be wrapped before being sent
back.

Rather than duplicating this little helper I want to expose it as part
of the public API of `core.http`.
  • Loading branch information
miltonhultgren authored May 6, 2024
1 parent 6f25042 commit 8a52c11
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 14 deletions.
7 changes: 1 addition & 6 deletions packages/core/http/core-http-router-server-internal/index.ts
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';
}

/**
* 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: {},
})
).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

0 comments on commit 8a52c11

Please sign in to comment.