SDKgen is a powerful code generator to automatically build client SDKs for your REST API.

Sign in   Sign up

Code

At the following page you can take a look at the generated code, the code is based on this TypeAPI specification. This is only a demo to give you a first insight, for live examples feel free to login at the backend and use the live generator.


Client.ts

/**
 * Client automatically generated by SDKgen please do not edit this file manually
 * {@link https://sdkgen.app}
 */

import axios, {AxiosRequestConfig} from "axios";
import {ClientAbstract, CredentialsInterface, TokenStoreInterface} from "sdkgen-client"
import {ClientException, UnknownStatusCodeException} from "sdkgen-client";

import {TestTag} from "./TestTag";

export class Client extends ClientAbstract {
    public test(): TestTag
    {
        return new TestTag(
            this.httpClient,
            this.parser
        );
    }



    public static build(credentials: CredentialsInterface): Client
    {
        return new Client('https://api.sdkgen.app/', credentials);
    }
}

Error.ts

/**
 * Error automatically generated by SDKgen please do not edit this file manually
 * {@link https://sdkgen.app}
 */

export interface Error {
    success?: boolean
    message?: string
}

ErrorException.ts

/**
 * ErrorException automatically generated by SDKgen please do not edit this file manually
 * {@link https://sdkgen.app}
 */

import {KnownStatusCodeException} from "sdkgen-client"

import {Error} from "./Error";

export class ErrorException extends KnownStatusCodeException {

    public constructor(private payload: Error) {
        super('The server returned an error');
    }

    public getPayload(): Error {
        return this.payload;
    }

}

HelloWorld.ts

/**
 * HelloWorld automatically generated by SDKgen please do not edit this file manually
 * {@link https://sdkgen.app}
 */

export interface HelloWorld {
    message?: string
}

Response.ts

/**
 * Response automatically generated by SDKgen please do not edit this file manually
 * {@link https://sdkgen.app}
 */

export interface Response {
    success?: boolean
    message?: string
}

TestTag.ts

/**
 * TestTag automatically generated by SDKgen please do not edit this file manually
 * {@link https://sdkgen.app}
 */

import axios, {AxiosRequestConfig} from "axios";
import {TagAbstract} from "sdkgen-client"
import {ClientException, UnknownStatusCodeException} from "sdkgen-client";

import {ErrorException} from "./ErrorException";
import {HelloWorld} from "./HelloWorld";
import {Response} from "./Response";
import {Todo} from "./Todo";
import {Todos} from "./Todos";

export class TestTag extends TagAbstract {
    /**
     * Returns a hello world message
     *
     * @returns {Promise<HelloWorld>}
     * @throws {ClientException}
     */
    public async getHello(): Promise<HelloWorld> {
        const url = this.parser.url('/hello/world', {
        });

        let params: AxiosRequestConfig = {
            url: url,
            method: 'GET',
            params: this.parser.query({
            }, [
            ]),
        };

        try {
            const response = await this.httpClient.request<HelloWorld>(params);
            return response.data;
        } catch (error) {
            if (error instanceof ClientException) {
                throw error;
            } else if (axios.isAxiosError(error) && error.response) {
                switch (error.response.status) {
                    default:
                        throw new UnknownStatusCodeException('The server returned an unknown status code');
                }
            } else {
                throw new ClientException('An unknown error occurred: ' + String(error));
            }
        }
    }

    /**
     * Returns available todo entries
     *
     * @returns {Promise<Todos>}
     * @throws {ClientException}
     */
    public async getEntries(startIndex?: number, count?: number): Promise<Todos> {
        const url = this.parser.url('/todo', {
        });

        let params: AxiosRequestConfig = {
            url: url,
            method: 'GET',
            params: this.parser.query({
                'startIndex': startIndex,
                'count': count,
            }, [
            ]),
        };

        try {
            const response = await this.httpClient.request<Todos>(params);
            return response.data;
        } catch (error) {
            if (error instanceof ClientException) {
                throw error;
            } else if (axios.isAxiosError(error) && error.response) {
                switch (error.response.status) {
                    default:
                        throw new UnknownStatusCodeException('The server returned an unknown status code');
                }
            } else {
                throw new ClientException('An unknown error occurred: ' + String(error));
            }
        }
    }

    /**
     * Inserts a new todo entry
     *
     * @returns {Promise<Response>}
     * @throws {ClientException}
     */
    public async insert(payload: Todo): Promise<Response> {
        const url = this.parser.url('/todo', {
        });

        let params: AxiosRequestConfig = {
            url: url,
            method: 'POST',
            params: this.parser.query({
            }, [
            ]),
            data: payload
        };

        try {
            const response = await this.httpClient.request<Response>(params);
            return response.data;
        } catch (error) {
            if (error instanceof ClientException) {
                throw error;
            } else if (axios.isAxiosError(error) && error.response) {
                switch (error.response.status) {
                    default:
                        throw new UnknownStatusCodeException('The server returned an unknown status code');
                }
            } else {
                throw new ClientException('An unknown error occurred: ' + String(error));
            }
        }
    }

    /**
     * Returns a hello world message
     *
     * @returns {Promise<HelloWorld>}
     * @throws {ErrorExceptionException}
     * @throws {ClientException}
     */
    public async throwException(): Promise<HelloWorld> {
        const url = this.parser.url('/exception', {
        });

        let params: AxiosRequestConfig = {
            url: url,
            method: 'GET',
            params: this.parser.query({
            }, [
            ]),
        };

        try {
            const response = await this.httpClient.request<HelloWorld>(params);
            return response.data;
        } catch (error) {
            if (error instanceof ClientException) {
                throw error;
            } else if (axios.isAxiosError(error) && error.response) {
                switch (error.response.status) {
                    case 500:
                        throw new ErrorException(error.response.data);
                    default:
                        throw new UnknownStatusCodeException('The server returned an unknown status code');
                }
            } else {
                throw new ClientException('An unknown error occurred: ' + String(error));
            }
        }
    }


}

Todo.ts

/**
 * Todo automatically generated by SDKgen please do not edit this file manually
 * {@link https://sdkgen.app}
 */

export interface Todo {
    title?: string
}

Todos.ts

/**
 * Todos automatically generated by SDKgen please do not edit this file manually
 * {@link https://sdkgen.app}
 */

import {Todo} from "./Todo";
export interface Todos {
    totalResults?: number
    startIndex?: number
    itemsPerPage?: number
    entry?: Array<Todo>
}