Browse Source

feat: create interfaces for self imports in schematics

pull/5137/head
Arman Ozak 6 years ago
parent
commit
556150999b
  1. 3
      npm/ng-packs/packages/schematics/src/commands/api/index.ts
  2. 1
      npm/ng-packs/packages/schematics/src/models/model.ts
  3. 27
      npm/ng-packs/packages/schematics/src/utils/model.ts

3
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]));

1
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);

27
npm/ng-packs/packages/schematics/src/utils/model.ts

@ -21,36 +21,51 @@ export function createImportRefsToModelMapper(solution: string, types: Record<st
const reduceImportRefToImport = createImportRefToImportReducer(namespace);
const imports = importRefs.reduce((accumulatedImports, ref) => {
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<string, Type>) {
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);

Loading…
Cancel
Save