1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | (function (define) {
|
9 | 'use strict';
|
10 |
|
11 | define(function (require) {
|
12 |
|
13 | var Promise = require('when/lib/Promise'),
|
14 | when = require('when'),
|
15 | normalizeHeaderName = require('./normalizeHeaderName');
|
16 |
|
17 |
|
18 | function ResponsePromise() {
|
19 | return Promise.apply(this, arguments);
|
20 | }
|
21 | ResponsePromise.prototype = Object.create(Promise.prototype);
|
22 |
|
23 |
|
24 |
|
25 | function property(promise, name) {
|
26 | return promise.then(
|
27 | function (value) {
|
28 | return value && value[name];
|
29 | },
|
30 | function (value) {
|
31 | return when.reject(value && value[name]);
|
32 | }
|
33 | );
|
34 | }
|
35 |
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | ResponsePromise.prototype.entity = function entity() {
|
42 | return property(this, 'entity');
|
43 | };
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 | ResponsePromise.prototype.status = function status() {
|
51 | return property(property(this, 'status'), 'code');
|
52 | };
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 | ResponsePromise.prototype.headers = function headers() {
|
60 | return property(this, 'headers');
|
61 | };
|
62 |
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | ResponsePromise.prototype.header = function header(headerName) {
|
70 | headerName = normalizeHeaderName(headerName);
|
71 | return property(this.headers(), headerName);
|
72 | };
|
73 |
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | function makeResponsePromise(promise) {
|
81 | return new ResponsePromise(promise.then.bind(promise));
|
82 | }
|
83 |
|
84 | makeResponsePromise.ResponsePromise = ResponsePromise;
|
85 |
|
86 | return makeResponsePromise;
|
87 |
|
88 | });
|
89 |
|
90 | }(
|
91 | typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
|
92 |
|
93 | ));
|