Skip to content
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
8 changes: 7 additions & 1 deletion packages/kbn-server-route-repository-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
*/

export { createRepositoryClient } from './src/create_repository_client';
export type { DefaultClientOptions } from '@kbn/server-route-repository-utils';
export { isHttpFetchError } from './src/is_http_fetch_error';

export type {
DefaultClientOptions,
ClientRequestParamsOf,
RouteRepositoryClient,
} from '@kbn/server-route-repository-utils';
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 { IHttpFetchError, isHttpFetchError as coreIsHttpFetchError } from '@kbn/core-http-browser';

interface ErrorBody {
statusCode: number;
message: string;
error: string;
}

export function isHttpFetchError(error: unknown): error is IHttpFetchError<ErrorBody> {
return coreIsHttpFetchError(error);
}
1 change: 1 addition & 0 deletions packages/kbn-server-route-repository-client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"kbn_references": [
"@kbn/server-route-repository-utils",
"@kbn/core-lifecycle-browser",
"@kbn/core-http-browser",
]
}
38 changes: 35 additions & 3 deletions packages/kbn-server-route-repository-utils/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,38 @@ import { z } from '@kbn/zod';
import * as t from 'io-ts';
import { RequiredKeys } from 'utility-types';

type PathMaybeOptional<T extends { path: Record<string, any> }> = RequiredKeys<
T['path']
> extends never
? { path?: T['path'] }
: { path: T['path'] };

type QueryMaybeOptional<T extends { query: Record<string, any> }> = RequiredKeys<
T['query']
> extends never
? { query?: T['query'] }
: { query: T['query'] };

type BodyMaybeOptional<T extends { body: Record<string, any> }> = RequiredKeys<
T['body']
> extends never
? { body?: T['body'] }
: { body: T['body'] };

type ParamsMaybeOptional<
TPath extends Record<string, any>,
TQuery extends Record<string, any>,
TBody extends Record<string, any>
> = PathMaybeOptional<{ path: TPath }> &
QueryMaybeOptional<{ query: TQuery }> &
BodyMaybeOptional<{ body: TBody }>;

type ZodMaybeOptional<T extends { path: any; query: any; body: any }> = ParamsMaybeOptional<
T['path'],
T['query'],
T['body']
>;

type MaybeOptional<T extends { params: Record<string, any> }> = RequiredKeys<
T['params']
> extends never
Expand Down Expand Up @@ -93,7 +125,7 @@ type ClientRequestParamsOfType<TRouteParamsRT extends RouteParamsRT> =
}>
: TRouteParamsRT extends z.Schema
? MaybeOptional<{
params: z.TypeOf<TRouteParamsRT>;
params: ZodMaybeOptional<z.input<TRouteParamsRT>>;
}>
: {};

Expand All @@ -104,7 +136,7 @@ type DecodedRequestParamsOfType<TRouteParamsRT extends RouteParamsRT> =
}>
: TRouteParamsRT extends z.Schema
? MaybeOptional<{
params: z.TypeOf<TRouteParamsRT>;
params: ZodMaybeOptional<z.output<TRouteParamsRT>>;
}>
: {};

Expand Down Expand Up @@ -162,7 +194,7 @@ type MaybeOptionalArgs<T extends Record<string, any>> = RequiredKeys<T> extends

export type RouteRepositoryClient<
TServerRouteRepository extends ServerRouteRepository,
TAdditionalClientOptions extends Record<string, any>
TAdditionalClientOptions extends Record<string, any> = DefaultClientOptions
> = <TEndpoint extends Extract<keyof TServerRouteRepository, string>>(
endpoint: TEndpoint,
...args: MaybeOptionalArgs<
Expand Down
4 changes: 1 addition & 3 deletions packages/kbn-server-route-repository/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ The client can be created either in `setup` or `start`.

> browser/plugin.ts
```javascript
import { isHttpFetchError } from '@kbn/core-http-browser';
import { DefaultClientOptions } from '@kbn/server-route-repository-utils';
import { createRepositoryClient } from '@kbn/server-route-repository-client';
import { createRepositoryClient, isHttpFetchError, DefaultClientOptions } from '@kbn/server-route-repository-client';
import type { MyPluginRouteRepository } from '../server/plugin';

export type MyPluginRepositoryClient =
Expand Down