Skip to content

Commit

Permalink
Fix TypeScript errors
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Oct 28, 2020
1 parent 00349cc commit f8c2ac4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/core/server/http/cookie_session_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export async function createCookieSessionStorageFactory<T>(
isHttpOnly: true,
isSameSite: cookieOptions.sameSite === 'None' ? false : cookieOptions.sameSite ?? false,
},
validateFunc: async (req, session: T | T[]) => {
validateFunc: async (req: Request, session: T | T[]) => {
const result = cookieOptions.validate(session);
if (!result.isValid) {
clearInvalidCookie(req, result.path);
Expand Down
3 changes: 3 additions & 0 deletions src/core/server/http/http_server.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ function createKibanaRequestMock<P = any, Q = any, B = any>({
method,
url,
route: {
// @ts-expect-error According to types/hapi__hapi the following settings-fields have problems:
// - `auth` can't be a boolean, but it can according to the @hapi/hapi source (https://github.com/hapijs/hapi/blob/v18.4.2/lib/route.js#L139)
// - `app` isn't a valid property, but it is and this was fixed in the types in v19.0.1 (https://github.com/DefinitelyTyped/DefinitelyTyped/pull/41968)
settings: { tags: routeTags, auth: routeAuthRequired, app: kibanaRouteOptions },
},
raw: {
Expand Down
1 change: 1 addition & 0 deletions src/core/server/http/http_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const createInternalSetupContractMock = () => {
start: jest.fn(),
stop: jest.fn(),
config: jest.fn().mockReturnValue(configMock.create()),
// @ts-expect-error somehow it thinks that `Server` isn't a `Construtable`
} as unknown) as jest.MockedClass<Server>,
createCookieSessionStorageFactory: jest.fn(),
registerOnPreRouting: jest.fn(),
Expand Down
6 changes: 6 additions & 0 deletions src/core/server/http/router/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ describe('KibanaRequest', () => {
const request = httpServerMock.createRawRequest({
route: {
settings: {
// @ts-expect-error According to types/hapi__hapi, `auth` can't be a boolean, but it can according to the @hapi/hapi source (https://github.com/hapijs/hapi/blob/v18.4.2/lib/route.js#L139)
auth,
},
},
Expand All @@ -211,6 +212,7 @@ describe('KibanaRequest', () => {
const request = httpServerMock.createRawRequest({
route: {
settings: {
// @ts-expect-error According to types/hapi__hapi, the `auth` object has to have a `strategies` array, but it doesn't look like it needs it
auth,
},
},
Expand All @@ -225,6 +227,7 @@ describe('KibanaRequest', () => {
const request = httpServerMock.createRawRequest({
route: {
settings: {
// @ts-expect-error According to types/hapi__hapi, the `auth` object has to have a `strategies` array, but it doesn't look like it needs it
auth,
},
},
Expand All @@ -239,6 +242,7 @@ describe('KibanaRequest', () => {
const request = httpServerMock.createRawRequest({
route: {
settings: {
// @ts-expect-error According to types/hapi__hapi, the `auth` object has to have a `strategies` array, but it doesn't look like it needs it
auth,
},
},
Expand All @@ -253,6 +257,7 @@ describe('KibanaRequest', () => {
const request = httpServerMock.createRawRequest({
route: {
settings: {
// @ts-expect-error According to types/hapi__hapi, `auth` can't be a string, but I'm not sure that's true
auth,
},
},
Expand All @@ -268,6 +273,7 @@ describe('KibanaRequest', () => {
const request = httpServerMock.createRawRequest({
route: {
settings: {
// @ts-expect-error According to types/hapi__hapi, the `auth` object has to have a `strategies` array, but it doesn't look like it needs it
auth,
},
},
Expand Down
15 changes: 12 additions & 3 deletions src/core/server/http/router/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { URL } from 'url';
import uuid from 'uuid';
import { Request, RouteOptionsApp, RequestApplicationState } from '@hapi/hapi';
import { Request, RouteOptionsApp, RequestApplicationState, RouteOptions } from '@hapi/hapi';
import { Observable, fromEvent, merge } from 'rxjs';
import { shareReplay, first, takeUntil } from 'rxjs/operators';
import { RecursiveReadonly } from '@kbn/utility-types';
Expand Down Expand Up @@ -260,8 +260,16 @@ export class KibanaRequest<
const socketTimeout = (request.raw.req.socket as any)?.timeout;
const options = ({
authRequired: this.getAuthRequired(request),
// some places in LP call KibanaRequest.from(request) manually. remove fallback to true before v8
xsrfRequired: (request.route.settings.app as KibanaRouteOptions)?.xsrfRequired ?? true,
// TypeScript note: Casting to `RouterOptions` to fix the following error:
//
// Property 'app' does not exist on type 'RouteSettings'
//
// In @types/hapi__hapi v18, `request.route.settings` is of type
// `RouteSettings`, which doesn't have an `app` property. I think this is
// a mistake. In v19, the `RouteSettings` interface does have an `app`
// property.
xsrfRequired:
((request.route.settings as RouteOptions).app as KibanaRouteOptions)?.xsrfRequired ?? true, // some places in LP call KibanaRequest.from(request) manually. remove fallback to true before v8
tags: request.route.settings.tags || [],
timeout: {
payload: payloadTimeout,
Expand Down Expand Up @@ -301,6 +309,7 @@ export class KibanaRequest<
return true;
}

// @ts-expect-error According to @types/hapi__hapi, `route.settings` should be of type `RouteSettings`, but it seems that it's actually `RouteOptions`.
if (authOptions === false) return false;
throw new Error(
`unexpected authentication options: ${JSON.stringify(authOptions)} for route: ${
Expand Down

0 comments on commit f8c2ac4

Please sign in to comment.