Skip to content

Commit

Permalink
[8.0] replace any with unknown in http client and types (elastic#114265
Browse files Browse the repository at this point in the history
…) (elastic#118348)

* replace any with unknown in http client and types (elastic#114265)

# Conflicts:
#	x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_logic.ts
#	x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_overview_logic.ts
#	x-pack/plugins/enterprise_search/public/applications/app_search/components/curations/components/suggestions_logic.tsx
#	x-pack/plugins/enterprise_search/public/applications/app_search/components/curations/views/curation_suggestion/curation_suggestion_logic.ts
#	x-pack/plugins/enterprise_search/public/applications/app_search/components/curations/views/curations_settings/curations_settings_logic.ts

* commit using @elastic.co

* remove unused file
  • Loading branch information
orouz authored Nov 15, 2021
1 parent edf0021 commit 835f2d8
Show file tree
Hide file tree
Showing 165 changed files with 725 additions and 424 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<b>Signature:</b>

```typescript
export interface HttpResponse<TResponseBody = any>
export interface HttpResponse<TResponseBody = unknown>
```

## Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
readonly body?: any;
readonly body?: TResponseBody;
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
<b>Signature:</b>

```typescript
export interface IHttpFetchError extends Error
export interface IHttpFetchError<TResponseBody = unknown> extends Error
```
<b>Extends:</b> Error
## Properties
| Property | Type | Description |
| --- | --- | --- |
| [body?](./kibana-plugin-core-public.ihttpfetcherror.body.md) | any | <i>(Optional)</i> |
| [body?](./kibana-plugin-core-public.ihttpfetcherror.body.md) | TResponseBody | <i>(Optional)</i> |
| [name](./kibana-plugin-core-public.ihttpfetcherror.name.md) | string | |
| [req](./kibana-plugin-core-public.ihttpfetcherror.req.md) | Request | |
| [request](./kibana-plugin-core-public.ihttpfetcherror.request.md) | Request | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Properties that can be returned by HttpInterceptor.request to override the respo
<b>Signature:</b>

```typescript
export interface IHttpResponseInterceptorOverrides<TResponseBody = any>
export interface IHttpResponseInterceptorOverrides<TResponseBody = unknown>
```

## Properties
Expand Down
1 change: 1 addition & 0 deletions docs/development/core/public/kibana-plugin-core-public.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [Plugin\_2](./kibana-plugin-core-public.plugin_2.md) | The interface that should be returned by a <code>PluginInitializer</code>. |
| [PluginInitializerContext](./kibana-plugin-core-public.plugininitializercontext.md) | The available core services passed to a <code>PluginInitializer</code> |
| [ResolvedSimpleSavedObject](./kibana-plugin-core-public.resolvedsimplesavedobject.md) | This interface is a very simple wrapper for SavedObjects resolved from the server with the [SavedObjectsClient](./kibana-plugin-core-public.savedobjectsclient.md)<!-- -->. |
| [ResponseErrorBody](./kibana-plugin-core-public.responseerrorbody.md) | |
| [SavedObject](./kibana-plugin-core-public.savedobject.md) | |
| [SavedObjectAttributes](./kibana-plugin-core-public.savedobjectattributes.md) | The data for a Saved Object is stored as an object in the <code>attributes</code> property. |
| [SavedObjectError](./kibana-plugin-core-public.savedobjecterror.md) | |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [ResponseErrorBody](./kibana-plugin-core-public.responseerrorbody.md) &gt; [attributes](./kibana-plugin-core-public.responseerrorbody.attributes.md)

## ResponseErrorBody.attributes property

<b>Signature:</b>

```typescript
attributes?: Record<string, unknown>;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [ResponseErrorBody](./kibana-plugin-core-public.responseerrorbody.md)

## ResponseErrorBody interface


<b>Signature:</b>

```typescript
export interface ResponseErrorBody
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [attributes?](./kibana-plugin-core-public.responseerrorbody.attributes.md) | Record&lt;string, unknown&gt; | <i>(Optional)</i> |
| [message](./kibana-plugin-core-public.responseerrorbody.message.md) | string | |
| [statusCode](./kibana-plugin-core-public.responseerrorbody.statuscode.md) | number | |

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [ResponseErrorBody](./kibana-plugin-core-public.responseerrorbody.md) &gt; [message](./kibana-plugin-core-public.responseerrorbody.message.md)

## ResponseErrorBody.message property

<b>Signature:</b>

```typescript
message: string;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [ResponseErrorBody](./kibana-plugin-core-public.responseerrorbody.md) &gt; [statusCode](./kibana-plugin-core-public.responseerrorbody.statuscode.md)

## ResponseErrorBody.statusCode property

<b>Signature:</b>

```typescript
statusCode: number;
```
5 changes: 3 additions & 2 deletions examples/preboot_example/public/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
EuiText,
} from '@elastic/eui';
import React, { useEffect, useState } from 'react';
import type { HttpSetup, IHttpFetchError } from 'src/core/public';
import type { HttpSetup, IHttpFetchError, ResponseErrorBody } from 'src/core/public';

export const App = ({ http, token }: { http: HttpSetup; token?: string }) => {
const onCompleteSetup = async ({ shouldReloadConfig }: { shouldReloadConfig: boolean }) => {
Expand All @@ -41,7 +41,8 @@ export const App = ({ http, token }: { http: HttpSetup; token?: string }) => {
.post('/api/preboot/connect_to_es', { body: JSON.stringify(elasticsearchConfig) })
.then(
(response) => setConnectResponse(JSON.stringify(response)),
(err: IHttpFetchError) => setConnectResponse(err?.body?.message || 'ERROR')
(err: IHttpFetchError<ResponseErrorBody>) =>
setConnectResponse(err?.body?.message || 'ERROR')
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/core/public/http/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ describe('Fetch', () => {
headers: { 'Content-Type': 'application/ndjson' },
});

const data = await fetchInstance.post('/my/path', {
const data = await fetchInstance.post<FormData>('/my/path', {
body,
headers: {
'Content-Type': undefined,
Expand Down
20 changes: 14 additions & 6 deletions src/core/public/http/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ export class Fetch {
);

if (optionsWithPath.asResponse) {
resolve(interceptedResponse);
resolve(interceptedResponse as HttpResponse<TResponseBody>);
} else {
resolve(interceptedResponse.body);
resolve(interceptedResponse.body as TResponseBody);
}
} catch (error) {
if (!(error instanceof HttpInterceptHaltError)) {
Expand Down Expand Up @@ -142,7 +142,9 @@ export class Fetch {
return new Request(url, fetchOptions as RequestInit);
}

private async fetchResponse(fetchOptions: HttpFetchOptionsWithPath): Promise<HttpResponse<any>> {
private async fetchResponse(
fetchOptions: HttpFetchOptionsWithPath
): Promise<HttpResponse<unknown>> {
const request = this.createRequest(fetchOptions);
let response: Response;
let body = null;
Expand Down Expand Up @@ -181,9 +183,15 @@ export class Fetch {
}

private shorthand(method: string): HttpHandler {
return (pathOrOptions: string | HttpFetchOptionsWithPath, options?: HttpFetchOptions) => {
const optionsWithPath = validateFetchArguments(pathOrOptions, options);
return this.fetch({ ...optionsWithPath, method });
return <T = unknown>(
pathOrOptions: string | HttpFetchOptionsWithPath,
options?: HttpFetchOptions
) => {
const optionsWithPath: HttpFetchOptionsWithPath = validateFetchArguments(
pathOrOptions,
options
);
return this.fetch<HttpResponse<T>>({ ...optionsWithPath, method });
};
}
}
Expand Down
28 changes: 18 additions & 10 deletions src/core/public/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,19 @@ export interface HttpFetchOptionsWithPath extends HttpFetchOptions {
* @public
*/
export interface HttpHandler {
<TResponseBody = any>(path: string, options: HttpFetchOptions & { asResponse: true }): Promise<
<TResponseBody = unknown>(
path: string,
options: HttpFetchOptions & { asResponse: true }
): Promise<HttpResponse<TResponseBody>>;
<TResponseBody = unknown>(options: HttpFetchOptionsWithPath & { asResponse: true }): Promise<
HttpResponse<TResponseBody>
>;
<TResponseBody = any>(options: HttpFetchOptionsWithPath & { asResponse: true }): Promise<
HttpResponse<TResponseBody>
>;
<TResponseBody = any>(path: string, options?: HttpFetchOptions): Promise<TResponseBody>;
<TResponseBody = any>(options: HttpFetchOptionsWithPath): Promise<TResponseBody>;
<TResponseBody = unknown>(path: string, options?: HttpFetchOptions): Promise<TResponseBody>;
<TResponseBody = unknown>(options: HttpFetchOptionsWithPath): Promise<TResponseBody>;
}

/** @public */
export interface HttpResponse<TResponseBody = any> {
export interface HttpResponse<TResponseBody = unknown> {
/** The original {@link HttpFetchOptionsWithPath} used to send this request. */
readonly fetchOptions: Readonly<HttpFetchOptionsWithPath>;
/** Raw request sent to Kibana server. */
Expand All @@ -322,15 +323,22 @@ export interface HttpResponse<TResponseBody = any> {
* Properties that can be returned by HttpInterceptor.request to override the response.
* @public
*/
export interface IHttpResponseInterceptorOverrides<TResponseBody = any> {
export interface IHttpResponseInterceptorOverrides<TResponseBody = unknown> {
/** Raw response received, may be undefined if there was an error. */
readonly response?: Readonly<Response>;
/** Parsed body received, may be undefined if there was an error. */
readonly body?: TResponseBody;
}

/** @public */
export interface IHttpFetchError extends Error {
export interface ResponseErrorBody {
message: string;
statusCode: number;
attributes?: Record<string, unknown>;
}

/** @public */
export interface IHttpFetchError<TResponseBody = unknown> extends Error {
readonly name: string;
readonly request: Request;
readonly response?: Response;
Expand All @@ -342,7 +350,7 @@ export interface IHttpFetchError extends Error {
* @deprecated Provided for legacy compatibility. Prefer the `response` property instead.
*/
readonly res?: Response;
readonly body?: any; // TODO: this should be unknown
readonly body?: TResponseBody;
}

/** @public */
Expand Down
1 change: 1 addition & 0 deletions src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export type {
IAnonymousPaths,
IExternalUrl,
IHttpInterceptController,
ResponseErrorBody,
IHttpFetchError,
IHttpResponseInterceptorOverrides,
} from './http';
Expand Down
26 changes: 18 additions & 8 deletions src/core/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -820,17 +820,17 @@ export interface HttpFetchQuery {
// @public
export interface HttpHandler {
// (undocumented)
<TResponseBody = any>(path: string, options: HttpFetchOptions & {
<TResponseBody = unknown>(path: string, options: HttpFetchOptions & {
asResponse: true;
}): Promise<HttpResponse<TResponseBody>>;
// (undocumented)
<TResponseBody = any>(options: HttpFetchOptionsWithPath & {
<TResponseBody = unknown>(options: HttpFetchOptionsWithPath & {
asResponse: true;
}): Promise<HttpResponse<TResponseBody>>;
// (undocumented)
<TResponseBody = any>(path: string, options?: HttpFetchOptions): Promise<TResponseBody>;
<TResponseBody = unknown>(path: string, options?: HttpFetchOptions): Promise<TResponseBody>;
// (undocumented)
<TResponseBody = any>(options: HttpFetchOptionsWithPath): Promise<TResponseBody>;
<TResponseBody = unknown>(options: HttpFetchOptionsWithPath): Promise<TResponseBody>;
}

// @public
Expand Down Expand Up @@ -882,7 +882,7 @@ export interface HttpRequestInit {
}

// @public (undocumented)
export interface HttpResponse<TResponseBody = any> {
export interface HttpResponse<TResponseBody = unknown> {
readonly body?: TResponseBody;
readonly fetchOptions: Readonly<HttpFetchOptionsWithPath>;
readonly request: Readonly<Request>;
Expand Down Expand Up @@ -949,9 +949,9 @@ export interface IExternalUrlPolicy {
}

// @public (undocumented)
export interface IHttpFetchError extends Error {
export interface IHttpFetchError<TResponseBody = unknown> extends Error {
// (undocumented)
readonly body?: any;
readonly body?: TResponseBody;
// (undocumented)
readonly name: string;
// @deprecated (undocumented)
Expand All @@ -971,7 +971,7 @@ export interface IHttpInterceptController {
}

// @public
export interface IHttpResponseInterceptorOverrides<TResponseBody = any> {
export interface IHttpResponseInterceptorOverrides<TResponseBody = unknown> {
readonly body?: TResponseBody;
readonly response?: Readonly<Response>;
}
Expand Down Expand Up @@ -1204,6 +1204,16 @@ export interface ResolvedSimpleSavedObject<T = unknown> {
saved_object: SimpleSavedObject<T>;
}

// @public (undocumented)
export interface ResponseErrorBody {
// (undocumented)
attributes?: Record<string, unknown>;
// (undocumented)
message: string;
// (undocumented)
statusCode: number;
}

// Warning: (ae-missing-release-tag) "SavedObject" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
Expand Down
14 changes: 7 additions & 7 deletions src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export class SavedObjectsClient {
overwrite: options.overwrite,
};

const createRequest: Promise<SavedObject<T>> = this.savedObjectsFetch(path, {
const createRequest = this.savedObjectsFetch<SavedObject<T>>(path, {
method: 'POST',
query,
body: JSON.stringify({
Expand Down Expand Up @@ -571,10 +571,10 @@ export class SavedObjectsClient {
upsert,
};

return this.savedObjectsFetch(path, {
return this.savedObjectsFetch<SavedObject<T>>(path, {
method: 'PUT',
body: JSON.stringify(body),
}).then((resp: SavedObject<T>) => {
}).then((resp) => {
return this.createSavedObject(resp);
});
}
Expand All @@ -588,11 +588,11 @@ export class SavedObjectsClient {
public bulkUpdate<T = unknown>(objects: SavedObjectsBulkUpdateObject[] = []) {
const path = this.getPath(['_bulk_update']);

return this.savedObjectsFetch(path, {
return this.savedObjectsFetch<{ saved_objects: Array<SavedObject<T>> }>(path, {
method: 'PUT',
body: JSON.stringify(objects),
}).then((resp) => {
resp.saved_objects = resp.saved_objects.map((d: SavedObject<T>) => this.createSavedObject(d));
resp.saved_objects = resp.saved_objects.map((d) => this.createSavedObject(d));
return renameKeys<
PromiseType<ReturnType<SavedObjectsApi['bulkUpdate']>>,
SavedObjectsBatchResponse
Expand Down Expand Up @@ -624,8 +624,8 @@ export class SavedObjectsClient {
* the old kfetch error format of `{res: {status: number}}` whereas `http.fetch`
* uses `{response: {status: number}}`.
*/
private savedObjectsFetch(path: string, { method, query, body }: HttpFetchOptions) {
return this.http.fetch(path, { method, query, body });
private savedObjectsFetch<T = unknown>(path: string, { method, query, body }: HttpFetchOptions) {
return this.http.fetch<T>(path, { method, query, body });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const setupValueSuggestionProvider = (
}

const requestSuggestions = memoize(
(
<T = unknown>(
index: string,
field: IFieldType,
query: string,
Expand All @@ -68,7 +68,7 @@ export const setupValueSuggestionProvider = (
) => {
usageCollector?.trackRequest();
return core.http
.fetch(`/api/kibana/suggestions/values/${index}`, {
.fetch<T>(`/api/kibana/suggestions/values/${index}`, {
method: 'POST',
body: JSON.stringify({
query,
Expand Down
Loading

0 comments on commit 835f2d8

Please sign in to comment.