Package Exports
- swagger-angular-generator
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (swagger-angular-generator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Purpose
Generate minimalistic TypeScript API layer for Angular with full type reflection of backend model.
- Source: swagger scheme
- Destination: Angular-cli based Angular app.
Install
npm i swagger-angular-generator --save-dev- check usage via
./node_modules/.bin/swagger-angular-generator -h
Use
Run generator
- get the swagger scheme (typically at http(s)://[server]/[app-path]/v2/api/api-docs)
- save it to json file in input directory and optionally format it for better diff
- run via
- directly
./node_modules/.bin/swagger-angular-generator - as module
swagger-angular-generatorpackage,npm run generate"script": { "generate": "swagger-angular-generator -s src/api/scheme.json -d src/api/generated" ... }
- or programatically as a method invocation
import {generate} from 'swagger-angular-generator'; // or using CommonJS loader const {generate} = require('swagger-angular-generator'); generate('conf/api/api-docs.json', 'src/api');
- directly
The resulting API layer contains the following structure in the destination directory:
defdirectory stores all response interfaces and enumsmodel.tsfile reexports all of them together for a simple accesscontrollersdirectory stores services containing all API methods devided by controllers
When updating your code for new backend version, we recommend you to follow these steps:
git diffthe changes- run
tscfor immediate problems - adjust the code, solve problems
- commit
Use
In order to consume generated model, follow the steps 1-9 in the following example to use generated API model.
Usage in the service or component
// 1. import used response types
import {ItemDto, PageDto} from '[relative-path-to-destination-directory]/model';
// 2. import used controller service and optionally param types
import {DataService, MethodParams} from '[relative-path-to-destination-directory]/api/DataService';
@Component({
...
// 3. make the service injectable
providers: [DataService],
})
export class MyComponent implements OnInit {
// 4. link response objects to generated API types
public items: ItemDto[] = [];
public page: PageDto;
// 5. link request params to generated API types (all params are passed together in one object)
private params: MethodParams = {
page: 0,
size: 10,
sort: ['name:asc']
};
// 6. inject the service
constructor(private dataService: DataService) {}
public ngOnInit() {
// 7. the returned observable is fully typed
this.dataService
.get(this.params)
// 8. returned data are fully typed
.subscribe(data => {
// 9. assignments type-checked
const {content, page} = data;
this.items = content;
this.page = page;
});
}
}Assumptions / limitations
- swagger file is in version 2 format, it must be json
- each endpoint must have a
tagsattribute defined. In addition, there must be exactly one tag defined. The http methods are grouped to services based on the tags, i.e. if two methods have tag "order", both will be generated inside Order.ts in: headerdefinitions are ignoredgetanddeletemethods do not containbody- swagger file should contain values for the keys
hostandbasePathso that each generated service method can contain a link to the swagger UI method reference, e.g.http://example.com/swagger/swagger-ui.html#!/Order/Order
Development
- at least node 8 is needed
Docker image
docker build . -t swagger-angular-generatordocker run -u $(id -u) -it -v "$PWD":/code swagger-angular-generator bashnpm i
Testing
How the testing works
- tests are written in the demo-app
- the test swagger files can be found in demo-app/client/test-swaggers
- upon these swagger files, interfaces and services are generated
- the generated services are manually imported to the app.module.ts
- unit tests can be found in demo-app/client/src/tests
Running the tests
cd demo-app/clientnpm run generatenpm run test
or instead of step 2 and 3 run: npm run testci
TODO
- forms - add select fields for eums and checkboxes for booleans
- forms do not currently work for nested data structures
- come up with a way how to define inital states values
- State initerfaces and initial values move to separate importable file
- the State interface definition should be equal to the response types
- reducers should use such states
- this will make possible to have create/update/get methods share the same store attributes