Manweill / swagger-axios-codegen

swagger client to use axios and typescript
MIT License
306 stars 83 forks source link

Add baseUrl option into ISwaggerOptions #12

Closed Formyown closed 5 years ago

Formyown commented 5 years ago

I know there is a way to customize base url for all APIs, but it's better to add an option into ISwaggerOption. Cause most time we need to specify the backend server host and port.

What I want is some thing looks like:

swaggerCodegen.codegen({
    useStaticMethod: false,
    baseUrl: "http://localhost:8000/", // here we can set base url for APIs
    remoteUrl: "http://localhost:8000/v2/api-docs",
    outputDir: "./src/service/",
    fileName: "ApiServices.ts"
});

and I look through the source code, there may useless tips(lol):

${options.useStaticMethod ? 'static' : ''} ${camelcase(name)}(${parameters}options:IRequestOptions={}):Promise<${responseType}> {
  return new Promise((resolve, reject) => {
    const configs:IRequestConfig = {...options, method: "${method}" };
    configs.headers = {
      ...options.headers,
      'Content-Type':'${contentType}'
    }
    let url = '${path}'  // add new features here?  let url = '${baseUrl + path}' 
    ${pathReplace}
    configs.url = url
    ${parsedParameters && queryParameters.length > 0
        ? 'configs.params = {' + queryParameters.join(',') + '}'
        : ''}
    let data = ${parsedParameters && bodyParameters.length > 0
        ? '{' + bodyParameters.join(',') + '}'
Manweill commented 5 years ago

@Formyown If u use Axios, u can set baseUrl like

axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

or

const instance = axios.create({
  baseURL: 'https://api.example.com'
});