Skip to content

Commit 2235b84

Browse files
committed
allow swagger to be deployed on a forked repo for testing
1 parent aba58f4 commit 2235b84

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

docs/src/components/SwaggerUI.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
import React from 'react';
22
import SwaggerUI from 'swagger-ui-react';
33
import 'swagger-ui-react/swagger-ui.css';
4-
// eslint-disable-next-line import/no-webpack-loader-syntax
5-
import localSpec from '!!raw-loader!@signer-api-spec';
64

7-
const IS_LOCALHOST = typeof window !== 'undefined' && window.location.hostname === 'localhost';
5+
function getSpecUrl() {
6+
if (typeof window === 'undefined') return null;
87

9-
const PROD_URL = 'https://raw.githubusercontent.com/Commit-Boost/commit-boost-client/main/api/signer-api.yml';
8+
// localhost dev: webpack bundles the spec via raw-loader
9+
if (window.location.hostname === 'localhost') return null;
10+
11+
// Production GitHub Pages: derive org/repo/branch from the current URL
12+
// e.g. https://commit-boost.github.io/commit-boost-client/ → Commit-Boost/commit-boost-client/main
13+
const host = window.location.hostname; // e.g. commit-boost.github.io
14+
const org = host.replace('.github.io', ''); // e.g. commit-boost → Commit-Boost
15+
const pathParts = window.location.pathname.split('/').filter(Boolean);
16+
const repo = pathParts[0] || 'commit-boost-client';
17+
const branch = window.location.searchParams?.get('branch') || 'main';
18+
19+
return `https://raw.githubusercontent.com/${org}/${repo}/${branch}/api/signer-api.yml`;
20+
}
1021

1122
const SwaggerUIComponent = () => {
12-
if (IS_LOCALHOST) {
23+
const specUrl = getSpecUrl();
24+
if (!specUrl) {
25+
// eslint-disable-next-line @typescript-eslint/no-var-requires
26+
const localSpec = require('!!raw-loader!@signer-api-spec').default;
1327
return <SwaggerUI spec={localSpec} />;
1428
}
15-
return <SwaggerUI url={PROD_URL} />;
29+
return <SwaggerUI url={specUrl} />;
1630
};
1731

1832
export default SwaggerUIComponent;

0 commit comments

Comments
 (0)