Skip to content

<rapi-doc> dispatches DOM events you can listen to with addEventListener. The full list, with every field, is in the API reference.

Change a request before it is sent: before-try

Section titled “Change a request before it is sent: before-try”

before-try fires when a reader presses Try, before the request goes out. e.detail.request is a plain object you can change:

Field Type
url string: can be replaced
method string
headers Headers: add, change or delete headers
body the request body
credentials the fetch credentials mode

e.detail.controller is the request’s AbortController; call abort() to cancel it.

const doc = document.querySelector('rapi-doc');
doc.addEventListener('before-try', (e) => {
const { request } = e.detail;
request.headers.set('X-Correlation-ID', crypto.randomUUID());
if (request.url.includes('/billing/')) {
request.headers.set('X-Audit', 'strict');
}
});

Listeners must be synchronous

RapiDoc reads the request as soon as the event returns, so it doesn’t wait for promises. Work out any value you need (a signature, a fresh token) before the reader presses Try, and set it here synchronously.

after-try fires when the response arrives:

Field Type
request the Request that was sent
response a clone of the Response
responseHeaders the response headers
responseBody the parsed body
responseStatus boolean: true for a 2xx status

If the request fails at the network level, detail is { err, request } instead.

doc.addEventListener('after-try', (e) => {
if (e.detail.err) return;
analytics.track('Try', {
url: e.detail.request.url,
status: e.detail.response.status,
});
});

To measure how long a request took, note performance.now() in before-try and compare in after-try.

React to the spec: before-render and spec-loaded

Section titled “React to the spec: before-render and spec-loaded”
  • before-render fires after the spec is parsed and before it is rendered. e.detail.spec is the parsed spec; changes you make to it show up in the page.
  • spec-loaded fires when rendering is done. e.detail is the parsed spec itself (info, tags, servers, securitySchemes, …).
doc.addEventListener('spec-loaded', (e) => {
console.log(`${e.detail.info.title}: ${e.detail.servers.length} server(s)`);
});

Fires when the reader picks another server in the API Servers section. e.detail.selectedServer is the server object: url, description, variables and computedUrl (the URL with its variables filled in).

doc.addEventListener('api-server-change', (e) => {
if (e.detail.selectedServer.url.includes('production')) {
showWarning('Try now calls the production API.');
}
});

The reference lists every event and its detail. Two have guides of their own: nav-active-change, in Links & Navigation, and the OAuth events before-authorize and before-token-request, in OAuth 2.0.