Skip to content

Commit d294285

Browse files
authored
refactor: add content type filter function (orval-labs#2907)
1 parent 7c375de commit d294285

File tree

4 files changed

+52
-35
lines changed

4 files changed

+52
-35
lines changed

packages/core/src/getters/body.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
OpenApiRequestBodyObject,
88
OverrideOutputContentType,
99
} from '../types';
10-
import { camel, isReference, sanitize } from '../utils';
10+
import { camel, filterByContentType, isReference, sanitize } from '../utils';
1111
import { getResReqTypes } from './res-req-types';
1212

1313
interface GetBodyOptions {
@@ -29,22 +29,7 @@ export function getBody({
2929
context,
3030
);
3131

32-
const filteredBodyTypes = contentType
33-
? allBodyTypes.filter((type) => {
34-
let include = true;
35-
let exclude = false;
36-
37-
if (contentType.include) {
38-
include = contentType.include.includes(type.contentType);
39-
}
40-
41-
if (contentType.exclude) {
42-
exclude = contentType.exclude.includes(type.contentType);
43-
}
44-
45-
return include && !exclude;
46-
})
47-
: allBodyTypes;
32+
const filteredBodyTypes = filterByContentType(allBodyTypes, contentType);
4833

4934
const imports = filteredBodyTypes.flatMap(({ imports }) => imports);
5035
const schemas = filteredBodyTypes.flatMap(({ schemas }) => schemas);

packages/core/src/getters/response.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
OverrideOutputContentType,
66
ResReqTypesValue,
77
} from '../types';
8-
import { dedupeUnionType } from '../utils';
8+
import { dedupeUnionType, filterByContentType } from '../utils';
99
import { getResReqTypes } from './res-req-types';
1010

1111
interface GetResponseOptions {
@@ -43,22 +43,7 @@ export function getResponse({
4343
(type) => `${type.key}-${type.value}`,
4444
);
4545

46-
const filteredTypes = contentType
47-
? types.filter((type) => {
48-
let include = true;
49-
let exclude = false;
50-
51-
if (contentType.include) {
52-
include = contentType.include.includes(type.contentType);
53-
}
54-
55-
if (contentType.exclude) {
56-
exclude = contentType.exclude.includes(type.contentType);
57-
}
58-
59-
return include && !exclude;
60-
})
61-
: types;
46+
const filteredTypes = filterByContentType(types, contentType);
6247

6348
const imports = filteredTypes.flatMap(({ imports }) => imports);
6449
const schemas = filteredTypes.flatMap(({ schemas }) => schemas);

packages/core/src/utils/content-type.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,50 @@ export function getFormDataFieldFileType(
7878

7979
return undefined;
8080
}
81+
82+
/**
83+
* Filter configuration for content types
84+
*/
85+
export interface ContentTypeFilter {
86+
include?: string[];
87+
exclude?: string[];
88+
}
89+
90+
/**
91+
* Filters items by content type based on include/exclude rules
92+
*
93+
* @param items - Array of items with contentType property
94+
* @param filter - Optional filter configuration
95+
* @returns Filtered array
96+
*
97+
* @example
98+
* ```ts
99+
* const types = [
100+
* { contentType: 'application/json', value: '...' },
101+
* { contentType: 'text/xml', value: '...' }
102+
* ];
103+
*
104+
* // Include only JSON
105+
* filterByContentType(types, { include: ['application/json'] });
106+
*
107+
* // Exclude XML
108+
* filterByContentType(types, { exclude: ['text/xml'] });
109+
* ```
110+
*/
111+
export function filterByContentType<T extends { contentType: string }>(
112+
items: T[],
113+
filter?: ContentTypeFilter,
114+
): T[] {
115+
if (!filter) {
116+
return items;
117+
}
118+
119+
return items.filter((item) => {
120+
const shouldInclude =
121+
!filter.include || filter.include.includes(item.contentType);
122+
123+
const shouldExclude = filter.exclude?.includes(item.contentType) ?? false;
124+
125+
return shouldInclude && !shouldExclude;
126+
});
127+
}

packages/core/src/utils/merge-deep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const isObject = (obj: unknown) => obj && typeof obj === 'object';
1+
import { isObject } from './assertion';
22

33
export function mergeDeep<
44
T extends Record<string, any>,

0 commit comments

Comments
 (0)