question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problems building and testing locally the library

See original GitHub issue

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 :

  1. I have created a test project
  2. I have run npm install {local-path-to-your-forked-library} ( i called it test-swagger-axios-codegen just to be able to easier to recognize)
  3. This project has next index.js file :
"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);
  1. 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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:26 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
azakordonetscommented, Aug 12, 2020

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 😃

0reactions
azakordonetscommented, Aug 12, 2020

Manweill i would love to try the latest version, but i don’t know which one is it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting Continuous Integration, or How to Debug ...
Troubleshooting Continuous Integration, or How to Debug Tests That Fail on CI, but Pass Locally · Don't Relaunch the CI Build · Rebase...
Read more >
Build local unit tests - Android Developers
A local test runs directly on your own workstation, rather than an Android device or emulator. As such, it uses your local Java...
Read more >
Library facilities and the inherent planning problems ... - ERIC
Problems in Planning Library Facilities. ... the building problems relevant to their libraries. ... The program should be written locally by the li-....
Read more >
Express Tutorial: The Local Library website - MDN Web Docs
In the first few tutorial articles we will define a simple browse-only library that library members can use to find out what books...
Read more >
Common Build Problems - Travis CI Docs
A very common cause when a test is suddenly breaking without any major code changes involved is a change in upstream dependencies. This...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found