2

I got recommended in another thread that I should use ServiceClient when using a ServiceStack API.

I would like to create a template function that can post any type of objects like this:

public Post2<T>(object: T, url: string, httpOptions)
{
try
{
  var client = new JsonServiceClient(`${environment.apiEndpoint}` + url)
  client.headers = httpOptions;
  client.post<T>(object);

}
catch(e)
{

}

}

The problem is that it tells me that "argument of type T is not assignable to parameter of type IReturn.

typescript-ref http://techstacks.io generated the following DTO's (for this purpose)

// @Route("/Equipments", "POST")
export class CreateEquipment
{
    public name: string;
}

// @Route("/Equipments/{Name}", "GET")
export class GetEquipment implements IReturn<Equipment>
{
    public name: string;
    public createResponse() { return new Equipment(); }
    public getTypeName() { return 'GetEquipment'; }
 }

 // @Route("/Equipments", "GET")
 export class GetEquipments implements IReturn<Equipment[]>
{
    public createResponse() { return new Array<Equipment>(); }
    public getTypeName() { return 'GetEquipments'; }
}

// @Route("/Equipments/{Name}", "DELETE")
export class DeleteEquipment
{
    public name: string;
}

I tried to use the following code to create the Post request.

var request = new CreateEquipment();
request.name = equipment.name;
var client = new JsonServiceClient(environment.apiEndpoint);
var response = await client.post(request);

This gives an error in the VS17; argument of type 'CreateEquipment' is not assignable to parameter of type 'IReturn<{}>'.Property createResponse is missing in type'CreateEquipment'

Which I presume means that I am missing something in my ServiceModel

1 Answer 1

1

ServiceStack's TypeScript Service Client should only be constructed with the BaseUrl for where your ServiceStack Host is located, or if you don't specify an argument it will use / by default:

var client = new JsonServiceClient(environment.apiEndpoint);

You also need to use it with the TypeScript generated DTOs from your ServiceStack Service which you can generate by installing the @servicestack\cli npm util:

$ npm install -g @servicestack/cli

Then using it to generate your Server DTOs from the BaseUrl (should be the same as environment.apiEndpoint):

$ typescript-ref http://example.org

Then you can use it to send populated Request DTOs:

var request = new MyRequest();
var response = await client.post(request);

Note your ServiceStack Services should be annotated with the IReturn<T> (or IReturnVoid) interface marker specifying the Services Response DTO.

You can also make API Requests using URLs, but you'll need to specify the Response DTO Type it returns, e.g:

client.get<GetTechnologyResponse>("/technology", { Slug: "ServiceStack" }) 
Sign up to request clarification or add additional context in comments.

17 Comments

I just have to say, even thought I am not supposed too, but that solution was really nice! Keep up the good work!
I spoke to early.. The solution is still really nice, but I get argument of type "CreateEquipment is not assignable to parameter of type IReturn<T> property createResponse is missing in CreateEquipment" I presume that this means that I am missing something in the class on the server.
@AtleKristiansen please update your question with the generated DTOs, your client code and full error details
@AtleKristiansen your CreateEquipment Request DTO needs an IReturn<T> interface marker, rerun your server then you can run ts-ref without any arguments to update your TypeScript DTOs.
@AtleKristiansen You need to Enable CORS which doesn’t sound like you can use a wildcard host so you’ll need to specify an origin whitelist, see docs for an example. This could potentially also be the cause of your Angular HTTP client issues.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.