> Markdown version of [TypeScript Client](https://vaadin.com/docs/next/flow/react-views/browser-callable-services/client). Section index: [llms.txt](https://vaadin.com/docs/next/flow/llms.txt)

# TypeScript Client

The generated modules of browser-callable services send their calls through a `ConnectClient` from the `@vaadin/hilla-frontend` package. Vaadin generates a default client in `src/main/frontend/generated/connect-client.default.ts`, which all the generated modules share:

```typescript
import { ConnectClient } from '@vaadin/hilla-frontend';

const client = new ConnectClient({ prefix: 'connect' });

export default client;
```

Don’t edit the generated file, because Vaadin overwrites it.

## <a id="custom-client"></a>A Client of Your Own

To configure the client, create `src/main/frontend/connect-client.ts`, with the client as its default export. When this file exists, the generated modules use its client, and Vaadin doesn’t generate `connect-client.default.ts`:

`src/main/frontend/connect-client.ts`

```typescript
import { ConnectClient } from '@vaadin/hilla-frontend';
import { logMiddleware } from './log-middleware';

const client = new ConnectClient({ prefix: 'connect', middlewares: [logMiddleware] });

export default client;
```

The constructor takes these options:

- `prefix`

  The path of the services, relative to the base URL of the application. It must match the server, as described in [Service URLs](https://vaadin.com/docs/next/flow/react-views/browser-callable-services.md#service-urls). Defaults to `connect`.

- `middlewares`

  The [middleware](#middleware) that intercepts the calls.

## <a id="calls-without-a-generated-module"></a>Calls Without a Generated Module

The generated functions call the `call()` method of the client, which a view can also call directly. It takes the service name, the method name, an object with the arguments by parameter name, and the optional [request options](https://vaadin.com/docs/next/flow/react-views/browser-callable-services.md#request-options). It returns a `Promise` of the return value. Import the client from `Frontend/connect-client` when the application has a client of its own:

```typescript
import client from 'Frontend/generated/connect-client.default';

const result = await client.call('CounterService', 'addOne', { number: 1 });
```

The `subscribe()` method does the same for [reactive services](https://vaadin.com/docs/next/flow/react-views/browser-callable-services/reactive-services.md), and returns a `Subscription`.

## <a id="middleware"></a>Middleware

Middleware intercepts each call of the client. It can, for example, log or time the calls, add headers, or return a response without calling the server. Middleware is an `async` function that takes these arguments:

- `context`

  The call. Its `endpoint`, `method`, and `params` properties are the arguments of `call()`. Its `request` property is the Fetch API [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) that the client sends.

- `next`

  A function that passes the context on to the next middleware, or sends the request, and returns the [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).

Middleware gets the response before the client checks it, so it sees responses with an error status as well. The client turns the response into a return value or an [error](https://vaadin.com/docs/next/flow/react-views/browser-callable-services.md#errors) after all the middleware has run. Middleware doesn’t intercept the values of [reactive services](https://vaadin.com/docs/next/flow/react-views/browser-callable-services/reactive-services.md).

The following middleware logs each call:

`log-middleware.ts`

```typescript
import type { MiddlewareContext, MiddlewareNext } from '@vaadin/hilla-frontend';

export async function logMiddleware(context: MiddlewareContext, next: MiddlewareNext): Promise<Response> {
  const { endpoint, method, params } = context;
  console.log(`Calling ${endpoint}.${method} with ${JSON.stringify(params)}`);

  const response = await next(context);

  // Clone the response before reading its body
  const body = await response.clone().text();
  console.log(`Received ${response.status}: ${body}`);
  return response;
}
```

Give the middleware to the client of your own, as shown in [A Client of Your Own](#custom-client). The middleware runs in the order of the array. A call uses the middleware that the client has when the call starts. A middleware can also be an object with an `invoke(context, next)` method.

To change the request, replace `context.request` before calling `next`. The following middleware sends the calls of one service to another server:

```typescript
import type { MiddlewareContext, MiddlewareNext } from '@vaadin/hilla-frontend';

export async function dispatchMiddleware(context: MiddlewareContext, next: MiddlewareNext): Promise<Response> {
  if (context.endpoint === 'ExternalService') {
    const url = context.request.url.replace('https://my-app.example.com', 'https://external.example.com');
    context.request = new Request(url, context.request);
  }
  return next(context);
}
```

To answer a call without calling the server, return a `Response` instead of calling `next`:

```typescript
import type { MiddlewareContext, MiddlewareNext } from '@vaadin/hilla-frontend';

export async function stubMiddleware(context: MiddlewareContext, next: MiddlewareNext): Promise<Response> {
  if (context.endpoint === 'StubService') {
    return new Response('{}');
  }
  return next(context);
}
```

The `@vaadin/hilla-frontend` package also has an `InvalidSessionMiddleware`, which handles calls that fail because the session of the user has expired. See [Expired Sessions](https://vaadin.com/docs/next/flow/security/react-views/authentication.md#expired-sessions).
