Gathering detailed insights and metrics for swaggie-beta
Gathering detailed insights and metrics for swaggie-beta
Gathering detailed insights and metrics for swaggie-beta
Gathering detailed insights and metrics for swaggie-beta
npm install swaggie-beta
Typescript
Module System
Min. Node Version
Node Version
NPM Version
56.8
Supply Chain
97.5
Quality
75.4
Maintenance
50
Vulnerability
99.3
License
TypeScript (84.82%)
EJS (13.99%)
JavaScript (1.2%)
Total Downloads
488
Last Day
1
Last Week
2
Last Month
10
Last Year
85
22 Stars
439 Commits
9 Forks
5 Watching
3 Branches
17 Contributors
Minified
Minified + Gzipped
Latest Version
0.7.9
Package Id
swaggie-beta@0.7.9
Unpacked Size
83.24 kB
Size
23.24 kB
File Count
48
NPM Version
9.5.0
Node Version
18.14.2
Publised On
17 Mar 2023
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-33.3%
2
Compared to previous week
Last month
150%
10
Compared to previous month
Last year
-71.7%
85
Compared to previous year
此分支用于发版 swaggie-bate ,不要合并
Generate ES6 or Typescript code from an OpenAPI 2.0 spec, so that accessing REST API resources from the client code is less error-prone, static-typed and just easier to use long-term.
You can take a look at the Examples section down below.
Project is based and inspired by OpenApi Client.
In your project
npm install swaggie --save-dev
Or globally to run CLI from anywhere
npm install swaggie -g
Usage: swaggie [options]
Options:
-h, --help output usage information
-V, --version output the version number
-c, --config <path> The path to the configuration JSON file. You can do all the set up there instead of parameters in the CLI
-s, --src <url|path> The url or path to the Open API spec file
-t, --template <string> Template used forgenerating API client. Default: "axios"
-o, --out <path> The path to the file where the API would be generated
-b, --baseUrl <string> Base URL that will be used as a default value in the clients. Default: ""
--preferAny Use "any" type instead of "unknown". Default: false
--servicePrefix <string> Prefix for service names. Useful when you have multiple APIs and you want to avoid name collisions. Default: ''
--queryModels <bool> Generate models for query string instead list of parameters. Default: false
Sample CLI usage using Swagger's Pet Store:
1swaggie -s https://petstore.swagger.io/v2/swagger.json -o ./client/petstore/
swaggie
outputs TypeScript that is somehow formatted, but it's far from perfect. You can adjust the generated code by prettifying output using your preferred beautify tool using your repo's styling guidelines. For example involving prettier
looks like this:
1swaggie -s $URL -o ./client/petstore.ts && prettier ./client/petstore.ts --write`
And this can be easily automated (in the npm scripts for example)
Instead of providing all required flags from the command line you can alternatively create a new JSON file where you can fill up all settings.
Sample configuration looks like this:
1{ 2 "$schema": "https://raw.githubusercontent.com/yhnavein/swaggie/master/schema.json", 3 "out": "./src/client/petstore.ts", 4 "src": "https://petstore.swagger.io/v2/swagger.json", 5 "template": "axios", 6 "baseUrl": "/api", 7 "preferAny": true, 8 "servicePrefix": "", 9 "queryModels": true, 10 "dateFormat": "Date" // "string" | "Date" 11}
The following templates are bundled with Swaggie:
axios Default template. Recommended for React / Vue / similar frameworks. Uses axios
swr-axios Template that embraces SRW for GET requests and as a fallback uses axios.
fetch Template similar to axios, but with fetch API instead. Recommended for React / Vue / similar frameworks
ng1 Template for Angular 1 (this is for the old one)
ng2 Template for Angular 2+ (uses HttpClient, InjectionTokens, etc)
If you want to use your own template, you can use the path to your template for the -t
parameter:
swaggie -s https://petstore.swagger.io/v2/swagger.json -o ./client/petstore --template ./my-swaggie-template/
1const swaggie = require('swaggie'); 2swaggie 3 .genCode({ 4 src: 'http://petstore.swagger.io/v2/swagger.json', 5 out: './api/petstore.ts', 6 }) 7 .then(complete, error); 8 9function complete(spec) { 10 console.info('Service generation complete'); 11} 12 13function error(e) { 14 console.error(e.toString()); 15}
Let's assume that you have a PetStore API as your REST API and you are developing a client app written in TypeScript that will consume this API.
Instead of writing any code by hand for fetching particular resources, we will let Swaggie do it for us.
Please note that it's recommended to pipe Swaggie command to some prettifier like
prettier
ordprint
to make the generated code look not only nice, but also persistent. Because Swaggie relies on a templating engine, whitespaces are generally a mess, so they may change between versions.
prettier - the most popular one
1prettier ./FILE_PATH.ts --write
dprint - the superfast one
1dprint fmt ./FILE_PATH.ts
You are not limited to any of these, but in our examples we will use Prettier. Please remember that these tools needs to be installed first and they need a config file in your project.
Let's run swaggie
against PetStore API and see what will happen:
1swaggie -s https://petstore.swagger.io/v2/swagger.json -o ./api/petstore.ts && prettier ./api/petstore.ts --write
1// ./api/petstore.ts 2 3import Axios, { AxiosPromise } from 'axios'; 4const axios = Axios.create({ 5 baseURL: '/api', 6}); 7 8/** [...] **/ 9 10export const petClient = { 11 /** 12 * @param petId 13 * @return Success 14 */ 15 getPetById(petId: number): AxiosPromise<Pet> { 16 let url = '/pet/{petId}'; 17 18 url = url.replace('{petId}', encodeURIComponent('' + petId)); 19 20 return axios.request<Pet>({ 21 url: url, 22 method: 'GET', 23 }); 24 }, 25 26 // ... and other methods ... 27};
When we have that we can write some domain code and use this auto-generated classes:
1// app.ts 2 3import { petClient } from './api/petClient'; 4 5petClient.getPetById(123).then((pet) => console.log('Pet: ', pet));
If Petstore owners decide to remove method we use, then after running swaggie
again it will no longer be present in the petClient
class. This will result in the build error, which is very much appreciated at this stage.
Without this approach, the error would be spotted by our end-user and he/she would not appreciate it at all!
You might wonder how to set up server to fully utilize Swaggie's features. For that I've added a samples/
folder with sample configurations.
Server is not necessary to use Swaggie. Swaggie cares only about the JSON/yaml file with the Open API spec, but for your development purpose you might want to have a server that can serve this file automatically from the actual endpoints.
If you are familiar with the client-code generators for the Swagger / OpenAPI standards then you might wonder why swaggie
is better than existing tools. Currently the most popular alternative is an open-source NSwag
.
Quick comparison table:
swaggie | NSwag |
---|---|
- Written in node.js | - Written in .NET |
- Fast | - Slow |
- | - |
- Easy to contribute to | - Contributing hard |
- Lightweight | - Complicated templates |
- Only features generating API clients for TS/JS | - Many more features (but mostly for .NET apps) |
2023-03-17
2023-01-11
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
Found 0/6 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More