appstore and support: async'ify

This commit is contained in:
Girish Ramakrishnan
2021-08-18 15:54:53 -07:00
parent 200018a022
commit 03e22170da
16 changed files with 458 additions and 633 deletions
+27 -32
View File
@@ -10,50 +10,47 @@ exports = module.exports = {
getSubscription
};
var appstore = require('../appstore.js'),
const appstore = require('../appstore.js'),
assert = require('assert'),
BoxError = require('../boxerror.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess;
HttpSuccess = require('connect-lastmile').HttpSuccess,
safe = require('safetydance');
function getApps(req, res, next) {
appstore.getApps(function (error, apps) {
if (error) return next(BoxError.toHttpError(error));
async function getApps(req, res, next) {
const [error, apps] = await safe(appstore.getApps());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { apps }));
});
next(new HttpSuccess(200, { apps }));
}
function getApp(req, res, next) {
async function getApp(req, res, next) {
assert.strictEqual(typeof req.params.appstoreId, 'string');
appstore.getApp(req.params.appstoreId, function (error, app) {
if (error) return next(BoxError.toHttpError(error));
const [error, app] = await safe(appstore.getApp(req.params.appstoreId));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, app));
});
next(new HttpSuccess(200, app));
}
function getAppVersion(req, res, next) {
async function getAppVersion(req, res, next) {
assert.strictEqual(typeof req.params.appstoreId, 'string');
assert.strictEqual(typeof req.params.versionId, 'string');
appstore.getAppVersion(req.params.appstoreId, req.params.versionId, function (error, manifest) {
if (error) return next(BoxError.toHttpError(error));
const [error, manifest] = await safe(appstore.getAppVersion(req.params.appstoreId, req.params.versionId));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, manifest));
});
next(new HttpSuccess(200, manifest));
}
function createUserToken(req, res, next) {
appstore.getUserToken(function (error, result) {
if (error) return next(BoxError.toHttpError(error));
async function createUserToken(req, res, next) {
const [error, accessToken] = await safe(appstore.createUserToken());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, { accessToken: result }));
});
next(new HttpSuccess(201, { accessToken }));
}
function registerCloudron(req, res, next) {
async function registerCloudron(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.email !== 'string' || !req.body.email) return next(new HttpError(400, 'email must be string'));
@@ -61,19 +58,17 @@ function registerCloudron(req, res, next) {
if ('totpToken' in req.body && typeof req.body.totpToken !== 'string') return next(new HttpError(400, 'totpToken must be string'));
if (typeof req.body.signup !== 'boolean') return next(new HttpError(400, 'signup must be a boolean'));
appstore.registerWithLoginCredentials(req.body, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(appstore.registerWithLoginCredentials(req.body));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, {}));
});
next(new HttpSuccess(201, {}));
}
function getSubscription(req, res, next) {
async function getSubscription(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
appstore.getSubscription(function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(appstore.getSubscription());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, result)); // { email, cloudronId, cloudronCreatedAt, plan, current_period_end, canceled_at, cancel_at, status, features }
});
next(new HttpSuccess(200, result)); // { email, cloudronId, cloudronCreatedAt, plan, current_period_end, canceled_at, cancel_at, status, features }
}