All Projects → vojtechhabarta → Typescript Generator

vojtechhabarta / Typescript Generator

Licence: mit
Generates TypeScript from Java - JSON declarations, REST service client

Programming Languages

java
68154 projects - #9 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Typescript Generator

Jsonschema2pojo
Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
Stars: ✭ 5,633 (+672.7%)
Mutual labels:  gradle-plugin, maven-plugin, json, jackson
native-build-tools
Native-image plugins for various build tools
Stars: ✭ 168 (-76.95%)
Mutual labels:  maven-plugin, gradle-plugin
gradle-git-versioning-plugin
This extension will set project version, based on current Git branch or tag.
Stars: ✭ 44 (-93.96%)
Mutual labels:  maven-plugin, gradle-plugin
Jackson Databind
General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)
Stars: ✭ 2,959 (+305.9%)
Mutual labels:  json, jackson
boost
Boost Maven and Gradle plugins for MicroProfile development
Stars: ✭ 27 (-96.3%)
Mutual labels:  maven-plugin, gradle-plugin
RapidMavenPushPlugin
A Gradle plugin : Upload Artifacts to Multi Maven Repository
Stars: ✭ 21 (-97.12%)
Mutual labels:  maven-plugin, gradle-plugin
poetimizely
Generate Kotlin type safe accessors for Optimizely experiments and features
Stars: ✭ 17 (-97.67%)
Mutual labels:  maven-plugin, gradle-plugin
Jersey Jwt
Example of REST API with JWT authentication using Jersey, Jackson, Undertow, Weld, Hibernate and Arquillian.
Stars: ✭ 131 (-82.03%)
Mutual labels:  jax-rs, jackson
Javapackager
📦 Gradle/Maven plugin to package Java applications as native Windows, Mac OS X, or GNU/Linux executables and create installers for them.
Stars: ✭ 285 (-60.91%)
Mutual labels:  gradle-plugin, maven-plugin
Json Schema Validator
A fast Java JSON schema validator that supports draft V4, V6, V7 and V2019-09
Stars: ✭ 292 (-59.95%)
Mutual labels:  json, jackson
Jsonista
Clojure library for fast JSON encoding and decoding.
Stars: ✭ 290 (-60.22%)
Mutual labels:  json, jackson
spring-rest-2-ts
spring rest 2 ts is typescript generator which produces data model and services in typescript based on Spring MVC annotations. It supports generation for Angular and React
Stars: ✭ 59 (-91.91%)
Mutual labels:  jax-rs, jackson
dmn-check
A tool which performs static analyses on Decision Model Notation (DMN) files to detect bugs
Stars: ✭ 34 (-95.34%)
Mutual labels:  maven-plugin, gradle-plugin
kobby
Kobby is a codegen plugin of Kotlin DSL Client by GraphQL schema. The generated DSL supports execution of complex GraphQL queries, mutation and subscriptions in Kotlin with syntax similar to native GraphQL syntax.
Stars: ✭ 52 (-92.87%)
Mutual labels:  maven-plugin, gradle-plugin
catnap
Partial JSON response framework for RESTful web services
Stars: ✭ 55 (-92.46%)
Mutual labels:  jax-rs, jackson
id-mask
IDMask is a Java library for masking internal ids (e.g. from your DB) when they need to be published to hide their actual value and to prevent forging. It has support optional randomisation has a wide support for various Java types including long, UUID and BigInteger. This library bases its security on strong cryptographic primitives.
Stars: ✭ 39 (-94.65%)
Mutual labels:  jax-rs, jackson
Maven Git Versioning Extension
This extension will virtually set project versions, based on current git branch or tag.
Stars: ✭ 178 (-75.58%)
Mutual labels:  gradle-plugin, maven-plugin
Jackson Jaxrs Providers
Multi-module project that contains Jackson-based JAX-RS providers for JSON, XML, YAML, Smile, CBOR formats
Stars: ✭ 98 (-86.56%)
Mutual labels:  jax-rs, jackson
Dependencycheck
OWASP dependency-check is a software composition analysis utility that detects publicly disclosed vulnerabilities in application dependencies.
Stars: ✭ 3,571 (+389.85%)
Mutual labels:  gradle-plugin, maven-plugin
Jslt
JSON query and transformation language
Stars: ✭ 367 (-49.66%)
Mutual labels:  json, jackson

