You can dynamically configure request values for Replay by ejecting and customizing the useConfigureReplay.ts
file. This lets you fetch configuration from external APIs at runtime, updating based on the current operation. The useConfigureReplay
hook fetches fresh configuration when the Replay is opened or when the 'Reset request' button is clicked, ensuring you always get the latest configuration.
Make sure you have the following:
- a
redocly.yaml
file located in the root directory of your project - a basic understanding of TypeScript and React hooks
- OpenAPI description files with examples
- an external API endpoint to fetch configuration from
To customize dynamic Replay values, first eject the configuration file:
npx @redocly/cli eject component ext/useConfigureReplay.ts
This command creates a local copy of useConfigureReplay.ts
in your project's @theme
folder.
The useConfigureReplay.ts
file exports a useConfigureReplay
hook that fetches request values dynamically. The hook receives a context parameter with operation details and returns an object containing the configuration and a refresh function that can be used to manually reload the configuration.
Here are examples of how to implement dynamic Replay configuration:
import { useEffect, useState, useCallback } from 'react';
import type {
ConfigureRequestValues,
ConfigureServerRequestValues,
} from '@redocly/theme/ext/configure';
import type { UserClaims, OpenAPIServer } from '@redocly/theme/core/types';
type ContextProps = {
operation: {
name: string;
path: string;
operationId: string;
href: string;
method: string;
};
servers: OpenAPIServer[];
userClaims: UserClaims;
};
async function getReplayConfiguration(
context: ContextProps,
): Promise<ConfigureRequestValues | ConfigureServerRequestValues | null> {
try {
const response = await fetch(`/api/replay-config/${context.operation.operationId}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const { token } = await response.json();
// Return global request values that apply to all servers
return {
security: {
token: {
access_token: token
}
}
};
} catch (error) {
console.warn('Failed to fetch replay configuration:', error);
throw error;
}
}
export function useConfigureReplay(context: ContextProps, isOpened: boolean) {
const [config, setConfig] = useState<
ConfigureRequestValues | ConfigureServerRequestValues | null
>();
const refresh = useCallback(async () => {
try {
const result = await getReplayConfiguration(context);
setConfig(result);
} catch (error) {
console.warn(
'Failed to configure replay for operation:',
context.operation.operationId,
error,
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
if (isOpened) {
refresh();
}
}, [isOpened, refresh]);
return { config, refresh };
}
Dynamic Replay configuration is useful for:
- Token management: fetching fresh access tokens from your authentication service
- User-specific data: including user context in requests
- Environment-specific values: retrieving configuration based on the current environment
- Server variable configuration: dynamically setting server variables for URLs
- External service integration: fetching configuration from third-party services
The context parameter includes:
userClaims
: information about the authenticated user and their original IdP tokensemail
: user emailname
: user namefederatedAccessToken
: access token of the original identity provider. RequiresREDOCLY_OAUTH_USE_INTROSPECT
to be set totrue
in the environment variables.federatedIdToken
: ID token of the original identity provider. RequiresREDOCLY_OAUTH_USE_INTROSPECT
to be set totrue
in the environment variables.
operation
: details about the current API operationname
: operation namepath
: API pathoperationId
: operation IDhref
: operation URLmethod
: HTTP method
servers
: server information from the OpenAPI description
The useConfigureReplay
hook returns an object with:
config
: the request values configuration, which can be:ConfigureRequestValues
: set global request values that apply to all serversConfigureServerRequestValues
: set different request values per server URLnull
: when configuration cannot be fetched or an error occurs
refresh
: a function to manually refresh the configuration when called
- Configure request values - Set static request values for headers, parameters, and security details in your OpenAPI descriptions
- Component ejection guide - Learn to eject and customize built-in components for advanced request value handling and UI modifications