Files
e-ticket/assets/modules/api-env-switcher.js
Serreau Jovann bb35e0d8ae Add Insomnia export and dynamic hostname for API doc
Insomnia export (/api/doc/insomnia.json):
- Generates Insomnia v4 export format with all API routes
- Workspace with environment variables (base_url, env, email, password, jwt_token)
- Folders per section (Auth, Events, Categories, Billets, Scanner)
- Each request with correct method, URL with Insomnia template vars, headers, body
- Auth routes use base_url directly, others use base_url/api/{env}/...
- Download button (indigo) next to Spec JSON button

Dynamic hostname:
- Insomnia export uses request.getSchemeAndHttpHost() (not hardcoded)
- Template passes host via data-host attribute
- JS env switcher reads host from data-host or falls back to location.origin
- Base URLs update dynamically when switching sandbox/live

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 19:35:36 +01:00

55 lines
1.8 KiB
JavaScript

const BTN_BASE = 'env-btn px-5 py-2 font-black uppercase text-xs tracking-widest transition-all cursor-pointer '
function getEnvs(host) {
return {
sandbox: {
prefix: '/api/sandbox',
baseUrl: host + '/api/sandbox',
color: 'text-orange-400',
btnBg: 'bg-orange-500',
desc: 'Environnement de test. Les donnees ne sont pas modifiees.',
},
live: {
prefix: '/api/live',
baseUrl: host + '/api/live',
color: 'text-green-400',
btnBg: 'bg-green-600',
desc: 'Environnement de production. Les donnees sont reelles.',
},
}
}
function switchEnv(env, envs) {
const config = envs[env]
if (!config) return
document.querySelectorAll('.env-btn').forEach(btn => {
const isActive = btn.dataset.env === env
btn.className = BTN_BASE + (isActive ? config.btnBg + ' text-white' : 'bg-gray-800 text-gray-400 hover:text-white')
})
const baseUrlEl = document.getElementById('env-base-url')
if (baseUrlEl) baseUrlEl.textContent = config.baseUrl
const descEl = document.getElementById('env-description')
if (descEl) descEl.textContent = config.desc
document.querySelectorAll('.api-env-prefix').forEach(el => {
el.textContent = config.prefix
el.className = 'api-env-prefix ' + config.color
})
}
export function initApiEnvSwitcher() {
const switcher = document.getElementById('env-switcher')
if (!switcher) return
const hostEl = document.querySelector('[data-host]')
const host = hostEl ? hostEl.dataset.host : globalThis.location.origin
const envs = getEnvs(host)
document.querySelectorAll('.env-btn').forEach(btn => {
btn.addEventListener('click', () => switchEnv(btn.dataset.env, envs))
})
}