Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
typescript-rest-swagger-plural
Advanced tools
Generate Swagger files from a typescript-rest project
Adds support for pluralizing endpoints within class/controller inheritance e.g. /myservice -> /myservices (GET,PUT,POST,etc)
This is a tool to generate swagger files from a typescript-rest project.
Table of Contents
npm install typescript-rest-swagger -g
swaggerGen -c ./swaggerConfig.json
Where the swaggerConfig.json file, contains settings about the swagger generation. For example:
{
"swagger": {
"outputDirectory": "./dist",
"entryFile": "./tests/data/apis.ts"
}
}
The documentation will be generated consulting all typescript-rest decorators present on your code. However, there are some additional informations that can be provided, only with documentation purposes, through some other decorators present into this library.
Some examples:
import {Path, Accept, GET} from 'typescript-rest';
import {Tags} from 'typescript-rest-swagger';
@Path('mypath')
export class MyService {
@GET
@Tags('adminMethod', 'otheTag')
@Accept('text/html')
test( ): string {
return 'OK';
}
@GET
@Path('secondpath')
test2( @QueryParam('testParam')test?: string ): Person {
return {name: 'OK'};
}
}
It is also important to observe that all JsDoc provided on your methods, classes, and parameters is outputed into the generated swagger file:
@Accept('text/plain')
@Path('mypath')
export class MyService {
/**
* This description will be used to describe the get operation of path '/mypath' on the generated swagger
* @param test And this will describe the parameter test of this same operation
*/
@GET
@Path('secondpath')
test2( @QueryParam('testParam')test?: string ): Person {
return {name: 'OK'};
}
}
These are the available swagger decorators, provided by typescript-rest-swagger:
A decorator to document the responses that a given service method can return. It is used to generate documentation for the REST service.
interface MyError {
message: string
}
@Path('people')
class PeopleService {
@Response<string>(200, 'Retrieve a list of people.')
@Response<MyError>(401, 'The user is unauthorized.', {message: 'The user is not authorized to access this operation.'})
@GET
getPeople(@Param('name') name: string) {
// ...
}
}
A Default response is already created in swagger documentation from the method return analisys. So any response declared through this decorator is an additional response created.
Used to provide an example of method return to be added into the method response section of the generated documentation for this method.
@Path('people')
class PeopleService {
@Example<Array<Person>>([{
name: 'Joe'
}])
@GET
getPeople(@Param('name') name: string): Person[] {
// ...
}
}
Add tags for a given method on generated swagger documentation.
@Path('people')
class PeopleService {
@Tags('adiministrative', 'department1')
@GET
getPeople(@Param('name') name: string) {
// ...
}
}
Add a security constraint to method generated docs, referencing the security name from securityDefinitions.
@Security
can be used at the controller and method level; if defined on both, method security overwrites controller security.
Multiple security schemes may be specified to require all of them.
@Path('people')
class PeopleService {
@Security('api_key')
@GET
getPeople(@Param('name') name: string) {
// ...
}
}
Document the produces property in generated swagger docs
@Path('people')
@Produces('text/html')
class PeopleService {
@GET
getPeople(@Param('name') name: string) {
// ...
}
}
A Default produces is already created in swagger documentation from the method return analisys. You can use this decorator to override this default produces.
Document the type of a number
property or parameter in generated swagger docs.
If no decorator is present, the number
type defaults to double
format.
class Person {
@IsInt id: number;
}
@Path('people')
class PeopleService {
@Path(':id')
@GET
getById(@PathParam('id') @IsLong id: number) {
// ...
}
}
Because decorators don't work on type and interface properties, this can also be specified as a JSDoc tag.
interface Person {
/**
* The person's id
* @IsInt
*/
id: number;
}
The swagger config file supports the following properties:
Property | Type | Description |
---|---|---|
basePath | string | Base API path; e.g. the 'v1' in https://myapi.com/v1 |
consumes | [string] | Default consumes property for the entire API |
description | string | API description; defaults to npm package description |
entryFile | string | The entry point to your API |
host | string | The hostname to be informed in the generated swagger file |
license | string | API license number; defaults to npm package license |
name | string | API name; defaults to npm package name |
outputDirectory | string | Where to write the generated swagger file |
produces | [string] | Default produces property for the entire API |
version | string | API version number; defaults to npm package version |
yaml | boolean | Generates the output also as an yaml file |
spec | any | Extend generated swagger spec with this object. Note that generated properties will always take precedence over what get specified here |
securityDefinitions | *SecurityDefinition | Security Definitions Object. A declaration of the security schemes available to be used in the specification. This does not enforce the security schemes on the operations and only serves to provide the relevant details for each scheme. |
collectionFormat | string | Default collectionFormat property for the entire API. Possible values are csv , ssv , tsv , pipes , multi . If not specified, Swagger defaults to csv . |
Where the SecurityDefinition contract is defined as:
{
[name: string]: {
type: string;
name?: string;
authorizationUrl?: string;
tokenUrl?: string;
flow?: string;
in?: string;
scopes?: { [scopeName: string]: string; }
}
}
See an example:
{
"swagger": {
"outputDirectory": "./dist",
"entryFile": "./tests/data/apis.ts",
"host": "localhost:3000",
"version": "1.0",
"name": "Typescript-rest Test API",
"description": "a description",
"license": "MIT",
"basePath": "/v1",
"securityDefinitions": {
"api_key": {
"type": "apiKey",
"name": "access_token",
"in": "query"
}
}
}
}
FAQs
Generate Swagger files from a typescript-rest project
The npm package typescript-rest-swagger-plural receives a total of 19 weekly downloads. As such, typescript-rest-swagger-plural popularity was classified as not popular.
We found that typescript-rest-swagger-plural demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.