Skip to content

Commit 9ba6fd2

Browse files
authored
expand upon js api (#2641)
* expand upon js api * Make clear mutally exclusive options
1 parent 66fb1e6 commit 9ba6fd2

File tree

1 file changed

+182
-5
lines changed

1 file changed

+182
-5
lines changed

docs/src/pages/reference/integration.mdx

Lines changed: 182 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,191 @@ id: integration
33
title: Integration
44
---
55

6-
## Import script
6+
## Programmatic Usage
77

8-
Orval gives you the possibility to import Orval.
8+
Orval provides a powerful programmatic API that allows you to integrate code generation into your build processes, scripts, or tools.
99

10-
It expose a function which takes a path to the Orval config as the argument.
10+
### Basic Usage
1111

12-
```js
12+
#### Using the Default Export
13+
14+
```typescript
1315
import orval from 'orval';
1416

15-
orval('./orval.config.js');
17+
// Generate using a config file path
18+
await orval('./orval.config.js');
19+
20+
// Generate using current directory's orval config
21+
await orval();
22+
```
23+
24+
#### Using the Named Export
25+
26+
```typescript
27+
import { generate } from 'orval';
28+
29+
// Same functionality as default export
30+
await generate('./orval.config.js');
31+
```
32+
33+
### Advanced Usage
34+
35+
#### Direct Configuration Object
36+
37+
You can pass a configuration object directly instead of using a config file:
38+
39+
```typescript
40+
import { generate, type Options } from 'orval';
41+
42+
const config: Options = {
43+
input: {
44+
target: './api-spec.yaml',
45+
},
46+
output: {
47+
target: './src/api.ts',
48+
client: 'axios',
49+
},
50+
};
51+
52+
await generate(config);
53+
```
54+
55+
#### Global Options Override
56+
57+
Pass global options to override config file settings. See the complete [GlobalOptions interface](https://github.com/orval-labs/orval/blob/master/packages/core/src/types.ts#L718) for all available options.
58+
59+
```typescript
60+
import { generate, type GlobalOptions } from 'orval';
61+
62+
const globalOptions: GlobalOptions = {
63+
// File watching
64+
watch: true, // Enable watch mode (boolean)
65+
66+
// OR
67+
// watch: './specs/**/*.yaml', // Watch specific file pattern (string)
68+
69+
// OR
70+
// watch: ['./specs/*.yaml', './src/types.ts'], // Watch multiple patterns (string[])
71+
72+
// Output control
73+
clean: true, // Clean all output directories (boolean)
74+
75+
// OR
76+
// clean: ['./src/api', './types'], // Clean specific directories (string[])
77+
78+
output: './src/generated', // Override output directory
79+
80+
// Code formatting
81+
prettier: true, // Format with Prettier [Pretter Output Options](https://orval.dev/reference/configuration/output#prettier)
82+
biome: true, // Format with Biome [Biome Output Options](https://orval.dev/reference/configuration/output#biome)
83+
84+
// HTTP client configuration
85+
client: 'fetch', // Override HTTP client: 'axios' | 'angular' | 'fetch' | 'swr' | 'react-query' | 'vue-query' | 'svelte-query' [Client Output Options](https://orval.dev/reference/configuration/output#client)
86+
httpClient: 'fetch', // HTTP implementation: 'axios' | 'fetch' [HttpClient Output Options](https://orval.dev/reference/configuration/output#httpclient)
87+
88+
// Generation mode
89+
mode: 'split', // Output mode: 'single' | 'split' | 'tags' | 'tags-split'
90+
91+
// Mock data generation
92+
mock: true, // Enable mock data generation (boolean)
93+
94+
// OR
95+
// mock: { // Configure mock options (GlobalMockOptions)
96+
// type: 'msw',
97+
// delay: 1000,
98+
// },
99+
100+
// TypeScript configuration
101+
// Use EITHER a custom tsconfig path (string)...
102+
tsconfig: './tsconfig.json', // Custom tsconfig path (string)
103+
// ...OR an inline tsconfig object:
104+
// tsconfig: {
105+
// compilerOptions: {
106+
// target: 'ES2020',
107+
// esModuleInterop: true,
108+
// },
109+
// },
110+
111+
// Package configuration
112+
packageJson: './package.json', // Custom package.json path
113+
input: './api-spec.yaml', // Override input specification
114+
};
115+
116+
await generate('./orval.config.js', process.cwd(), globalOptions);
117+
```
118+
119+
#### Custom Workspace
120+
121+
Specify a custom workspace directory (default `process.cwd()`):
122+
123+
```typescript
124+
import { generate } from 'orval';
125+
126+
const workspace = '/path/to/your/project';
127+
await generate('./orval.config.js', workspace);
128+
```
129+
130+
### Function Signature
131+
132+
```typescript
133+
function generate(
134+
optionsExport?: string | OptionsExport,
135+
workspace?: string,
136+
options?: GlobalOptions,
137+
): Promise<void>
16138
```
139+
140+
#### Parameters
141+
142+
- **`optionsExport`** *(optional)*: Path to config file or configuration object
143+
- **`workspace`** *(optional)*: Working directory (defaults to `process.cwd()`)
144+
- **`options`** *(optional)*: Global options to override config settings
145+
146+
### Watch Mode
147+
148+
Enable file watching for automatic regeneration:
149+
150+
```typescript
151+
import { generate } from 'orval';
152+
153+
// Enable watch mode via global options
154+
await generate('./orval.config.js', process.cwd(), {
155+
watch: true
156+
});
157+
158+
// Watch specific files/directories
159+
await generate('./orval.config.js', process.cwd(), {
160+
watch: ['./specs/*.yaml', './src/custom-types.ts']
161+
});
162+
```
163+
164+
### Build Scripts Integration
165+
166+
#### npm/yarn scripts
167+
168+
```json
169+
{
170+
"scripts": {
171+
"generate": "node scripts/generate-api.js",
172+
"dev": "npm run generate && next dev",
173+
"build": "npm run generate && next build"
174+
}
175+
}
176+
```
177+
178+
```javascript
179+
// scripts/generate-api.js
180+
const { generate } = require('orval');
181+
182+
async function main() {
183+
try {
184+
await generate();
185+
console.log('API client generated successfully');
186+
} catch (error) {
187+
console.error('Failed to generate API client:', error);
188+
process.exit(1);
189+
}
190+
}
191+
192+
main();
193+
```

0 commit comments

Comments
 (0)