| 22 | } |
| 23 | |
| 24 | class JSCodeGen implements APIRequestCodegen { |
| 25 | generatePlaywrightRequestCall(request: har.Request, body: string | undefined): string { |
| 26 | let method = request.method.toLowerCase(); |
| 27 | const url = new URL(request.url); |
| 28 | const urlParam = `${url.origin}${url.pathname}`; |
| 29 | const options: any = {}; |
| 30 | if (!['delete', 'get', 'head', 'post', 'put', 'patch'].includes(method)) { |
| 31 | options.method = method; |
| 32 | method = 'fetch'; |
| 33 | } |
| 34 | if (url.searchParams.size) |
| 35 | options.params = Object.fromEntries(url.searchParams.entries()); |
| 36 | if (body) |
| 37 | options.data = body; |
| 38 | if (request.headers.length) |
| 39 | options.headers = Object.fromEntries(request.headers.map(header => [header.name, header.value])); |
| 40 | |
| 41 | const params = [`'${urlParam}'`]; |
| 42 | const hasOptions = Object.keys(options).length > 0; |
| 43 | if (hasOptions) |
| 44 | params.push(this.prettyPrintObject(options)); |
| 45 | return `await page.request.${method}(${params.join(', ')});`; |
| 46 | } |
| 47 | |
| 48 | private prettyPrintObject(obj: any, indent = 2, level = 0): string { |
| 49 | // Handle null and undefined |
| 50 | if (obj === null) |
| 51 | return 'null'; |
| 52 | if (obj === undefined) |
| 53 | return 'undefined'; |
| 54 | |
| 55 | // Handle primitive types |
| 56 | if (typeof obj !== 'object') { |
| 57 | if (typeof obj === 'string') |
| 58 | return this.stringLiteral(obj); |
| 59 | return String(obj); |
| 60 | } |
| 61 | |
| 62 | // Handle arrays |
| 63 | if (Array.isArray(obj)) { |
| 64 | if (obj.length === 0) |
| 65 | return '[]'; |
| 66 | const spaces = ' '.repeat(level * indent); |
| 67 | const nextSpaces = ' '.repeat((level + 1) * indent); |
| 68 | |
| 69 | const items = obj.map(item => |
| 70 | `${nextSpaces}${this.prettyPrintObject(item, indent, level + 1)}` |
| 71 | ).join(',\n'); |
| 72 | |
| 73 | return `[\n${items}\n${spaces}]`; |
| 74 | } |
| 75 | |
| 76 | // Handle regular objects |
| 77 | if (Object.keys(obj).length === 0) |
| 78 | return '{}'; |
| 79 | const spaces = ' '.repeat(level * indent); |
| 80 | const nextSpaces = ' '.repeat((level + 1) * indent); |
| 81 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…