Skip to content

Struggling to find good documentation describing how to use javascript ApiClient with a basePath override #9179

@davesargrad

Description

@davesargrad

I generate my client code in this fashion:
openapi-generator-cli generate -i ../api/api_v1.json -g javascript -o .

I've successfully been able to exercise my API on a dev server. However the ApiClient uses a hard-coded basePath

class ApiClient {
    constructor() {
        /**
         * The base URL against which to resolve every API call's (relative) path.
         * @type {String}
         * @default http://192.168.56.101:8080
         */
        this.basePath = 'http://192.168.56.101:8080'.replace(/\/+$/, '');

I currently invoke my API as follows:

let apiInstance = new VideoDescriptorApi();
export function requestPostVideoDescriptor(videoDescriptor) {
	let p = new Promise((resolve, reject) => {
		apiInstance.addVideoDescriptor(

where addVideoDescriptor is inside the generated code. It in turn uses ApiClient. That method, invokes the ApiClient as follows:

    return this.apiClient.callApi(
        '/video_descriptor', 'POST',
        pathParams, queryParams, headerParams, formParams, postBody,
        authNames, contentTypes, accepts, returnType, null, callback
      );

Its not clear to me how to override the ApiClient hardcoded basePath (http://192.168.56.101:8080). Ultimately my service will run in kubernetes and I dont know the URL apriori.

How can I change the basepath, without having to regenerate client code?

How would I modify the following code to use an alternative base URL (e.g. replace the hard-coded http://192.168.56.101:8080 with https://example.com:3000)?

import {ApiClient, VideoDescriptorApi} from "api/learning_center/v2";
let defaultClient = ApiClient.instance;
// Configure API key authorization: api_key
let api_key = defaultClient.authentications["api_key"];
api_key.apiKey = "YOUR API KEY";
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//api_key.apiKeyPrefix = 'Token';
let apiInstance = new VideoDescriptorApi();
export function requestPostVideoDescriptor(videoDescriptor) {
	let p = new Promise((resolve, reject) => {
		apiInstance.addVideoDescriptor(
			videoDescriptor,
			(error, data, response) => {
				if (error) {
					console.error("requestPostVideoDescriptor error", error);
					reject(error);
				} else {
					resolve({ data: data, response: response });
				}
			}
		);
	});
	return p;
}

I've searched for a while online. I certainly have found related issues (where other developers were looking for the same answer), but no clear example.

I doubt that this is a code bug (please forgive my posting it as a bug- git really did not provide an alternative, since I dont view it as a feature request either). However I definitely view it as a "documentation bug", or at least a documentation shortcoming.

I am sure that there must be an easy way to do this in my client code.

Please help me with a tweak to the code snippet above, that illustrates how I'd configure the ApiClient and the VideoDescriptorApi to use an alternative basePath (https://example.com:3000).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions