
Security News
Ruby's Bundler 4.0.18 Extends Cooldown to bundle lock and bundle cache
The supply chain control that delays freshly published gems now covers lockfile generation and gem vendoring in Ruby projects.
@testgorilla/tgo-yasag
Advanced tools
This is a fork of the original YASAG done by the Intelligenia team.
Generate minimalistic TypeScript API layer for Angular with full type reflection of backend model.
npm i yasag --save-dev
-s, --src - source directory
-d, --dest - destination directory, default: src/api
--no-store - do not generate the ngrx modules
-u, --swagger-URL-path - swagger URL path, where the swagger ui documentation can be found; default: /swagger, i.e. the resulting address would be http://example/swagger
-o, --omit-version - disables API version information to be generated in comments for each file
-b, --omit-basepath - Omit basepath given in the swagger file
-v, --environment-var - Name of the environment variable for the base path
-h, --omit-header - Omit print header on each file
-r, --read-only <ending> - Omit attributes ending by <ending> in PUT, POST and PATCH methods, default: None
--use-app-config - Use AppConfigService for configuration, instead of environmen, default: false
get the swagger scheme in JSON (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/yasag -s src/swagger/scheme.json -d src/api
as module yasag package, npm run apigen
"script": {
"apigen": "yasag -s src/swagger/scheme.json -d src/api"
...
}
or programatically as a method invocation
import { generate } from 'yasag';
// or using CommonJS loader
const { generate } = require('yasag');
generate('src/swagger/scheme.json', 'src/api');
The resulting API layer contains the following structure in the destination directory:
controllers directory stores services containing all API methods devided by controllersdefs directory stores all response interfaces and enumsforms directory has modules, which contain associated form serviceform-services.ts file reexports all of the form services together for a simple accessmodel.ts file reexports all of them together for a simple accessIn order to consume generated model, follow the steps 1-9 in the following example to use generated API model.
// 1. import used response interfaces
import { ItemDto, PageDto } from '[relative-path-to-destination-directory]/model';
// 2. import used API form service and optionally param interfaces
import { DataFormService } from '[relative-path-to-destination-directory]/form-service';
@Component({
...
// 3. make the service injectable (can be also imported in the module)
// can be also imported in the app module through the apiforms.module,
// which includes every FormService
providers: [DataFormService],
})
export class MyComponent implements OnInit {
// 4. declare response object variables based on the generated API interfaces
public items: ItemDto[] = [];
public page: PageDto;
// 5. declare request params based on the generated API interface (all params are passed together in one object)
private params: MethodParams = {
page: 0,
size: 10,
sort: ['name:asc']
};
// 6. inject the API service
constructor(private dataFS: DataFormService) {}
public ngOnInit() {
// 7. the returned observable is fully typed
this.dataFS
.submit(this.params)
// 8. returned data are fully typed
.subscribe(data => {
// 9. assignments are type-checked
this.items = data.content;
this.page = data.page;
});
}
}
// 10. in your environment.ts configuration it is necessary to specify the apiURL and the apiRetries
export const environment = {
apiUrl : '/api', // root URL for your backend
apiRetries : 10 // number of retries while the api gets a 5xx error
};
dataFS service is generated and holds the FormGroup definition that corresponds
with the request data structure<form [formGroup]="dataFS.form" (ngSubmit)="dataFS.submit()" class="full-width">
<input type="text" name="email" placeholder="email" formControlName="email" />
<button type="submit" [disabled]="dataFS.form.invalid">Save</button>
</form>
@Component({
selector: 'example-component',
templateUrl: 'example-component.html',
})
export class ExampleComponent {
constructor(private dataFS: DataFormService) {}
}
dataFS service is generated and holds the FormGroup definition that corresponds
with the request data structure, inside it, we have a FormArray structure to manage<form [formGroup]="dataFS.form" (ngSubmit)="dataFS.submit()" class="full-width">
<div *ngFor="let value of dataFS.form.controls.emails.controls; let i=index">
<div [formGroup]="value">
<input type="text" name="email" placeholder="email" formControlName="email" />
<button (click)="dataFS.removeContacts(i)">Delete</button>
</div>
</div>
<button (click)="dataFS.addContacts()">Append new contact</button>
<button type="submit" [disabled]="dataFS.form.invalid">Save</button>
</form>
@Component({
selector: 'example-component',
templateUrl: 'example-component.html',
})
export class ExampleComponent {
constructor(private dataFS: DataFormService) {}
}
form (FormGroup): gives access to the internat FormGroup, so patchValue or value are reachablesubmit (method) to tun the endpoint. It allows several parameters:
value (any): if not set, the submit method send the content of form.
the passed arguments has to match with FormGroup definitioncache (boolean): by default, in get methods, YASAG saves the last response given the same input
so the Observable generated by submit(), throw the saved cache if exists, and then executes the endpoint,
in the case of a different response than the saved one, YASAG updates the caché,
and emmit again with the new response. If cache is set to false, YASAG does not save any response,
so every time has to wait until the endpoint execution endsonly_cache (boolean): it is false by default, but if it is set to true, when YASAG has a saved response
for a given input, it does not try to execute the endpoint again, so just emit the saved cache datacancelPreviousRequest (method) to cancel every running endpoint. This is very useful for seach endpoints
where the user is writing the search word, and at the same time we are getting results from the backendreset (method) to initialize the FormService. This method gets an optional parameter with a value,
if a value is passed, the FormGroup is set to match the corresponding value
(including nested objects, and nested arrays of objects)patch (method) to update the FormService. This method gets a parameter with a value,
the FormGroup is set to match the corresponding value
(including nested objects, and nested arrays of objects)loading$ Observable to know if the endpoint is running right now or not
(very useful to inform the user)serverErrors$ Observable, in case of 4xx error, the endpoint response
is emitted on serverErrors$, it is very useful to manage the errors in the HTML sidetags attribute 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, and also both FormServices will be generaqted in /forms/orderget and delete methods do not contain bodyFAQs
Yet Another Swagger Angular Generator
We found that @testgorilla/tgo-yasag demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The supply chain control that delays freshly published gems now covers lockfile generation and gem vendoring in Ruby projects.

Security News
During a UK cyber test, a Mythos 5 agent used sockpuppets, social engineering, and prompt injection to try to get a maintainer to merge malware.

Company News
Socket is now in the AWS Security Hub Extended plan. Adopt it through AWS, apply committed spend, and block malicious open source packages.