swagger-axios-codegen: Problems building and testing locally the library
Hi, i’m trying to add a feature to your library that would allow to create multiple instances of the same service with different settings. However, i faced with some weird issues that i cannot figure out and i was hoping that you could help me. Here’s a basic setup that doesn’t want to work for me :
- I have created a test project
- I have run
npm install {local-path-to-your-forked-library}( i called ittest-swagger-axios-codegenjust to be able to easier to recognize) - This project has next
index.jsfile :
"use strict";
exports.__esModule = true;
var swagger_axios_codegen_1 = require("test-swagger-axios-codegen");
var defaultOptions = {
serviceNameSuffix: 'Service',
enumNamePrefix: 'Enum',
methodNameMode: 'operationId',
outputDir: './service',
fileName: 'service.index.ts',
useStaticMethod: false,
useCustomerRequestInstance: true,
include: [],
strictNullChecks: true,
/** definition Class mode ,auto use interface mode to streamlined code*/
modelMode: 'interface',
useClassTransformer: false,
source: require('./swagger.json')
};
swagger_axios_codegen_1.codegen(defaultOptions);
- When i run this file using local version of the library, my service template has this broken part of the code :
function serviceHeader(options, basePath) {
const classTransformerImport = options.useClassTransformer
? `import { Expose, Transform, Type, plainToClass } from 'class-transformer';
`
: '';
return `/** Generate by swagger-axios-codegen */
// tslint:disable
/* eslint-disable */
import axiosStatic, { AxiosInstance } from 'axios';
const basePath = '${utils_1.trimString(basePath, '/', 'right')}'
${classTransformerImport}
export interface IRequestOptions {
headers?: any;
baseURL?: string;
responseType?: string;
}
export interface IRequestConfig {
method?: any;
headers?: any;
url?: any;
data?: any;
params?: any;
}
// Add options interface
export interface ServiceOptions {
axios?: AxiosInstance,
}
${requestHeader()}
`;
}
export interface IList<T> extends Array<T> {}
export interface List<T> extends Array<T> {}
export interface IDictionary<TValue> {
[key: string]: TValue;
}
This is because utils_1.trimString is not recognized as a function.
I tried my test code with your package from npm and it works just fine. Before installing this local library i did run npm run prepublish command.
This is how transpiled serviceHeader.js looks like.
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const genericTypeDefinitionTemplate_1 = require("./genericTypeDefinitionTemplate");
const utils_1 = require("../utils");
function serviceHeader(options, basePath) {
const classTransformerImport = options.useClassTransformer
? `import { Expose, Transform, Type, plainToClass } from 'class-transformer';
` : '';
return `/** Generate by swagger-axios-codegen */
// tslint:disable
/* eslint-disable */
import axiosStatic, { AxiosInstance } from 'axios';
const basePath = '${utils_1.trimString(basePath, '/', 'right')}'
${classTransformerImport}
export interface IRequestOptions {
headers?: any;
baseURL?: string;
responseType?: string;
}
export interface IRequestConfig {
method?: any;
headers?: any;
url?: any;
data?: any;
params?: any;
}
// Add options interface
export interface ServiceOptions {
axios?: AxiosInstance,
}
${requestHeader()}
`;
}
exports.serviceHeader = serviceHeader;
function customerServiceHeader(options, basePath) {
return `/** Generate by swagger-axios-codegen */
// tslint:disable
/* eslint-disable */
export interface IRequestOptions {
headers?: any;
}
export interface IRequestPromise<T=any> extends Promise<IRequestResponse<T>> {}
export interface IRequestResponse<T=any> {
data: T;
status: number;
statusText: string;
headers: any;
config: any;
request?: any;
}
export interface IRequestInstance {
(config: any): IRequestPromise;
(url: string, config?: any): IRequestPromise;
request<T = any>(config: any): IRequestPromise<T>;
}
export interface IRequestConfig {
method?: any;
headers?: any;
url?: any;
data?: any;
params?: any;
}
const basePath = '${basePath}'
// Add options interface
export interface ServiceOptions {
axios?: IRequestInstance,
}
${requestHeader()}
`;
}
exports.customerServiceHeader = customerServiceHeader;
function requestHeader() {
return `
// Add default options
export const serviceOptions: ServiceOptions = {
};
// Instance selector
export function axios(configs: IRequestConfig, resolve: (p: any) => void, reject: (p: any) => void): Promise<any> {
if (serviceOptions.axios) {
return serviceOptions.axios.request(configs).then(res => {
resolve(res.data);
})
.catch(err => {
reject(err);
});
} else {
throw new Error('please inject yourself instance like axios ')
}
}
export function getConfigs(method: string, contentType: string, url: string,options: any):IRequestConfig {
url = basePath + url
const configs: IRequestConfig = { ...options, method, url };
configs.headers = {
...options.headers,
'Content-Type': contentType,
};
return configs
}
`;
}
function definitionHeader(fileDir) {
let fileStr = '// empty ';
if (!!fileDir) {
console.log('extendDefinitionFile url : ', path.resolve(fileDir));
if (fs.existsSync(path.resolve(fileDir))) {
const buffs = fs.readFileSync(path.resolve(fileDir));
fileStr = buffs.toString('utf8');
}
}
return `
${genericTypeDefinitionTemplate_1.universalGenericTypeDefinition()}
${genericTypeDefinitionTemplate_1.abpGenericTypeDefinition()}
// customer definition
${fileStr}
`;
}
exports.definitionHeader = definitionHeader;
//# sourceMappingURL=serviceHeader.js.map
Am i missing something in my setup ? Tried to run this with node 14 and node 10 - same result.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 26 (8 by maintainers)
I just saw that you changed something in your library 10 days ago and gave a try with latest master again and the problem seem to disappear. Thanks for updating the library 😃