Skip to content

Commit 673bb55

Browse files
authoredApr 8, 2023
feat(packages): add option to filter generated endpoints (#810)
In the case orval is run on a system with a lot of endpoints, it can be useful to limit which endpoints are actually generated.
1 parent daf83a5 commit 673bb55

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed
 

‎docs/src/pages/reference/configuration/input.md

+21
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ module.exports = {
6363

6464
Example of transformer <a href="https://github.com/anymaniax/orval/blob/master/samples/basic/api/transformer/add-version.js" target="_blank">here</a>
6565

66+
### filters
67+
68+
Type: `Object`.
69+
70+
If specified, Orval only generates the endpoints after applying the filter.
71+
It is possible to filter on `tags`.
72+
73+
For instance the example below only generates the endpoints that contain the tag `pets`.
74+
75+
```js
76+
module.exports = {
77+
petstore: {
78+
input: {
79+
filters: {
80+
tags: ['pets'],
81+
},
82+
},
83+
},
84+
};
85+
```
86+
6687
### converterOptions
6788

6889
Type: `Object`.

‎packages/core/src/generators/verbs-options.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ContextSpecs,
1919
GeneratorVerbOptions,
2020
GeneratorVerbsOptions,
21+
NormalizedInputOptions,
2122
NormalizedOperationOptions,
2223
NormalizedOutputOptions,
2324
NormalizedOverrideOutput,
@@ -188,17 +189,19 @@ const generateVerbOptions = async ({
188189

189190
export const generateVerbsOptions = ({
190191
verbs,
192+
input,
191193
output,
192194
route,
193195
context,
194196
}: {
195197
verbs: PathItemObject;
198+
input: NormalizedInputOptions;
196199
output: NormalizedOutputOptions;
197200
route: string;
198201
context: ContextSpecs;
199202
}): Promise<GeneratorVerbsOptions> =>
200203
asyncReduce(
201-
Object.entries(verbs),
204+
filteredVerbs(verbs, input.filters),
202205
async (acc, [verb, operation]: [string, OperationObject]) => {
203206
if (isVerb(verb)) {
204207
const verbOptions = await generateVerbOptions({
@@ -217,3 +220,18 @@ export const generateVerbsOptions = ({
217220
},
218221
[] as GeneratorVerbsOptions,
219222
);
223+
224+
const filteredVerbs = (
225+
verbs: PathItemObject,
226+
filters: NormalizedInputOptions['filters'],
227+
) => {
228+
if (filters === undefined || filters.tags === undefined) {
229+
return Object.entries(verbs);
230+
}
231+
232+
return Object.entries(verbs).filter(
233+
([_verb, operation]: [string, OperationObject]) => {
234+
return operation.tags?.some((tag) => filters.tags?.includes(tag));
235+
},
236+
);
237+
};

‎packages/core/src/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ export type NormalizedInputOptions = {
137137
override: OverrideInput;
138138
converterOptions: swagger2openapi.Options;
139139
parserOptions: SwaggerParserOptions;
140+
filters?: {
141+
tags?: string[];
142+
};
140143
};
141144

142145
export type OutputClientFunc = (
@@ -169,6 +172,9 @@ export type InputOptions = {
169172
override?: OverrideInput;
170173
converterOptions?: swagger2openapi.Options;
171174
parserOptions?: SwaggerParserOptions;
175+
filters?: {
176+
tags?: string[];
177+
};
172178
};
173179

174180
export type OutputClient =

‎packages/orval/src/api.ts

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
GeneratorSchema,
88
getRoute,
99
isReference,
10+
NormalizedInputOptions,
1011
NormalizedOutputOptions,
1112
resolveRef,
1213
} from '@orval/core';
@@ -21,9 +22,11 @@ import {
2122
} from './client';
2223

2324
export const getApiBuilder = async ({
25+
input,
2426
output,
2527
context,
2628
}: {
29+
input: NormalizedInputOptions;
2730
output: NormalizedOutputOptions;
2831
context: ContextSpecs;
2932
}): Promise<GeneratorApiBuilder> => {
@@ -52,6 +55,7 @@ export const getApiBuilder = async ({
5255

5356
let verbsOptions = await generateVerbsOptions({
5457
verbs: resolvedVerbs,
58+
input,
5559
output,
5660
route,
5761
context: resolvedContext,

‎packages/orval/src/import-open-api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const importOpenApi = async ({
3333
const schemas = getApiSchemas({ output, target, workspace, specs });
3434

3535
const api = await getApiBuilder({
36+
input,
3637
output,
3738
context: {
3839
specKey: target,

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

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export const normalizeOptions = async (
103103
parserDefaultOptions,
104104
inputOptions.parserOptions ?? {},
105105
),
106+
filters: inputOptions.filters,
106107
},
107108
output: {
108109
target: globalOptions.output

1 commit comments

Comments
 (1)

vercel[bot] commented on Apr 8, 2023

@vercel[bot]
Please sign in to comment.