Maven Central Appveyor Stars

Quick links: Configuration parameters | Breaking changes | Release notes | Playground (beta)

typescript-generator

typescript-generator is a tool for generating TypeScript definition files (.d.ts) from Java JSON classes. If you have REST service written in Java using object to JSON mapping you can use typescript-generator to generate TypeScript interfaces from Java classes.

For example for this Java class:

public class Person {
    public String name;
    public int age;
    public boolean hasChildren;
    public List<String> tags;
    public Map<String, String> emails;
}

typescript-generator outputs this TypeScript interface:

interface Person {
    name: string;
    age: number;
    hasChildren: boolean;
    tags: string[];
    emails: { [index: string]: string };
}

Supported types include:

  • all Java primitive types with their corresponding wrappers (for example int and Integer, boolean and Boolean, etc.)
  • String
  • Date
  • enum
  • array
  • List and Map (including derived interfaces and implementation classes)
  • customized type mapping

For more details see Type Mapping Wiki page.

Note: typescript-generator works with compiled classes using Java reflection. It doesn't use source files (except for Javadoc feature). In Maven plugin this means either classes compiled from source files in the same module or classes added using <dependency> element.

Maven

In Maven build you can use typescript-generator-maven-plugin like this:

<plugin>
    <groupId>cz.habarta.typescript-generator</groupId>
    <artifactId>typescript-generator-maven-plugin</artifactId>
    <version>x.y.z</version>
    <executions>
        <execution>
            <id>generate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>process-classes</phase>
        </execution>
    </executions>
    <configuration>
        <jsonLibrary>jackson2</jsonLibrary>
        <classes>
            <class>cz.habarta.typescript.generator.Person</class>
        </classes>
        <outputKind>module</outputKind>
    </configuration>
</plugin>

More complete sample can be found here. Detailed description how to configure typescript-generator-maven-plugin is on generated site.

Gradle

In Gradle build you can use cz.habarta.typescript-generator plugin like this:

apply plugin: 'cz.habarta.typescript-generator'
buildscript {
    repositories {
        mavenCentral()
   	    jcenter()
    }
    dependencies {
        classpath group: 'cz.habarta.typescript-generator', name: 'typescript-generator-gradle-plugin', version: 'x.y.z'
    }
}
generateTypeScript {
    jsonLibrary = 'jackson2'
    classes = [
        'cz.habarta.typescript.generator.sample.Person'
    ]
    outputFile = 'build/sample.d.ts'
    outputKind = 'global'
    namespace = 'Rest';
}

For the Kotlin Gradle DSL you can alternatively use the cz.habarta.typescript-generator plugin like this:

build.gradle.kts

import cz.habarta.typescript.generator.JsonLibrary
import cz.habarta.typescript.generator.TypeScriptFileType

plugins {
    id("cz.habarta.typescript-generator") version "x.y.z"
}

tasks {
    generateTypeScript {
        jsonLibrary = JsonLibrary.jackson2
        outputFileType = TypeScriptFileType.implementationFile
        ...
    }
}

settings.gradle.kts

We use this to resolve the plugin, as the plugin is not yet in the Gradle Plugin Repository

pluginManagement {
    repositories {
        mavenCentral()
        gradlePluginPortal()
        jcenter()
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "cz.habarta.typescript-generator") {
                useModule("cz.habarta.typescript-generator:typescript-generator-gradle-plugin:${requested.version ?: "+"}")
            }
        }
    }
}

