From 253368fc5c9a095711fd75a9ee62daf601606187 Mon Sep 17 00:00:00 2001 From: masumulu28 Date: Thu, 20 Jun 2024 14:54:31 +0300 Subject: [PATCH] add: self generic prop resolver for types from proxy config --- .../schematics/src/commands/api/index.ts | 3 +++ .../packages/schematics/src/utils/generics.ts | 6 ++--- .../packages/schematics/src/utils/model.ts | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/commands/api/index.ts b/npm/ng-packs/packages/schematics/src/commands/api/index.ts index c04ca1d433..249f80cd65 100644 --- a/npm/ng-packs/packages/schematics/src/commands/api/index.ts +++ b/npm/ng-packs/packages/schematics/src/commands/api/index.ts @@ -29,6 +29,7 @@ import { sanitizeTypeNames, sanitizeControllerTypeNames, serializeParameters, + resolveSelfGenericProps, } from '../../utils'; import * as cases from '../../utils/text'; @@ -49,6 +50,8 @@ export default function (schema: GenerateProxySchema) { const data = readProxyConfig(tree); data.types = sanitizeTypeNames(data.types); + resolveSelfGenericProps({ solution, types: data.types }); + const types = data.types; const modules = data.modules; const serviceType = schema.serviceType || defaultEServiceType; diff --git a/npm/ng-packs/packages/schematics/src/utils/generics.ts b/npm/ng-packs/packages/schematics/src/utils/generics.ts index 96376900e7..c6dfffbe18 100644 --- a/npm/ng-packs/packages/schematics/src/utils/generics.ts +++ b/npm/ng-packs/packages/schematics/src/utils/generics.ts @@ -72,7 +72,7 @@ export function extractSimpleGenerics(sourceType: string) { return { identifier: getLastSegment(identifier), generics: generics.map(getLastSegment), - array + array, }; } @@ -80,10 +80,10 @@ export function extractGenerics(sourceType: string) { const isArray = /\[\]$/.test(sourceType); const regex = /(?[^<]+)(<(?.+)>)?/g; const { identifier = '', generics = '' } = regex.exec(sourceType)?.groups ?? {}; - return { + return { identifier, generics: generics.split(/,\s*/).filter(Boolean), - array: isArray ? '[]':'' + array: isArray ? '[]' : '', }; } diff --git a/npm/ng-packs/packages/schematics/src/utils/model.ts b/npm/ng-packs/packages/schematics/src/utils/model.ts index 52b9b36be0..720a76b1b3 100644 --- a/npm/ng-packs/packages/schematics/src/utils/model.ts +++ b/npm/ng-packs/packages/schematics/src/utils/model.ts @@ -201,3 +201,27 @@ export function parseBaseTypeWithGenericTypes(type: string): string[] { return nodeToText(parsedTypeNode); } + +export function resolveSelfGenericProps(params: Partial) { + const { types, solution } = params; + if (!types || !solution) { + return; + } + + Object.keys(types) + .filter(f => f.startsWith(solution)) + .forEach(key => { + const type = types[key]; + if (type.genericArguments?.length) { + type.properties?.map(prop => { + if (prop.type.includes('<>')) { + prop.type = prop.type.replace('<>', `<${type.genericArguments!.join(', ')}>`); + prop.typeSimple = prop.typeSimple.replace( + '<>', + `<${type.genericArguments!.join(', ')}>`, + ); + } + }); + } + }); +}