import type { FolderConfig, AuthConfig, HeaderItem, VarItem } from '../types.js' const INDENT = ' ' function dictBlock(name: string, entries: string[]): string { if (entries.length === 0) return `${name} {\n}\n` return `${name} {\n${entries.map(e => `${INDENT}${e}`).join('\n')}\n}\n` } function textBlock(name: string, content: string): string { const trimmed = content.trimEnd() return `${name} {\n${trimmed.split('\n').map(l => `${INDENT}${l}`).join('\n')}\n}\n` } function quoteKey(key: string): string { if (/[:{}"' ]/.test(key)) return `"${key.replace(/"/g, '\\"')}"` return key } function formatHeaders(headers: HeaderItem[]): string { const entries = headers.map(h => { const prefix = h.disabled ? '~' : '' return `${prefix}${quoteKey(h.name)}: ${h.value}` }) return dictBlock('headers', entries) } function formatAuthBlock(auth: AuthConfig): string { const type = auth.type if (type === 'none') return '' let out = dictBlock('auth', [`mode: ${type}`]) if (type === 'basic' && auth.basic) { out += '\n' + dictBlock('auth:basic', [ `username: ${auth.basic.username}`, `password: ${auth.basic.password}`, ]) } else if (type === 'bearer' && auth.bearer) { out += '\n' + dictBlock('auth:bearer', [`token: ${auth.bearer.token}`]) } return out } function formatVars(blockName: string, vars: VarItem[]): string { if (vars.length === 0) return '' const entries = vars.map(v => { const disabled = v.enabled === false ? '~' : '' const local = v.local ? '@' : '' return `${disabled}${local}${v.name}: ${v.value}` }) return dictBlock(blockName, entries) } export function formatFolder(folder: FolderConfig): string { const parts: string[] = [] // meta block const metaEntries = [`name: ${folder.meta.name}`] if (folder.meta.seq !== undefined) metaEntries.push(`seq: ${folder.meta.seq}`) parts.push(dictBlock('meta', metaEntries)) const req = folder.request if (!req) return parts.join('\n') if (req.headers && req.headers.length > 0) { parts.push(formatHeaders(req.headers)) } if (req.auth) { const authStr = formatAuthBlock(req.auth) if (authStr) parts.push(authStr) } if (req.vars?.req && req.vars.req.length > 0) { parts.push(formatVars('vars:pre-request', req.vars.req)) } if (req.script?.req) parts.push(textBlock('script:pre-request', req.script.req)) if (req.script?.res) parts.push(textBlock('script:post-response', req.script.res)) if (req.tests) parts.push(textBlock('tests', req.tests)) return parts.join('\n') }