You can run typescript-generator on demand using gradle generateTypeScript command or you can invoke it as part of another task by adding dependency from that task to generateTypeScript task in Gradle build file.

More complete sample can be found here. Gradle plugin has the same features as Maven plugin, for detailed description see Maven generated site.

Direct invocation

If you do not use Maven or Gradle you can invoke typescript-generator directly using TypeScriptGenerator.generateTypeScript() method.

Input classes

Input classes can be specified using several parameters:

  • classes - list of fully qualified class names, includes all listed classes and their dependencies, $ character is used for nested classes like com.example.ClassName$NestedClassName
  • classPatterns - list of glob patterns like com.example.*Json, includes all classes matched by the pattern, supported are * and ** wildcards
  • classesFromJaxrsApplication - fully qualified name of JAX-RS application class, all classes used by application resources will be included, recommended if you have JAX-RS application class
  • classesFromAutomaticJaxrsApplication - value true will include classes from automatically discovered REST resources, recommended if you have JAX-RS application without Application subclass
  • excludeClasses - list of fully qualified class names, excluded classes will be mapped to TypeScript any type, if excluded class is a resource then this resource will not be scanned for used classes

Note: it is possible to use multiple parameters at the same time.

For more details see Class Names Glob Patterns and JAX RS Application Wiki pages.

Output parameters

Output is configured using several parameters:

  • outputKind (required parameter) - determines if and how module will be generated
    • values are: global, module, ambientModule
  • outputFileType - specifies TypeScript file type
    • values are: declarationFile (.d.ts) or implementationFile (.ts)
  • outputFile - specifies path and name of output file

For more details see Modules and Namespaces page.

REST frameworks

Typescript-generator can generate not only TypeScript declarations for JSON Java classes but it can also generate client classes for REST services. Suppported REST frameworks are JAXR-RS and Spring. Client for JAX-RS service can be generated using generateJaxrsApplicationClient parameter, client for Spring service can be generated using generateSpringApplicationClient. Since Spring support is in separate module it is needed to add this module to typescript-generator dependencies. Here is example for Maven:

<plugin>
    <groupId>cz.habarta.typescript-generator</groupId>
    <artifactId>typescript-generator-maven-plugin</artifactId>
    <version>${typescript-generator.version}</version>
    <configuration>
        <generateSpringApplicationClient>true</generateSpringApplicationClient>
        ...
    </configuration>
    <dependencies>
        <dependency>
            <groupId>cz.habarta.typescript-generator</groupId>
            <artifactId>typescript-generator-spring</artifactId>
            <version>${typescript-generator.version}</version>
        </dependency>
    </dependencies>
</plugin>

Download

Releases are available from Maven Central Repository. Search for dependency information for your build tool or download typescript-generator-core directly.

Wiki

For more detailed description of some topics see Wiki pages.

Architecture

TypeScriptGenerator has 3 main parts (ModelParser, ModelCompiler and Emitter) which work together to produce TypeScript declarations for specified Java classes.

           (Model)            (TsModel)
ModelParser  ==>  ModelCompiler  ==>  Emitter
         |         |
         V         V
        TypeProcessor
  • ModelParser reads Java JSON classes and their properties using Java reflections and creates Model. It uses TypeProcessors for finding used classes. For example if property type is List<Person> it discovers that Person class should be also parsed. ModelParsers are specific for each JSON library (for example Jackson2Parser).
  • ModelCompiler transforms Java model to TypeScript model (Model class to TsModel class). It uses TypeProcessors for mapping Java types to TypeScript types (for example for int returns number).
  • Emitter takes TsModel and produces TypeScript declaration file.

Links

Contributing

  • current major version supports Java 8 and later (version 1 supported Java 7 and 8)
  • keep pull requests small and focused (10 tips for better Pull Requests)
  • do not add dependencies unless previously discussed in issue

Code formatting

  • use 4 spaces for indentation in Java files
  • sort java imports alphabetically (including static imports), do not use wildcards
  • please do not reformat whole files in IDE
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].