Replies: 1 comment
-
In case anyone finds this useful. I decided to modify the generated output for the axios client so that I could just create ad-hoc client instances anywhere I need to:
import { createApiClient } from './generated.ts';
const client = createApiClient({ baseURL: 'url'});
const res = await client.healthCheck();
client.axios.defaults.baseURL = 'other url'; // access to axios instance I used this config: import { builder, generateAxiosFooter } from '@orval/axios';
const config = {
output: {
mode: 'single',
workspace: './',
target: 'generated.ts',
indexFiles: false,
clean: false,
client: () => {
const axiosBuilder = builder({ type: 'axios' })();
return {
...axiosBuilder,
dependencies: () => {
// https://github.com/orval-labs/orval/blob/a154264719ccc49b3ab95dadbb3d62513110d8c3/packages/axios/src/index.ts#L22
return [
{
exports: [
{
name: 'Axios',
default: true,
values: true,
syntheticDefaultImport: true,
},
{ name: 'AxiosRequestConfig' },
{ name: 'AxiosResponse' },
{ name: 'CreateAxiosDefaults' },
],
dependency: 'axios',
},
];
},
header: () => {
return `
export const createApiClient = (config?: CreateAxiosDefaults) => {
const axios = Axios.create(config);
`;
},
footer: (params) => {
const result = generateAxiosFooter(params);
return result.replace(
/return {(.+?)}/,
(_, captured) => `return {${captured}, axios}`,
);
},
};
},
},
input: {
target: './openapi.json',
},
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
What are the steps to reproduce this issue?
Setup a custom instance like so:
We attempted to DRY the declarations here as we have multiple clients.
What happens?
Running the generate Orval CLI generator will result in a
Strictly speaking, this isn't necessarily true. A function of this name is in fact exported and it works when running the application. The issue is in how the function is declared. During generation, the script isn't able to determine the following variables
numberOfParams
andreturnNumberOfParams
when this kind of a factory pattern is used.The processing of the AST result fails because
parseFunction
assumes there to be a body when in the above case the value is aCallExpression
which doesn't have a body (link is to relevant line in the source).The error is:
Caused by this line
The error can be avoided like so:
This pattern doesn't allow us to DRY up the declaration of the custom instances. It's not an insurmountable issue.
What were you expecting to happen?
I assume that the a factory pattern won't ever be supported as it would require too much processing of the AST result. But before understanding that the implementation relied on AST, it was easy to assume that any JS compatible implementation would work as long as it abided by the API that was advised in the documentation.
Something that could be worth consideration is to amend the documentation or the error message to more clearly communicate the supported patterns.
Alternatively it's possible that there's some other way to DRY up the code we are missing. In that case some sort of sample that allows DRYness when dealing with multiple custom clients could be interesting.
Beta Was this translation helpful? Give feedback.
All reactions