rest-client-generator
Generate REST endpoint client from Swagger or WADL for you project. Useful for typed languages such TypeScript and Dart. Currently support generation for platforms:
- Angular5 TypeScript (via @angular/common/http)
- Angular2 TypeScript (via @angular/http)
- Angular2 Dart
- Dojo2 TypeScript
Features:
- Request/response representation
application/json
is handled as interface - Mimetypes such as
text/*
,application/xml
, etc. are handled as strings - Mimetype
application/octet-stream
is handled as File - Other mimetypes are handled as Blob
- Translate date fields in response JSON to js Date object
- Full suport XSD schema types (
xs:string
,xs:number
,xs:boolean
,xs:datetime
, etc.) - XSD schema enumeration handled as enum
- XSD schema extension handled as object inheritance
- Support fileupload in multipart/form-data
Instalation
Install globally rest-client-generator
npm install --global rest-client-generator
Generate
From WADL
Get some WADL schema, for example app.wadl
:
WADL file include schema <include href="app.xsd"/>
with request and response types, here is app.xsd
:
For example, you have TypeScript project with Angular2, to generate client run:
rest-client-generator --output-file services.ts --platform angular2-ts app.wadl
From Swagger
If you don't have WADL schema, you can generate client from Swagger YAML or JSON.
Alternative of upper mentioned WADL schema in Swagger is app.yaml
:
swagger: '2.0'info: version: v1 title: Test APIhost: 'localhost:8080'basePath: /restapischemes: - httptags: - name: auth - name: personpaths: /auth/login: post: tags: - auth summary: '' description: '' operationId: login produces: - text/plain parameters: - name: login in: query required: true type: string - name: password in: query required: true type: string responses: '200': description: OK /auth/logout: post: tags: - auth summary: '' description: '' operationId: logout responses: '200': description: OK /person/user/{id}: get: tags: - person summary: '' description: '' operationId: getPerson produces: - application/json parameters: - name: id in: path required: true type: integer format: int34 responses: '200': description: OK schema: $ref: '#/definitions/Person' /person/user: post: tags: - person summary: '' description: '' operationId: createPerson consumes: - application/json parameters: - name: body in: body required: true schema: $ref: '#/definitions/Person' responses: '200': description: OKdefinitions: Person: type: object required: - id - firstName - lastName - birthDate properties: id: type: integer format: int32 firstName: type: string lastName: type: string birthDate: type: string format: date
To to generate client run command:
rest-client-generator --output-file services.ts --platform angular2-ts app.yaml
Generated client
Lets watch your generated rest client services.ts
;;...
In your app you can change url of your REST api, with provide constant SERVICE_ROOT_URL
:
bootstrapAppComponent,
In JSON date types has string representation (ISO 8601). TypeScript is not able to recognize it and convert to Date object. Constant SERVICE_JSON_DATE_PATTERN
is regular expression, which test all received strings, if they matched is converted to Date object.
Interface Person
is type from schema app.xsd
. Services AuthService
and PersonService
are resources from WADL app.wadl
with they methods. HTTP call are asynchronous, so mehods return Observable
.
Usage
You have generated rest client in services.ts
, first you must import service module to your application module.
;
Now, you can enjoy your client :-)
;
;
License
Apache 2.0