orval
orval copied to clipboard
Run prettier on generated code.
Prettier is already imported. I see no disadvantages to use it for generated code. That will correct all ugly linebreaks produced
- add
src/utils/formatter.ts
import Prettier from 'prettier';
export function prettifyTypescript(data){
return Prettier.format(data, {
parser: 'typescript',
"useTabs": false,
"tabWidth": 4,
"singleQuote": true,
"semi": true,
"printWidth": 100,
"trailingComma": "all",
"proseWrap": "never"
});
}
- apply
prettifyTypescript
in all writers before writing TS-code.
Prettier is use as dev dependency at the moment. That's a new feature that can be added but should be config in the orval config file. Also could be good to try to find a prettier config in the project and not only apply a custom config.
Have some concerns about using hardcoded config instead of flexible user config. But as a default option - definitly.
p.s. @anymaniax, btw have you checked https://openapi.tools ? Orval deserves to be in the list.
@Boorj I didn't know about https://openapi.tools could be interesting to add Orval there
@Boorj We should only use the hardcoded config when we cannot find the user config in its own project. And what I meant is to add a property in the orval config to activate prettier or override the config found or the default one.
I am having trouble with this feature. I have Orval and Prettier in my devDependencies
"orval": "^6.5.3",
"prettier": "^2.5.1",
When I run npm run codegen
i get this output..
⚠️ authorization - Prettier not found 🎉 authorization - Your OpenAPI spec has been converted into ready to use orval!
Probably because with this code it's only using the global installed prettier
hmmm i dont have prettier installed globally.
yep, it's a bug need to find a better solution for this
You can run prettier on pre-commit
hook. Probably another solution could be to add prettier as a peer dependency.
Also, I made a pull request #459 where you can run a script (prettier, lint, etc...) when client is generated by orval
.
This is easy too..
"scripts": {
"format": "prettier --write \"{src,__tests__}/**/*.{ts,tsx}\"",
"codegen": "orval && npm run format",
"start": "react-app-rewired start"