Gathering detailed insights and metrics for swagger-axios-codegen
Gathering detailed insights and metrics for swagger-axios-codegen
Gathering detailed insights and metrics for swagger-axios-codegen
Gathering detailed insights and metrics for swagger-axios-codegen
@open-match/api
This package contains types and [axios](https://www.npmjs.com/package/axios) helpers for using the HTTP interfaces from [Open Match](https://openmatch.dev/site/). They are generated using [swagger-axios-codegen](https://www.npmjs.com/package/swagger-axios
@devexperts/swagger-codegen-ts
TS generator for swagger spec
swagger-typescript-api
Generate the API client for Fetch or Axios from an OpenAPI Specification
iceboom
nestjs, swagger-axios-codegen adapter
npm install swagger-axios-codegen
58.5
Supply Chain
97.6
Quality
82.7
Maintenance
100
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
307 Stars
487 Commits
84 Forks
5 Watching
4 Branches
29 Contributors
Updated on 26 Nov 2024
TypeScript (95.92%)
JavaScript (4.08%)
Cumulative downloads
Total Downloads
Last day
19.6%
1,688
Compared to previous day
Last week
8.4%
7,414
Compared to previous week
Last month
18.3%
30,037
Compared to previous month
Last year
43.2%
301,330
Compared to previous year
5
A swagger client uses axios and typescript
< v0.16 require node > v10.0.0
>= v0.16 require node >= v16
it will always resolve axios.response.data
or reject axios.error
with Promise
support other similar to axios
library, for example Fly.js, required setting ISwaggerOptions.useCustomerRequestInstance = true
the es6 version is generated by calling typescript
Welcome PRs and commit issue
By the way. you can support this repo via Star star sta st s... ⭐️ ⭐️ ⭐️ ⭐️ ⭐️
yarn add swagger-axios-codegen
1 2export interface ISwaggerOptions { 3 serviceNameSuffix?: string 4 enumNamePrefix?: string 5 methodNameMode?: 'operationId' | 'path' | 'shortOperationId' | ((reqProps: IRequestMethod) => string) 6 classNameMode?: 'parentPath' | 'normal' | ((path: string, method: string, reqProps:IRequestMethod) => string) 7 /** only effect classNameMode='parentPath' */ 8 pathClassNameDefaultName?: string 9 outputDir?: string 10 fileName?: string 11 remoteUrl?: string 12 source?: any 13 useStaticMethod?: boolean | undefined 14 useCustomerRequestInstance?: boolean | undefined 15 include?: Array<string | IInclude> 16 /** include types which are not included during the filtering **/ 17 includeTypes?: Array<string> 18 format?: (s: string) => string 19 /** match with tsconfig */ 20 strictNullChecks?: boolean | undefined 21 /** definition Class mode */ 22 modelMode?: 'class' | 'interface' 23 /** use class-transformer to transform the results */ 24 useClassTransformer?: boolean 25 /** force the specified swagger or openAPI version, */ 26 openApi?: string | undefined 27 /** extend file url. It will be inserted in front of the service method */ 28 extendDefinitionFile?: string | undefined 29 /** mark generic type */ 30 extendGenericType?: string[] | undefined 31 /** generate validation model (class model mode only) */ 32 generateValidationModel?: boolean 33 /** split request service. Can't use with sharedServiceOptions*/ 34 multipleFileMode?: boolean | undefined 35 /** url prefix filter*/ 36 urlFilters?: string[] | null | undefined 37 /** shared service options to multiple service. Can't use with MultipleFileMode */ 38 sharedServiceOptions?: boolean | undefined 39 /** use parameters in header or not*/ 40 useHeaderParameters?: boolean 41} 42 43const defaultOptions: ISwaggerOptions = { 44 serviceNameSuffix: 'Service', 45 enumNamePrefix: 'Enum', 46 methodNameMode: 'operationId', 47 classNameMode: 'normal', 48 PathClassNameDefaultName: 'Global', 49 outputDir: './service', 50 fileName: 'index.ts', 51 useStaticMethod: true, 52 useCustomerRequestInstance: false, 53 include: [], 54 strictNullChecks: true, 55 /** definition Class mode ,auto use interface mode to streamlined code*/ 56 modelMode?: 'interface', 57 useClassTransformer: false 58} 59
1 2const { codegen } = require('swagger-axios-codegen') 3codegen({ 4 methodNameMode: 'operationId', 5 source: require('./swagger.json') 6}) 7 8
1 2const { codegen } = require('swagger-axios-codegen') 3codegen({ 4 methodNameMode: 'operationId', 5 remoteUrl:'You remote Url' 6}) 7 8
1codegen({ 2 methodNameMode: 'operationId', 3 remoteUrl: 'http://localhost:22742/swagger/v1/swagger.json', 4 outputDir: '.', 5 useStaticMethod: true 6}); 7
before
1 2import { UserService } from './service' 3const userService = new UserService() 4await userService.GetAll(); 5
after
1 2import { UserService } from './service' 3 4await UserService.GetAll(); 5
1import axios from 'axios' 2import { serviceOptions } from './service' 3const instance = axios.create({ 4 baseURL: 'https://some-domain.com/api/', 5 timeout: 1000, 6 headers: {'X-Custom-Header': 'foobar'} 7}); 8 9serviceOptions.axios = instance 10
1import YourLib from '<Your lib>' 2import { serviceOptions } from './service' 3 4serviceOptions.axios = YourLib 5
fliter by multimatch using 'include' setting
1codegen({ 2 methodNameMode: 'path', 3 source: require('../swagger.json'), 4 outputDir: './swagger/services', 5 include: [ 6 '*', 7 // 'Products*', 8 '!Products', 9 { 'User': ['*', '!history'] }, 10 ] 11})
If you are using special characters in your service name (which is the first tag) you must assume they have been escaped.
For example the service names are MyApp.FirstModule.Products
, MyApp.FirstModule.Customers
, MyApp.SecondModule.Orders
:
1// API 2"paths": { 3 "/Products/Get": { 4 "post": { 5 "tags": [ 6 "MyApp.FirstModule.Products" 7 ], 8 "operationId": "Get",
1// Codegen config 2codegen({ 3 methodNameMode: 'path', 4 source: require('../swagger.json'), 5 outputDir: './swagger/services', 6 include: ['MyAppFirstModule*'] // Only Products and Customers will be included. As you can see dots are escaped being contract names. 7})
This is helpful if you want to transform dates to real date
objects. Swagger can define string formats for different types. Two if these formats are date
and date-time
If a class-transformer
is enabled and a format is set on a string, the result string will be transformed to a Date
instance
// swagger.json
1{ 2 "ObjectWithDate": { 3 "type": "object", 4 "properties": { 5 "date": { 6 "type": "string", 7 "format": "date-time" 8 } 9 } 10 } 11}
1 2const { codegen } = require('swagger-axios-codegen') 3codegen({ 4 methodNameMode: 'operationId', 5 source:require('./swagger.json'), 6 useClassTransformer: true, 7})
Resulting class:
1export class ObjectWithDate { 2 @Expose() 3 @Type(() => Date) 4 public date: Date; 5}
The service method will transform the json response and return an instance of this class
1codegen({ 2 ... 3 modelMode: 'class', 4 generateValidationModel: true 5});
The option above among with class model mode allows to render the model validation rules. The result of this will be as follows:
1export class FooFormVm { 2 'name'?: string; 3 'description'?: string; 4 5 constructor(data: undefined | any = {}) { 6 this['name'] = data['name']; 7 this['description'] = data['description']; 8 } 9 10 public static validationModel = { 11 name: { required: true, maxLength: 50 }, 12 description: { maxLength: 250 }, 13 }; 14}
So you can use the validation model in your application:
1function isRequired(vm: any, fieldName: string): boolean {
2 return (vm && vm[fieldName] && vm[fieldName].required === true);
3}
4function maxLength(vm: any, fieldName: string): number {
5 return (vm && vm[fieldName] && vm[fieldName].maxLength ? vm[fieldName].maxLength : 4000);
6}
Now you can use the functions
1var required = isRequired(FooFormVm.validationModel, 'name'); 2var maxLength = maxLength(FooFormVm.validationModel, 'description');
At the moment there are only two rules are supported - required
and maxLength
.
see in #53, use package json-schema-ref-parser
Microservice Gateway
1const {codegen} = require('swagger-axios-codegen') 2const axios = require('axios') 3// host 地址 4const host = 'http://your-host-name' 5 6// 7const modules = [ 8 ... 9] 10 11axios.get(`${host}/swagger-resources`).then(async ({data}) => { 12 console.warn('code', host) 13 for (let n of data) { 14 if (modules.includes(n.name)) { 15 try { 16 await codegen({ 17 remoteUrl: `${host}${n.url}`, 18 methodNameMode: 'operationId', 19 modelMode: 'interface', 20 strictNullChecks: false, 21 outputDir: './services', 22 fileName: `${n.name}.ts`, 23 sharedServiceOptions: true, 24 extendDefinitionFile: './customerDefinition.ts', 25 }) 26 } catch (e) { 27 console.log(`${n.name} service error`, e.message) 28 } 29 } 30 } 31}) 32
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 5/24 approved changesets -- score normalized to 2
Reason
9 existing vulnerabilities detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
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