diff --git a/build/main.js b/build/main.js index 6f86629..89437b8 100644 --- a/build/main.js +++ b/build/main.js @@ -105,8 +105,7 @@ class AirApi { // Ping server to see if the token is good. async testAuth(token) { if (!(token || this.config.token)) { - _log2.default.i('Airbnbapi: No token included for testAuth() call'); - return null; + throw Error('Airbnbapi: No token included for testAuth() call'); } else { const options = this.buildOptions({ method: 'POST', @@ -121,11 +120,9 @@ class AirApi { // Grab a new auth token using a 'username and password' login method. async newAccessToken({ username, password } = {}) { if (!username) { - _log2.default.e("Airbnbapi: Can't apply for a token without a username."); - return null; + throw Error("Airbnbapi: Can't apply for a token without a username."); } else if (!password) { - _log2.default.e("Airbnbapi: Can't apply for a token without a password."); - return null; + throw Error("Airbnbapi: Can't apply for a token without a password."); } const options = this.buildOptions({ token: 'public', @@ -138,34 +135,20 @@ class AirApi { } }); - try { - const response = await (0, _requestPromise2.default)(options); - if (response && response.access_token) { - _log2.default.i(`Airbnbapi: Successful login for [${username}], auth ID is [${response.access_token}]`); - return { token: response.access_token }; - } else { - _log2.default.e('Airbnbapi: no response from server when fetching token'); - return null; - } - } catch (e) { - // if(e.response.access_token) { - // log.i('Airbnbapi: Successful login for [ ' + username + ' ], auth ID is [ ' + e.response.access_token + ' ]') - // return { token: e.response.access_token } - // } - // log.i(JSON.stringify(e, null, 4)) - _log2.default.e("Airbnbapi: Couldn't get auth token for " + username); - _log2.default.e(e.error); - return { error: e.error }; + const response = await (0, _requestPromise2.default)(options); + if (response && response.access_token) { + _log2.default.i(`Airbnbapi: Successful login for [${username}], auth ID is [${response.access_token}]`); + return { token: response.access_token }; + } else { + throw Error('Airbnbapi: no response from server when fetching token'); } } async login({ email, password } = {}) { if (!email) { - _log2.default.e("Airbnbapi: Can't login without an email."); - return null; + throw Error("Airbnbapi: Can't login without an email."); } else if (!password) { - _log2.default.e("Airbnbapi: Can't apply for a token without a password."); - return null; + throw Error("Airbnbapi: Can't apply for a token without a password."); } const options = this.buildOptions({ token: 'public', @@ -176,19 +159,12 @@ class AirApi { password } }); - try { - const response = await (0, _requestPromise2.default)(options); - if (response && response.login) { - _log2.default.i(`Airbnbapi: Successful login for [${email}], auth ID is [${response.login.id}]`); - return response; - } else { - _log2.default.e('Airbnbapi: no response from server when fetching token'); - return null; - } - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get auth token for " + email); - _log2.default.e(e.error); - return { error: e.error }; + const response = await (0, _requestPromise2.default)(options); + if (response && response.login) { + _log2.default.i(`Airbnbapi: Successful login for [${email}], auth ID is [${response.login.id}]`); + return response; + } else { + throw Error('Airbnbapi: no response from server when fetching token'); } } @@ -198,8 +174,7 @@ class AirApi { async getPublicListingCalendar({ id, month = '1', year = '2018', count = '1' } = {}) { if (!id) { - _log2.default.e("Airbnbapi: Can't get public listing calendar without an id"); - return null; + throw Error("Airbnbapi: Can't get public listing calendar without an id"); } const options = this.buildOptions({ token: 'public', @@ -212,29 +187,19 @@ class AirApi { count } }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get public calendar for listing " + id); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } async getCalendar({ token, id, startDate, endDate } = {}) { //log.i(colors.magenta('Airbnbapi: Requesting calendar for [ ' + id + ' ] --')) if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get a calendar without a token"); - return null; + throw Error("Airbnbapi: Can't get a calendar without a token"); } else if (!id) { - _log2.default.e("Airbnbapi: Can't get a calendar without an id"); - return null; + throw Error("Airbnbapi: Can't get a calendar without an id"); } else if (!startDate) { - _log2.default.e("Airbnbapi: Can't get a calendar without a start date"); - return null; + throw Error("Airbnbapi: Can't get a calendar without a start date"); } else if (!endDate) { - _log2.default.e("Airbnbapi: Can't get a calendar without a end date"); - return null; + throw Error("Airbnbapi: Can't get a calendar without a end date"); } const options = this.buildOptions({ @@ -249,13 +214,8 @@ class AirApi { }, timeout: 10000 }); - try { - const response = await (0, _requestPromise2.default)(options).catch(console.log); - return response.calendar_days; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get calendar for listing " + id); - _log2.default.e(e); - } + const response = await (0, _requestPromise2.default)(options); + return response.calendar_days; } async setPriceForDay({ token, id, date, price, currency = this.config.currency }) { @@ -273,14 +233,9 @@ class AirApi { }, timeout: 10000 }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't set price for cal day for listing " + id); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } + async setAvailabilityForDay({ token, id, date, availability }) { const options = this.buildOptions({ method: 'PUT', @@ -292,13 +247,8 @@ class AirApi { }, timeout: 10000 }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't set availability for cal day for listing " + id); - _log2.default.e(e); - } + + return await (0, _requestPromise2.default)(options); } //////////// LISTING SECTION //////////// @@ -307,14 +257,11 @@ class AirApi { async setHouseManual({ token, id, manual } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't set a house manual without a token"); - return null; + throw Error("Airbnbapi: Can't set a house manual without a token"); } else if (!id) { - _log2.default.e("Airbnbapi: Can't set a house manual without an id"); - return null; + throw Error("Airbnbapi: Can't set a house manual without an id"); } else if (!manual) { - _log2.default.e("Airbnbapi: Can't set a house manual without manual text"); - return null; + throw Error("Airbnbapi: Can't set a house manual without manual text"); } const options = this.buildOptions({ method: 'POST', @@ -324,98 +271,65 @@ class AirApi { listing: { house_manual: manual } } }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't set house manual for listing " + id); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } async getListingInfo(id) { if (!id) { - _log2.default.e("Airbnbapi: Can't get public listing information without an id"); - return null; + throw Error("Airbnbapi: Can't get public listing information without an id"); } const options = this.buildOptions({ token: 'public', route: `/v1/listings/${id}` }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get info for listing " + id); - _log2.default.e(e); - } + const response = await (0, _requestPromise2.default)(options); + return response; } async getListingInfoHost({ token, id } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get a listing without a token"); - return null; + throw Error("Airbnbapi: Can't get a listing without a token"); } else if (!id) { - _log2.default.e("Airbnbapi: Can't get a listing without an id"); - return null; + throw Error("Airbnbapi: Can't get a listing without an id"); } const options = this.buildOptions({ route: `/v1/listings/${id}`, token, format: 'v1_legacy_long_manage_listing' }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get listing info for id " + id); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } async getHostSummary(token) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get a summary without a token"); - return null; + throw Error("Airbnbapi: Can't get a summary without a token"); } const options = this.buildOptions({ route: `/v1/account/host_summary`, token }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get a host summary for token " + token); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } async getOwnActiveListings(token) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get an active listing list without a token"); - return null; + throw Error("Airbnbapi: Can't get an active listing list without a token"); } const options = this.buildOptions({ route: `/v1/account/host_summary`, token }); - try { - const response = await (0, _requestPromise2.default)(options); - if (response.active_listings) { - return response.active_listings.map(listing => listing.listing.listing); - } else { - return []; - } - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get an active listing list for token " + token); - _log2.default.e(e); + + const response = await (0, _requestPromise2.default)(options); + if (response.active_listings) { + return response.active_listings.map(listing => listing.listing.listing); + } else { + return []; } } async getOwnListings({ token, userId }) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get an listing list without a token"); - return null; + throw Error("Airbnbapi: Can't get an listing list without a token"); } const options = this.buildOptions({ route: `/v2/listings`, @@ -425,16 +339,11 @@ class AirApi { }, token }); - try { - const response = await (0, _requestPromise2.default)(options); - if (response) { - return response.listings; - } else { - return []; - } - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get an listing list for token " + token); - _log2.default.e(e); + const response = await (0, _requestPromise2.default)(options); + if (response) { + return response.listings; + } else { + return []; } } @@ -445,34 +354,25 @@ class AirApi { // Gets all the data for one thread async getThread({ token, id, currency = this.config.currency } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get a thread without a token"); - return null; + throw Error("Airbnbapi: Can't get a thread without a token"); } else if (!id) { - _log2.default.e("Airbnbapi: Can't get a thread without an id"); - return null; + throw Error("Airbnbapi: Can't get a thread without an id"); } const options = this.buildOptions({ route: '/v1/threads/' + id, token, qs: { currency } }); - try { - const response = await (0, _requestPromise2.default)(options); - return response.thread; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get thread " + id); - _log2.default.e(e); - } + const response = await (0, _requestPromise2.default)(options); + return response.thread; } async getThreadsBatch({ token, ids, currency = this.config.currency } = {}) { //log.i(colors.magenta('Airbnbapi: Requesting calendar for [ ' + id + ' ] --')) if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get threads without a token"); - return null; + throw Error("Airbnbapi: Can't get threads without a token"); } else if (!ids) { - _log2.default.e("Airbnbapi: Can't get threads without at least one id"); - return null; + throw Error("Airbnbapi: Can't get threads without at least one id"); } const operations = ids.map(id => ({ @@ -493,23 +393,14 @@ class AirApi { }); // log.i(JSON.stringify(options, null, 4)) - let response = {}; - - try { - response = await (0, _requestPromise2.default)(options).catch(_log2.default.e); - return response.operations.map(o => o.response); - // log.i(JSON.stringify(response, null, 4)) - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get threads for threads " + ids); - _log2.default.e(e); - } + const response = await (0, _requestPromise2.default)(options); + return response.operations.map(o => o.response); } // Gets a list of thread id's for a host async getThreadsFull({ token, offset = '0', limit = '2' } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get a thread list without a token"); - return null; + throw Error("Airbnbapi: Can't get a thread list without a token"); } const options = this.buildOptions({ route: '/v2/threads', @@ -517,26 +408,19 @@ class AirApi { format: 'for_messaging_sync_with_posts', qs: { _offset: offset, _limit: limit } }); - try { - const response = await (0, _requestPromise2.default)(options); - if (response.threads) { - return response.threads; //.map(item => item.id) - } else return null; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get thread list for token " + token); - _log2.default.e(e); - } + const response = await (0, _requestPromise2.default)(options); + if (response.threads) { + return response.threads; //.map(item => item.id) + } else return []; } // Gets a list of thread id's for a host async getThreadFull({ token, id } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get a thread without a token"); - return null; + throw Error("Airbnbapi: Can't get a thread without a token"); } if (!id) { - _log2.default.e("Airbnbapi: Can't get a thread without an id"); - return null; + throw Error("Airbnbapi: Can't get a thread without an id"); } const options = this.buildOptions({ @@ -544,56 +428,37 @@ class AirApi { token, format: 'for_messaging_sync_with_posts' }); - try { - const response = await (0, _requestPromise2.default)(options); - if (response) { - return response; - } else return null; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get thread for id " + id); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } // Gets a list of thread id's for a host async getThreads({ token, offset = '0', limit = '2' } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get a thread list without a token"); - return null; + throw Error("Airbnbapi: Can't get a thread list without a token"); } const options = this.buildOptions({ route: '/v2/threads', token, qs: { _offset: offset, _limit: limit } }); - try { - const response = await (0, _requestPromise2.default)(options); - if (response.threads) { - return response.threads.map(item => item.id); - } else return null; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get thread list for token " + token); - _log2.default.e(e); - } + const response = await (0, _requestPromise2.default)(options); + if (response.threads) { + return response.threads.map(item => item.id); + } else return []; } // Create a new thread async createThread({ token, id, checkIn, checkOut, guestNum = 1, message } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't create a thread without a token"); - return null; + throw Error("Airbnbapi: Can't create a thread without a token"); } else if (!id) { - _log2.default.e("Airbnbapi: Can't create a thread without an id"); - return null; + throw Error("Airbnbapi: Can't create a thread without an id"); } else if (!checkIn) { - _log2.default.e("Airbnbapi: Can't create a thread without a checkin"); - return null; + throw Error("Airbnbapi: Can't create a thread without a checkin"); } else if (!checkOut) { - _log2.default.e("Airbnbapi: Can't create a thread without a checkout"); - return null; + throw Error("Airbnbapi: Can't create a thread without a checkout"); } else if (!message || message.trim() === '') { - _log2.default.e("Airbnbapi: Can't create a thread without a message body"); - return null; + throw Error("Airbnbapi: Can't create a thread without a message body"); } const options = this.buildOptions({ method: 'POST', @@ -607,13 +472,7 @@ class AirApi { checkout_date: checkOut } }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't send create thread for listing " + id); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } //////////// RESERVATIONS SECTION //////////// @@ -622,8 +481,7 @@ class AirApi { async getReservations({ token, offset = '0', limit = '20' } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get a reservation list without a token"); - return null; + throw Error("Airbnbapi: Can't get a reservation list without a token"); } const options = this.buildOptions({ route: '/v2/reservations', @@ -631,24 +489,17 @@ class AirApi { format: 'for_mobile_host', qs: { _offset: offset, _limit: limit } }); - try { - const response = await (0, _requestPromise2.default)(options); - return response.reservations; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get reservation list for token " + token); - _log2.default.e(e); - } + const response = await (0, _requestPromise2.default)(options); + return response.reservations; } async getReservationsBatch({ token, ids, currency = this.config.currency } = {}) { // TODO change to reservation //log.i(colors.magenta('Airbnbapi: Requesting calendar for [ ' + id + ' ] --')) if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get reservations without a token"); - return null; + throw Error("Airbnbapi: Can't get reservations without a token"); } else if (!ids || !Array.isArray(ids)) { - _log2.default.e("Airbnbapi: Can't get reservations without at least one id"); - return null; + throw Error("Airbnbapi: Can't get reservations without at least one id"); } const operations = ids.map(id => ({ method: 'GET', @@ -669,23 +520,15 @@ class AirApi { timeout: 30000 }); // log.i(JSON.stringify(options, null, 4)) - try { - const response = await (0, _requestPromise2.default)(options).catch(console.error); - return response.operations.map(o => o.response); - // log.i(JSON.stringify(response, null, 4)) - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get reservations for ids " + ids); - _log2.default.e(e); - } + const response = await (0, _requestPromise2.default)(options); + return response.operations.map(o => o.response); } async getReservation({ token, id, currency } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get a reservation without a token"); - return null; + throw Error("Airbnbapi: Can't get a reservation without a token"); } else if (!id) { - _log2.default.e("Airbnbapi: Can't get a reservation without an id"); - return null; + throw Error("Airbnbapi: Can't get a reservation without an id"); } const options = this.buildOptions({ route: `/v2/reservations/${id}`, @@ -693,26 +536,18 @@ class AirApi { format: 'for_mobile_host', currency }); - try { - const response = await (0, _requestPromise2.default)(options); - return response.reservation; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get reservation for token " + token); - _log2.default.e(e); - } + const response = await (0, _requestPromise2.default)(options); + return response.reservation; } // Send a message to a thread (guest) async sendMessage({ token, id, message } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't send a message without a token"); - return null; + throw Error("Airbnbapi: Can't send a message without a token"); } else if (!id) { - _log2.default.e("Airbnbapi: Can't send a message without an id"); - return null; + throw Error("Airbnbapi: Can't send a message without an id"); } else if (!message || message.trim() === '') { - _log2.default.e("Airbnbapi: Can't send a message without a message body"); - return null; + throw Error("Airbnbapi: Can't send a message without a message body"); } _log2.default.i('Airbnbapi: send message for thread: ' + id + ' --'); _log2.default.i("'" + message.substring(70) + "'"); @@ -722,27 +557,18 @@ class AirApi { token, body: { thread_id: id, message: message.trim() } }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't send message for thread " + id); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } // Send pre-approval to an inquiry // requires a id. id, and optional message async sendPreApproval({ token, thread_id, listing_id, message = '' } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't send pre-approval without a token"); - return null; + throw Error("Airbnbapi: Can't send pre-approval without a token"); } else if (!thread_id) { - _log2.default.e("Airbnbapi: Can't send pre-approval without a thread_id"); - return null; + throw Error("Airbnbapi: Can't send pre-approval without a thread_id"); } else if (!listing_id) { - _log2.default.e("Airbnbapi: Can't send pre-approval without a listing_id"); - return null; + throw Error("Airbnbapi: Can't send pre-approval without a listing_id"); } const options = this.buildOptions({ method: 'POST', @@ -754,13 +580,7 @@ class AirApi { status: 'preapproved' } }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't send preapproval for thread " + thread_id); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } async sendReview({ @@ -774,11 +594,9 @@ class AirApi { recommend = true } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't send a review without a token"); - return null; + throw Error("Airbnbapi: Can't send a review without a token"); } else if (!id) { - _log2.default.e("Airbnbapi: Can't send review without an id"); - return null; + throw Error("Airbnbapi: Can't send review without an id"); } const options = this.buildOptions({ method: 'POST', @@ -793,13 +611,7 @@ class AirApi { recommend } }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't send a review for thread " + thread_id); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } async sendSpecialOffer({ @@ -813,26 +625,19 @@ class AirApi { currency = this.config.currency } = {}) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't send a special offer without a token"); - return null; + throw Error("Airbnbapi: Can't send a special offer without a token"); } else if (!startDate) { - _log2.default.e("Airbnbapi: Can't send a special offer without a startDate"); - return null; + throw Error("Airbnbapi: Can't send a special offer without a startDate"); } else if (!guests) { - _log2.default.e("Airbnbapi: Can't send a special offer without guests"); - return null; + throw Error("Airbnbapi: Can't send a special offer without guests"); } else if (!listingId) { - _log2.default.e("Airbnbapi: Can't send a special offer without a listingId"); - return null; + throw Error("Airbnbapi: Can't send a special offer without a listingId"); } else if (!nights) { - _log2.default.e("Airbnbapi: Can't send a special offer without nights (staying)"); - return null; + throw Error("Airbnbapi: Can't send a special offer without nights (staying)"); } else if (!price) { - _log2.default.e("Airbnbapi: Can't send a special offer without a price"); - return null; + throw Error("Airbnbapi: Can't send a special offer without a price"); } else if (!threadId) { - _log2.default.e("Airbnbapi: Can't send a special offer without a threadId"); - return null; + throw Error("Airbnbapi: Can't send a special offer without a threadId"); } const options = this.buildOptions({ @@ -849,13 +654,7 @@ class AirApi { thread_id: threadId } }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't send a review for thread " + thread_id); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } async alterationRequestResponse({ @@ -892,44 +691,25 @@ class AirApi { }, timeout: 10000 }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Can't send an alteration request response fro reservation " + reservationId); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } async getGuestInfo(id) { if (!id) { - _log2.default.e("Airbnbapi: Can't get guest info without an id"); - return null; + throw Error("Airbnbapi: Can't get guest info without an id"); } const options = this.buildOptions({ token: 'public', route: `/v2/users/${id}` }); - try { - const response = await (0, _requestPromise2.default)(options); - return response && response.user ? response.user : undefined; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get guest info with user id " + id); - _log2.default.e(e); - } + const response = await (0, _requestPromise2.default)(options); + return response && response.user ? response.user : null; } async getOwnUserInfo(token) { if (!(token || this.config.token)) { - _log2.default.e("Airbnbapi: Can't get user info without a token"); - return null; + throw Error("Airbnbapi: Can't get user info without a token"); } const options = this.buildOptions({ route: '/v2/users/me', token }); - try { - const response = await (0, _requestPromise2.default)(options); - return response && response.user ? response.user : undefined; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get own info with token" + token); - _log2.default.e(e); - return null; - } + const response = await (0, _requestPromise2.default)(options); + return response && response.user ? response.user : null; } async listingSearch({ @@ -984,13 +764,7 @@ class AirApi { sort: sortDirection } }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't get listings for search of " + location); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } async newAccount({ @@ -1004,20 +778,15 @@ class AirApi { bdayYear = 1980 } = {}) { if (!username) { - _log2.default.e("Airbnbapi: Can't make a new account without a username"); - return null; + throw Error("Airbnbapi: Can't make a new account without a username"); } else if (!password) { - _log2.default.e("Airbnbapi: Can't make a new account without a password"); - return null; + throw Error("Airbnbapi: Can't make a new account without a password"); } else if (!authenticity_token) { - _log2.default.e("Airbnbapi: Can't make a new account without an authenticity_token"); - return null; + throw Error("Airbnbapi: Can't make a new account without an authenticity_token"); } else if (!authenticity_token) { - _log2.default.e("Airbnbapi: Can't make a new account without a firstname"); - return null; + throw Error("Airbnbapi: Can't make a new account without a firstname"); } else if (!authenticity_token) { - _log2.default.e("Airbnbapi: Can't make a new account without a lastname"); - return null; + throw Error("Airbnbapi: Can't make a new account without a lastname"); } const options = this.buildOptions({ method: 'POST', @@ -1035,13 +804,7 @@ class AirApi { 'user_profile_info[receive_promotional_email]': 0 } }); - try { - const response = await (0, _requestPromise2.default)(options); - return response; - } catch (e) { - _log2.default.e("Airbnbapi: Couldn't make new account for username " + username); - _log2.default.e(e); - } + return await (0, _requestPromise2.default)(options); } } diff --git a/build/metapoints.js b/build/metapoints.js index 5330a09..bbab9aa 100644 --- a/build/metapoints.js +++ b/build/metapoints.js @@ -14,8 +14,7 @@ exports.default = { todo: 'make meta endpoints for combining data from multiple endpoints', async mGetOwnActiveListingsFull(token) { if (!token) { - _log2.default.e("Airbnbapi: Can't get an active listing list without a token"); - return null; + throw Error("Airbnbapi: Can't get an active listing list without a token"); } const listings = await this.getOwnActiveListings(token); const fullListings = await Promise.all(listings.map(listing => { diff --git a/package-lock.json b/package-lock.json index d638a53..a828196 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "airbnbapijs", - "version": "0.10.0", + "version": "0.10.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1216,6 +1216,15 @@ "type-detect": "^4.0.5" } }, + "chai-as-promised": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/chai-as-promised/-/chai-as-promised-7.1.1.tgz", + "integrity": "sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA==", + "dev": true, + "requires": { + "check-error": "^1.0.2" + } + }, "chalk": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", diff --git a/package.json b/package.json index caa91b4..4556c1c 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "babel-plugin-transform-object-rest-spread": "^6.26.0", "babel-preset-env": "^1.7.0", "chai": "^4.2.0", + "chai-as-promised": "^7.1.1", "coveralls": "^3.0.3", "dotenv": "^8.0.0", "lodash": "^4.17.11", diff --git a/src/main.js b/src/main.js index 895d07b..89467d4 100644 --- a/src/main.js +++ b/src/main.js @@ -91,8 +91,7 @@ class AirApi { // Ping server to see if the token is good. async testAuth(token) { if (!(token || this.config.token)) { - log.i('Airbnbapi: No token included for testAuth() call') - return null + throw Error('Airbnbapi: No token included for testAuth() call') } else { const options = this.buildOptions({ method: 'POST', @@ -107,11 +106,9 @@ class AirApi { // Grab a new auth token using a 'username and password' login method. async newAccessToken({ username, password } = {}) { if (!username) { - log.e("Airbnbapi: Can't apply for a token without a username.") - return null + throw Error("Airbnbapi: Can't apply for a token without a username.") } else if (!password) { - log.e("Airbnbapi: Can't apply for a token without a password.") - return null + throw Error("Airbnbapi: Can't apply for a token without a password.") } const options = this.buildOptions({ token: 'public', @@ -124,38 +121,24 @@ class AirApi { } }) - try { - const response = await requestPromise(options) - if (response && response.access_token) { - log.i( - `Airbnbapi: Successful login for [${username}], auth ID is [${ - response.access_token - }]` - ) - return { token: response.access_token } - } else { - log.e('Airbnbapi: no response from server when fetching token') - return null - } - } catch (e) { - // if(e.response.access_token) { - // log.i('Airbnbapi: Successful login for [ ' + username + ' ], auth ID is [ ' + e.response.access_token + ' ]') - // return { token: e.response.access_token } - // } - // log.i(JSON.stringify(e, null, 4)) - log.e("Airbnbapi: Couldn't get auth token for " + username) - log.e(e.error) - return { error: e.error } + const response = await requestPromise(options) + if (response && response.access_token) { + log.i( + `Airbnbapi: Successful login for [${username}], auth ID is [${ + response.access_token + }]` + ) + return { token: response.access_token } + } else { + throw Error('Airbnbapi: no response from server when fetching token') } } async login({ email, password } = {}) { if (!email) { - log.e("Airbnbapi: Can't login without an email.") - return null + throw Error("Airbnbapi: Can't login without an email.") } else if (!password) { - log.e("Airbnbapi: Can't apply for a token without a password.") - return null + throw Error("Airbnbapi: Can't apply for a token without a password.") } const options = this.buildOptions({ token: 'public', @@ -166,21 +149,12 @@ class AirApi { password } }) - try { - const response = await requestPromise(options) - if (response && response.login) { - log.i( - `Airbnbapi: Successful login for [${email}], auth ID is [${response.login.id}]` - ) - return response - } else { - log.e('Airbnbapi: no response from server when fetching token') - return null - } - } catch (e) { - log.e("Airbnbapi: Couldn't get auth token for " + email) - log.e(e.error) - return { error: e.error } + const response = await requestPromise(options) + if (response && response.login) { + log.i(`Airbnbapi: Successful login for [${email}], auth ID is [${response.login.id}]`) + return response + } else { + throw Error('Airbnbapi: no response from server when fetching token') } } @@ -190,8 +164,7 @@ class AirApi { async getPublicListingCalendar({ id, month = '1', year = '2018', count = '1' } = {}) { if (!id) { - log.e("Airbnbapi: Can't get public listing calendar without an id") - return null + throw Error("Airbnbapi: Can't get public listing calendar without an id") } const options = this.buildOptions({ token: 'public', @@ -204,29 +177,19 @@ class AirApi { count } }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't get public calendar for listing " + id) - log.e(e) - } + return await requestPromise(options) } async getCalendar({ token, id, startDate, endDate } = {}) { //log.i(colors.magenta('Airbnbapi: Requesting calendar for [ ' + id + ' ] --')) if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get a calendar without a token") - return null + throw Error("Airbnbapi: Can't get a calendar without a token") } else if (!id) { - log.e("Airbnbapi: Can't get a calendar without an id") - return null + throw Error("Airbnbapi: Can't get a calendar without an id") } else if (!startDate) { - log.e("Airbnbapi: Can't get a calendar without a start date") - return null + throw Error("Airbnbapi: Can't get a calendar without a start date") } else if (!endDate) { - log.e("Airbnbapi: Can't get a calendar without a end date") - return null + throw Error("Airbnbapi: Can't get a calendar without a end date") } const options = this.buildOptions({ @@ -241,13 +204,8 @@ class AirApi { }, timeout: 10000 }) - try { - const response = await requestPromise(options).catch(console.log) - return response.calendar_days - } catch (e) { - log.e("Airbnbapi: Couldn't get calendar for listing " + id) - log.e(e) - } + const response = await requestPromise(options) + return response.calendar_days } async setPriceForDay({ token, id, date, price, currency = this.config.currency }) { @@ -265,14 +223,9 @@ class AirApi { }, timeout: 10000 }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't set price for cal day for listing " + id) - log.e(e) - } + return await requestPromise(options) } + async setAvailabilityForDay({ token, id, date, availability }) { const options = this.buildOptions({ method: 'PUT', @@ -284,13 +237,8 @@ class AirApi { }, timeout: 10000 }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't set availability for cal day for listing " + id) - log.e(e) - } + + return await requestPromise(options) } //////////// LISTING SECTION //////////// @@ -299,14 +247,11 @@ class AirApi { async setHouseManual({ token, id, manual } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't set a house manual without a token") - return null + throw Error("Airbnbapi: Can't set a house manual without a token") } else if (!id) { - log.e("Airbnbapi: Can't set a house manual without an id") - return null + throw Error("Airbnbapi: Can't set a house manual without an id") } else if (!manual) { - log.e("Airbnbapi: Can't set a house manual without manual text") - return null + throw Error("Airbnbapi: Can't set a house manual without manual text") } const options = this.buildOptions({ method: 'POST', @@ -316,98 +261,65 @@ class AirApi { listing: { house_manual: manual } } }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't set house manual for listing " + id) - log.e(e) - } + return await requestPromise(options) } async getListingInfo(id) { if (!id) { - log.e("Airbnbapi: Can't get public listing information without an id") - return null + throw Error("Airbnbapi: Can't get public listing information without an id") } const options = this.buildOptions({ token: 'public', route: `/v1/listings/${id}` }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't get info for listing " + id) - log.e(e) - } + const response = await requestPromise(options) + return response } async getListingInfoHost({ token, id } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get a listing without a token") - return null + throw Error("Airbnbapi: Can't get a listing without a token") } else if (!id) { - log.e("Airbnbapi: Can't get a listing without an id") - return null + throw Error("Airbnbapi: Can't get a listing without an id") } const options = this.buildOptions({ route: `/v1/listings/${id}`, token, format: 'v1_legacy_long_manage_listing' }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't get listing info for id " + id) - log.e(e) - } + return await requestPromise(options) } async getHostSummary(token) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get a summary without a token") - return null + throw Error("Airbnbapi: Can't get a summary without a token") } const options = this.buildOptions({ route: `/v1/account/host_summary`, token }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't get a host summary for token " + token) - log.e(e) - } + return await requestPromise(options) } async getOwnActiveListings(token) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get an active listing list without a token") - return null + throw Error("Airbnbapi: Can't get an active listing list without a token") } const options = this.buildOptions({ route: `/v1/account/host_summary`, token }) - try { - const response = await requestPromise(options) - if (response.active_listings) { - return response.active_listings.map(listing => listing.listing.listing) - } else { - return [] - } - } catch (e) { - log.e("Airbnbapi: Couldn't get an active listing list for token " + token) - log.e(e) + + const response = await requestPromise(options) + if (response.active_listings) { + return response.active_listings.map(listing => listing.listing.listing) + } else { + return [] } } async getOwnListings({ token, userId }) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get an listing list without a token") - return null + throw Error("Airbnbapi: Can't get an listing list without a token") } const options = this.buildOptions({ route: `/v2/listings`, @@ -417,16 +329,11 @@ class AirApi { }, token }) - try { - const response = await requestPromise(options) - if (response) { - return response.listings - } else { - return [] - } - } catch (e) { - log.e("Airbnbapi: Couldn't get an listing list for token " + token) - log.e(e) + const response = await requestPromise(options) + if (response) { + return response.listings + } else { + return [] } } @@ -437,34 +344,25 @@ class AirApi { // Gets all the data for one thread async getThread({ token, id, currency = this.config.currency } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get a thread without a token") - return null + throw Error("Airbnbapi: Can't get a thread without a token") } else if (!id) { - log.e("Airbnbapi: Can't get a thread without an id") - return null + throw Error("Airbnbapi: Can't get a thread without an id") } const options = this.buildOptions({ route: '/v1/threads/' + id, token, qs: { currency } }) - try { - const response = await requestPromise(options) - return response.thread - } catch (e) { - log.e("Airbnbapi: Couldn't get thread " + id) - log.e(e) - } + const response = await requestPromise(options) + return response.thread } async getThreadsBatch({ token, ids, currency = this.config.currency } = {}) { //log.i(colors.magenta('Airbnbapi: Requesting calendar for [ ' + id + ' ] --')) if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get threads without a token") - return null + throw Error("Airbnbapi: Can't get threads without a token") } else if (!ids) { - log.e("Airbnbapi: Can't get threads without at least one id") - return null + throw Error("Airbnbapi: Can't get threads without at least one id") } const operations = ids.map(id => ({ @@ -485,23 +383,14 @@ class AirApi { }) // log.i(JSON.stringify(options, null, 4)) - let response = {} - - try { - response = await requestPromise(options).catch(log.e) - return response.operations.map(o => o.response) - // log.i(JSON.stringify(response, null, 4)) - } catch (e) { - log.e("Airbnbapi: Couldn't get threads for threads " + ids) - log.e(e) - } + const response = await requestPromise(options) + return response.operations.map(o => o.response) } // Gets a list of thread id's for a host async getThreadsFull({ token, offset = '0', limit = '2' } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get a thread list without a token") - return null + throw Error("Airbnbapi: Can't get a thread list without a token") } const options = this.buildOptions({ route: '/v2/threads', @@ -509,26 +398,19 @@ class AirApi { format: 'for_messaging_sync_with_posts', qs: { _offset: offset, _limit: limit } }) - try { - const response = await requestPromise(options) - if (response.threads) { - return response.threads //.map(item => item.id) - } else return null - } catch (e) { - log.e("Airbnbapi: Couldn't get thread list for token " + token) - log.e(e) - } + const response = await requestPromise(options) + if (response.threads) { + return response.threads //.map(item => item.id) + } else return [] } // Gets a list of thread id's for a host async getThreadFull({ token, id } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get a thread without a token") - return null + throw Error("Airbnbapi: Can't get a thread without a token") } if (!id) { - log.e("Airbnbapi: Can't get a thread without an id") - return null + throw Error("Airbnbapi: Can't get a thread without an id") } const options = this.buildOptions({ @@ -536,56 +418,37 @@ class AirApi { token, format: 'for_messaging_sync_with_posts' }) - try { - const response = await requestPromise(options) - if (response) { - return response - } else return null - } catch (e) { - log.e("Airbnbapi: Couldn't get thread for id " + id) - log.e(e) - } + return await requestPromise(options) } // Gets a list of thread id's for a host async getThreads({ token, offset = '0', limit = '2' } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get a thread list without a token") - return null + throw Error("Airbnbapi: Can't get a thread list without a token") } const options = this.buildOptions({ route: '/v2/threads', token, qs: { _offset: offset, _limit: limit } }) - try { - const response = await requestPromise(options) - if (response.threads) { - return response.threads.map(item => item.id) - } else return null - } catch (e) { - log.e("Airbnbapi: Couldn't get thread list for token " + token) - log.e(e) - } + const response = await requestPromise(options) + if (response.threads) { + return response.threads.map(item => item.id) + } else return [] } // Create a new thread async createThread({ token, id, checkIn, checkOut, guestNum = 1, message } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't create a thread without a token") - return null + throw Error("Airbnbapi: Can't create a thread without a token") } else if (!id) { - log.e("Airbnbapi: Can't create a thread without an id") - return null + throw Error("Airbnbapi: Can't create a thread without an id") } else if (!checkIn) { - log.e("Airbnbapi: Can't create a thread without a checkin") - return null + throw Error("Airbnbapi: Can't create a thread without a checkin") } else if (!checkOut) { - log.e("Airbnbapi: Can't create a thread without a checkout") - return null + throw Error("Airbnbapi: Can't create a thread without a checkout") } else if (!message || message.trim() === '') { - log.e("Airbnbapi: Can't create a thread without a message body") - return null + throw Error("Airbnbapi: Can't create a thread without a message body") } const options = this.buildOptions({ method: 'POST', @@ -599,13 +462,7 @@ class AirApi { checkout_date: checkOut } }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't send create thread for listing " + id) - log.e(e) - } + return await requestPromise(options) } //////////// RESERVATIONS SECTION //////////// @@ -614,8 +471,7 @@ class AirApi { async getReservations({ token, offset = '0', limit = '20' } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get a reservation list without a token") - return null + throw Error("Airbnbapi: Can't get a reservation list without a token") } const options = this.buildOptions({ route: '/v2/reservations', @@ -623,24 +479,17 @@ class AirApi { format: 'for_mobile_host', qs: { _offset: offset, _limit: limit } }) - try { - const response = await requestPromise(options) - return response.reservations - } catch (e) { - log.e("Airbnbapi: Couldn't get reservation list for token " + token) - log.e(e) - } + const response = await requestPromise(options) + return response.reservations } async getReservationsBatch({ token, ids, currency = this.config.currency } = {}) { // TODO change to reservation //log.i(colors.magenta('Airbnbapi: Requesting calendar for [ ' + id + ' ] --')) if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get reservations without a token") - return null + throw Error("Airbnbapi: Can't get reservations without a token") } else if (!ids || !Array.isArray(ids)) { - log.e("Airbnbapi: Can't get reservations without at least one id") - return null + throw Error("Airbnbapi: Can't get reservations without at least one id") } const operations = ids.map(id => ({ method: 'GET', @@ -661,23 +510,15 @@ class AirApi { timeout: 30000 }) // log.i(JSON.stringify(options, null, 4)) - try { - const response = await requestPromise(options).catch(console.error) - return response.operations.map(o => o.response) - // log.i(JSON.stringify(response, null, 4)) - } catch (e) { - log.e("Airbnbapi: Couldn't get reservations for ids " + ids) - log.e(e) - } + const response = await requestPromise(options) + return response.operations.map(o => o.response) } async getReservation({ token, id, currency } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get a reservation without a token") - return null + throw Error("Airbnbapi: Can't get a reservation without a token") } else if (!id) { - log.e("Airbnbapi: Can't get a reservation without an id") - return null + throw Error("Airbnbapi: Can't get a reservation without an id") } const options = this.buildOptions({ route: `/v2/reservations/${id}`, @@ -685,26 +526,18 @@ class AirApi { format: 'for_mobile_host', currency }) - try { - const response = await requestPromise(options) - return response.reservation - } catch (e) { - log.e("Airbnbapi: Couldn't get reservation for token " + token) - log.e(e) - } + const response = await requestPromise(options) + return response.reservation } // Send a message to a thread (guest) async sendMessage({ token, id, message } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't send a message without a token") - return null + throw Error("Airbnbapi: Can't send a message without a token") } else if (!id) { - log.e("Airbnbapi: Can't send a message without an id") - return null + throw Error("Airbnbapi: Can't send a message without an id") } else if (!message || message.trim() === '') { - log.e("Airbnbapi: Can't send a message without a message body") - return null + throw Error("Airbnbapi: Can't send a message without a message body") } log.i('Airbnbapi: send message for thread: ' + id + ' --') log.i("'" + message.substring(70) + "'") @@ -714,27 +547,18 @@ class AirApi { token, body: { thread_id: id, message: message.trim() } }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't send message for thread " + id) - log.e(e) - } + return await requestPromise(options) } // Send pre-approval to an inquiry // requires a id. id, and optional message async sendPreApproval({ token, thread_id, listing_id, message = '' } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't send pre-approval without a token") - return null + throw Error("Airbnbapi: Can't send pre-approval without a token") } else if (!thread_id) { - log.e("Airbnbapi: Can't send pre-approval without a thread_id") - return null + throw Error("Airbnbapi: Can't send pre-approval without a thread_id") } else if (!listing_id) { - log.e("Airbnbapi: Can't send pre-approval without a listing_id") - return null + throw Error("Airbnbapi: Can't send pre-approval without a listing_id") } const options = this.buildOptions({ method: 'POST', @@ -746,13 +570,7 @@ class AirApi { status: 'preapproved' } }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't send preapproval for thread " + thread_id) - log.e(e) - } + return await requestPromise(options) } async sendReview({ @@ -766,11 +584,9 @@ class AirApi { recommend = true } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't send a review without a token") - return null + throw Error("Airbnbapi: Can't send a review without a token") } else if (!id) { - log.e("Airbnbapi: Can't send review without an id") - return null + throw Error("Airbnbapi: Can't send review without an id") } const options = this.buildOptions({ method: 'POST', @@ -785,13 +601,7 @@ class AirApi { recommend } }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't send a review for thread " + thread_id) - log.e(e) - } + return await requestPromise(options) } async sendSpecialOffer({ @@ -805,26 +615,19 @@ class AirApi { currency = this.config.currency } = {}) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't send a special offer without a token") - return null + throw Error("Airbnbapi: Can't send a special offer without a token") } else if (!startDate) { - log.e("Airbnbapi: Can't send a special offer without a startDate") - return null + throw Error("Airbnbapi: Can't send a special offer without a startDate") } else if (!guests) { - log.e("Airbnbapi: Can't send a special offer without guests") - return null + throw Error("Airbnbapi: Can't send a special offer without guests") } else if (!listingId) { - log.e("Airbnbapi: Can't send a special offer without a listingId") - return null + throw Error("Airbnbapi: Can't send a special offer without a listingId") } else if (!nights) { - log.e("Airbnbapi: Can't send a special offer without nights (staying)") - return null + throw Error("Airbnbapi: Can't send a special offer without nights (staying)") } else if (!price) { - log.e("Airbnbapi: Can't send a special offer without a price") - return null + throw Error("Airbnbapi: Can't send a special offer without a price") } else if (!threadId) { - log.e("Airbnbapi: Can't send a special offer without a threadId") - return null + throw Error("Airbnbapi: Can't send a special offer without a threadId") } const options = this.buildOptions({ @@ -841,13 +644,7 @@ class AirApi { thread_id: threadId } }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't send a review for thread " + thread_id) - log.e(e) - } + return await requestPromise(options) } async alterationRequestResponse({ @@ -884,47 +681,25 @@ class AirApi { }, timeout: 10000 }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e( - "Airbnbapi: Can't send an alteration request response fro reservation " + - reservationId - ) - log.e(e) - } + return await requestPromise(options) } async getGuestInfo(id) { if (!id) { - log.e("Airbnbapi: Can't get guest info without an id") - return null + throw Error("Airbnbapi: Can't get guest info without an id") } const options = this.buildOptions({ token: 'public', route: `/v2/users/${id}` }) - try { - const response = await requestPromise(options) - return response && response.user ? response.user : undefined - } catch (e) { - log.e("Airbnbapi: Couldn't get guest info with user id " + id) - log.e(e) - } + const response = await requestPromise(options) + return response && response.user ? response.user : null } async getOwnUserInfo(token) { if (!(token || this.config.token)) { - log.e("Airbnbapi: Can't get user info without a token") - return null + throw Error("Airbnbapi: Can't get user info without a token") } const options = this.buildOptions({ route: '/v2/users/me', token }) - try { - const response = await requestPromise(options) - return response && response.user ? response.user : undefined - } catch (e) { - log.e("Airbnbapi: Couldn't get own info with token" + token) - log.e(e) - return null - } + const response = await requestPromise(options) + return response && response.user ? response.user : null } async listingSearch({ @@ -979,13 +754,7 @@ class AirApi { sort: sortDirection } }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't get listings for search of " + location) - log.e(e) - } + return await requestPromise(options) } async newAccount({ @@ -999,20 +768,15 @@ class AirApi { bdayYear = 1980 } = {}) { if (!username) { - log.e("Airbnbapi: Can't make a new account without a username") - return null + throw Error("Airbnbapi: Can't make a new account without a username") } else if (!password) { - log.e("Airbnbapi: Can't make a new account without a password") - return null + throw Error("Airbnbapi: Can't make a new account without a password") } else if (!authenticity_token) { - log.e("Airbnbapi: Can't make a new account without an authenticity_token") - return null + throw Error("Airbnbapi: Can't make a new account without an authenticity_token") } else if (!authenticity_token) { - log.e("Airbnbapi: Can't make a new account without a firstname") - return null + throw Error("Airbnbapi: Can't make a new account without a firstname") } else if (!authenticity_token) { - log.e("Airbnbapi: Can't make a new account without a lastname") - return null + throw Error("Airbnbapi: Can't make a new account without a lastname") } const options = this.buildOptions({ method: 'POST', @@ -1030,13 +794,7 @@ class AirApi { 'user_profile_info[receive_promotional_email]': 0 } }) - try { - const response = await requestPromise(options) - return response - } catch (e) { - log.e("Airbnbapi: Couldn't make new account for username " + username) - log.e(e) - } + return await requestPromise(options) } } diff --git a/src/metapoints.js b/src/metapoints.js index cb9827b..66a25f8 100644 --- a/src/metapoints.js +++ b/src/metapoints.js @@ -4,8 +4,7 @@ export default { todo: 'make meta endpoints for combining data from multiple endpoints', async mGetOwnActiveListingsFull(token) { if (!token) { - log.e("Airbnbapi: Can't get an active listing list without a token") - return null + throw Error("Airbnbapi: Can't get an active listing list without a token") } const listings = await this.getOwnActiveListings(token) const fullListings = await Promise.all( diff --git a/test/main.js b/test/main.js index cc43898..e69dc71 100644 --- a/test/main.js +++ b/test/main.js @@ -3,10 +3,13 @@ process.env.NODE_ENV = 'test' let abba = require('../build/main.js') let log = require('../build/log.js') let chai = require('chai') +let chaiAsPromised = require('chai-as-promised') let nock = require('nock') let _ = require('lodash') let {assert, should, expect} = chai +chai.use(chaiAsPromised); + let dummyData = require('./dummydata.json') // console.log(JSON.stringify(dummyData, null, 4)) @@ -19,8 +22,8 @@ const nockauthl = _ => nockauth().log(console.log) describe('airbnbapi', () => { describe('#testAuth(token)', () => { - it('should return null if a token is not passed', async () => { - expect(await abba.testAuth()).to.be.null + it('should throw if a token is not passed', async () => { + await expect(abba.testAuth()).to.be.rejected }) // Mock endpoint: invalid token @@ -32,7 +35,7 @@ describe('airbnbapi', () => { it('should return false for incorrect token', async () => { // console.log(await abba.testAuth('z')) - expect(await abba.testAuth('z')).to.be.false + await expect(abba.testAuth('z')).to.eventually.be.false }) // Mock endpoint: valid token 'mockcorrecttoken' @@ -41,16 +44,16 @@ describe('airbnbapi', () => { .query(true) .reply(200, {operations:[]}) it('should return true for correct token', async () => { - expect(await abba.testAuth('mockcorrecttoken')).to.be.true + await expect(abba.testAuth('mockcorrecttoken')).to.eventually.be.true }) }) describe('#newAccessToken({username, password})', () => { // Mock endpoint: invalid info - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await abba.newAccessToken()).to.be.null - expect(await abba.newAccessToken({password: 'asdf'})).to.be.null - expect(await abba.newAccessToken({username: 'asdf'})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(abba.newAccessToken()).to.be.rejected + await expect(abba.newAccessToken({password: 'asdf'})).to.be.rejected + await expect(abba.newAccessToken({username: 'asdf'})).to.be.rejected }) nock(apiBaseUrl) @@ -62,8 +65,8 @@ describe('airbnbapi', () => { .query(true) .reply(400, {"error": "mock invalid username or password"}) - it('should return error object if login details are incorrect', async () => { - expect(await abba.newAccessToken({username: 'wrong', password: 'wrong'})).to.have.property('error') + it('should return error object if login details are incorrect', () => { + abba.newAccessToken({username: 'wrong', password: 'wrong'}).catch(e => expect(e).to.have.property('error')) }) // Mock endpoint: valid info 'mockuser'. 'mockpass' nock(apiBaseUrl) @@ -76,17 +79,17 @@ describe('airbnbapi', () => { .reply(200, {access_token:'mocktoken'}) it('should return a token obejct if the login details are correct', async () => { - expect(await abba.newAccessToken({username: 'mockuser', password: 'mockpass' })).to.have.property('token') + await expect(abba.newAccessToken({username: 'mockuser', password: 'mockpass' })).to.eventually.have.property('token') }) }) describe('#login({email, password})', () => { const testFunc = abba.login.bind(abba) // Mock endpoint: invalid info - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({password: 'asdf'})).to.be.null - expect(await testFunc({email: 'asdf'})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({password: 'asdf'})).to.be.rejected + await expect(testFunc({email: 'asdf'})).to.be.rejected }) nock(apiBaseUrl) @@ -97,8 +100,8 @@ describe('airbnbapi', () => { .query(true) .reply(400, {"error": "mock invalid username or password"}) - it('should return error object if login details are incorrect', async () => { - expect(await testFunc({email: 'wrong', password: 'wrong'})).to.have.property('error') + it('should throw error object if login details are incorrect', async () => { + testFunc({email: 'wrong', password: 'wrong'}).catch(e => expect(e).to.have.property('error')) }) // Mock endpoint: valid info 'mockuser'. 'mockpass' @@ -111,15 +114,15 @@ describe('airbnbapi', () => { .reply(200, { login:{id:'mocktoken'}}) it('should return a summary object if the login details are correct', async () => { - expect(await testFunc({email: 'mockuser', password: 'mockpass' })).to.have.property('login') + await expect(testFunc({email: 'mockuser', password: 'mockpass' })).to.eventually.have.property('login') }) }) describe('#getPublicListingCalendar({id, month, year, count})', () => { const testFunc = abba.getPublicListingCalendar.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({month: '1', year: '2018', count: '1' })).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({month: '1', year: '2018', count: '1' })).to.be.rejected }) nock(apiBaseUrl) .get('/v2/calendar_months') @@ -135,18 +138,18 @@ describe('airbnbapi', () => { .reply(200, { public:'calendar'}) it('should return a calendar if arguments are correct', async () => { - expect(await testFunc({id: 1234, month: '1', year: '2018', count: '1' })).to.have.property('public') + await expect(testFunc({id: 1234, month: '1', year: '2018', count: '1' })).to.eventually.have.property('public') }) }) describe('#getCalendar({token, id, startDate, endDate})', () => { const testFunc = abba.getCalendar.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({id:1234, startDate:'2017/11/01', endDate:'2017/12/01'})).to.be.null - expect(await testFunc({token: 'mocktoken', startDate:'2017/11/01', endDate:'2017/12/01'})).to.be.null - expect(await testFunc({token: 'mocktoken', id:1234, endDate:'2017/12/01'})).to.be.null - expect(await testFunc({token: 'mocktoken', id:1234, startDate:'2017/11/01'})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({id:1234, startDate:'2017/11/01', endDate:'2017/12/01'})).to.be.rejected + await expect(testFunc({token: 'mocktoken', startDate:'2017/11/01', endDate:'2017/12/01'})).to.be.rejected + await expect(testFunc({token: 'mocktoken', id:1234, endDate:'2017/12/01'})).to.be.rejected + await expect(testFunc({token: 'mocktoken', id:1234, startDate:'2017/11/01'})).to.be.rejected }) nockauth() .get('/v2/calendar_days') @@ -160,7 +163,7 @@ describe('airbnbapi', () => { }) .reply(200, {calendar_days: []}) it('should return a array of calendar days if arguments are correct', async () => { - expect(await testFunc({token: 'mockcorrecttoken', id:1234, startDate:'2017-11-01', endDate:'2017-12-01'})).to.be.an('array') + await expect(testFunc({token: 'mockcorrecttoken', id:1234, startDate:'2017-11-01', endDate:'2017-12-01'})).to.eventually.be.an('array') }) }) @@ -175,7 +178,7 @@ describe('airbnbapi', () => { .query(true) .reply(200, {response: 'success'}) it('should return result object for correct arguments', async () => { - expect(await testFunc({token:'mockcorrecttoken', id:1234, date:'2017-11-01', price:123, currency:'USD'})).to.be.an('object') + await expect(testFunc({token:'mockcorrecttoken', id:1234, date:'2017-11-01', price:123, currency:'USD'})).to.eventually.be.an('object') }) }) @@ -188,17 +191,17 @@ describe('airbnbapi', () => { .query(true) .reply(200, {response: 'success'}) it('should return result object for correct arguments', async () => { - expect(await testFunc({token:'mockcorrecttoken', id:1234, date:'2017-11-01', availability:'available'})).to.be.an('object') + await expect(testFunc({token:'mockcorrecttoken', id:1234, date:'2017-11-01', availability:'available'})).to.eventually.be.an('object') }) }) describe('#setHouseManual({token, id, manual})', () => { const testFunc = abba.setHouseManual.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({id:1234, manual:'manual'})).to.be.null - expect(await testFunc({token: 'mocktoken', manual:'manual'})).to.be.null - expect(await testFunc({token: 'mocktoken', id:1234})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({id:1234, manual:'manual'})).to.be.rejected + await expect(testFunc({token: 'mocktoken', manual:'manual'})).to.be.rejected + await expect(testFunc({token: 'mocktoken', id:1234})).to.be.rejected }) nockauth() .post('/v1/listings/1234/update', { @@ -207,14 +210,14 @@ describe('airbnbapi', () => { .query(true) .reply(200, {response:'ok'}) it('should return response object', async () => { - expect(await testFunc({token: 'mockcorrecttoken', id:1234, manual:'manual'})).to.be.an('object') + await expect(testFunc({token: 'mockcorrecttoken', id:1234, manual:'manual'})).to.eventually.be.an('object') }) }) describe('#getListingInfo(id)', () => { const testFunc = abba.getListingInfo.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected // expect(await testFunc({not_id:1234})).to.be.null }) nock(apiBaseUrl) @@ -223,16 +226,16 @@ describe('airbnbapi', () => { .reply(200, {listing:{}}) it('should return a response object if arguments are correct', async () => { - expect(await testFunc(1234)).to.have.property('listing') + await expect(testFunc(1234)).to.eventually.have.property('listing') }) }) describe('#getListingInfoHost({token, id})', () => { const testFunc = abba.getListingInfoHost.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({id: 1234})).to.be.null - expect(await testFunc({token: 'mockcorrecttoken'})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({id: 1234})).to.be.rejected + await expect(testFunc({token: 'mockcorrecttoken'})).to.be.rejected }) nockauth() .get('/v1/listings/1234') @@ -240,14 +243,14 @@ describe('airbnbapi', () => { .reply(200, {listing:{}}) it('should return a response object if arguments are correct', async () => { - expect(await testFunc({token: 'mockcorrecttoken', id: 1234})).to.have.property('listing') + await expect(testFunc({token: 'mockcorrecttoken', id: 1234})).to.eventually.have.property('listing') }) }) describe('#getHostSummary(token)', () => { const testFunc = abba.getHostSummary.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected }) nockauth() .get('/v1/account/host_summary') @@ -255,15 +258,15 @@ describe('airbnbapi', () => { .reply(200, {active_listings:[{id: 6789}]}) it('should return a response object if arguments are correct', async () => { - expect(await testFunc('mockcorrecttoken')).to.have.property('active_listings') + await expect(testFunc('mockcorrecttoken')).to.eventually.have.property('active_listings') }) }) describe('#getOwnActiveListings(token)', () => { console.log(JSON.stringify(dummyData.getOwnActiveListings, null, 4)) const testFunc = abba.getOwnActiveListings.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected }) nockauth() .get('/v1/account/host_summary') @@ -271,16 +274,16 @@ describe('airbnbapi', () => { .reply(200, {active_listings: dummyData.getOwnActiveListings}) it('should return a response object if arguments are correct', async () => { - expect(await testFunc('mockcorrecttoken')).to.be.an('array') + await expect(testFunc('mockcorrecttoken')).to.eventually.be.an('array') }) }) describe('#getThread({token, id, currency})', () => { const testFunc = abba.getThread.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({token:'mockcorrecttoken'})).to.be.null - expect(await testFunc({id:1234})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({token:'mockcorrecttoken'})).to.be.rejected + await expect(testFunc({id:1234})).to.be.rejected }) nockauth() .get('/v1/threads/1234') @@ -288,18 +291,18 @@ describe('airbnbapi', () => { .reply(200, {thread:{id: 1234}}) it('should return a thread object if arguments are correct', async () => { - expect(await testFunc({token: 'mockcorrecttoken', id: 1234})).to.have.property('id') + await expect(testFunc({token: 'mockcorrecttoken', id: 1234})).to.eventually.have.property('id') }) }) describe('#getThreadsBatch({token, ids, currency})', () => { const testFunc = abba.getThreadsBatch.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected }) - it('should return null if token or ids is not passed', async () => { - expect(await testFunc({id:1234})).to.be.null - expect(await testFunc({token: 'mocktoken'})).to.be.null + it('should throw if token or ids is not passed', async () => { + await expect(testFunc({id:1234})).to.be.rejected + await expect(testFunc({token: 'mocktoken'})).to.be.rejected }) nockauth() .post('/v2/batch', { @@ -324,16 +327,16 @@ describe('airbnbapi', () => { .query(true) .reply(200, {operations: [{id: 987}, {id: 876}]}) it('should return type array if arguments are correct', async () => { - expect(await testFunc({token: 'mockcorrecttoken', ids:[987,876]})).to.be.an('array') + await expect(testFunc({token: 'mockcorrecttoken', ids:[987,876]})).to.eventually.be.an('array') }) }) describe('#getThreadsFull({token, offset, limit})', () => { const testFunc = abba.getThreadsFull.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({offset:2})).to.be.null - expect(await testFunc({offset:2, limit:0})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({offset:2})).to.be.rejected + await expect(testFunc({offset:2, limit:0})).to.be.rejected }) nockauth() .get('/v2/threads') @@ -341,16 +344,16 @@ describe('airbnbapi', () => { .reply(200, {threads:[{id: 1234},{id: 2345},{id: 3456},{id: 5687},{id: 6789}]}) it('should return a list(array) of threads if arguments are correct', async () => { - expect(await testFunc({token: 'mockcorrecttoken', offset:0, limit:10})).to.be.an('array') + await expect(testFunc({token: 'mockcorrecttoken', offset:0, limit:10})).to.eventually.be.an('array') }) }) describe('#getThreads({token, offset, limit})', () => { const testFunc = abba.getThreads.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({offset:2})).to.be.null - expect(await testFunc({offset:2, limit:0})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({offset:2})).to.be.rejected + await expect(testFunc({offset:2, limit:0})).to.be.rejected }) nockauth() .get('/v2/threads') @@ -358,19 +361,19 @@ describe('airbnbapi', () => { .reply(200, {threads:[{id: 1234},{id: 2345},{id: 3456},{id: 5687},{id: 6789}]}) it('should return a list(array) of threads if arguments are correct', async () => { - expect(await testFunc({token: 'mockcorrecttoken', offset:0, limit:10})).to.be.an('array') + await expect(testFunc({token: 'mockcorrecttoken', offset:0, limit:10})).to.eventually.be.an('array') }) }) describe('#createThread({token, id, checkin, checkout, guestNum, message})', () => { const testFunc = abba.createThread.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({id:1234, checkIn:'2017-01-01', checkOut:'2017-01-02', message:'asd'})).to.be.null - expect(await testFunc({token: 'mocktoken', checkIn:'2017-01-01', checkOut:'2017-01-02', message:'asd'})).to.be.null - expect(await testFunc({token: 'mocktoken', id:1234, checkOut:'2017-01-02', message:'asd'})).to.be.null - expect(await testFunc({token: 'mocktoken', id:1234, checkIn:'2017-01-01', message:'asd'})).to.be.null - expect(await testFunc({token: 'mocktoken', id:1234, checkIn:'2017-01-01', checkOut:'2017-01-02'})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({id:1234, checkIn:'2017-01-01', checkOut:'2017-01-02', message:'asd'})).to.be.rejected + await expect(testFunc({token: 'mocktoken', checkIn:'2017-01-01', checkOut:'2017-01-02', message:'asd'})).to.be.rejected + await expect(testFunc({token: 'mocktoken', id:1234, checkOut:'2017-01-02', message:'asd'})).to.be.rejected + await expect(testFunc({token: 'mocktoken', id:1234, checkIn:'2017-01-01', message:'asd'})).to.be.rejected + await expect(testFunc({token: 'mocktoken', id:1234, checkIn:'2017-01-01', checkOut:'2017-01-02'})).to.be.rejected }) nockauth() .post('/v1/threads/create', { @@ -383,16 +386,16 @@ describe('airbnbapi', () => { .query(true) .reply(200, {response:'ok'}) it('should return response object', async () => { - expect(await testFunc({token: 'mockcorrecttoken', id:1234, checkIn:'2017-01-01', checkOut:'2017-01-02', message:'hello!'})).to.be.an('object') + await expect(testFunc({token: 'mockcorrecttoken', id:1234, checkIn:'2017-01-01', checkOut:'2017-01-02', message:'hello!'})).to.eventually.be.an('object') }) }) - describe('getReservations({token, offset, limit}', () => { + describe('getReservations({token, offset, limit}', async () => { const testFunc = abba.getReservations.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({offset:2})).to.be.null - expect(await testFunc({offset:2, limit:0})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({offset:2})).to.be.rejected + await expect(testFunc({offset:2, limit:0})).to.be.rejected }) nockauth() .get('/v2/reservations') @@ -400,34 +403,34 @@ describe('airbnbapi', () => { .reply(200, {reservations:[{id: 1234},{id: 2345},{id: 3456},{id: 5687},{id: 6789}]}) it('should return a list(array) of threads if arguments are correct', async () => { - expect(await testFunc({token: 'mockcorrecttoken', offset:0, limit:10})).to.be.an('array') + await expect(testFunc({token: 'mockcorrecttoken', offset:0, limit:10})).to.eventually.be.an('array') }) }) describe('#getReservationsBatch({ token, ids, currency})', () => { - const testFunc = abba.getThreadsBatch.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null + const testFunc = abba.getReservationsBatch.bind(abba) + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected }) - it('should return null if token or ids is not passed', async () => { - expect(await testFunc({id:1234})).to.be.null - expect(await testFunc({token: 'mocktoken'})).to.be.null + it('should throw if token or ids is not passed', async () => { + await expect(testFunc({id:1234})).to.be.rejected + await expect(testFunc({token: 'mocktoken'})).to.be.rejected }) nockauth() .post('/v2/batch', { operations: [ { method: 'GET', - path: '/threads/987', + path: '/reservations/987', query: { - _format: 'for_messaging_sync_with_posts' + _format: 'for_mobile_host' } }, { method: 'GET', - path: '/threads/876', + path: '/reservations/876', query: { - _format: 'for_messaging_sync_with_posts' + _format: 'for_mobile_host' } }, ], @@ -436,16 +439,16 @@ describe('airbnbapi', () => { .query(true) .reply(200, {operations: [{id: 987}, {id: 876}]}) it('should return type array if arguments are correct', async () => { - expect(await testFunc({token: 'mockcorrecttoken', ids:[987,876]})).to.be.an('array') + await expect(testFunc({token: 'mockcorrecttoken', ids:[987,876]})).to.eventually.be.an('array') }) }) describe('#getReservation({token, id, currency})', () => { const testFunc = abba.getReservation.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({token:'mockcorrecttoken'})).to.be.null - expect(await testFunc({id:1234})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({token:'mockcorrecttoken'})).to.be.rejected + await expect(testFunc({id:1234})).to.be.rejected }) nockauth() .get('/v2/reservations/1234') @@ -453,17 +456,17 @@ describe('airbnbapi', () => { .reply(200, {reservation:{id: 1234}}) it('should return a reservation object if arguments are correct', async () => { - expect(await testFunc({token: 'mockcorrecttoken', id: 1234})).to.have.property('id') + await expect(testFunc({token: 'mockcorrecttoken', id: 1234})).to.eventually.have.property('id') }) }) describe('#sendMessage({ token, id, message })', () => { const testFunc = abba.sendMessage.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({id:1234, message:'hello'})).to.be.null - expect(await testFunc({token: 'mocktoken', message:'hello'})).to.be.null - expect(await testFunc({token: 'mocktoken', id:1234})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({id:1234, message:'hello'})).to.be.rejected + await expect(testFunc({token: 'mocktoken', message:'hello'})).to.be.rejected + await expect(testFunc({token: 'mocktoken', id:1234})).to.be.rejected }) nockauth() .post('/v2/messages', { @@ -473,17 +476,17 @@ describe('airbnbapi', () => { .query(true) .reply(200, {response:'ok'}) it('should return response object', async () => { - expect(await testFunc({token: 'mockcorrecttoken', id:1234, message:'hello!'})).to.be.an('object') + await expect(testFunc({token: 'mockcorrecttoken', id:1234, message:'hello!'})).to.eventually.be.an('object') }) }) describe('#sendPreApproval({token, thread_id, listing_id, message})', () => { const testFunc = abba.sendPreApproval.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({thread_id:987, listing_id:1234})).to.be.null - expect(await testFunc({token: 'mocktoken', listing_id:1234})).to.be.null - expect(await testFunc({token: 'mocktoken', thread_id:987})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({thread_id:987, listing_id:1234})).to.be.rejected + await expect(testFunc({token: 'mocktoken', listing_id:1234})).to.be.rejected + await expect(testFunc({token: 'mocktoken', thread_id:987})).to.be.rejected }) nockauth() .post('/v1/threads/987/update', { @@ -494,16 +497,16 @@ describe('airbnbapi', () => { .query(true) .reply(200, {response:'ok'}) it('should return response object', async () => { - expect(await testFunc({token: 'mockcorrecttoken', listing_id:1234, thread_id:987})).to.be.an('object') + await expect(testFunc({token: 'mockcorrecttoken', listing_id:1234, thread_id:987})).to.eventually.be.an('object') }) }) describe('#sendReview({token, id, comments, private_feedback, cleanliness, communication, respect_house_rules, recommend})', () => { const testFunc = abba.sendReview.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc({id:456})).to.be.null - expect(await testFunc({token: 'mocktoken'})).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc({id:456})).to.be.rejected + await expect(testFunc({token: 'mocktoken'})).to.be.rejected }) nockauth() .post('/v1/reviews/456/update', { @@ -517,22 +520,22 @@ describe('airbnbapi', () => { .query(true) .reply(200, {response:'ok'}) it('should return response object', async () => { - expect(await testFunc({token: 'mockcorrecttoken', id:456})).to.be.an('object') + await expect(testFunc({token: 'mockcorrecttoken', id:456})).to.eventually.be.an('object') }) }) describe('#sendSpecialOffer({token, startDate, guests, listingId, nights, price, threadId, currency})', () => { const testFunc = abba.sendSpecialOffer.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected // expect(await testFunc({token:'mocktoken', startDate: '2017-01-01', guests:1, listingId:1234, nights:2, price:10000, threadId: 987})).to.be.null - expect(await testFunc({startDate: '2017-01-01', guests:1, listingId:1234, nights:2, price:10000, threadId: 987})).to.be.null - expect(await testFunc({token:'mocktoken', guests:1, listingId:1234, nights:2, price:10000, threadId: 987})).to.be.null - expect(await testFunc({token:'mocktoken', startDate: '2017-01-01', listingId:1234, nights:2, price:10000, threadId: 987})).to.be.null - expect(await testFunc({token:'mocktoken', startDate: '2017-01-01', guests:1, nights:2, price:10000, threadId: 987})).to.be.null - expect(await testFunc({token:'mocktoken', startDate: '2017-01-01', guests:1, listingId:1234, price:10000, threadId: 987})).to.be.null - expect(await testFunc({token:'mocktoken', startDate: '2017-01-01', guests:1, listingId:1234, nights:2, threadId: 987})).to.be.null - expect(await testFunc({token:'mocktoken', startDate: '2017-01-01', guests:1, listingId:1234, nights:2, price:10000})).to.be.null + await expect(testFunc({startDate: '2017-01-01', guests:1, listingId:1234, nights:2, price:10000, threadId: 987})).to.be.rejected + await expect(testFunc({token:'mocktoken', guests:1, listingId:1234, nights:2, price:10000, threadId: 987})).to.be.rejected + await expect(testFunc({token:'mocktoken', startDate: '2017-01-01', listingId:1234, nights:2, price:10000, threadId: 987})).to.be.rejected + await expect(testFunc({token:'mocktoken', startDate: '2017-01-01', guests:1, nights:2, price:10000, threadId: 987})).to.be.rejected + await expect(testFunc({token:'mocktoken', startDate: '2017-01-01', guests:1, listingId:1234, price:10000, threadId: 987})).to.be.rejected + await expect(testFunc({token:'mocktoken', startDate: '2017-01-01', guests:1, listingId:1234, nights:2, threadId: 987})).to.be.rejected + await expect(testFunc({token:'mocktoken', startDate: '2017-01-01', guests:1, listingId:1234, nights:2, price:10000})).to.be.rejected }) nockauth() .post('/v2/special_offers', { @@ -546,14 +549,14 @@ describe('airbnbapi', () => { .query(true) .reply(200, {response:'ok'}) it('should return response object', async () => { - expect(await testFunc({token:'mockcorrecttoken', startDate: '2017-01-01', guests:1, listingId:1234, nights:2, price:10000, threadId: 987})).to.be.an('object') + await expect(testFunc({token:'mockcorrecttoken', startDate: '2017-01-01', guests:1, listingId:1234, nights:2, price:10000, threadId: 987})).to.eventually.be.an('object') }) }) describe('#getGuestInfo(id)', () => { const testFunc = abba.getGuestInfo.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected }) nock(apiBaseUrl) .get('/v2/users/1234') @@ -561,15 +564,15 @@ describe('airbnbapi', () => { .reply(200, {user:{id:1234}}) it('should return a response object if arguments are correct', async () => { - expect(await testFunc(1234)).to.have.property('id') + await expect(testFunc(1234)).to.eventually.have.property('id') }) }) describe('#getOwnUserInfo(token)', () => { const testFunc = abba.getOwnUserInfo.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null - expect(await testFunc('wrongtoken')).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected + await expect(testFunc('wrongtoken')).to.be.rejected }) nockauth() .get('/v2/users/me') @@ -577,7 +580,7 @@ describe('airbnbapi', () => { .reply(200, {user:{id:1234}}) it('should return a user info object if arguments are correct', async () => { - expect(await testFunc('mockcorrecttoken')).to.have.property('id') + await expect(testFunc('mockcorrecttoken')).to.eventually.have.property('id') }) }) @@ -590,15 +593,15 @@ describe('airbnbapi', () => { .twice() .query(true) .reply(200, {search_results:[{id:123},{id:456},{id:789}], metadata:{foo:'bar'}}) - expect(await testFunc()).to.have.property('search_results') - expect(await testFunc({location: 'New York, United States', offset: 0, limit: 50, language: 'en', currency: 'USD'})).to.have.property('search_results') + await expect(testFunc()).to.eventually.have.property('search_results') + await expect(testFunc({location: 'New York, United States', offset: 0, limit: 50, language: 'en', currency: 'USD'})).to.eventually.have.property('search_results') }) }) describe('#mGetOwnActiveListings(token)', () => { const testFunc = abba.mGetOwnActiveListingsFull.bind(abba) - it('should return null if no arguments are passed or arguments are missing', async () => { - expect(await testFunc()).to.be.null + it('should throw if no arguments are passed or arguments are missing', async () => { + await expect(testFunc()).to.be.rejected }) nockauth() .get('/v1/account/host_summary') @@ -613,7 +616,7 @@ describe('airbnbapi', () => { }) it('should return a response object if arguments are correct', async () => { - expect(await testFunc('mockcorrecttoken')).to.be.an('array') + await expect(testFunc('mockcorrecttoken')).to.eventually.be.an('array') }) }) })