Typescript typed API in VS Code

I have multiple Ionic mobile apps that I have backend services written with ServiceStack. The apps are using TypeScript and I am very interested in generating a strongly typed API definition as described here, however the projects have nothing to do with Visual Studio and the Add TypeScript Reference functionality.

Can you suggest a method whereby a dtos.ts file can be generated such that I can include in the VS Code projects?

Hi Dylan, you can just get the generated TypeScript from the /typescript or /typescript.d routes of your Service, e.g:

Then save it to a dtos.ts or dtos.d.ts file.

We also have a command-line ssutil.exe that does this if you prefer.

1 Like

That is very awesome Demis thank you. Can definitely use this.

1 Like

I am in a similar situation in VS Code, I have used ssutil.exe to create the Typescript file, however it has included types such as IRequest and IResponse which reference unknown types such as Cookie and Uri:

export interface IRequest
{
...
    Cookies?: { [index:string]: Cookie; };
...
    UrlReferrer?: Uri;
}

I know I can change what types I want returned in Visual Studio by modifying the attributes at the top of the file:

//GlobalNamespace: 
//MakePropertiesOptional: True
AddServiceStackTypes: False
//AddResponseStatus: False

However invoking ssutil.exe again just re-writes this file, calling it with just the filename as suggested by Git repo to update the file with the appropriate settings tells me *.ts is not a valid extension:

ssutil src\app\services\dtos.ts --lang=typescript
Failed to complete operation. Update file path provided does not have a valid ServiceStack reference extension.

What is the recommended way of resolving this? I know I can start a simple Visual Studio project and do the same to get the right files, but I would like this file to get updated on the build server against the running API to catch any breaking changes at compile time.

Edit:

Also I have operations such as this, which return a HttpResult due to only needing to provide a StatusCode, this results in HttpResult being created in the dtos.ts file - I would rather represent the result with StatusCode than adding a ResponseDto with the appropriate flags.

    public HttpResult Delete(DeleteBankAccountRequest request)
    {
        var bankAccount = BankAccountRepository.GetById(request.BankAccountId);
        if (bankAccount == null || bankAccount.OrganisationId != this.OrganisationId)
        {
            return new HttpResult(HttpStatusCode.NotFound);
        }

        BankAccountRepository.Delete(bankAccount);
        return new HttpResult(HttpStatusCode.OK);
    }

Thanks,
James

You should never have HttpResult in IReturn Interface markers, they’re a server-side decorator to add additional metadata like Headers to the Response. Only the Response DTO Type returned in the HTTP Response should be in IReturn markers.

There is no IReturn on this request DTO:

[Route("/bankaccounts/{BankAccountId}", "DELETE")]
public class DeleteBankAccountRequest : RequestMessageBase
{
    public Guid BankAccountId { get; set; }
}

It shouldn’t be in any Request DTO, also what’s the definition of the base class?

Any Request DTO that returns no specific Response DTO do not have any IReturn marker interface. Base class definition:

public abstract class RequestMessageBase
{
    public Guid? CorrelationId { get; set; }
    public int Version { get; set; }
}

Or is the issue due to the HttpResult return Type, can you change it to object?

That does seem better, but no getTypeName() is included on the created definition, is this required?

How exactly does JsonServiceClient actually resolve the Route for these DTOs?

public object Delete(DeleteBankAccountRequest request)

// @Route("/bankaccounts/{BankAccountId}", "DELETE")
export class DeleteBankAccountRequest extends RequestMessageBase
{
    BankAccountId: string;
}

Yes, removing all the public HttpResult VERB( definitions into returning object removes HttpResult, IRequest and IResponse from the generated definition. Aside from blowing up my unit tests projects :smiley:

Thank you Mythz as always a rapid response with quick resolution!

1 Like