| 177 | } |
| 178 | |
| 179 | class CSharpCodeGen implements APIRequestCodegen { |
| 180 | generatePlaywrightRequestCall(request: har.Request, body: string | undefined): string { |
| 181 | const url = new URL(request.url); |
| 182 | const urlParam = `${url.origin}${url.pathname}`; |
| 183 | const options: any = {}; |
| 184 | |
| 185 | const initLines: string[] = []; |
| 186 | |
| 187 | let method = request.method.toLowerCase(); |
| 188 | if (!['delete', 'get', 'head', 'post', 'put', 'patch'].includes(method)) { |
| 189 | options.Method = method; |
| 190 | method = 'fetch'; |
| 191 | } |
| 192 | |
| 193 | if (url.searchParams.size) |
| 194 | options.Params = Object.fromEntries(url.searchParams.entries()); |
| 195 | if (body) |
| 196 | options.Data = body; |
| 197 | if (request.headers.length) |
| 198 | options.Headers = Object.fromEntries(request.headers.map(header => [header.name, header.value])); |
| 199 | |
| 200 | const params = [`"${urlParam}"`]; |
| 201 | const hasOptions = Object.keys(options).length > 0; |
| 202 | if (hasOptions) |
| 203 | params.push(this.prettyPrintObject(options)); |
| 204 | |
| 205 | return `${initLines.join('\n')}${initLines.length ? '\n' : ''}await request.${this.toFunctionName(method)}(${params.join(', ')});`; |
| 206 | } |
| 207 | |
| 208 | private toFunctionName(method: string): string { |
| 209 | return method[0].toUpperCase() + method.slice(1) + 'Async'; |
| 210 | } |
| 211 | |
| 212 | private prettyPrintObject(obj: any, indent = 2, level = 0): string { |
| 213 | // Handle null and undefined |
| 214 | if (obj === null) |
| 215 | return 'null'; |
| 216 | if (obj === undefined) |
| 217 | return 'null'; |
| 218 | |
| 219 | // Handle primitive types |
| 220 | if (typeof obj !== 'object') { |
| 221 | if (typeof obj === 'string') |
| 222 | return this.stringLiteral(obj); |
| 223 | if (typeof obj === 'boolean') |
| 224 | return obj ? 'true' : 'false'; |
| 225 | return String(obj); |
| 226 | } |
| 227 | |
| 228 | // Handle arrays |
| 229 | if (Array.isArray(obj)) { |
| 230 | if (obj.length === 0) |
| 231 | return 'new object[] {}'; |
| 232 | const spaces = ' '.repeat(level * indent); |
| 233 | const nextSpaces = ' '.repeat((level + 1) * indent); |
| 234 | |
| 235 | const items = obj.map(item => |
| 236 | `${nextSpaces}${this.prettyPrintObject(item, indent, level + 1)}` |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…