OpenAPITools/openapi-generator

[REQ][javascript] Ability to intercept requests and dynamically compute an authorization header value

Open

#1,955 建立於 2019年1月21日

在 GitHub 查看
 (4 留言) (2 反應) (0 負責人)Java (7,516 fork)batch import
Client: JavaScript/Node.jsEnhancement: FeatureHacktoberfesthelp wanted

倉庫指標

Star
 (26,243 star)
PR 合併指標
 (平均合併 9天 1小時) (30 天內合併 123 個 PR)

描述

Hi,

Is your feature request related to a problem? Please describe.

I generated a client library using openapi-generator-cli and the following command:

java -jar openapi-generator-cli.jar generate -i *.yaml -g javascript -o out

I'm now searching for a way to inject an OAuth header computed from the request without changing the generated code.

Describe the solution you'd like

The ability to pass superagent request interceptors or "authenticators" to the ApiClient instance.

Describe alternatives you've considered

I tried the following:

  1. Override applyAuthToRequest to set the header
client.applyAuthToRequest = function(request) {
    const auth = computeAuthorization(request);
    request.set({'Authorization': auth });
  };

But applyAuthToRequest is called too early, when the request object is not entirely filled.

  1. Override applyAuthToRequest to add a listener to requests
client.applyAuthToRequest = function(request) {
    request.addListener("end", function() {
        // ...
    });
  };

But the end event seems to be the only event we can listen to, and it happens to late.

  1. Pass a custom requestAgent to the ApiClient
const apiClient = require('../../ApiClient.js');
const requestAgent = new require("https").Agent({ /* Options */});
const client = apiClient.instance;
client.requestAgent = requestAgent; 
(...)

In that case the custom agent is used, but I haven't find any way of adding interceptors or listening to requests.

Any help would be appreciated.

貢獻者指南