swagger-api / swagger-api/swagger-codegen

[Javascript] How can I intercept the response globally?

Open
#8,242 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I want to register a global interceptor to handle the response. We have customized error code that will be returned as a Http 200 successful response.
I have checked the source code I found there is a line of code: this.requestAgent = null;.
Then I did below

let Agent = require('superagent');
let agent = new Agent().use((reqequest) => {
  console.log(request);
  request.on('response', function(response) {
    console.log(response.mydata.responseCode);
    return request;
  })
});
const myService= require('@myService');
myService.ApiClient.instance.requestAgent = agent;

I can see the reqequest from the console, means it got called. but never get the response. If I add .end it calls the API, However, the URL is ttp://localhost:8080/undefined.

I think I didn't do it correctly. Can any one show me an example to how to intercept the response??

I don't want to repeat my code to add those checks in each API's callback...

Swagger-codegen version

2.3.1

Swagger declaration file content or url
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at myService.ApiClient.instance.requestAgent and the superagent .use() response handler, then reproduce the undefined URL and missing response with the generated JavaScript client. Done means the supported global response-interception behavior is identified and demonstrated without breaking the request URL.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.