From d2c9189a7cc85e92813386d904cb522500335515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Mon, 26 Sep 2016 18:33:12 +0200 Subject: [PATCH] Feature: add responseHooks option for callbacks on responses Add a responseHooks option that allows the user to process the response of an API call. The responseHooks option is an object where key is an operationId and value is a function that receives the XHR response object and the OperationView object. Use case: process authentication requests done with the "Try out!" button to inject apiKey in the AuthView and the router on success. --- src/main/javascript/view/OperationView.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/javascript/view/OperationView.js b/src/main/javascript/view/OperationView.js index 8c7c9b8a..8d45337b 100644 --- a/src/main/javascript/view/OperationView.js +++ b/src/main/javascript/view/OperationView.js @@ -788,6 +788,11 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({ $('.request_headers', $(this.el)).html('
' + _.escape(JSON.stringify(requestHeaders, null, '  ')).replace(/\n/g, '
') + '
'); } + // Call user-defined hook + if (opts.responseHooks && opts.responseHooks[this.nickname]) { + opts.responseHooks[this.nickname](response, this); + } + var response_body_el = $('.response_body', $(this.el))[0]; // only highlight the response if response is less than threshold, default state is highlight response if (opts.highlightSizeThreshold && typeof response.data !== 'undefined' && response.data.length > opts.highlightSizeThreshold) {