/typescript-mapper

TypeScript mapper for Java.

Primary LanguageJavaMIT LicenseMIT

Maven Central codecov CircleCI License: MIT

What?

Typescript mapper is a tool for generating TypeScript .d.ts files from Java source code.

See the CHANGELOG for a more complete list of available features.

How?

Include the plugin in your Maven project:

src/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>myapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <typescript-mapper.version>LATEST</typescript-mapper.version>
    </properties>

    <dependencies>
        <!-- For annotations -->
        <dependency>
            <groupId>io.github.daniloarcidiacono.typescriptmapper</groupId>
            <artifactId>typescript-mapper-core</artifactId>
            <version>${typescript-mapper.version}</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>io.github.daniloarcidiacono.typescriptmapper</groupId>
                <artifactId>typescript-mapper-maven-plugin</artifactId>
                <version>${typescript-mapper.version}</version>
                <configuration>
                    <basePackage>com.example.myapp</basePackage>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>map</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Consider the following classes:

src/main/java/com/example/myapp/domain/Person.java

package com.example.myapp.domain;

import com.example.myapp.info.Tag;
import io.github.daniloarcidiacono.typescriptmapper.core.annotation.TypescriptComments;
import io.github.daniloarcidiacono.typescriptmapper.core.annotation.TypescriptDTO;
import java.util.List;
import java.util.Map;

@TypescriptDTO
public class Person {
    @TypescriptComments("The name of the domain.")
    private String name;
    private int age;
    private boolean hasChildren;
    private List<Tag> tags;

    @TypescriptComments({
        "The emails of the domain.",
        "Key is provider, value is email."
    })
    private Map<String, String> emails;
    
    // ... rest of the code ...
}

src/main/java/com/example/myapp/info/Tag.java

package com.example.myapp.info;

import io.github.daniloarcidiacono.typescriptmapper.core.annotation.TypescriptComments;
import io.github.daniloarcidiacono.typescriptmapper.core.annotation.TypescriptDTO;
import io.github.daniloarcidiacono.typescriptmapper.core.annotation.TypescriptField;

@TypescriptDTO
@TypescriptComments("Colored tag.")
public class Tag {
    private String name;

    @TypescriptComments("Optional tag color")
    @TypescriptField(required = false)
    private Color color;
    
    // ... rest of the code ...
}

@TypescriptDTO
@TypescriptComments("Tag colors")
enum Color {
    RED,
    GREEN
}

Running mvn compile produces the following output:

target/mapped/com/example/myapp/domain.ts

/**
 * This file is automatically generated by TypescriptMapper.
 * Do not modify this file -- YOUR CHANGES WILL BE ERASED!
 */
import { Tag } from './info';
export interface Person {
	// The name of the domain.
	name: string;
	age: number;
	hasChildren: boolean;
	tags: Tag[];

	/**
	 * The emails of the domain.
	 * Key is provider, value is email.
	 */
	emails: { [ index: string ]: string };
}

target/mapped/com/example/myapp/info.ts

/**
 * This file is automatically generated by TypescriptMapper.
 * Do not modify this file -- YOUR CHANGES WILL BE ERASED!
 */
// Colored tag.
export interface Tag {
	name: string;

	// Optional tag color
	color?: Color;
}

// Tag colors
export enum Color {
	RED = 'RED',
	GREEN = 'GREEN'
}

The folder structure can be customized with any logic (the imports will be adjusted accordingly).

Why?

Type-safe DTOs exchanged by back-end and front-end components.

Where?

The latest version is available on Maven Central.