Custom HTTP Client
To set up a custom HTTP client, add a mutator function to the configuration file, and setup a custom instance of your preferred HTTP client.
module.exports = {petstore: {output: {...override: {mutator: {path: './api/mutator/custom-instance.ts',name: 'customInstance',},},}...},};
// custom-instance.tsconst baseURL = '<BACKEND URL>'; // use your own URL here or environment variableexport const customInstance = async <T>(url: string,{method,params,body,}: {method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';params?: any;body?: BodyType<unknown>;responseType?: string;},): Promise<T> => {let targetUrl = `${baseURL}${url}`;if (params) {targetUrl += '?' + new URLSearchParams(params);}const response = await fetch(targetUrl, {method,body,});return response.json();};export default customInstance;// In some case with react-query and swr you want to be able to override the return error type so you can also do it here like thisexport type ErrorType<Error> = AxiosError<Error>;// In case you want to wrap the body type (optional)// (if the custom instance is processing data before sending it, like changing the case for example)export type BodyType<BodyData> = CamelCase<BodyType>;
Alternatively, refer to the using custom Fetch with Next.js sample app here.
Angular
The Angular client allows adding mutator functions to the configuration to set up your preferred HTTP client.
module.exports = {petstore: {output: {...override: {mutator: 'src/api/mutator/response-type.ts'}}...},};
// response-type.tsimport { HttpClient } from '@angular/common/http';import { Observable } from 'rxjs';const responseType = <Result>({url,method,params,data,}: {url: string;method: string;params?: any;data?: any;headers?: any;},http: HttpClient,): Observable<Result> =>http.request<Result>(method, url, {params,body: data,responseType: 'json',});export default responseType;
Refer to Angular sample app for more details.
https://github.com/orval-labs/orval/tree/master/samples/angular-app
Was this page helpful?