xcoobee-sdk
Version:
The XcooBee SDK is a facility to abstract lower level calls and implement standard behaviors. The XcooBee team is providing this to improve the speed of implementation and show the best practices while interacting with XcooBee.
166 lines (151 loc) • 5.79 kB
JavaScript
const InboxApi = require('../api/InboxApi');
const ErrorResponse = require('./ErrorResponse');
const SdkUtils = require('./SdkUtils');
const SuccessResponse = require('./SuccessResponse');
/**
* The Inbox SDK service.
* Instances are not created directly. An {@link Sdk} instance will have a
* reference to an `Inbox` SDK instance through the {@link Sdk#inbox inbox}
* property.
*
* ```js
* const XcooBee = require('xcoobee-sdk');
*
* const sdk = new XcooBee.Sdk(...);
* sdk.inbox.listInbox(...).then(...);
* ```
*
* @param {Config} config
* @param {ApiAccessTokenCache} apiAccessTokenCache
* @param {UsersCache} usersCache
*/
class Inbox {
/* eslint-disable-next-line valid-jsdoc */
/**
* Constructs an `Inbox` SDK service instance.
*/
constructor(config, apiAccessTokenCache, usersCache) {
this._ = {
apiAccessTokenCache,
config: config || null,
usersCache,
};
}
/**
* @protected
* @param {Config} config
*/
set config(config) {
this._.config = config;
}
/**
* @protected
*/
_assertValidState() {
if (!this._.config) {
throw TypeError('Illegal State: Default config has not been set yet.');
}
}
/**
* Deletes an item from the inbox with the specified message ID.
*
* @async
* @param {string} messageId
* @param {Config} [config]
*
* @returns {Promise<SuccessResponse | ErrorResponse>}
* @property {number} code - The response status code.
* @property {Error} [error] - The response error if status is not successful.
* @property {string} [error.message] - The error message.
* @property {string} request_id - The ID of the request generated by the XcooBee
* system.
* @property {true} [result] - The result of the response if status is successful.
*
* @throws {XcooBeeError}
*/
async deleteInboxItem(messageId, config = null) {
this._assertValidState();
const apiCfg = SdkUtils.resolveApiCfg(config, this._.config);
const { apiKey, apiSecret, apiUrlRoot } = apiCfg;
try {
const apiAccessToken = await this._.apiAccessTokenCache.get(apiUrlRoot, apiKey, apiSecret);
const user = await this._.usersCache.get(apiUrlRoot, apiKey, apiSecret);
const userCursor = user.cursor;
const result = await InboxApi.deleteInboxItem(apiUrlRoot, apiAccessToken, userCursor, messageId);
const response = new SuccessResponse(result);
return response;
} catch (err) {
throw new ErrorResponse(400, err);
}
}
/**
* Fetches an item from the inbox with the specified message ID.
*
* @async
* @param {string} messageId
* @param {Config} [config]
*
* @returns {Promise<SuccessResponse | ErrorResponse>}
* @property {number} code - The response status code.
* @property {Error} [error] - The response error if status is not successful.
* @property {string} [error.message] - The error message.
* @property {string} request_id - The ID of the request generated by the XcooBee
* system.
* @property {Object} [result] - The result of the response if status is successful.
* @property {InboxItemDetails} result.inbox_item - The inbox item details.
* @property {string} result.inbox_item.download_link - The inbox item download
* link.
* @property {InboxItem} result.inbox_item.info - The inbox item info.
*
* @throws {XcooBeeError}
*/
async getInboxItem(messageId, config = null) {
this._assertValidState();
const apiCfg = SdkUtils.resolveApiCfg(config, this._.config);
const { apiKey, apiSecret, apiUrlRoot } = apiCfg;
try {
const apiAccessToken = await this._.apiAccessTokenCache.get(apiUrlRoot, apiKey, apiSecret);
const user = await this._.usersCache.get(apiUrlRoot, apiKey, apiSecret);
const userCursor = user.cursor;
const result = await InboxApi.getInboxItem(apiUrlRoot, apiAccessToken, userCursor, messageId);
const response = new SuccessResponse(result);
return response;
} catch (err) {
throw new ErrorResponse(400, err);
}
}
/**
* Fetch a page of items from the inbox.
*
* @async
* @param {Config} [config]
*
* @returns {Promise<PagingResponse | ErrorResponse>} - The response.
* @property {number} code - The response status code.
* @property {Error} [error] - The response error if status is not successful.
* @property {string} [error.message] - The error message.
* @property {string} request_id - The ID of the request generated by the XcooBee
* system.
* @property {Object} [result] - The result of the response if status is successful.
* @property {InboxItem[]} result.data - Inbox items for this page.
* @property {Object} [result.page_info] - The page information.
* @property {boolean} result.page_info.has_next_page - Flag indicating whether there is
* another page of data to may be fetched.
* @property {string} result.page_info.end_cursor - The end cursor.
*
* @throws {XcooBeeError}
*/
async listInbox(config = null) {
this._assertValidState();
const fetchPage = async (apiCfg, params) => {
const { apiKey, apiSecret, apiUrlRoot } = apiCfg;
const { after, limit } = params;
const apiAccessToken = await this._.apiAccessTokenCache.get(apiUrlRoot, apiKey, apiSecret);
const inboxPage = await InboxApi.listInbox(apiUrlRoot, apiAccessToken, after, limit);
return inboxPage;
};
const apiCfg = SdkUtils.resolveApiCfg(config, this._.config);
return SdkUtils.startPaging(fetchPage, apiCfg, {});
}
}// eo class Inbox
module.exports = Inbox;