From 556150999b2e894b2af3fac4d6e3237def604c81 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Thu, 20 Aug 2020 09:51:28 +0300 Subject: [PATCH] feat: create interfaces for self imports in schematics --- .../schematics/src/commands/api/index.ts | 3 +-- .../packages/schematics/src/models/model.ts | 1 + .../packages/schematics/src/utils/model.ts | 27 ++++++++++++++----- 3 files changed, 23 insertions(+), 8 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 652ec60189..f18ee5985d 100644 --- a/npm/ng-packs/packages/schematics/src/commands/api/index.ts +++ b/npm/ng-packs/packages/schematics/src/commands/api/index.ts @@ -55,8 +55,7 @@ export default function(params: GenerateProxySchema) { }), move(normalize(targetPath)), ]); - } - ), + }), ); return branchAndMerge(chain([createServiceFiles, createModelFiles])); diff --git a/npm/ng-packs/packages/schematics/src/models/model.ts b/npm/ng-packs/packages/schematics/src/models/model.ts index 1f24b0b317..2318d6c181 100644 --- a/npm/ng-packs/packages/schematics/src/models/model.ts +++ b/npm/ng-packs/packages/schematics/src/models/model.ts @@ -17,6 +17,7 @@ export class Interface { base: string | null; identifier: string; properties: Property[] = []; + ref: string; constructor(options: InterfaceOptions) { Object.assign(this, options); diff --git a/npm/ng-packs/packages/schematics/src/utils/model.ts b/npm/ng-packs/packages/schematics/src/utils/model.ts index cadede7e6b..d02ec1db04 100644 --- a/npm/ng-packs/packages/schematics/src/utils/model.ts +++ b/npm/ng-packs/packages/schematics/src/utils/model.ts @@ -21,36 +21,51 @@ export function createImportRefsToModelMapper(solution: string, types: Record { - if (!types[ref].isEnum) model.interfaces.push(mapImportRefToInterface(ref)); + const interfaceDirect = mapImportRefToInterface(ref); + if (interfaceDirect && !types[ref].isEnum) model.interfaces.push(interfaceDirect); return reduceImportRefToImport(accumulatedImports, ref); }, []); sortImports(imports); + const selfPath = relativePathToModel(namespace, namespace); - imports.forEach(i => { - if (i.path === selfPath) return; - model.imports.push(i); + imports.forEach(_import => { + if (_import.path === selfPath) + return _import.refs.forEach(ref => { + if (model.interfaces.some(i => i.ref === ref)) return; + + const interfaceIndirect = mapImportRefToInterface(ref); + if (interfaceIndirect) model.interfaces.push(interfaceIndirect); + }); + + model.imports.push(_import); }); - model.interfaces.sort((a, b) => (a.identifier > b.identifier ? 1 : -1)); + sortInterfaces(model.interfaces); return model; }; } +function sortInterfaces(interfaces: Interface[]) { + interfaces.sort((a, b) => (a.identifier > b.identifier ? 1 : -1)); +} + export function createImportRefToInterfaceMapper(solution: string, types: Record) { const simplifyType = createTypeSimplifier(solution); return (ref: string) => { const typeDef = types[ref]; + if (!typeDef) return; + const identifier = (typeDef.genericArguments ?? []).reduce( (acc, t, i) => acc.replace(`T${i}`, t), simplifyType(ref), ); const base = typeDef.baseType ? simplifyType(typeDef.baseType) : null; - const _interface = new Interface({ identifier, base }); + const _interface = new Interface({ identifier, base, ref }); typeDef.properties?.forEach(({ name, typeSimple }) => { name = strings.camelize(name);