(buildResponse)
| 4 | import axios from 'axios'; |
| 5 | |
| 6 | const createCaptureTransport = (buildResponse) => { |
| 7 | return { |
| 8 | request(options, onResponse) { |
| 9 | const chunks = []; |
| 10 | |
| 11 | const req = new Writable({ |
| 12 | write(chunk, _encoding, callback) { |
| 13 | chunks.push(Buffer.from(chunk)); |
| 14 | callback(); |
| 15 | }, |
| 16 | }); |
| 17 | |
| 18 | req.destroyed = false; |
| 19 | req.setTimeout = () => {}; |
| 20 | req.write = req.write.bind(req); |
| 21 | req.close = () => { |
| 22 | req.destroyed = true; |
| 23 | }; |
| 24 | |
| 25 | const originalDestroy = req.destroy.bind(req); |
| 26 | req.destroy = (...args) => { |
| 27 | req.destroyed = true; |
| 28 | return originalDestroy(...args); |
| 29 | }; |
| 30 | |
| 31 | const originalEnd = req.end.bind(req); |
| 32 | req.end = (...args) => { |
| 33 | originalEnd(...args); |
| 34 | |
| 35 | const body = Buffer.concat(chunks); |
| 36 | const response = buildResponse ? buildResponse(body, options) : {}; |
| 37 | |
| 38 | const res = new PassThrough(); |
| 39 | res.statusCode = response.statusCode !== undefined ? response.statusCode : 200; |
| 40 | res.statusMessage = response.statusMessage || 'OK'; |
| 41 | res.headers = response.headers || { 'content-type': 'application/json' }; |
| 42 | res.req = req; |
| 43 | |
| 44 | onResponse(res); |
| 45 | res.end(response.body || JSON.stringify({ ok: true })); |
| 46 | }; |
| 47 | |
| 48 | return req; |
| 49 | }, |
| 50 | }; |
| 51 | }; |
| 52 | |
| 53 | const bodyAsUtf8 = (value) => { |
| 54 | return Buffer.isBuffer(value) ? value.toString('utf8') : String(value); |
no outgoing calls
no test coverage detected