1 |
|
2 |
|
3 |
|
4 | var requestHandler = require( 'wpcom-xhr-request' );
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var Me = require( './lib/me' );
|
10 | var Site = require( './lib/site' );
|
11 | var Domains = require( './lib/domains' );
|
12 | var Domain = require( './lib/domain' );
|
13 | var Users = require( './lib/users' );
|
14 | var Batch = require( './lib/batch' );
|
15 | var Req = require( './lib/util/request' );
|
16 | var sendRequest = require( './lib/util/send-request' );
|
17 | var Pinghub = require( './lib/util/pinghub' );
|
18 | var debug = require( 'debug' )( 'wpcom' );
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var DEFAULT_ASYNC_TIMEOUT = 30000;
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function WPCOM( token, reqHandler ) {
|
38 | if ( ! ( this instanceof WPCOM ) ) {
|
39 | return new WPCOM( token, reqHandler );
|
40 | }
|
41 |
|
42 |
|
43 | if ( 'function' === typeof token ) {
|
44 | reqHandler = token;
|
45 | token = null;
|
46 | }
|
47 |
|
48 | if ( token ) {
|
49 | debug( 'Token defined: %s…', token.substring( 0, 6 ) );
|
50 | this.token = token;
|
51 | }
|
52 |
|
53 |
|
54 | if ( ! reqHandler ) {
|
55 | debug( 'No request handler. Adding default XHR request handler' );
|
56 |
|
57 | this.request = function( params, fn ) {
|
58 | params = params || {};
|
59 |
|
60 |
|
61 | if ( token ) {
|
62 | params.authToken = token;
|
63 | }
|
64 |
|
65 | return requestHandler( params, fn );
|
66 | };
|
67 | } else {
|
68 | this.request = reqHandler;
|
69 | }
|
70 |
|
71 |
|
72 | this.req = new Req( this );
|
73 |
|
74 |
|
75 | this.pinghub = new Pinghub( this );
|
76 |
|
77 |
|
78 | this.apiVersion = '1.1';
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | WPCOM.prototype.me = function() {
|
87 | return new Me( this );
|
88 | };
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | WPCOM.prototype.domains = function() {
|
96 | return new Domains( this );
|
97 | };
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | WPCOM.prototype.domain = function( domainId ) {
|
106 | return new Domain( domainId, this );
|
107 | };
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | WPCOM.prototype.site = function( id ) {
|
116 | return new Site( id, this );
|
117 | };
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | WPCOM.prototype.users = function() {
|
125 | return new Users( this );
|
126 | };
|
127 |
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 | WPCOM.prototype.batch = function() {
|
134 | return new Batch( this );
|
135 | };
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | WPCOM.prototype.freshlyPressed = function( query, fn ) {
|
145 | return this.req.get( '/freshly-pressed', query, fn );
|
146 | };
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | WPCOM.prototype.sendRequest = function( params, query, body, fn ) {
|
154 | var msg = 'WARN! Don use `sendRequest() anymore. Use `this.req` method.';
|
155 |
|
156 | if ( console && console.warn ) {
|
157 | console.warn( msg );
|
158 | } else {
|
159 | console.log( msg );
|
160 | }
|
161 |
|
162 | return sendRequest.call( this, params, query, body, fn );
|
163 | };
|
164 |
|
165 | if ( ! Promise.prototype.timeout ) {
|
166 | |
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | Promise.prototype.timeout = function( delay = DEFAULT_ASYNC_TIMEOUT ) {
|
177 | let cancelTimeout, timer, timeout;
|
178 |
|
179 | timeout = new Promise( ( resolve, reject ) => {
|
180 | timer = setTimeout( () => {
|
181 | reject( new Error( 'Action timed out while waiting for response.' ) );
|
182 | }, delay );
|
183 | } );
|
184 |
|
185 | cancelTimeout = () => {
|
186 | clearTimeout( timer );
|
187 | return this;
|
188 | };
|
189 |
|
190 | return Promise.race( [
|
191 | this.then( cancelTimeout ).catch( cancelTimeout ),
|
192 | timeout
|
193 | ] );
|
194 | };
|
195 | }
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | module.exports = WPCOM;
|