Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(packages): add option to filter generated endpoints #810

Merged
merged 1 commit into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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