Skip to content

Commit 8a12c3d

Browse files
authoredOct 19, 2023
feat(orval): allow custom baseUrl for each OpenAPI specification (#968)
1 parent 9a4ce8f commit 8a12c3d

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed
 

‎docs/src/pages/guides/set-base-url.md

+55-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
## Use your own base url
22

3-
orval doesn't set any base url by default but you have multiple possibility to add it
3+
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.
4+
5+
```ts
6+
module.exports = {
7+
petstore: {
8+
output: {
9+
target: 'src/petstore.ts',
10+
baseUrl: '/api/v2',
11+
// baseUrl: 'https://petstore.swagger.io/v2',
12+
},
13+
},
14+
};
15+
```
16+
17+
It's also possible to configure the base url directly on your HTTP client instead.
418

519
### Axios
620

7-
You can set a default baseUrl
21+
You can set a default baseUrl for all requests:
822

923
```ts
10-
Axios.defaults.baseURL = '<BACKEND URL>'; // use your own URL here or environment variable
24+
axios.defaults.baseURL = '<BACKEND URL>'; // use your own URL here or environment variable
1125
```
1226

13-
You can also use an interceptor to do it
27+
You can also use an interceptor to do it:
1428

1529
```ts
1630
axios.interceptors.request.use((config) => {
@@ -21,16 +35,50 @@ axios.interceptors.request.use((config) => {
2135
});
2236
```
2337

24-
There is also the possibilty to create a custom axios instance
38+
There is also the possibility to create a custom axios instance. Check the [full guide](../guides/custom-axios.md) for more details.
2539

2640
```ts
27-
const AXIOS_INSTANCE = Axios.create({ baseURL: '<BACKEND URL>' }); // use your own URL here or environment variable
41+
const AXIOS_INSTANCE = axios.create({ baseURL: '<BACKEND URL>' }); // use your own URL here or environment variable
2842
```
2943

3044
### Angular http client
3145

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

48+
```ts
49+
import { Injectable } from '@angular/core';
50+
import {
51+
HttpEvent,
52+
HttpInterceptor,
53+
HttpHandler,
54+
HttpRequest,
55+
} from '@angular/common/http';
56+
import { Observable } from 'rxjs/Observable';
57+
58+
@Injectable()
59+
export class APIInterceptor implements HttpInterceptor {
60+
intercept(
61+
req: HttpRequest<any>,
62+
next: HttpHandler,
63+
): Observable<HttpEvent<any>> {
64+
const apiReq = req.clone({ url: `<BACKEND URL>/${req.url}` });
65+
return next.handle(apiReq);
66+
}
67+
}
68+
```
69+
70+
Also remember to add the interceptor to your providers in your module.
71+
72+
```ts
73+
providers: [
74+
{
75+
provide: HTTP_INTERCEPTORS,
76+
useClass: APIInterceptor,
77+
multi: true,
78+
},
79+
];
80+
```
81+
3482
### Other client
3583

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

‎packages/core/src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export type NormalizedOutputOptions = {
5252
packageJson?: PackageJson;
5353
headers: boolean;
5454
indexFiles: boolean;
55+
baseUrl?: string;
5556
};
5657

5758
export type NormalizedOverrideOutput = {
@@ -165,6 +166,7 @@ export type OutputOptions = {
165166
packageJson?: string;
166167
headers?: boolean;
167168
indexFiles?: boolean;
169+
baseUrl?: string;
168170
};
169171

170172
export type SwaggerParserOptions = Omit<SwaggerParser.Options, 'validate'> & {

‎packages/orval/src/api.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,18 @@ export const getApiBuilder = async ({
9595
[] as GeneratorSchema[],
9696
);
9797

98+
let fullRoute = route;
99+
if (output.baseUrl) {
100+
if (output.baseUrl.endsWith('/') && route.startsWith('/')) {
101+
fullRoute = route.slice(1);
102+
}
103+
fullRoute = `${output.baseUrl}${fullRoute}`;
104+
}
98105
const pathOperations = await generateOperations(
99106
output.client,
100107
verbsOptions,
101108
{
102-
route,
109+
route: fullRoute,
103110
pathRoute,
104111
override: output.override,
105112
context: resolvedContext,

‎packages/orval/src/utils/options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export const normalizeOptions = async (
121121
packageJson,
122122
headers: outputOptions.headers ?? false,
123123
indexFiles: outputOptions.indexFiles ?? true,
124+
baseUrl: outputOptions.baseUrl,
124125
override: {
125126
...outputOptions.override,
126127
mock: {

1 commit comments

Comments
 (1)

vercel[bot] commented on Oct 19, 2023

@vercel[bot]
Please sign in to comment.