swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

TypeError: Cannot read properties of undefined (reading 'mergeRequestParams')

Open afridley opened this issue 3 years ago • 3 comments

this may be an issue with next.js is anyone has set something like this up for it

I am recieving the error TypeError: Cannot read properties of undefined (reading 'mergeRequestParams')

On initial generation with the --axios flag though I get the same error without it Am I missing something in the setup.

When I console.log this in the method it is undefined but the api instance is defined and being used

  public request = async <T = any, _E = any>({
    secure,
    path,
    type,
    query,
    format,
    body,
    ...params
  }: FullRequestParams): Promise<AxiosResponse<T>> => {
    const secureParams =
      ((typeof secure === "boolean" ? secure : this.secure) &&
        this.securityWorker &&
        (await this.securityWorker(this.securityData))) ||
      {};
    console.log(this) // undefined
    const requestParams = this.mergeRequestParams(params, secureParams);
    const responseFormat = (format && this.format) || void 0;

    if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
      requestParams.headers.common = { Accept: "*/*" };
      requestParams.headers.post = {};
      requestParams.headers.put = {};

      body = this.createFormData(body as Record<string, unknown>);
    }

    return this.instance.request({
      ...requestParams,
      headers: {
        ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}),
        ...(requestParams.headers || {}),
      },
      params: query,
      responseType: responseFormat,
      data: body,
      url: path,
    });
  };
}
    const cool = async () => {
        const api2 = await new Api()
        console.log('req', api2.request)
        const res = await api2.api.tokensCreate({ email: 'someemail', password: 'pass' }, { secure: false })
        console.log(api)
        console.log(res)
    }
    useEffect(() => cool(), [])

afridley avatar Feb 24 '22 07:02 afridley

Upon further inspection it seems that in the compiled typescript file for httpClient the this keyword is undefined for some reason. Maybe a compiler setting

var HttpClient = /*#__PURE__*/ function() {
    "use strict";
    function HttpClient() {
        var _param1 = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
        var _this = this; // this is undefined but why
        _classCallCheck(this, HttpClient);
        var securityWorker = _param1.securityWorker, secure1 = _param1.secure, format1 = _param1.format, axiosConfig = _objectWithoutProperties(_param1, [
            "securityWorker",
            "secure",
            "format"
        ]);
        this.securityData = null;
        this.setSecurityData = function(data) {
            _this.securityData = data;
        };
        this.request = (function() {
            var _ref = _asyncToGenerator(_home_x2_Projects_cmhp_blue_house_fe_node_modules_next_dist_compiled_regenerator_runtime_runtime_js__WEBPACK_IMPORTED_MODULE_0___default().mark(function _callee(_param) {
                var secure, path, type, query, format, body, params, secureParams, requestParams, responseFormat;
                return _home_x2_Projects_cmhp_blue_house_fe_node_modules_next_dist_compiled_regenerator_runtime_runtime_js__WEBPACK_IMPORTED_MODULE_0___default().wrap(function _callee$(_ctx) {
                    while(1)switch(_ctx.prev = _ctx.next){
                        case 0:
                            secure = _param.secure, path = _param.path, type = _param.type, query = _param.query, format = _param.format, body = _param.body, params = _objectWithoutProperties(_param, [
                                "secure",
                                "path",
                                "type",
                                "query",
                                "format",
                                "body"
                            ]);
                            _ctx.t1 = (typeof secure === "boolean" ? secure : this.secure) && this.securityWorker;
                            if (!_ctx.t1) {
                                _ctx.next = 6;
                                break;
                            }
                            _ctx.next = 5;
                            return this.securityWorker(this.securityData);
                        case 5:
                            _ctx.t1 = _ctx.sent;
                        case 6:
                            _ctx.t0 = _ctx.t1;
                            if (_ctx.t0) {
                                _ctx.next = 9;
                                break;
                            }
                            _ctx.t0 = {};
                        case 9:
                            secureParams = _ctx.t0;
                            requestParams = this.mergeRequestParams(params, secureParams);
                            responseFormat = format && this.format || void 0;
                            if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
                                requestParams.headers.common = {
                                    Accept: "*/*"
                                };
                                requestParams.headers.post = {};
                                requestParams.headers.put = {};
                                body = this.createFormData(body);
                            }
                            return _ctx.abrupt("return", this.instance.request(_objectSpread({}, requestParams, {
                                headers: _objectSpread({}, type && type !== ContentType.FormData ? {
                                    "Content-Type": type
                                } : {}, requestParams.headers || {}),
                                params: query,
                                responseType: responseFormat,
                                data: body,
                                url: path
                            })));
                        case 14:
                        case "end":
                            return _ctx.stop();
                    }
                }, _callee, this);
            }).bind(this)).bind(this);
            return function(_param) {
                return _ref.apply(this, arguments);
            };
        })();
        this.instance = axios__WEBPACK_IMPORTED_MODULE_1___default().create(_objectSpread({}, axiosConfig, {
            baseURL: axiosConfig.baseURL || ""
        }));
        this.secure = secure1;
        this.format = format1;
        this.securityWorker = securityWorker;
    }
    _createClass(HttpClient, [
        {
            key: "mergeRequestParams",
            value: function mergeRequestParams(params1, params2) {
                return _objectSpread({}, this.instance.defaults, params1, params2 || {}, {
                    headers: _objectSpread({}, this.instance.defaults.headers || {}, params1.headers || {}, params2 && params2.headers || {})
                });
            }
        },
        {
            key: "createFormData",
            value: function createFormData(input) {
                return Object.keys(input || {}).reduce(function(formData, key) {
                    var property = input[key];
                    formData.append(key, _instanceof(property, Blob) ? property : typeof property === "object" && property !== null ? JSON.stringify(property) : "".concat(property));
                    return formData;
                }, new FormData());
            }
        }
    ]);
    return HttpClient;
}();

afridley avatar Feb 24 '22 19:02 afridley

I have the same problem (

ryskin avatar Apr 06 '22 07:04 ryskin

Updated next.js to latest and problem dissappear

ryskin avatar Apr 06 '22 07:04 ryskin