Search in sources :

Example 1 with TsProperty

use of cz.habarta.typescript.generator.TsProperty in project typescript-generator by vojtechhabarta.

the class ModelCompiler method createRestClients.

private void createRestClients(TsModel tsModel, SymbolTable symbolTable, List<RestApplicationModel> restApplications, Symbol responseSymbol, TsType.GenericVariableType optionsGenericVariable, TsType optionsType) {
    final Symbol httpClientSymbol = symbolTable.getSyntheticSymbol("HttpClient");
    final List<TsType.GenericVariableType> typeParameters = Utils.listFromNullable(optionsGenericVariable);
    // HttpClient interface
    final TsType.GenericVariableType returnGenericVariable = new TsType.GenericVariableType("R");
    tsModel.getBeans().add(new TsBeanModel(null, TsBeanCategory.ServicePrerequisite, false, httpClientSymbol, typeParameters, null, null, null, null, null, Arrays.asList(new TsMethodModel("request", TsModifierFlags.None, Arrays.asList(returnGenericVariable), Arrays.asList(new TsParameterModel("requestConfig", new TsType.ObjectType(new TsProperty("method", TsType.String), new TsProperty("url", TsType.String), new TsProperty("queryParams", new TsType.OptionalType(TsType.Any)), new TsProperty("data", new TsType.OptionalType(TsType.Any)), new TsProperty("copyFn", new TsType.OptionalType(new TsType.FunctionType(Arrays.asList(new TsParameter("data", returnGenericVariable)), returnGenericVariable))), optionsType != null ? new TsProperty("options", new TsType.OptionalType(optionsType)) : null))), new TsType.GenericReferenceType(responseSymbol, returnGenericVariable), null, null)), null));
    // application client classes
    final TsType.ReferenceType httpClientType = optionsGenericVariable != null ? new TsType.GenericReferenceType(httpClientSymbol, optionsGenericVariable) : new TsType.ReferenceType(httpClientSymbol);
    final TsConstructorModel constructor = new TsConstructorModel(TsModifierFlags.None, Arrays.asList(new TsParameterModel(TsAccessibilityModifier.Protected, "httpClient", httpClientType)), Collections.<TsStatement>emptyList(), null);
    final boolean bothInterfacesAndClients = settings.generateJaxrsApplicationInterface || settings.generateSpringApplicationInterface;
    final String groupingSuffix = bothInterfacesAndClients ? null : "Client";
    final Map<Symbol, List<TsMethodModel>> groupedMethods = processRestMethods(tsModel, restApplications, symbolTable, groupingSuffix, responseSymbol, optionsType, true);
    for (Map.Entry<Symbol, List<TsMethodModel>> entry : groupedMethods.entrySet()) {
        final Symbol symbol = bothInterfacesAndClients ? symbolTable.addSuffixToSymbol(entry.getKey(), "Client") : entry.getKey();
        final TsType interfaceType = bothInterfacesAndClients ? new TsType.ReferenceType(entry.getKey()) : null;
        final TsBeanModel clientModel = new TsBeanModel(null, TsBeanCategory.Service, true, symbol, typeParameters, null, null, Utils.listFromNullable(interfaceType), null, constructor, entry.getValue(), null);
        tsModel.getBeans().add(clientModel);
    }
    // helper
    tsModel.getHelpers().add(TsHelper.loadFromResource("/helpers/uriEncoding.ts"));
}
Also used : TsType(cz.habarta.typescript.generator.TsType) TsProperty(cz.habarta.typescript.generator.TsProperty) TsParameter(cz.habarta.typescript.generator.TsParameter) TsConstructorModel(cz.habarta.typescript.generator.emitter.TsConstructorModel) List(java.util.List) ArrayList(java.util.ArrayList) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Example 2 with TsProperty

use of cz.habarta.typescript.generator.TsProperty in project typescript-generator by vojtechhabarta.

the class ModelCompiler method processRestMethod.

private TsMethodModel processRestMethod(TsModel tsModel, SymbolTable symbolTable, String pathPrefix, Symbol responseSymbol, RestMethodModel method, boolean createLongName, TsType optionsType, boolean implement) {
    final String path = Utils.joinPath(pathPrefix, method.getPath());
    final PathTemplate pathTemplate = PathTemplate.parse(path);
    final List<String> comments = Utils.concat(method.getComments(), Arrays.asList("HTTP " + method.getHttpMethod() + " /" + path, "Java method: " + method.getOriginClass().getName() + "." + method.getName()));
    final List<TsParameterModel> parameters = new ArrayList<>();
    // path params
    for (MethodParameterModel parameter : method.getPathParams()) {
        parameters.add(processParameter(symbolTable, method, parameter));
    }
    // entity param
    if (method.getEntityParam() != null) {
        parameters.add(processParameter(symbolTable, method, method.getEntityParam()));
    }
    // query params
    final List<RestQueryParam> queryParams = method.getQueryParams();
    final TsParameterModel queryParameter;
    if (queryParams != null && !queryParams.isEmpty()) {
        final List<TsType> types = new ArrayList<>();
        if (queryParams.stream().anyMatch(param -> param instanceof RestQueryParam.Map)) {
            types.add(new TsType.IndexedArrayType(TsType.String, TsType.Any));
        } else {
            final List<TsProperty> currentSingles = new ArrayList<>();
            final Runnable flushSingles = () -> {
                if (!currentSingles.isEmpty()) {
                    types.add(new TsType.ObjectType(currentSingles));
                    currentSingles.clear();
                }
            };
            for (RestQueryParam restQueryParam : queryParams) {
                if (restQueryParam instanceof RestQueryParam.Single) {
                    final MethodParameterModel queryParam = ((RestQueryParam.Single) restQueryParam).getQueryParam();
                    final TsType type = typeFromJava(symbolTable, queryParam.getType(), method.getName(), method.getOriginClass());
                    currentSingles.add(new TsProperty(queryParam.getName(), restQueryParam.required ? type : new TsType.OptionalType(type)));
                }
                if (restQueryParam instanceof RestQueryParam.Bean) {
                    final BeanModel queryBean = ((RestQueryParam.Bean) restQueryParam).getBean();
                    flushSingles.run();
                    final Symbol queryParamsSymbol = symbolTable.getSymbol(queryBean.getOrigin(), "QueryParams");
                    if (tsModel.getBean(queryParamsSymbol) == null) {
                        tsModel.getBeans().add(new TsBeanModel(queryBean.getOrigin(), TsBeanCategory.Data, /*isClass*/
                        false, queryParamsSymbol, /*typeParameters*/
                        null, /*parent*/
                        null, /*extendsList*/
                        null, /*implementsList*/
                        null, processProperties(symbolTable, null, queryBean), /*constructor*/
                        null, /*methods*/
                        null, /*comments*/
                        null));
                    }
                    types.add(new TsType.ReferenceType(queryParamsSymbol));
                }
            }
            flushSingles.run();
        }
        boolean allQueryParamsOptional = queryParams.stream().noneMatch(queryParam -> queryParam.required);
        TsType.IntersectionType queryParamType = new TsType.IntersectionType(types);
        queryParameter = new TsParameterModel("queryParams", allQueryParamsOptional ? new TsType.OptionalType(queryParamType) : queryParamType);
        parameters.add(queryParameter);
    } else {
        queryParameter = null;
    }
    if (optionsType != null) {
        final TsParameterModel optionsParameter = new TsParameterModel("options", new TsType.OptionalType(optionsType));
        parameters.add(optionsParameter);
    }
    // return type
    final TsType returnType = typeFromJava(symbolTable, method.getReturnType(), method.getName(), method.getOriginClass());
    final TsType wrappedReturnType = new TsType.GenericReferenceType(responseSymbol, returnType);
    // method name
    final String nameSuffix;
    if (createLongName) {
        nameSuffix = "$" + method.getHttpMethod() + "$" + pathTemplate.format("", "", false).replaceAll("/", "_").replaceAll("\\W", "");
    } else {
        nameSuffix = "";
    }
    // implementation
    final List<TsStatement> body;
    if (implement) {
        body = new ArrayList<>();
        body.add(new TsReturnStatement(new TsCallExpression(new TsMemberExpression(new TsMemberExpression(new TsThisExpression(), "httpClient"), "request"), new TsObjectLiteral(new TsPropertyDefinition("method", new TsStringLiteral(method.getHttpMethod())), new TsPropertyDefinition("url", processPathTemplate(pathTemplate)), queryParameter != null ? new TsPropertyDefinition("queryParams", new TsIdentifierReference("queryParams")) : null, method.getEntityParam() != null ? new TsPropertyDefinition("data", new TsIdentifierReference(method.getEntityParam().getName())) : null, optionsType != null ? new TsPropertyDefinition("options", new TsIdentifierReference("options")) : null))));
    } else {
        body = null;
    }
    // method
    final TsMethodModel tsMethodModel = new TsMethodModel(method.getName() + nameSuffix, TsModifierFlags.None, null, parameters, wrappedReturnType, body, comments);
    return tsMethodModel;
}
Also used : TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) TsObjectLiteral(cz.habarta.typescript.generator.emitter.TsObjectLiteral) ArrayList(java.util.ArrayList) PathTemplate(cz.habarta.typescript.generator.parser.PathTemplate) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) RestQueryParam(cz.habarta.typescript.generator.parser.RestQueryParam) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) BeanModel(cz.habarta.typescript.generator.parser.BeanModel) MethodParameterModel(cz.habarta.typescript.generator.parser.MethodParameterModel) TsType(cz.habarta.typescript.generator.TsType) TsProperty(cz.habarta.typescript.generator.TsProperty) TsReturnStatement(cz.habarta.typescript.generator.emitter.TsReturnStatement) TsPropertyDefinition(cz.habarta.typescript.generator.emitter.TsPropertyDefinition) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Aggregations

TsProperty (cz.habarta.typescript.generator.TsProperty)2 TsType (cz.habarta.typescript.generator.TsType)2 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)2 TsMethodModel (cz.habarta.typescript.generator.emitter.TsMethodModel)2 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)2 ArrayList (java.util.ArrayList)2 TsParameter (cz.habarta.typescript.generator.TsParameter)1 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)1 TsConstructorModel (cz.habarta.typescript.generator.emitter.TsConstructorModel)1 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)1 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)1 TsObjectLiteral (cz.habarta.typescript.generator.emitter.TsObjectLiteral)1 TsPropertyDefinition (cz.habarta.typescript.generator.emitter.TsPropertyDefinition)1 TsReturnStatement (cz.habarta.typescript.generator.emitter.TsReturnStatement)1 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)1 TsStringLiteral (cz.habarta.typescript.generator.emitter.TsStringLiteral)1 TsThisExpression (cz.habarta.typescript.generator.emitter.TsThisExpression)1 BeanModel (cz.habarta.typescript.generator.parser.BeanModel)1 MethodParameterModel (cz.habarta.typescript.generator.parser.MethodParameterModel)1 PathTemplate (cz.habarta.typescript.generator.parser.PathTemplate)1