Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
107 lines
4.4 KiB
TypeScript
107 lines
4.4 KiB
TypeScript
import express, { Router } from 'express';
|
|
import path from 'path';
|
|
import { mount, routes } from './route';
|
|
import { buildOpenApi } from './openapi';
|
|
import { errorHandler, notFoundHandler } from './errors';
|
|
import auth from '../middleware/authMiddleware';
|
|
// Resources: importing one registers its routes.
|
|
// eventsManage first: /events/manage must match before /events/:id.
|
|
import './resources/eventsManage';
|
|
import './resources/eventDetails';
|
|
import './resources/setup';
|
|
import './resources/seriesDeals';
|
|
import './resources/events';
|
|
import './resources/me';
|
|
import './resources/registrations';
|
|
import './resources/registrationsManage';
|
|
import './resources/money';
|
|
import './resources/attendance';
|
|
import './resources/kidsVolunteers';
|
|
import './resources/peopleMessaging';
|
|
import './resources/adminPrivacy';
|
|
import './resources/checkin';
|
|
import './resources/auth';
|
|
|
|
const pkg = require('../../package.json') as { version: string };
|
|
|
|
/** /api/v2 — see route.ts for how endpoints are declared, openapi.ts for the docs. */
|
|
export function v2Router(): Router {
|
|
const router = express.Router();
|
|
|
|
// The API description is for the people building on it (admins), like /docs.
|
|
router.get('/openapi.json', auth.protect, auth.admin, (_req, res) => {
|
|
res.json(buildOpenApi({ version: pkg.version }));
|
|
});
|
|
|
|
mount(router, routes);
|
|
router.use(notFoundHandler);
|
|
router.use(errorHandler);
|
|
return router;
|
|
}
|
|
|
|
/**
|
|
* /docs/v2: Swagger UI (served from swagger-ui-dist, nothing from a CDN). The page itself is
|
|
* public; it asks for an admin token and uses it to load openapi.json and to try requests.
|
|
*/
|
|
export function v2DocsRouter(): Router {
|
|
const router = express.Router();
|
|
const assets = path.dirname(require.resolve('swagger-ui-dist/package.json'));
|
|
router.use('/assets', express.static(assets, { index: false, maxAge: '1d' }));
|
|
router.get('/', (_req, res) => {
|
|
res.type('html').send(DOCS_HTML);
|
|
});
|
|
return router;
|
|
}
|
|
|
|
const DOCS_HTML = `<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Cross Events API v2</title>
|
|
<link rel="stylesheet" href="/docs/v2/assets/swagger-ui.css">
|
|
<style>
|
|
body { margin: 0; font-family: system-ui, sans-serif; }
|
|
#login { max-width: 40rem; margin: 3rem auto; padding: 0 1rem; }
|
|
#login input { width: 100%; padding: .5rem; font: inherit; border: 1px solid #9ca3af; border-radius: 6px; box-sizing: border-box; }
|
|
#login button { margin-top: .75rem; padding: .5rem 1rem; font: inherit; background: #1d4ed8; color: #fff; border: 0; border-radius: 6px; cursor: pointer; }
|
|
#login .err { color: #b91c1c; margin-top: .5rem; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<main id="login" hidden>
|
|
<h1>Cross Events API v2</h1>
|
|
<p>The API reference is for admins. Paste your admin token (after signing in, it's under <code>hope_events_token</code> in the browser's local storage).</p>
|
|
<label for="token">Admin token</label>
|
|
<input id="token" autocomplete="off">
|
|
<button id="go" type="button">Show the API</button>
|
|
<p class="err" id="err" role="alert"></p>
|
|
</main>
|
|
<div id="ui"></div>
|
|
<script src="/docs/v2/assets/swagger-ui-bundle.js"></script>
|
|
<script>
|
|
(function () {
|
|
var params = new URLSearchParams(location.search);
|
|
var token = params.get('token') || sessionStorage.getItem('v2docs_token') || localStorage.getItem('hope_events_token') || '';
|
|
function show(t) {
|
|
fetch('/api/v2/openapi.json', { headers: { Authorization: 'Bearer ' + t } }).then(function (r) {
|
|
if (!r.ok) return r.json().then(function (b) { throw new Error((b.error && b.error.message) || r.statusText); });
|
|
return r.json();
|
|
}).then(function (spec) {
|
|
sessionStorage.setItem('v2docs_token', t);
|
|
if (params.has('token')) history.replaceState(null, '', location.pathname);
|
|
document.getElementById('login').hidden = true;
|
|
var ui = SwaggerUIBundle({ spec: spec, dom_id: '#ui', deepLinking: true, persistAuthorization: false, docExpansion: 'none', filter: true });
|
|
ui.preauthorizeApiKey('bearerAuth', t);
|
|
}).catch(function (e) {
|
|
document.getElementById('login').hidden = false;
|
|
document.getElementById('err').textContent = t ? 'That token did not work: ' + e.message : '';
|
|
});
|
|
}
|
|
document.getElementById('go').addEventListener('click', function () { show(document.getElementById('token').value.trim()); });
|
|
show(token);
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>`;
|