Browse Source

fix: enum type checks for the service proxy generation

pull/22037/head
sumeyye 1 year ago
parent
commit
6a947809b0
  1. 26
      npm/ng-packs/packages/schematics/src/utils/service.ts
  2. 10
      npm/ng-packs/packages/schematics/src/utils/type.ts

26
npm/ng-packs/packages/schematics/src/utils/service.ts

@ -18,6 +18,7 @@ import {
createTypeAdapter,
createTypeParser,
createTypesToImportsReducer,
getTypeForEnumList,
removeTypeModifiers,
} from './type';
import { eBindingSourceId } from '../enums';
@ -80,7 +81,19 @@ export function createActionToBodyMapper() {
const adaptType = createTypeAdapter();
return ({ httpMethod, parameters, returnValue, url }: Action) => {
const responseType = adaptType(returnValue.typeSimple);
let responseType = adaptType(returnValue.typeSimple);
if (responseType.includes('enum')) {
const type = returnValue.typeSimple.replace('enum', returnValue.type);
if (responseType === 'enum') {
responseType = adaptType(type);
}
if (responseType === 'enum[]') {
const normalizedType = getTypeForEnumList(type);
responseType = adaptType(normalizedType);
}
}
const responseTypeWithNamespace = returnValue.typeSimple;
const body = new Body({ method: httpMethod, responseType, url, responseTypeWithNamespace });
@ -109,7 +122,12 @@ export function createActionToSignatureMapper() {
if (isFormData || isFormArray) {
return new Property({ name: p.name, type: 'FormData' });
}
const type = adaptType(p.typeSimple);
let type = adaptType(p.typeSimple);
if (p.typeSimple === 'enum' || p.typeSimple === '[enum]') {
type = adaptType(p.type);
}
const parameter = new Property({ name: p.name, type });
parameter.setDefault(p.defaultValue);
parameter.setOptional(p.isOptional);
@ -183,7 +201,9 @@ function createActionToImportsReducer(
parseGenerics(paramType)
.toGenerics()
.forEach(type => {
if (types[type]) acc.push({ type, isEnum: types[type].isEnum });
if (types[type]) {
acc.push({ type, isEnum: types[type].isEnum });
}
}),
);

10
npm/ng-packs/packages/schematics/src/utils/type.ts

@ -58,6 +58,10 @@ export function removeTypeModifiers(type: string) {
return type.replace(/\[\]/g, '');
}
export function getTypeForEnumList(type: string) {
return type.replace(/^.*<([^>]+)>.*$/, '[$1]');
}
export function createTypesToImportsReducer(solution: string, namespace: string) {
const mapTypeToImport = createTypeToImportMapper(solution, namespace);
@ -68,7 +72,7 @@ export function createTypesToImportsReducer(solution: string, namespace: string)
return;
}
if(newImport.specifiers.some(f => f.toLocaleLowerCase() === type.toLocaleLowerCase())){
if (newImport.specifiers.some(f => f.toLocaleLowerCase() === type.toLocaleLowerCase())) {
return;
}
@ -76,7 +80,7 @@ export function createTypesToImportsReducer(solution: string, namespace: string)
({ keyword, path }) => keyword === newImport.keyword && path === newImport.path,
);
if (!existingImport){
if (!existingImport) {
return imports.push(newImport);
}
@ -101,7 +105,7 @@ export function createTypeToImportMapper(solution: string, namespace: string) {
const refs = [removeTypeModifiers(type)];
const specifiers = [adaptType(simplifyType(refs[0]).split('<')[0])];
let path = relativePathToModel(namespace, modelNamespace);
if (VOLO_REGEX.test(type)) {
path = '@abp/ng.core';
}

Loading…
Cancel
Save