slovely / TypeScriptDefinitionsGenerator

Generate TypeScript .d.ts files for your server classes, plus optionally create client-side interfaces for WebAPI / SignalR server implementations
MIT License
10 stars 1 forks source link

Add support for Aurelia compatible WebAPI stubs #8

Closed slovely closed 6 years ago

slovely commented 6 years ago

When using Aurelia, I'd like the generated actions.ts to look something like this:

import {autoinject} from "aurelia-dependency-injection";
import {HttpClient} from "aurelia-fetch-client";

@autoinject
export class Person {
    constructor(private http: HttpClient) {
    }

    public getThePerson(id: string, ajaxOptions: RequestInit = null)
        : PromiseLike<SampleApp.Models.Person> {
        const options: RequestInit = { method: "POST"};
        if (ajaxOptions) {
            Object.assign(options, ajaxOptions);
        }
        return this
            .http
            .fetch("api/person/getThePerson/" + id, options)
            .then(response => response.json());
    }
}

That way, a service that can call the WebAPI can be injected in like:


import Actions = require("../server/actions");
@autoinject
export class MyViewModel {
    constructor(personControllerService: Actions.Person) {
        personControllerService.getThePerson("abc")
            .then(p => {
               // 'p' will now be of type: "SampleApp.Models.Person"
            });
    }
}