Use your own base url

Orval allows you to set a custom base url for each OpenAPI specification. This can be a part of the url that's been omitted from the specification or the entire url. You can also configure Orval to read the servers field in the specification.

module.exports = {
petstore: {
output: {
target: 'src/petstore.ts',
baseUrl: '/api/v2',
// baseUrl: 'https://petstore.swagger.io/v2',
},
},
};

Using the url from the OpenAPI specification

If you want to use the url defined in your specification, described by the servers field, you can set getBaseUrlFromSpecification to true, which will make Orval correctly identify which base url to use for each operation. Read more about the different settings in the output configuration.

module.exports = {
petstore: {
output: {
target: 'src/petstore.ts',
baseUrl: {
getBaseUrlFromSpecification: true,
variables: {
environment: 'api.dev',
},
},
},
},
};

It's also possible to configure the base url directly on your HTTP client instead.

Axios

You can set a default baseUrl for all requests:

axios.defaults.baseURL = '<BACKEND URL>'; // use your own URL here or environment variable

You can also use an interceptor to do it:

axios.interceptors.request.use((config) => {
return {
...config,
baseURL: '<BACKEND URL>', // use your own URL here or environment variable
};
});

There is also the possibility to create a custom axios instance. Check the full guide for more details.

const AXIOS_INSTANCE = axios.create({ baseURL: '<BACKEND URL>' }); // use your own URL here or environment variable

Fetch client

Also, if you are using the fetch client, you can still set the request URL with the custom fetch client.

const getUrl = (contextUrl: string): string => {
const url = new URL(contextUrl);
const pathname = url.pathname;
const search = url.search;
const baseUrl =
process.env.NODE_ENV === 'production'
? 'productionBaseUrl'
: 'http://localhost:3000';
const requestUrl = new URL(`${baseUrl}${pathname}${search}`);
return requestUrl.toString();
};
export const customFetch = async <T>(
url: string,
options: RequestInit,
): Promise<T> => {
const requestUrl = getUrl(url);
const requestInit: RequestInit = {
...options,
};
const request = new Request(requestUrl, requestInit);
const response = await fetch(request);
const data = await getBody<T>(response);
return { status: response.status, data } as T;
};

Please refer to the complete sample here

Angular http client

You can use an interceptor to automatically add the url of your API. Like you would do to add an authorization header.

import { Injectable } from '@angular/core';
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class APIInterceptor implements HttpInterceptor {
intercept(
req: HttpRequest<any>,
next: HttpHandler,
): Observable<HttpEvent<any>> {
const apiReq = req.clone({ url: `<BACKEND URL>/${req.url}` });
return next.handle(apiReq);
}
}

Also remember to add the interceptor to your providers in your module.

providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: APIInterceptor,
multi: true,
},
];

Other client

Depending on the client that you are using, you will need to add it by yourself

Was this page helpful?

Copyright © 2024 Victor Bury. All Rights Reserved.