feat: collect interfaces from types based on importRefs

pull/5137/head
Arman Ozak 5 years ago
parent dc60ff57ff
commit e1959594a6

@ -20,14 +20,14 @@ export default function(params: GenerateProxySchema) {
const mapControllerToService = createControllerToServiceMapper(solution, definition.remoteServiceName);
const controllers = Object.values(definition.controllers || {});
const importRefs: Record<string, string[]> = {};
const serviceImports: Record<string, string[]> = {};
const createServiceFiles = chain(
controllers.map(controller => {
const service = mapControllerToService(controller);
service.imports.forEach(({refs, path}) => refs.forEach(ref => {
if (!importRefs[path]) return (importRefs[path] = [ref]);
importRefs[path] = [...new Set([...importRefs[path], ref])];
if (!serviceImports[path]) return (serviceImports[path] = [ref]);
serviceImports[path] = [...new Set([...serviceImports[path], ref])];
}));
return applyWithOverwrite(url('./files-service'), [
@ -42,10 +42,10 @@ export default function(params: GenerateProxySchema) {
),
);
const mapImportRefsToModel = createImportRefsToModelMapper(solution);
const mapImportRefsToModel = createImportRefsToModelMapper(solution, data.types);
const createModelFiles = chain(
Object.values(importRefs).map(refs => {
Object.values(serviceImports).map(refs => {
return applyWithOverwrite(url('./files-model'), [
applyTemplates({
...cases,

@ -1,10 +1,39 @@
import { Model } from '../models';
import { strings } from '@angular-devkit/core';
import { Interface, Model, Property, Type } from '../models';
import { parseNamespace } from './namespace';
import { createTypeSimplifier } from './type';
export function createImportRefsToModelMapper(solution: string, types: Record<string, Type>) {
const simplifyType = createTypeSimplifier(solution);
export function createImportRefsToModelMapper(solution: string) {
return (importRefs: string[]) => {
return new Model({
const model = new Model({
namespace: parseNamespace(solution, importRefs[0]),
});
importRefs.forEach(ref => {
const typeDef = types[ref];
let identifier = simplifyType(ref);
(typeDef.genericArguments ?? []).forEach((t, i) => {
identifier = identifier.replace(`T${i}`, t);
});
const base = typeDef.baseType ? simplifyType(typeDef.baseType) : null;
const _interface = new Interface({ identifier, base });
typeDef.properties?.forEach(({ name, typeSimple }) => {
name = strings.camelize(name);
const type = simplifyType(typeSimple);
const optional = typeSimple.endsWith('?') ? '?' : '';
_interface.properties.push(new Property({ name, type, optional }));
});
console.log(_interface);
model.interfaces.push(_interface);
});
return model;
};
}

Loading…
Cancel
Save