Skip to content

Commit

Permalink
feat: re-retch schema, when authorization is changed swagger-api#7425
Browse files Browse the repository at this point in the history
  • Loading branch information
jtamm-red committed Sep 8, 2021
1 parent 325909f commit 1eb2840
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
6 changes: 5 additions & 1 deletion docker/configurator/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ const standardVariables = {
WITH_CREDENTIALS: {
type: "boolean",
name: "withCredentials",
}
},
RE_FETCH_SCHEMA_ON_AUTH_CHANGED: {
type: "boolean",
name: "reFetchSchemaOnAuthChanged",
},
}

const legacyVariables = {
Expand Down
1 change: 1 addition & 0 deletions docs/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Parameter name | Docker variable | Description
Parameter name | Docker variable | Description
--- | --- | -----
<a name="persistAuthorization"></a>`persistAuthorization` | `PERSIST_AUTHORIZATION` | `Boolean=false`. If set to `true`, it persists authorization data and it would not be lost on browser close/refresh
<a name="persistAuthorization"></a>`reFetchSchemaOnAuthChanged` | `RE_FETCH_SCHEMA_ON_AUTH_CHANGED` | `Boolean=false`. If set to `true`, refresh to fetch schema, when authorization is changed

### Instance methods

Expand Down
4 changes: 3 additions & 1 deletion src/core/components/auth/authorization-popup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class AuthorizationPopup extends React.Component {
}

render() {
let { authSelectors, authActions, getComponent, errSelectors, specSelectors, fn: { AST = {} } } = this.props
let { authSelectors, authActions, getComponent, errSelectors, specSelectors, fn: { AST = {} }, specActions, getConfigs } = this.props
let definitions = authSelectors.shownDefinitions()
const Auths = getComponent("auths")

Expand Down Expand Up @@ -38,6 +38,8 @@ export default class AuthorizationPopup extends React.Component {
errSelectors={ errSelectors }
authSelectors={ authSelectors }
authActions={ authActions }
specActions={specActions}
getConfigs={getConfigs}
specSelectors={ specSelectors }/>
})
}
Expand Down
17 changes: 13 additions & 4 deletions src/core/components/auth/auths.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ export default class Auths extends React.Component {
submitAuth =(e) => {
e.preventDefault()

let { authActions } = this.props
let { authActions, getConfigs } = this.props
authActions.authorizeWithPersistOption(this.state)
if(getConfigs().reFetchSchemaOnAuthChanged) {
this.props.specActions.download()
this.close(e)
}
}

logoutClick =(e) => {
e.preventDefault()

let { authActions, definitions } = this.props
let { authActions, definitions, getConfigs } = this.props
let auths = definitions.map( (val, key) => {
return key
}).toArray()
Expand All @@ -44,6 +47,10 @@ export default class Auths extends React.Component {
}, {}))

authActions.logoutWithPersistOption(auths)
if(getConfigs().reFetchSchemaOnAuthChanged) {
this.props.specActions.download()
this.close(e)
}
}

close =(e) => {
Expand Down Expand Up @@ -125,6 +132,8 @@ export default class Auths extends React.Component {
authSelectors: PropTypes.object.isRequired,
specSelectors: PropTypes.object.isRequired,
authActions: PropTypes.object.isRequired,
definitions: ImPropTypes.iterable.isRequired
definitions: ImPropTypes.iterable.isRequired,
specActions: ImPropTypes.iterable.isRequired,
getConfigs: ImPropTypes.iterable.isRequired,
}
}
39 changes: 36 additions & 3 deletions src/core/plugins/download-url.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { createSelector } from "reselect"
import { Map } from "immutable"
import win from "../window"
import { applySecurities as applySecurities3 } from "swagger-client/lib/execute/oas3/build-request"
import { applySecurities as applySecurities2 } from "swagger-client/lib/execute/swagger2/build-request"
import { isOAS3 } from "swagger-client/es/helpers"

export default function downloadUrlPlugin (toolbox) {
let { fn } = toolbox

const actions = {
download: (url)=> ({ errActions, specSelectors, specActions, getConfigs }) => {
download: (url)=> ({ errActions, specSelectors, specActions, getConfigs, authSelectors}) => {
let { fetch } = fn
const config = getConfigs()
url = url || specSelectors.url()
specActions.updateLoadingStatus("loading")
errActions.clear({source: "fetch"})
fetch({
let request = {
url,
loadSpec: true,
requestInterceptor: config.requestInterceptor || (a => a),
Expand All @@ -21,7 +24,37 @@ export default function downloadUrlPlugin (toolbox) {
headers: {
"Accept": "application/json,*/*"
}
}).then(next,next)
}
if(config.reFetchSchemaOnAuthChanged) {
const spec = specSelectors.specJsonWithResolvedSubtrees().toJS()
if(spec) {
let securities = {
authorized: authSelectors.authorized() && authSelectors.authorized().toJS(),
definitions: specSelectors.securityDefinitions() && specSelectors.securityDefinitions().toJS(),
specSecurity: specSelectors.security() && specSelectors.security().toJS()
}
const operation = {
security: Object.keys(securities.authorized).map(key => ({[key]: []}))
};
if(isOAS3(spec)) {
applySecurities3({
request,
securities,
operation,
spec,
})
}
else {
applySecurities2({
request,
securities,
operation,
spec
})
}
}
}
fetch(request).then(next,next)

function next(res) {
if(res instanceof Error || res.status >= 400) {
Expand Down

0 comments on commit 1eb2840

Please sign in to comment.