Swagger typescript / angular2 client

I generated my client using typescript2 / angular2 language using the swagger codegen code generator .

I cannot understand what was created and how to use artifacts.

As far as I managed to find out, headers are not included on request in the create parameters ( user and passwd ). Otherwise, I do not understand how queries are made. I set a breakpoint in the callback to the map method, and it never reached.

You can look at UsersApi.ts .

+6
source share
1 answer

In my current project, I use a client-generated script generated (typescript / ng2).

First, I take the generated output and put it into my project. You can automate it or not. Depending on how much your api changes. I put mine in "app / core / api" and this includes the api folder and the model folder and half a dozen or so other files. and the .swagger-codegen folder (although I'm not 100% sure that this is necessary).

  • Create a service to wrap your use of the generated api, separating your components from it (if it changes). Therefore, for your example, I would create my own user.service.ts, which will be used by my components.
  • In your UserService class, you need to import the public api from the generated code. this is usually in api / index.ts or simply: import * as a client from / core / api application; it is easier when you have only one api. if you have several, they are exported as an APIS array from api / api.ts /
  • To use api, you do not need to create headers or parameters or any of them. the generated service has methods that must take the values ​​necessary to call and handle the creation of json request and response objects. Looking through the folder with samples, and the service itself should make this a little clearer. but for your example you have only 2 methods. create and exist. in your UserService you create the CreateUser method or whatever you like, and inside of it you call the api create method. give it a username string and a password string. that's all. it will return the observable everything that the api call returns (it converts the json response into a typescript object (defined in the model directory in the generated api code).
  • Your example does not seem to use custom objects. The create method simply returns an Observable <{}>. Thus, you can call him and send a response to your own instance of the user class and use it to bind to your presentation template. Again staying separate from api details.

Hope this helps. I would be happy to answer more specific questions.

0
source

Source: https://habr.com/ru/post/1012901/


All Articles