Fetch API ​
WARNING
Fetch API client is currently in beta. The interface might change before it becomes stable. We encourage you to leave feedback on GitHub.
The Fetch API provides an interface for fetching resources (including across the network). It is a more powerful and flexible replacement for XMLHttpRequest.
Installation ​
Start by adding @hey-api/client-fetch
to your dependencies.
npm install @hey-api/client-fetch
pnpm add @hey-api/client-fetch
yarn add @hey-api/client-fetch
bun add @hey-api/client-fetch
In your configuration, add @hey-api/client-fetch
to your plugins and you'll be ready to generate client artifacts. 🎉
export default {
input: 'path/to/openapi.json',
output: 'src/client',
plugins: ['@hey-api/client-fetch'],
};
npx @hey-api/openapi-ts \
-i path/to/openapi.json \
-o src/client \
-c @hey-api/client-fetch
Configuration ​
The Fetch client is built as a thin wrapper on top of Fetch API, extending its functionality to work with Hey API. If you're already familiar with Fetch, configuring your client will feel like working directly with Fetch API.
When we installed the client above, it created a client.gen.ts
file. You will most likely want to configure the exported client
instance. There are two ways to do that.
setConfig()
​
This is the simpler approach. You can call the setConfig()
method at the beginning of your application or anytime you need to update the client configuration. You can pass any Fetch API configuration option to setConfig()
, and even your own Fetch implementation.
import { client } from 'client/client.gen';
client.setConfig({
baseUrl: 'https://example.com',
});
The disadvantage of this approach is that your code may call the client
instance before it's configured for the first time. Depending on your use case, you might need to use the second approach.
Runtime API ​
Since client.gen.ts
is a generated file, we can't directly modify it. Instead, we can tell our configuration to use a custom file implementing the Runtime API. We do that by specifying the runtimeConfigPath
option.
export default {
input: 'path/to/openapi.json',
output: 'src/client',
plugins: [
{
name: '@hey-api/client-fetch',
runtimeConfigPath: './src/hey-api.ts',
},
],
};
In our custom file, we need to export a createClientConfig()
method. This function is a simple wrapper allowing us to override configuration values.
import type { CreateClientConfig } from '@hey-api/client-fetch';
export const createClientConfig: CreateClientConfig = (config) => ({
...config,
baseUrl: 'https://example.com',
});
With this approach, client.gen.ts
will call createClientConfig()
before initializing the client
instance. If needed, you can still use setConfig()
to update the client configuration later.
createClient()
​
You can also create your own client instance. You can use it to manually send requests or point it to a different domain.
import { createClient } from '@hey-api/client-fetch';
const myClient = createClient({
baseUrl: 'https://example.com',
});
You can also pass this instance to any SDK function through the client
option. This will override the default instance from client.gen.ts
.
const response = await getFoo({
client: myClient,
});
SDKs ​
Alternatively, you can pass the client configuration options to each SDK function. This is useful if you don't want to create a client instance for one-off use cases.
const response = await getFoo({
baseUrl: 'https://example.com', // <-- override default configuration
});
Interceptors ​
Interceptors (middleware) can be used to modify requests before they're sent or responses before they're returned to your application. They can be added with use
and removed with eject
. Fetch API does not have the interceptor functionality, so we implement our own. Below is an example request interceptor
import { client } from 'client/client.gen';
// Supports async functions
client.interceptors.request.use(async (request) => {
// do something
return request;
});
import { client } from 'client/client.gen';
client.interceptors.request.eject((request) => {
// do something
return request;
});
and an example response interceptor
import { client } from 'client/client.gen';
client.interceptors.response.use((response) => {
// do something
return response;
});
import { client } from 'client/client.gen';
client.interceptors.response.eject((response) => {
// do something
return response;
});
TIP
To eject, you must provide a reference to the function that was passed to use()
.
Auth ​
The SDKs include auth mechanisms for every endpoint. You will want to configure the auth
field to pass the right token for each request. The auth
field can be a string or a function returning a string representing the token. The returned value will be attached only to requests that require auth.
import { client } from 'client/client.gen';
client.setConfig({
auth: () => '<my_token>',
baseUrl: 'https://example.com',
});
If you're not using SDKs or generating auth, using interceptors is a common approach to configuring auth for each request.
import { client } from 'client/client.gen';
client.interceptors.request.use((request, options) => {
request.headers.set('Authorization', 'Bearer <my_token>');
return request;
});
Build URL ​
If you need to access the compiled URL, you can use the buildUrl()
method. It's loosely typed by default to accept almost any value; in practice, you will want to pass a type hint.
type FooData = {
path: {
fooId: number;
};
query?: {
bar?: string;
};
url: '/foo/{fooId}';
};
const url = client.buildUrl<FooData>({
path: {
fooId: 1,
},
query: {
bar: 'baz',
},
url: '/foo/{fooId}',
});
console.log(url); // prints '/foo/1?bar=baz'
Bundling ​
Sometimes, you may not want to declare client packages as a dependency. This scenario is common if you're using Hey API to generate output that is repackaged and published for other consumers under your own brand. For such cases, our clients support bundling through the client.bundle
configuration option.
export default {
input: 'path/to/openapi.json',
output: 'src/client',
plugins: [
{
bundle: true,
name: '@hey-api/client-fetch',
},
],
};
Examples ​
You can view live examples on StackBlitz.
Sponsors ​
Love Hey API? Become our sponsor.