Commit dbb173b4 authored by Caner Candan's avatar Caner Candan
Browse files

feat(swagger-client-store): add swaggerUrl prop

parent 143c509f
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ class RctAuthStore {
      const { token } = body
      console.log(body)
      commonStore.setToken(token)
      await swaggerClientStore.buildClientWithToken(token)
      await swaggerClientStore.buildClientWithToken({ token })
      runInAction(() => {
        this.inProgress = false
        notificationStore.newMessage('You are logged in!')
+13 −7
Original line number Diff line number Diff line
import { observable, action } from 'mobx'
import Swagger from 'swagger-client';

function getUrlTree(apiUrl) {
function getUrlTree({ apiUrl }) {
  const l = document.createElement('a');
  l.href = apiUrl;
  return {
@@ -9,12 +9,12 @@ function getUrlTree(apiUrl) {
    host: `${l.hostname}:${l.port}`,
  };
}
async function resolveSwagger(apiUrl) {
async function resolveSwagger({ apiUrl, swaggerUrl }) {
  const { spec } = await Swagger.resolve({
    url: `${apiUrl}/swagger`
    url: `${swaggerUrl}/swagger`
  })
  // parse api url and override the host and scheme in the swagger client
  const { host, scheme } = getUrlTree(apiUrl)
  const { host, scheme } = getUrlTree({ apiUrl })
  spec.host = host
  spec.schemes = [scheme]
  return spec
@@ -23,11 +23,17 @@ async function resolveSwagger(apiUrl) {
class RctSwaggerClientStore {
  @observable client = undefined

  @action async buildClient(apiUrl) {
    const spec = await resolveSwagger(apiUrl || 'http://localhost:10010/v1')
  @action async buildClient({ apiUrl, swaggerUrl }) {
    if (!apiUrl) {
      apiUrl = 'http://localhost:10010/v1'
    }
    if (!swaggerUrl) {
      swaggerUrl = apiUrl
    }
    const spec = await resolveSwagger({ apiUrl, swaggerUrl })
    this.client = await new Swagger({ spec })
  }
  @action async buildClientWithToken(token) {
  @action async buildClientWithToken({ token }) {
    this.client = await new Swagger({
      spec: this.client.spec,
      authorizations: {
+3 −1
Original line number Diff line number Diff line
@@ -102,7 +102,9 @@ const HomeComponent = () => (
@observer
class App extends Component {
  async componentWillMount() {
    await this.props.swaggerClientStore.buildClient(process.env.STORYBOOK_API_URL || 'http://localhost:10010/v1')
    await this.props.swaggerClientStore.buildClient({
      apiUrl: process.env.STORYBOOK_API_URL || 'http://localhost:10010/v1'
    })
    console.log(this.props.swaggerClientStore.client)
    this.props.commonStore.setAppLoaded()
  }