UNPKG

4.33 kBJavaScriptView Raw
1/**
2 * Module dependencies.
3 */
4var requestHandler = require( 'wpcom-xhr-request' );
5
6/**
7 * Local module dependencies.
8 */
9var Me = require( './lib/me' );
10var Site = require( './lib/site' );
11var Domains = require( './lib/domains' );
12var Domain = require( './lib/domain' );
13var Users = require( './lib/users' );
14var Batch = require( './lib/batch' );
15var Req = require( './lib/util/request' );
16var sendRequest = require( './lib/util/send-request' );
17var Pinghub = require( './lib/util/pinghub' );
18var debug = require( 'debug' )( 'wpcom' );
19
20/**
21 * Local module constants
22 */
23var DEFAULT_ASYNC_TIMEOUT = 30000;
24
25/**
26 * XMLHttpRequest (and CORS) API access method.
27 *
28 * API authentication is done via an (optional) access `token`,
29 * which needs to be retrieved via OAuth.
30 *
31 * Request Handler is optional and XHR is defined as default.
32 *
33 * @param {String} [token] - OAuth API access token
34 * @param {Function} [reqHandler] - function Request Handler
35 * @return {WPCOM} wpcom instance
36 */
37function WPCOM( token, reqHandler ) {
38 if ( ! ( this instanceof WPCOM ) ) {
39 return new WPCOM( token, reqHandler );
40 }
41
42 // `token` is optional
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 // Set default request handler
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 // token is optional
61 if ( token ) {
62 params.authToken = token;
63 }
64
65 return requestHandler( params, fn );
66 };
67 } else {
68 this.request = reqHandler;
69 }
70
71 // Add Req instance
72 this.req = new Req( this );
73
74 // Add Pinghub instance
75 this.pinghub = new Pinghub( this );
76
77 // Default api version;
78 this.apiVersion = '1.1';
79}
80
81/**
82 * Return `Me` object instance
83 *
84 * @return {Me} Me instance
85 */
86WPCOM.prototype.me = function() {
87 return new Me( this );
88};
89
90/**
91 * Return `Domains` object instance
92 *
93 * @return {Domains} Domains instance
94 */
95WPCOM.prototype.domains = function() {
96 return new Domains( this );
97};
98
99/**
100 * Return `Domain` object instance
101 *
102 * @param {String} domainId - domain identifier
103 * @return {Domain} Domain instance
104 */
105WPCOM.prototype.domain = function( domainId ) {
106 return new Domain( domainId, this );
107};
108
109/**
110 * Return `Site` object instance
111 *
112 * @param {String} id - site identifier
113 * @return {Site} Site instance
114 */
115WPCOM.prototype.site = function( id ) {
116 return new Site( id, this );
117};
118
119/**
120 * Return `Users` object instance
121 *
122 * @return {Users} Users instance
123 */
124WPCOM.prototype.users = function() {
125 return new Users( this );
126};
127
128 /**
129 * Return `Batch` object instance
130 *
131 * @return {Batch} Batch instance
132 */
133WPCOM.prototype.batch = function() {
134 return new Batch( this );
135};
136
137/**
138 * List Freshly Pressed Posts
139 *
140 * @param {Object} [query] - query object
141 * @param {Function} fn - callback function
142 * @return {Function} request handler
143 */
144WPCOM.prototype.freshlyPressed = function( query, fn ) {
145 return this.req.get( '/freshly-pressed', query, fn );
146};
147
148/**
149 * Expose send-request
150 * @TODO: use `this.req` instead of this method
151 */
152
153WPCOM.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
165if ( ! Promise.prototype.timeout ) {
166 /**
167 * Returns a new promise with a deadline
168 *
169 * After the timeout interval, the promise will
170 * reject. If the actual promise settles before
171 * the deadline, the timer is cancelled.
172 *
173 * @param {number} delay how many ms to wait
174 * @return {Promise} promise
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 * Expose `WPCOM` module
199 */
200module.exports = WPCOM;