Skip to content

Commit

Permalink
feat(packages): add option to filter generated endpoints (orval-labs#810
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
yesyesufcurs authored Apr 8, 2023
1 parent daf83a5 commit 673bb55
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
21 changes: 21 additions & 0 deletions docs/src/pages/reference/configuration/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ module.exports = {

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

### filters

Type: `Object`.

If specified, Orval only generates the endpoints after applying the filter.
It is possible to filter on `tags`.

For instance the example below only generates the endpoints that contain the tag `pets`.

```js
module.exports = {
petstore: {
input: {
filters: {
tags: ['pets'],
},
},
},
};
```

### converterOptions

Type: `Object`.
Expand Down
20 changes: 19 additions & 1 deletion packages/core/src/generators/verbs-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ContextSpecs,
GeneratorVerbOptions,
GeneratorVerbsOptions,
NormalizedInputOptions,
NormalizedOperationOptions,
NormalizedOutputOptions,
NormalizedOverrideOutput,
Expand Down Expand Up @@ -188,17 +189,19 @@ const generateVerbOptions = async ({

export const generateVerbsOptions = ({
verbs,
input,
output,
route,
context,
}: {
verbs: PathItemObject;
input: NormalizedInputOptions;
output: NormalizedOutputOptions;
route: string;
context: ContextSpecs;
}): Promise<GeneratorVerbsOptions> =>
asyncReduce(
Object.entries(verbs),
filteredVerbs(verbs, input.filters),
async (acc, [verb, operation]: [string, OperationObject]) => {
if (isVerb(verb)) {
const verbOptions = await generateVerbOptions({
Expand All @@ -217,3 +220,18 @@ export const generateVerbsOptions = ({
},
[] as GeneratorVerbsOptions,
);

const filteredVerbs = (
verbs: PathItemObject,
filters: NormalizedInputOptions['filters'],
) => {
if (filters === undefined || filters.tags === undefined) {
return Object.entries(verbs);
}

return Object.entries(verbs).filter(
([_verb, operation]: [string, OperationObject]) => {
return operation.tags?.some((tag) => filters.tags?.includes(tag));
},
);
};
6 changes: 6 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ export type NormalizedInputOptions = {
override: OverrideInput;
converterOptions: swagger2openapi.Options;
parserOptions: SwaggerParserOptions;
filters?: {
tags?: string[];
};
};

export type OutputClientFunc = (
Expand Down Expand Up @@ -169,6 +172,9 @@ export type InputOptions = {
override?: OverrideInput;
converterOptions?: swagger2openapi.Options;
parserOptions?: SwaggerParserOptions;
filters?: {
tags?: string[];
};
};

export type OutputClient =
Expand Down
4 changes: 4 additions & 0 deletions packages/orval/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GeneratorSchema,
getRoute,
isReference,
NormalizedInputOptions,
NormalizedOutputOptions,
resolveRef,
} from '@orval/core';
Expand All @@ -21,9 +22,11 @@ import {
} from './client';

export const getApiBuilder = async ({
input,
output,
context,
}: {
input: NormalizedInputOptions;
output: NormalizedOutputOptions;
context: ContextSpecs;
}): Promise<GeneratorApiBuilder> => {
Expand Down Expand Up @@ -52,6 +55,7 @@ export const getApiBuilder = async ({

let verbsOptions = await generateVerbsOptions({
verbs: resolvedVerbs,
input,
output,
route,
context: resolvedContext,
Expand Down
1 change: 1 addition & 0 deletions packages/orval/src/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const importOpenApi = async ({
const schemas = getApiSchemas({ output, target, workspace, specs });

const api = await getApiBuilder({
input,
output,
context: {
specKey: target,
Expand Down
1 change: 1 addition & 0 deletions packages/orval/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const normalizeOptions = async (
parserDefaultOptions,
inputOptions.parserOptions ?? {},
),
filters: inputOptions.filters,
},
output: {
target: globalOptions.output
Expand Down

0 comments on commit 673bb55

Please sign in to comment.