1 | 'use strict'
|
2 |
|
3 | const { Readable } = require('stream')
|
4 | const headersFactory = require('./headers')
|
5 |
|
6 | module.exports = class Request extends Readable {
|
7 | _read () {
|
8 | this.push(this._body)
|
9 | this.push(null)
|
10 | }
|
11 |
|
12 | _fromObject ({ method, url, headers = {}, body = '', properties }) {
|
13 | this._method = method
|
14 | this._url = url
|
15 | this._headers = headersFactory(headers)
|
16 | this._body = body
|
17 | if (properties) {
|
18 | Object.assign(this, properties)
|
19 | }
|
20 | }
|
21 |
|
22 | _fromParams (method, url, headers, body, properties) {
|
23 | if (typeof body === 'object') {
|
24 | properties = body
|
25 | body = ''
|
26 | }
|
27 | if (typeof headers === 'string') {
|
28 | body = headers
|
29 | headers = {}
|
30 | }
|
31 | return this._fromObject({ method, url, headers, body, properties })
|
32 | }
|
33 |
|
34 | constructor (param) {
|
35 | super()
|
36 | if (typeof param === 'object') {
|
37 | this._fromObject(param)
|
38 | } else {
|
39 | this._fromParams(...arguments)
|
40 | }
|
41 | }
|
42 |
|
43 | get method () {
|
44 | return this._method
|
45 | }
|
46 |
|
47 | get url () {
|
48 | return this._url
|
49 | }
|
50 |
|
51 | get headers () {
|
52 | return this._headers
|
53 | }
|
54 |
|
55 | abort () {
|
56 | this.aborted = true
|
57 | this.emit('aborted')
|
58 | }
|
59 | }
|