Herts
GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

CodeGen for Typescript

HTTP Client code generation for Typescript

This tool supports HTTP Client code generation except gRPC.
Currently, support Typescript only.

Also, Code is generated from Herts Service interface.

Install requirements

Required Java 15+

build.gradle

dependencies {
    implementation 'org.hertsstack:herts-codegen:1.1.0'
}

Quick start

You can implement plane Java main.

public class Main {
    public static void main(String[] args) {
        HertsCodeGenEngine codeGenEngine = HertsCodeGenBuilder.builder()
                .hertsService(HttpCodegenTestService.class) // Your Herts Interface
                .lang(CodeGenLang.Typescript)
                .build();

        codeGenEngine.generate();
    }
}

Output Code Example

Herts Service interface.

@HertsHttp
public interface HttpService extends HertsService {
    String helloWorld();
    CustomModel create(CustomModel model);
}

Implementation class.

public class HttpServiceImpl extends HertsServiceHttp<HttpService> implements HttpService {
    @Override
    public String helloWorld() {
        return "hello world";
    }
}

Custom Model class.
If you want to create your custom class in typescript, you need to extends HertsMessage

public class CustomModel extends HertsMessage {
    private int id;
    private String data;
    private NestedCustomModel nestedCustomModel;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public NestedCustomModel getNestedCustomModel() {
        return nestedCustomModel;
    }

    public void setNestedCustomModel(NestedCustomModel nestedCustomModel) {
        this.nestedCustomModel = nestedCustomModel;
    }

    public static class NestedCustomModel extends HertsMessage {
        private long nestedId;
        private String pointer;

        public long getNestedId() {
            return nestedId;
        }

        public void setNestedId(long nestedId) {
            this.nestedId = nestedId;
        }

        public String getPointer() {
            return pointer;
        }

        public void setPointer(String pointer) {
            this.pointer = pointer;
        }
    }
}

Main class.
You can run Main class.

public class Main {
    public static void main(String[] args) {
        CodeGenEngine codeGenEngine = CodeGenBuilder.builder()
                .hertsService(HttpService.class)
                .lang(CodeGenLang.Typescript)
                .build();

        codeGenEngine.generate();
    }
}

Then You can get Typescript Code.
HTTP Client ts file.
herts-HttpService-client.gen.ts

// Don't edit this file because this file is generated by herts codegen.
import axios, {AxiosError} from 'axios'
import {RequestHeaders} from './herts-structure.gen'

import {CreateRequest} from './herts-HttpService-request-model.gen'
import {HelloWorldRequest} from './herts-HttpService-request-model.gen'

import {CreateResponse} from './herts-HttpService-response-model.gen'
import {HelloWorldResponse} from './herts-HttpService-response-model.gen'

import {CustomModel} from './herts-structure.gen'

export class HttpServiceClient {

    /**
     * API endpoint information with protocol schema.
     * @param apiSchema http|https://hoge.com
     */
     constructor(private apiSchema: string) {}

     public async create ( headers: RequestHeaders, body: CreateRequest ): Promise<CustomModel | null> {
        return await axios.post<CreateResponse>(
            `${(this.apiSchema)}/api/HttpService/create`,
             body,
             {
                headers: headers,
             })
             .then(res => {
                if (res.data.payload === undefined || res.data.payload === null) {
                    return null;
                }
                return res.data.payload.value;
             })
             .catch((e: AxiosError<any>) => {
                throw e;
             })
        }
     public async helloWorld ( headers: RequestHeaders ): Promise<string | null> {
        const body = null;
        return await axios.post<HelloWorldResponse>(
            `${(this.apiSchema)}/api/HttpService/helloWorld`,
             body,
             {
                headers: headers,
             })
             .then(res => {
                if (res.data.payload === undefined || res.data.payload === null) {
                    return null;
                }
                return res.data.payload.value;
             })
             .catch((e: AxiosError<any>) => {
                throw e;
             })
        }
}

Request definition typescript file.
herts-HttpService-request-model.gen.ts

// Don't edit this file because this file is generated by herts codegen.
import {CustomModel} from './herts-structure.gen'

export class CreateRequest {
    private constructor(payloads: Array<CreatePayload>) {
        this.payloads = payloads;
    }
    payloads: Array<CreatePayload>;
    public static createRequest(
        arg0 : CustomModel,
    ) {
        const payloads = new Array<CreatePayload>();
        const payload0 = new CreatePayload ('arg0', arg0, 'org.hertsstack.example.codegents.CustomModel');
        payloads.push(payload0);
        return new CreateRequest (payloads);
    };
}
export class HelloWorldRequest {
    private constructor(payloads: Array<HelloWorldPayload>) {
        this.payloads = payloads;
    }
    payloads: Array<HelloWorldPayload>;
    public static createRequest(
    ) {
        const payloads = new Array<HelloWorldPayload>();
        return new HelloWorldRequest (payloads);
    };
}

export class CreatePayload {
    constructor(keyName: string, value: any, classInfo: string) {
        this.keyName = keyName;
        this.value = value;
        this.classInfo = classInfo;
    }
    private keyName: string;
    private value: any;
    private classInfo: string;
}
export class HelloWorldPayload {
    constructor(keyName: string, value: any, classInfo: string) {
        this.keyName = keyName;
        this.value = value;
        this.classInfo = classInfo;
    }
    private keyName: string;
    private value: any;
    private classInfo: string;
}

Response definition typescript file.
herts-HttpService-response-model.gen.ts

// Don't edit this file because this file is generated by herts codegen.
import {CustomModel} from './herts-structure.gen'

export class CreateResponse {
    constructor() {
        this.payload = new CreatePayload ();
    }
    payload: CreatePayload;
}
export class HelloWorldResponse {
    constructor() {
        this.payload = new HelloWorldPayload ();
    }
    payload: HelloWorldPayload;
}

export class CreatePayload {
    constructor() {
        this.keyName = '';
        this.value = null;
        this.classInfo = '';
    }
    private keyName: string;
    value: CustomModel;
    private classInfo: string;
}
export class HelloWorldPayload {
    constructor() {
        this.keyName = '';
        this.value = null;
        this.classInfo = '';
    }
    private keyName: string;
    value: string;
    private classInfo: string;
}

Structure definition typescript file.
herts-structure.gen.ts

// Don't edit this file because this file is generated by herts codegen.
export type RequestHeaders = {
	[x: string]: string | number | boolean;
}

export class CustomModel {
    constructor(
        id : number,
        data : string,
        nestedCustomModel : NestedCustomModel,
    ) {
        this.id = id
        this.data = data
        this.nestedCustomModel = nestedCustomModel
    }
    id : number
    data : string
    nestedCustomModel : NestedCustomModel
}
export class NestedCustomModel {
    constructor(
        nestedId : number,
        pointer : string,
    ) {
        this.nestedId = nestedId
        this.pointer = pointer
    }
    nestedId : number
    pointer : string
}