diff --git a/npm/ng-packs/packages/schematics/src/utils/index.ts b/npm/ng-packs/packages/schematics/src/utils/index.ts index a1d2b04a80..ee38b83dc1 100644 --- a/npm/ng-packs/packages/schematics/src/utils/index.ts +++ b/npm/ng-packs/packages/schematics/src/utils/index.ts @@ -9,4 +9,5 @@ export * from './service'; export * from './source'; export * from './text'; export * from './tree'; +export * from './type'; export * from './workspace'; diff --git a/npm/ng-packs/packages/schematics/src/utils/service.ts b/npm/ng-packs/packages/schematics/src/utils/service.ts index b9347eacc2..bec531de1a 100644 --- a/npm/ng-packs/packages/schematics/src/utils/service.ts +++ b/npm/ng-packs/packages/schematics/src/utils/service.ts @@ -3,6 +3,7 @@ import { Action, Body, Controller, Import, Method, Property, Service, Signature import { parseNamespace } from './namespace'; import { relativePathFromServiceToModel } from './path'; import { parseGenerics } from './tree'; +import { createTypeSimplifier } from './type'; export function serializeParameters(parameters: Property[]) { return parameters.map(p => p.name + p.optional + ': ' + p.type + p.default, '').join(', '); @@ -128,26 +129,12 @@ function getMethodNameFromAction(action: Action): string { } function createTypeAdapter(solution: string) { - const removeNamespace = createNamespaceRemover(solution); + const simplifyType = createTypeSimplifier(solution); return (typeSimple: string) => { if (typeSimple === 'System.Void') return 'void'; - return parseGenerics(typeSimple, node => removeNamespace(node.data)).toString(); - }; -} - -function createNamespaceRemover(solution: string) { - const optionalRegex = /\?/g; - const solutionRegex = new RegExp(solution.replace(/\./g, `\.`) + `\.`); - const voloRegex = /^Volo\.(Abp\.?)(Application\.?)/; - - return (type: string) => { - type = type.replace(voloRegex, ''); - type = type.replace(solutionRegex, ''); - type = type.replace(optionalRegex, ''); - type = type.split('.').pop()!; - return type; + return parseGenerics(typeSimple, node => simplifyType(node.data)).toString(); }; } diff --git a/npm/ng-packs/packages/schematics/src/utils/type.ts b/npm/ng-packs/packages/schematics/src/utils/type.ts new file mode 100644 index 0000000000..5af905cabb --- /dev/null +++ b/npm/ng-packs/packages/schematics/src/utils/type.ts @@ -0,0 +1,21 @@ +import { strings } from '@angular-devkit/core'; +import { SYSTEM_TYPES } from '../constants'; + +export function createTypeSimplifier(solution: string) { + const optionalRegex = /\?/g; + const solutionRegex = new RegExp(solution.replace(/\./g, `\.`) + `\.`); + const voloRegex = /^Volo\.(Abp\.?)(Application\.?)/; + + return (type: string) => { + type = type.replace(voloRegex, ''); + type = type.replace(solutionRegex, ''); + type = type.replace(optionalRegex, ''); + type = type.replace( + /System\.([0-9A-Za-z]+)/g, + (_, match) => SYSTEM_TYPES.get(match) ?? strings.camelize(match), + ); + type = type.split('.').pop()!; + type = type.startsWith('[') ? type.slice(1, -1) + '[]' : type; + return type; + }; +}