Skip to content

Commit 81a75eb

Browse files
SH-ExactDaniel Knogl
authored andcommitted
fix: handle '{}' filter parameter type (#43)
1 parent 0992da5 commit 81a75eb

8 files changed

Lines changed: 61 additions & 32 deletions

File tree

src/generator/03-entities/index.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { generateInterface, InterfaceProperty } from '@ts/generateInterface';
44
import { generateStatements } from '@ts/generateStatements';
55
import { convertToTypeScriptType } from '@utils/openapi/convertToTypeScriptType';
66
import {
7-
isEnumSchemaObject, isFilterPathsSchemaObject, isFilterPropertySchemaObject,
7+
isEnumSchemaObject,
8+
isFilterPathsSchemaObject,
9+
isFilterPropertySchemaObject,
810
isNonArraySchemaObject,
911
isObjectSchemaObject,
1012
isReferenceObject,
@@ -49,7 +51,10 @@ export const generateEntities = (
4951
const filterableInterfaceProperties: InterfaceProperty[] = [];
5052
const properties = new Map<string, PropertyMetaData>();
5153

52-
const processProperties = (props: Record<string, OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject> = {}, isXweclappFilterProp?: boolean) => {
54+
const processProperties = (
55+
props: Record<string, OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject> = {},
56+
isXweclappFilterProp?: boolean
57+
) => {
5358
for (const [name, property] of Object.entries(props)) {
5459
const meta = isRelatedEntitySchema(property) ? property['x-weclapp'] : {};
5560
const type = convertToTypeScriptType(property, name).toString();
@@ -61,11 +66,11 @@ export const generateEntities = (
6166
: undefined
6267
: undefined;
6368

64-
if(meta.filterable !== false) {
69+
if (meta.filterable !== false) {
6570
filterableInterfaceProperties.push({ name, type });
6671
}
6772

68-
if(!isXweclappFilterProp) {
73+
if (!isXweclappFilterProp) {
6974
entityInterfaceProperties.push({
7075
name,
7176
type,
@@ -87,15 +92,15 @@ export const generateEntities = (
8792
} else if (isObjectSchemaObject(item)) {
8893
processProperties(item.properties);
8994

90-
if(isFilterPropertySchemaObject(item)) {
95+
if (isFilterPropertySchemaObject(item)) {
9196
processProperties(item['x-weclapp-filterProperties'], true);
9297
}
9398

94-
if(isFilterPathsSchemaObject(item)) {
99+
if (isFilterPathsSchemaObject(item)) {
95100
const fPaths: Record<string, string> = item['x-weclapp-filterPaths'];
96-
for(const path in fPaths) {
97-
if(!path.includes('.')) {
98-
filterableInterfaceProperties.push({name: path, type: fPaths[path]});
101+
for (const path in fPaths) {
102+
if (!path.includes('.')) {
103+
filterableInterfaceProperties.push({ name: path, type: fPaths[path] });
99104
}
100105
}
101106
}
@@ -108,7 +113,9 @@ export const generateEntities = (
108113
entities.set(schemaName, {
109114
name: schemaName,
110115
properties,
111-
filterableInterfaceProperties: filterableInterfaceProperties.sort((propA, propB) => propA.name.localeCompare(propB.name)),
116+
filterableInterfaceProperties: filterableInterfaceProperties.sort((propA, propB) =>
117+
propA.name.localeCompare(propB.name)
118+
),
112119
parentName: parentEntityInterfaceName ? camelCase(parentEntityInterfaceName) : undefined,
113120
source: generateStatements(
114121
generateInterface(entityInterfaceName, entityInterfaceProperties, parentEntityInterfaceName)
@@ -119,32 +126,41 @@ export const generateEntities = (
119126
return entities;
120127
};
121128

122-
export const generateEntityFilterProps = (entities: Map<string, GeneratedEntity>, enums: Map<string, GeneratedEnum>): Map<string, GeneratedEntityFilterProp> => {
129+
export const generateEntityFilterProps = (
130+
entities: Map<string, GeneratedEntity>,
131+
enums: Map<string, GeneratedEnum>
132+
): Map<string, GeneratedEntityFilterProp> => {
123133
const entityFilterProps: Map<string, GeneratedEntityFilterProp> = new Map();
124134

125135
const transformFilterProps = (props: InterfaceProperty[]) => {
126136
return props.map((prop) => {
127-
if(!prop.type || enums.has(prop.type) || prop.type === 'string' || prop.type === 'number' || prop.type === 'boolean' || prop.type.endsWith('[]')) {
137+
if (
138+
!prop.type ||
139+
enums.has(prop.type) ||
140+
prop.type === 'string' ||
141+
prop.type === 'number' ||
142+
prop.type === 'boolean' ||
143+
prop.type.endsWith('[]') ||
144+
prop.type === '{}'
145+
) {
128146
return prop;
129147
}
130148

131-
return {...prop, type: `${pascalCase(prop.type)}_${FILTER_PROPS_SUFFIX}`}
132-
})
133-
}
149+
return { ...prop, type: `${pascalCase(prop.type)}_${FILTER_PROPS_SUFFIX}` };
150+
});
151+
};
134152

135153
entities.forEach((entity, name) => {
136-
const entityFilterName = `${pascalCase(name)}_${FILTER_PROPS_SUFFIX}`
154+
const entityFilterName = `${pascalCase(name)}_${FILTER_PROPS_SUFFIX}`;
137155
const parentName = entity.parentName ? `${pascalCase(entity.parentName)}_${FILTER_PROPS_SUFFIX}` : undefined;
138156
const filterableInterfaceProperties = transformFilterProps(entity.filterableInterfaceProperties);
139157

140158
entityFilterProps.set(entityFilterName, {
141159
name: entityFilterName,
142160
parentName,
143-
source: generateStatements(
144-
generateInterface(entityFilterName, filterableInterfaceProperties, parentName)
145-
)
161+
source: generateStatements(generateInterface(entityFilterName, filterableInterfaceProperties, parentName))
146162
});
147163
});
148164

149165
return entityFilterProps;
150-
}
166+
};

src/generator/04-services/endpoints/count.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { generateString } from '@ts/generateString';
77
import { convertParametersToSchema } from '@utils/openapi/convertParametersToSchema';
88
import { convertToTypeScriptType, createObjectType } from '@utils/openapi/convertToTypeScriptType';
99
import { pascalCase } from 'change-case';
10-
import { FILTER_PROPS_SUFFIX } from "@generator/03-entities";
10+
import { FILTER_PROPS_SUFFIX } from '@generator/03-entities';
1111

1212
export const generateCountEndpoint: ServiceFunctionGenerator = ({
1313
aliases,
@@ -31,7 +31,8 @@ export const generateCountEndpoint: ServiceFunctionGenerator = ({
3131
const functionTypeSource = generateArrowFunctionType({
3232
type: functionTypeName,
3333
params: [
34-
`query${parametersType.isFullyOptional() ? '?' : ''}: CountQuery<${filterTypeName}>${path.parameters?.length ? ' & ' + parametersTypeName : ''}`, 'requestOptions?: RequestOptions'
34+
`query${parametersType.isFullyOptional() ? '?' : ''}: CountQuery<${filterTypeName}>${path.parameters?.length ? ' & ' + parametersTypeName : ''}`,
35+
'requestOptions?: RequestOptions'
3536
],
3637
returns: `${resolveResponseType(target)}<number>`
3738
});

src/generator/04-services/endpoints/generic.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ export const generateGenericEndpoint =
3939

4040
const functionTypeSource = generateArrowFunctionType({
4141
type: functionTypeName,
42-
params: [...(hasId ? ['id: string'] : []), `query${params.isFullyOptional() ? '?' : ''}: ${entityQuery}`, 'requestOptions?: RequestOptions'],
42+
params: [
43+
...(hasId ? ['id: string'] : []),
44+
`query${params.isFullyOptional() ? '?' : ''}: ${entityQuery}`,
45+
'requestOptions?: RequestOptions'
46+
],
4347
returns: `${resolveResponseType(target)}<${wrapBody(responseBody, target).toString()}>`
4448
});
4549

src/generator/04-services/endpoints/update.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ export const generateUpdateEndpoint: ServiceFunctionGenerator = ({
1717

1818
const functionTypeSource = generateArrowFunctionType({
1919
type: functionTypeName,
20-
params: ['id: string', `data: DeepPartial<${generateRequestBodyType(path).toString()}>`, 'options?: UpdateQuery', 'requestOptions?: RequestOptions'],
20+
params: [
21+
'id: string',
22+
`data: DeepPartial<${generateRequestBodyType(path).toString()}>`,
23+
'options?: UpdateQuery',
24+
'requestOptions?: RequestOptions'
25+
],
2126
returns: `${resolveResponseType(target)}<${generateResponseBodyType(path).toString()}>`
2227
});
2328

src/generator/04-services/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ export const generateServices = (
7979
for (const { path, endpoint } of paths) {
8080
const generator = generators[endpoint.type];
8181
for (const [method, config] of Object.entries(path)) {
82-
if ((method === 'get' && endpoint.type === WeclappEndpointType.ENTITY && !options.generateUnique)
83-
|| (method === 'post' && (endpoint.type === WeclappEndpointType.COUNT || endpoint.path.endsWith('query')))) {
82+
if (
83+
(method === 'get' && endpoint.type === WeclappEndpointType.ENTITY && !options.generateUnique) ||
84+
(method === 'post' && (endpoint.type === WeclappEndpointType.COUNT || endpoint.path.endsWith('query')))
85+
) {
8486
// Skip unique endpoints if generateUnique option is not set or if POST is used for filter queries
8587
continue;
8688
}

src/generator/generate.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ export const generate = (doc: OpenAPIV3.Document, options: GeneratorOptions): st
3232
return generateStatements(
3333
generateBase(options.target, doc.info.version, options),
3434
generateBlockComment('ENUMS', generateStatements(...[...enums.values()].map((v) => v.source))),
35-
generateBlockComment('ENTITY FILTER PROPS', generateStatements(...[...entityFilterProps.values()].map((v) => v.source))),
35+
generateBlockComment(
36+
'ENTITY FILTER PROPS',
37+
generateStatements(...[...entityFilterProps.values()].map((v) => v.source))
38+
),
3639
generateBlockComment('ENTITIES', generateStatements(...[...entities.values()].map((v) => v.source))),
3740
generateBlockComment('SERVICES', generateStatements(...[...services.values()].map((v) => v.source))),
3841
generateBlockComment('MAPS', generateStatements(maps.source))

src/ts/generateInterface.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ export const generateInterfaceType = (
4949
const bases = extend ? arrayify(extend).join(' & ') : undefined;
5050

5151
let typeDefinition = '';
52-
if(bases) {
52+
if (bases) {
5353
typeDefinition = bases;
54-
}
55-
else {
54+
} else {
5655
typeDefinition = body;
5756
}
58-
if(bases && body !== '{}') {
57+
if (bases && body !== '{}') {
5958
typeDefinition += ` & ${body}`;
6059
}
6160

src/utils/openapi/guards.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export const isFilterPropertySchemaObject = (v: any): v is WeclappSchemaObject =
5151
return isObject(v) && v.type === 'object' && isObject(v['x-weclapp-filterProperties']);
5252
};
5353

54-
5554
export interface WeclappMetaProperties {
5655
entity?: string;
5756
service?: string;

0 commit comments

Comments
 (0)