|
| 1 | +# Electron request |
| 2 | + |
| 3 | +The HTTP engine for the Advanced REST Client application. |
| 4 | + |
| 5 | +It works in the Electron's renderer process and allows to make a HTTP request resulting with detailed response. |
| 6 | + |
| 7 | +The detailed response contains information about redirects and timings similar to the ones presented by Chrome Dev Tools. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +The library contain two HTTP clients: |
| 12 | + |
| 13 | +- `SocketRequest` - ARC's original and own HTTP client. Operates directly on the socket. |
| 14 | +- `ElectronRequest` - A request engine using higher level Node's APIs |
| 15 | + |
| 16 | +Both classes use the same configuration and produce the same output. |
| 17 | + |
| 18 | +### Socket request |
| 19 | + |
| 20 | +Originally `SocketRequest` was develop for ARC Chrome Application as Chrome apps don't have access to low level request APIs and therefore the application was unable to produce detailed information about the request. |
| 21 | + |
| 22 | +```javascript |
| 23 | +import { SocketRequest } from '@advanced-rest-client/electron-request'; |
| 24 | + |
| 25 | +const opts = { |
| 26 | + timeout: 30000, |
| 27 | + hosts: [{from: 'domain.com', to: 'other.com'}], |
| 28 | + followRedirects: true |
| 29 | +}; |
| 30 | +const id = 'some-id'; |
| 31 | +const request = { |
| 32 | + url: 'http://api.domain.com', |
| 33 | + method: 'GET', |
| 34 | + headers: 'x-test: true' |
| 35 | +}; |
| 36 | + |
| 37 | +const connection = new SocketRequest(request, id, opts); |
| 38 | +request.on('load', (id, response, transport) => {}); |
| 39 | +request.on('error', (error, id, transport, response) => {}); |
| 40 | +try { |
| 41 | + await connection.send(); |
| 42 | + console.log('Request message sent.'); |
| 43 | +} catch (cause) { |
| 44 | + // usually it means that the server is down or configuration is invalid (URL). |
| 45 | + console.error('Connection error', cause); |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +The `transport` is defined in `@advanced-rest-client/arc-types` as `TransportRequest` interface and describes the final message that has been sent to the endpoint. This includes all transformations applied to the request like added headers. |
| 50 | + |
| 51 | +### Native request |
| 52 | + |
| 53 | +Electron application can access Node's APIs and therefore `SocketRequest` can be eventually replaced to reduce amount of code to maintain. |
| 54 | + |
| 55 | +```javascript |
| 56 | +import { ElectronRequest } from '@advanced-rest-client/electron-request'; |
| 57 | + |
| 58 | +const opts = { |
| 59 | + timeout: 30000, |
| 60 | + hosts: [{from: 'domain.com', to: 'other.com'}], |
| 61 | + followRedirects: true |
| 62 | +}; |
| 63 | +const id = 'some-id'; |
| 64 | +const request = { |
| 65 | + url: 'http://api.domain.com', |
| 66 | + method: 'GET', |
| 67 | + headers: 'x-test: true' |
| 68 | +}; |
| 69 | + |
| 70 | +const connection = new ElectronRequest(request, id, opts); |
| 71 | +request.on('load', (id, response, transport) => {}); |
| 72 | +request.on('error', (error, id, transport, response) => {}); |
| 73 | +try { |
| 74 | + await connection.send(); |
| 75 | + console.log('Request message sent.'); |
| 76 | +} catch (cause) { |
| 77 | + // usually it means that the server is down or configuration is invalid (URL). |
| 78 | + console.error('Connection error', cause); |
| 79 | +} |
| 80 | +``` |
0 commit comments