-
Notifications
You must be signed in to change notification settings - Fork 9
feat: prompt variable supported for bruno api docs #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
arpit-bruno
merged 7 commits into
usebruno:release/v0.4.0
from
sachin-bruno:sachin-bruno/prompt-variables
Sep 28, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
af03f80
feat: prompt variable changes for the bruno api docs
sachin-bruno 8cd3ced
fix: comments addressed
sachin-bruno 94ae049
fix: conflicts resolved
sachin-bruno 89a4ec4
fix: comments addressed
sachin-bruno 0cc57b4
fix: removed comments
sachin-bruno 65d8a4b
fix: disabled checkbox not considered for prompt variables
sachin-bruno 95c3aee
fix: lint errors and body filtering
sachin-bruno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
packages/bruno-api-docs/e2e/tests/playground/playground-prompt-variables.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import type { Locator, Page, Route } from '@playwright/test'; | ||
| import { test, expect } from '../../playwright'; | ||
|
|
||
| const VARS_PLAYGROUND = '/?fixture=vars#/?pg=1&dock=bottom'; | ||
|
|
||
| test.describe('Playground prompt variables', () => { | ||
| test.beforeEach(async ({ page, playground }) => { | ||
| await page.goto(VARS_PLAYGROUND); | ||
| await playground.runner.waitFor({ state: 'visible' }); | ||
| await playground.openTreeItem(['Customers', 'Variables Demo']); | ||
| await playground.view.waitFor({ state: 'visible' }); | ||
| await playground.envSwitcher.selectEnvironment('Dev'); | ||
| }); | ||
|
|
||
| const armRequest = async (page: Page, urlInput: Locator, url: string) => { | ||
| const sent: string[] = []; | ||
| await page.route('**/*', async (route: Route) => { | ||
| const target = route.request().url(); | ||
| if (target.startsWith('http') && !target.includes('127.0.0.1') && !target.includes('localhost')) { | ||
| sent.push(target); | ||
| await route.fulfill({ status: 200, contentType: 'application/json', body: '{}' }); | ||
| return; | ||
| } | ||
| await route.continue(); | ||
| }); | ||
|
|
||
| await urlInput.fill(url); | ||
| await expect(urlInput).toHaveValue(url); | ||
| return sent; | ||
| }; | ||
|
|
||
| test('asks for a value before sending and interpolates what the reader typed', async ({ page, playground, responsePane }) => { | ||
| const sent = await armRequest(page, playground.urlInput, 'https://api.example.com/otp/{{?OTP}}'); | ||
|
|
||
| await responsePane.send(); | ||
|
|
||
| const dialog = page.getByTestId('prompt-variables-modal-content'); | ||
| await expect(dialog).toBeVisible(); | ||
| await expect(page.getByText('Input Required')).toBeVisible(); | ||
|
|
||
| await page.getByTestId('prompt-variable-input-0').fill('123456'); | ||
| await page.getByTestId('prompt-variables-submit').click(); | ||
|
|
||
| await expect(dialog).toHaveCount(0); | ||
| await expect.poll(() => sent).toContain('https://api.example.com/otp/123456'); | ||
| }); | ||
|
|
||
| test('submits on Enter, so the reader never has to reach for the mouse', async ({ page, playground, responsePane }) => { | ||
| const sent = await armRequest(page, playground.urlInput, 'https://api.example.com/otp/{{?OTP}}'); | ||
|
|
||
| await responsePane.send(); | ||
| await expect(page.getByTestId('prompt-variables-modal-content')).toBeVisible(); | ||
|
|
||
| await page.keyboard.type('654321'); | ||
| await page.keyboard.press('Enter'); | ||
|
|
||
| await expect(page.getByTestId('prompt-variables-modal-content')).toHaveCount(0); | ||
| await expect.poll(() => sent).toContain('https://api.example.com/otp/654321'); | ||
| }); | ||
|
|
||
| test('asks again on the next send, because answers are never kept', async ({ page, playground, responsePane }) => { | ||
| const sent = await armRequest(page, playground.urlInput, 'https://api.example.com/otp/{{?OTP}}'); | ||
|
|
||
| await responsePane.send(); | ||
| await page.getByTestId('prompt-variable-input-0').fill('111111'); | ||
| await page.getByTestId('prompt-variables-submit').click(); | ||
| await expect.poll(() => sent.length).toBe(1); | ||
|
|
||
| await responsePane.send(); | ||
|
|
||
| const dialog = page.getByTestId('prompt-variables-modal-content'); | ||
| await expect(dialog).toBeVisible(); | ||
| await expect(page.getByTestId('prompt-variable-input-0')).toHaveValue(''); | ||
|
|
||
| await page.getByTestId('prompt-variable-input-0').fill('222222'); | ||
| await page.getByTestId('prompt-variables-submit').click(); | ||
| await expect.poll(() => sent).toContain('https://api.example.com/otp/222222'); | ||
| }); | ||
|
|
||
| test('sends nothing when the reader backs out', async ({ page, playground, responsePane }) => { | ||
| const sent = await armRequest(page, playground.urlInput, 'https://api.example.com/otp/{{?OTP}}'); | ||
|
|
||
| await responsePane.send(); | ||
| await page.getByTestId('prompt-variables-cancel').click(); | ||
|
|
||
| await expect(page.getByTestId('prompt-variables-modal-content')).toHaveCount(0); | ||
| await expect(responsePane.emptyHint).toBeVisible(); | ||
| expect(sent).toEqual([]); | ||
| }); | ||
|
|
||
| test('closes on Escape without sending', async ({ page, playground, responsePane }) => { | ||
| const sent = await armRequest(page, playground.urlInput, 'https://api.example.com/otp/{{?OTP}}'); | ||
|
|
||
| await responsePane.send(); | ||
| await expect(page.getByTestId('prompt-variables-modal-content')).toBeVisible(); | ||
| await page.keyboard.press('Escape'); | ||
|
|
||
| await expect(page.getByTestId('prompt-variables-modal-content')).toHaveCount(0); | ||
| await expect(responsePane.emptyHint).toBeVisible(); | ||
| expect(sent).toEqual([]); | ||
| }); | ||
|
|
||
| test('puts the cursor in the first field so the reader can type straight away', async ({ page, playground, responsePane }) => { | ||
| await armRequest(page, playground.urlInput, 'https://api.example.com/{{?First}}/{{?Second}}'); | ||
|
|
||
| await responsePane.send(); | ||
| await expect(page.getByTestId('prompt-variables-modal-content')).toBeVisible(); | ||
|
|
||
| await page.keyboard.type('typed-without-clicking'); | ||
| await expect(page.getByTestId('prompt-variable-input-0')).toHaveValue('typed-without-clicking'); | ||
| }); | ||
|
|
||
| test('asks for one field per prompt and sends both answers', async ({ page, playground, responsePane }) => { | ||
| const sent = await armRequest(page, playground.urlInput, 'https://api.example.com/{{?First}}/{{?Second}}'); | ||
|
|
||
| await responsePane.send(); | ||
|
|
||
| await expect(page.getByTestId('prompt-variable-input-container')).toHaveCount(2); | ||
| await page.getByTestId('prompt-variable-input-0').fill('one'); | ||
| await page.getByTestId('prompt-variable-input-1').fill('two'); | ||
| await page.getByTestId('prompt-variables-submit').click(); | ||
|
|
||
| await expect.poll(() => sent).toContain('https://api.example.com/one/two'); | ||
| }); | ||
|
|
||
| test('sends an empty string for a field left blank, rather than the token', async ({ page, playground, responsePane }) => { | ||
| const sent = await armRequest(page, playground.urlInput, 'https://api.example.com/otp/{{?OTP}}'); | ||
|
|
||
| await responsePane.send(); | ||
| await page.getByTestId('prompt-variables-submit').click(); | ||
|
|
||
| await expect.poll(() => sent).toContain('https://api.example.com/otp/'); | ||
| }); | ||
|
|
||
| test('sends without asking when the request uses no prompt variables', async ({ page, playground, responsePane }) => { | ||
| const sent = await armRequest(page, playground.urlInput, 'https://api.example.com/plain'); | ||
|
|
||
| await responsePane.send(); | ||
|
|
||
| await expect.poll(() => sent).toContain('https://api.example.com/plain'); | ||
| await expect(page.getByTestId('prompt-variables-modal-content')).toHaveCount(0); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...i-docs/src/components/Playground/Content/Views/PlaygroundView/usePromptVariables.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import React from 'react'; | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { useRenderToDom } from '@/hooks/useRenderToDom'; | ||
| import { usePromptVariables, type PromptVariablesController } from './usePromptVariables'; | ||
|
|
||
| const probeTree = () => { | ||
| const box: { current: PromptVariablesController | null } = { current: null }; | ||
|
|
||
| const Probe: React.FC = () => { | ||
| box.current = usePromptVariables(); | ||
| return null; | ||
| }; | ||
|
|
||
| return { box, tree: <Probe /> }; | ||
| }; | ||
|
|
||
| describe('asking the reader for prompt values', () => { | ||
| it('sends straight away when the request uses no prompts', async () => { | ||
| const { box, tree } = probeTree(); | ||
| useRenderToDom(tree); | ||
| const controller = box.current as PromptVariablesController; | ||
|
|
||
| await expect(controller.collect([])).resolves.toEqual({}); | ||
| }); | ||
|
|
||
| it('waits for the reader, then sends what they typed', async () => { | ||
| const { box, tree } = probeTree(); | ||
| useRenderToDom(tree); | ||
| const controller = box.current as PromptVariablesController; | ||
|
|
||
| const pending = controller.collect(['OTP', 'Region']); | ||
| let settled = false; | ||
| void pending.then(() => { | ||
| settled = true; | ||
| }); | ||
|
|
||
| await Promise.resolve(); | ||
| expect(settled).toBe(false); | ||
|
|
||
| controller.submit({ OTP: '123456', Region: 'eu' }); | ||
|
|
||
| await expect(pending).resolves.toEqual({ '?OTP': '123456', '?Region': 'eu' }); | ||
| }); | ||
|
|
||
| it('asks again on the next send, because answers are never kept', async () => { | ||
| const { box, tree } = probeTree(); | ||
| useRenderToDom(tree); | ||
| const controller = box.current as PromptVariablesController; | ||
|
|
||
| const first = controller.collect(['OTP']); | ||
| controller.submit({ OTP: '123456' }); | ||
| await expect(first).resolves.toEqual({ '?OTP': '123456' }); | ||
|
|
||
| const second = controller.collect(['OTP']); | ||
| let settled = false; | ||
| void second.then(() => { | ||
| settled = true; | ||
| }); | ||
|
|
||
| await Promise.resolve(); | ||
| expect(settled).toBe(false); | ||
|
|
||
| controller.cancel(); | ||
| await expect(second).resolves.toBeNull(); | ||
| }); | ||
|
|
||
| it('reports a dismissal so the send is abandoned rather than sent half-filled', async () => { | ||
| const { box, tree } = probeTree(); | ||
| useRenderToDom(tree); | ||
| const controller = box.current as PromptVariablesController; | ||
|
|
||
| const pending = controller.collect(['OTP']); | ||
| controller.cancel(); | ||
|
|
||
| await expect(pending).resolves.toBeNull(); | ||
| }); | ||
|
|
||
| it('sends an empty string for a prompt the reader left blank', async () => { | ||
| const { box, tree } = probeTree(); | ||
| useRenderToDom(tree); | ||
| const controller = box.current as PromptVariablesController; | ||
|
|
||
| const pending = controller.collect(['OTP']); | ||
| controller.submit({}); | ||
|
|
||
| await expect(pending).resolves.toEqual({ '?OTP': '' }); | ||
| }); | ||
| }); |
49 changes: 49 additions & 0 deletions
49
...uno-api-docs/src/components/Playground/Content/Views/PlaygroundView/usePromptVariables.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; | ||
| import { buildPromptVariableMap } from '@/utils/promptVariables'; | ||
| import type { Variables } from '@/runner/utils/variable-interpolator'; | ||
|
|
||
| interface PendingPrompt { | ||
| names: string[]; | ||
| } | ||
|
|
||
| export interface PromptVariablesController { | ||
| pending: PendingPrompt | null; | ||
| collect: (names: string[]) => Promise<Variables | null>; | ||
| submit: (values: Record<string, string>) => void; | ||
| cancel: () => void; | ||
| } | ||
|
|
||
| export const usePromptVariables = (): PromptVariablesController => { | ||
| const [pending, setPending] = useState<PendingPrompt | null>(null); | ||
| const resolveRef = useRef<((values: Record<string, string> | null) => void) | null>(null); | ||
|
|
||
| const settle = useCallback((values: Record<string, string> | null) => { | ||
| const resolve = resolveRef.current; | ||
| resolveRef.current = null; | ||
| setPending(null); | ||
| resolve?.(values); | ||
| }, []); | ||
|
|
||
| const collect = useCallback( | ||
| async (names: string[]): Promise<Variables | null> => { | ||
| if (!names.length) return {}; | ||
|
|
||
| settle(null); | ||
|
|
||
| const values = await new Promise<Record<string, string> | null>((resolve) => { | ||
| resolveRef.current = resolve; | ||
| setPending({ names }); | ||
| }); | ||
|
|
||
| return values ? buildPromptVariableMap(names, values) : null; | ||
| }, | ||
| [settle] | ||
| ); | ||
|
|
||
| const submit = useCallback((values: Record<string, string>) => settle(values), [settle]); | ||
| const cancel = useCallback(() => settle(null), [settle]); | ||
|
|
||
| useEffect(() => () => resolveRef.current?.(null), []); | ||
|
|
||
| return useMemo(() => ({ pending, collect, submit, cancel }), [pending, collect, submit, cancel]); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.