• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3/**
4 * response.js
5 *
6 * Response class provides content decoding
7 */
8
9const STATUS_CODES = require('http').STATUS_CODES
10const Headers = require('./headers.js')
11const Body = require('./body.js')
12const clone = Body.clone
13
14/**
15 * Response class
16 *
17 * @param   Stream  body  Readable stream
18 * @param   Object  opts  Response options
19 * @return  Void
20 */
21class Response {
22  constructor (body, opts) {
23    if (!opts) opts = {}
24    Body.call(this, body, opts)
25
26    this.url = opts.url
27    this.status = opts.status || 200
28    this.statusText = opts.statusText || STATUS_CODES[this.status]
29
30    this.headers = new Headers(opts.headers)
31
32    Object.defineProperty(this, Symbol.toStringTag, {
33      value: 'Response',
34      writable: false,
35      enumerable: false,
36      configurable: true
37    })
38  }
39
40  /**
41   * Convenience property representing if the request ended normally
42   */
43  get ok () {
44    return this.status >= 200 && this.status < 300
45  }
46
47  /**
48   * Clone this response
49   *
50   * @return  Response
51   */
52  clone () {
53    return new Response(clone(this), {
54      url: this.url,
55      status: this.status,
56      statusText: this.statusText,
57      headers: this.headers,
58      ok: this.ok
59    })
60  }
61}
62
63Body.mixIn(Response.prototype)
64
65Object.defineProperty(Response.prototype, Symbol.toStringTag, {
66  value: 'ResponsePrototype',
67  writable: false,
68  enumerable: false,
69  configurable: true
70})
71module.exports = Response
72