Write pre-request and post-response scripts using the Tanxium TypeScript runtime
Yasumu includes a powerful scripting system powered by Tanxium, a custom JavaScript runtime built on Deno. Scripts allow you to manipulate requests, process responses, and create dynamic testing workflows using TypeScript.
Post-response scripts run after receiving the HTTP response:
export function onResponse(req, res) { // Process response data const data = JSON.parse(res.body); console.log('Response data:', data); // Store values in environment for later requests if (data.token) { req.environment.set('AUTH_TOKEN', data.token); } // Check response status if (res.status >= 400) { console.error('Request failed:', res.statusText); }}
Use cases:
Extract data from responses
Update environment variables
Chain requests with dynamic data
Log response details
Test scripts run for assertions and validation:
export function onTest(req, res) { // Run test assertions console.assert(res.status === 200, 'Expected status 200'); const data = JSON.parse(res.body); console.assert(data.id, 'Response should have ID'); console.assert(data.email, 'Response should have email'); console.log('All tests passed!');}
export function onResponse(req, res) { if (res.status === 200) { const data = JSON.parse(res.body); // Store user ID for subsequent requests if (data.user?.id) { req.environment.set('USER_ID', data.user.id); } // Store pagination token if (data.nextToken) { req.environment.set('NEXT_TOKEN', data.nextToken); } // Store authentication token if (data.accessToken) { req.environment.set('ACCESS_TOKEN', data.accessToken); } console.log('Stored variables for next request'); }}
export function onTest(req, res) { // Check status code console.assert( res.status === 200, `Expected status 200, got ${res.status}` ); // Parse response let data; try { data = JSON.parse(res.body); } catch (error) { console.error('Failed to parse JSON:', error); return; } // Validate structure console.assert(data.id, 'Response should have id field'); console.assert(data.name, 'Response should have name field'); console.assert(data.email, 'Response should have email field'); // Validate data types console.assert( typeof data.id === 'string', 'ID should be a string' ); console.assert( data.email.includes('@'), 'Email should be valid' ); console.log('✓ All validations passed');}
Execute different logic based on conditions:
export function onRequest(req, res) { const env = req.environment.get('ENVIRONMENT'); if (env === 'development') { // Use development settings req.headers.set('X-Debug', 'true'); req.headers.set('X-Source', 'yasumu-dev'); } else if (env === 'production') { // Use production settings req.headers.delete('X-Debug'); req.headers.set('X-Source', 'yasumu-prod'); } // Add retry header for failed requests const retryCount = req.environment.get('RETRY_COUNT') || '0'; if (parseInt(retryCount) > 0) { req.headers.set('X-Retry-Count', retryCount); }}
Scripts have full access to environment variables and secrets:
export function onRequest(req, res) { // Get variables const apiUrl = req.environment.get('API_URL'); const version = req.environment.get('API_VERSION'); // Get secrets (same API) const apiKey = req.environment.get('API_KEY'); // Set new values req.environment.set('LAST_REQUEST_TIME', Date.now()); req.environment.set('REQUEST_COUNT', '1'); // Check if variable exists if (req.environment.get('DEBUG') === 'true') { console.log('Debug mode enabled'); }}
Changes to environment variables in scripts are temporary and only persist for the current request execution. To make permanent changes, update the environment through the API.