diff --git a/src/lib/OAuthModel.js b/src/OAuthModel.js similarity index 95% rename from src/lib/OAuthModel.js rename to src/OAuthModel.js index 18b9b69e..2921e7df 100644 --- a/src/lib/OAuthModel.js +++ b/src/OAuthModel.js @@ -5,9 +5,9 @@ import type { TokenObject, User, UserRepository, -} from '../types'; +} from './types'; -import ouathClients from '../oauthClients.json'; +import ouathClients from './oauthClients.json'; class OauthModel { _userRepository: UserRepository; diff --git a/src/lib/RouteConfig.js b/src/RouteConfig.js similarity index 92% rename from src/lib/RouteConfig.js rename to src/RouteConfig.js index f46907ac..5d375c49 100644 --- a/src/lib/RouteConfig.js +++ b/src/RouteConfig.js @@ -7,13 +7,13 @@ import type { Middleware, NextFunction, } from 'express'; -import type { Settings } from '../types'; +import type { Settings } from './types'; import type Controller from './controllers/Controller'; -import type HttpError from './HttpError'; -import OAuthModel from './OAuthModel'; import OAuthServer from 'express-oauth-server'; import multer from 'multer'; +import OAuthModel from './OAuthModel'; +import HttpError from './lib/HttpError'; // TODO fix flow errors, come up with better name; const maybe = (middleware: Middleware, condition: boolean): Middleware => @@ -77,7 +77,7 @@ export default ( allowedUploads ? injectFilesMiddleware.fields(allowedUploads) : defaultMiddleware, - async (request: $Request, response: $Response) => { + async (request: $Request, response: $Response): Promise => { const argumentNames = (route.match(/:[\w]*/g) || []).map( (argumentName: string): string => argumentName.replace(':', ''), ); @@ -110,8 +110,9 @@ export default ( response.status(functionResult.status).json(functionResult.data); } } catch (error) { - response.status(error.status).json({ - error: error.message, + const httpError = new HttpError(error); + response.status(httpError.status).json({ + error: httpError.message, ok: false, }); } diff --git a/src/app.js b/src/app.js index e872d4d8..042f8c99 100644 --- a/src/app.js +++ b/src/app.js @@ -17,18 +17,18 @@ import api from './views/api_v1'; import eventsV1 from './views/EventViews001'; // Repositories -import DeviceRepository from './lib/repository/DeviceRepository'; +import DeviceRepository from './repository/DeviceRepository'; import { DeviceAttributeFileRepository, DeviceKeyFileRepository, } from 'spark-protocol'; // Routing -import routeConfig from './lib/RouteConfig'; -import DevicesController from './lib/controllers/DevicesController'; -import ProvisioningController from './lib/controllers/ProvisioningController'; -import UsersController from './lib/controllers/UsersController'; -import WebhooksController from './lib/controllers/WebhooksController'; +import routeConfig from './RouteConfig'; +import DevicesController from './controllers/DevicesController'; +import ProvisioningController from './controllers/ProvisioningController'; +import UsersController from './controllers/UsersController'; +import WebhooksController from './controllers/WebhooksController'; export default (settings: Settings, deviceServer: Object): $Application => { const app = express(); diff --git a/src/lib/controllers/Controller.js b/src/controllers/Controller.js similarity index 91% rename from src/lib/controllers/Controller.js rename to src/controllers/Controller.js index c8c65b41..77294b81 100644 --- a/src/lib/controllers/Controller.js +++ b/src/controllers/Controller.js @@ -1,7 +1,7 @@ // @flow import type { $Request, $Response } from 'express'; -import type { User } from '../../types'; +import type { User } from '../types'; import type { HttpResult } from './types'; export default class Controller { diff --git a/src/lib/controllers/DevicesController.js b/src/controllers/DevicesController.js similarity index 78% rename from src/lib/controllers/DevicesController.js rename to src/controllers/DevicesController.js index a3083add..b631def1 100644 --- a/src/lib/controllers/DevicesController.js +++ b/src/controllers/DevicesController.js @@ -1,15 +1,15 @@ // @flow -import type { Device, DeviceRepository } from '../../types'; -import type { DeviceAPIType } from '../deviceToAPI'; +import type { Device, DeviceRepository } from '../types'; +import type { DeviceAPIType } from '../lib/deviceToAPI'; import Controller from './Controller'; -import HttpError from '../HttpError'; +import HttpError from '../lib/HttpError'; import allowUpload from '../decorators/allowUpload'; import httpVerb from '../decorators/httpVerb'; import route from '../decorators/route'; -import deviceToAPI from '../deviceToAPI'; +import deviceToAPI from '../lib/deviceToAPI'; class DevicesController extends Controller { _deviceRepository: DeviceRepository; @@ -31,7 +31,7 @@ class DevicesController extends Controller { @httpVerb('post') @route('/v1/binaries') - compileSources() { + compileSources() { // eslint-disable-line class-methods-use-this throw new HttpError('not supported in the current server version'); } @@ -81,7 +81,8 @@ class DevicesController extends Controller { return this.ok({ result: varValue }); } catch (error) { - if (error.indexOf && error.indexOf('Variable not found') >= 0) { + const errorMessage = error.message; + if (errorMessage.indexOf('Variable not found') >= 0) { throw new HttpError('Variable not found', 404); } throw error; @@ -107,22 +108,18 @@ class DevicesController extends Controller { } // TODO not implemented yet // 2 flash device with known app - try { - if (postBody.app_id) { - this._deviceRepository.flashKnownApp( - deviceID, - postBody.app_id, - ); - return this.ok({ id: deviceID, status: 'Update started' }); - } + if (postBody.app_id) { + this._deviceRepository.flashKnownApp( + deviceID, + postBody.app_id, + ); + return this.ok({ id: deviceID, status: 'Update started' }); + } - const file = this.request.files.file[0]; - if (file && file.originalname.endsWith('.bin')) { - await this._deviceRepository.flashBinary(deviceID, file); - return this.ok({ id: deviceID, status: 'Update started' }); - } - } catch (error) { - throw new HttpError(error.message); + const file = this.request.files.file[0]; + if (file && file.originalname.endsWith('.bin')) { + await this._deviceRepository.flashBinary(deviceID, file); + return this.ok({ id: deviceID, status: 'Update started' }); } throw new HttpError('Did not update device'); @@ -149,10 +146,11 @@ class DevicesController extends Controller { ); return this.ok(deviceToAPI(device, result)); } catch (error) { - if (error.indexOf && error.indexOf('Unknown Function') >= 0) { + const errorMessage = error.message; + if (errorMessage.indexOf('Unknown Function') >= 0) { throw new HttpError('Function not found', 404); } - throw new HttpError(error.message); + throw error; } } } diff --git a/src/lib/controllers/EventsController.js b/src/controllers/EventsController.js similarity index 61% rename from src/lib/controllers/EventsController.js rename to src/controllers/EventsController.js index 8d9cce1f..71f4401c 100644 --- a/src/lib/controllers/EventsController.js +++ b/src/controllers/EventsController.js @@ -1,5 +1,9 @@ +// @flow + +import Controller from './Controller'; + class EventsController extends Controller { - + } export default EventsController; diff --git a/src/lib/controllers/ProvisioningController.js b/src/controllers/ProvisioningController.js similarity index 85% rename from src/lib/controllers/ProvisioningController.js rename to src/controllers/ProvisioningController.js index 8af08eee..0de9d43b 100644 --- a/src/lib/controllers/ProvisioningController.js +++ b/src/controllers/ProvisioningController.js @@ -1,12 +1,12 @@ // @flow -import type { DeviceRepository } from '../../types'; +import type { DeviceRepository } from '../types'; import Controller from './Controller'; import httpVerb from '../decorators/httpVerb'; import route from '../decorators/route'; -import deviceToAPI from '../deviceToAPI'; -import HttpError from '../HttpError'; +import deviceToAPI from '../lib/deviceToAPI'; +import HttpError from '../lib/HttpError'; class ProvisioningController extends Controller { _deviceRepository: DeviceRepository; diff --git a/src/lib/controllers/UsersController.js b/src/controllers/UsersController.js similarity index 96% rename from src/lib/controllers/UsersController.js rename to src/controllers/UsersController.js index 10506954..848bc6f2 100644 --- a/src/lib/controllers/UsersController.js +++ b/src/controllers/UsersController.js @@ -3,11 +3,11 @@ import type { UserCredentials, UserRepository, -} from '../../types'; +} from '../types'; import basicAuthParser from 'basic-auth-parser'; import Controller from './Controller'; -import HttpError from '../HttpError'; +import HttpError from '../lib/HttpError'; import anonymous from '../decorators/anonymous'; import httpVerb from '../decorators/httpVerb'; import route from '../decorators/route'; diff --git a/src/lib/controllers/WebhooksController.js b/src/controllers/WebhooksController.js similarity index 97% rename from src/lib/controllers/WebhooksController.js rename to src/controllers/WebhooksController.js index 82a9548e..996d7b0c 100644 --- a/src/lib/controllers/WebhooksController.js +++ b/src/controllers/WebhooksController.js @@ -5,10 +5,10 @@ import type { RequestType, Webhook, WebhookMutator, -} from '../../types'; +} from '../types'; import Controller from './Controller'; -import HttpError from '../HttpError'; +import HttpError from '../lib/HttpError'; import httpVerb from '../decorators/httpVerb'; import route from '../decorators/route'; diff --git a/src/lib/controllers/types.js b/src/controllers/types.js similarity index 99% rename from src/lib/controllers/types.js rename to src/controllers/types.js index e4e2b066..c32a21e3 100644 --- a/src/lib/controllers/types.js +++ b/src/controllers/types.js @@ -1,6 +1,5 @@ // @flow - export type HttpResult = { data: ?TType, status: number, diff --git a/src/lib/decorators/allowUpload.js b/src/decorators/allowUpload.js similarity index 100% rename from src/lib/decorators/allowUpload.js rename to src/decorators/allowUpload.js diff --git a/src/lib/decorators/anonymous.js b/src/decorators/anonymous.js similarity index 100% rename from src/lib/decorators/anonymous.js rename to src/decorators/anonymous.js diff --git a/src/lib/decorators/httpVerb.js b/src/decorators/httpVerb.js similarity index 83% rename from src/lib/decorators/httpVerb.js rename to src/decorators/httpVerb.js index 74efc366..b7aadfc1 100644 --- a/src/lib/decorators/httpVerb.js +++ b/src/decorators/httpVerb.js @@ -1,6 +1,6 @@ // @flow -import type { Decorator, Descriptor } from './types'; +import type { Decorator, Descriptor, HttpVerb } from './types'; import type Controller from '../controllers/Controller'; /* eslint-disable no-param-reassign */ diff --git a/src/lib/decorators/route.js b/src/decorators/route.js similarity index 100% rename from src/lib/decorators/route.js rename to src/decorators/route.js diff --git a/src/lib/decorators/types.js b/src/decorators/types.js similarity index 100% rename from src/lib/decorators/types.js rename to src/decorators/types.js diff --git a/src/lib/HttpError.js b/src/lib/HttpError.js index 91269c22..bb4cb74b 100644 --- a/src/lib/HttpError.js +++ b/src/lib/HttpError.js @@ -3,9 +3,16 @@ class HttpError extends Error { status: number; - constructor(message: string, status?: number = 400) { - super(message); - this.status = status; + constructor( + error: string | Error | HttpError, + status?: number = 400, + ) { + super(error.message || error); + if (typeof error.status === 'number') { + this.status = error.status; + } else { + this.status = status; + } } } diff --git a/src/lib/repository/DeviceRepository.js b/src/repository/DeviceRepository.js similarity index 98% rename from src/lib/repository/DeviceRepository.js rename to src/repository/DeviceRepository.js index d9828213..865d6fac 100644 --- a/src/lib/repository/DeviceRepository.js +++ b/src/repository/DeviceRepository.js @@ -5,13 +5,12 @@ import type { DeviceServer } from 'spark-protocol'; import type { Device, DeviceAttributes, - DeviceAttributeRepository, Repository, - } from '../../types'; +} from '../types'; import Moniker from 'moniker'; import ursa from 'ursa'; -import HttpError from '../HttpError'; +import HttpError from '../lib/HttpError'; const NAME_GENERATOR = Moniker.generator([Moniker.adjective, Moniker.noun]); diff --git a/src/lib/repository/UserFileRepository.js b/src/repository/UserFileRepository.js similarity index 94% rename from src/lib/repository/UserFileRepository.js rename to src/repository/UserFileRepository.js index f4a87253..9b995033 100644 --- a/src/lib/repository/UserFileRepository.js +++ b/src/repository/UserFileRepository.js @@ -1,10 +1,10 @@ // @flow -import type { TokenObject, User, UserCredentials } from '../../types'; +import type { TokenObject, User, UserCredentials } from '../types'; import { JSONFileManager, uuid } from 'spark-protocol'; -import PasswordHasher from '../PasswordHasher'; -import HttpError from '../HttpError'; +import PasswordHasher from '../lib/PasswordHasher'; +import HttpError from '../lib/HttpError'; class UserFileRepository { _fileManager: JSONFileManager; diff --git a/src/lib/repository/WebhookFileRepository.js b/src/repository/WebhookFileRepository.js similarity index 91% rename from src/lib/repository/WebhookFileRepository.js rename to src/repository/WebhookFileRepository.js index e8bbf1fd..9c1f54c7 100644 --- a/src/lib/repository/WebhookFileRepository.js +++ b/src/repository/WebhookFileRepository.js @@ -1,9 +1,9 @@ // @flow -import type { Webhook } from '../../types'; +import type { Webhook } from '../types'; import { JSONFileManager, uuid } from 'spark-protocol'; -import HttpError from '../HttpError'; +import HttpError from '../lib/HttpError'; class WebhookFileRepository { _fileManager: JSONFileManager; diff --git a/src/settings.js b/src/settings.js index 6f8642e6..e5250486 100644 --- a/src/settings.js +++ b/src/settings.js @@ -20,8 +20,8 @@ */ import path from 'path'; -import WebhookFileRepository from './lib/repository/WebhookFileRepository'; -import UsersFileRepository from './lib/repository/UserFileRepository'; +import WebhookFileRepository from './repository/WebhookFileRepository'; +import UsersFileRepository from './repository/UserFileRepository'; export default { accessTokenLifetime: 7776000, // 90 days, diff --git a/test/setup/settings.js b/test/setup/settings.js index 2441817d..25e69536 100644 --- a/test/setup/settings.js +++ b/test/setup/settings.js @@ -1,8 +1,8 @@ // @flow import path from 'path'; -import WebhookFileRepository from '../../src/lib/repository/WebhookFileRepository'; -import UsersFileRepository from '../../src/lib/repository/UserFileRepository'; +import WebhookFileRepository from '../../src/repository/WebhookFileRepository'; +import UsersFileRepository from '../../src/repository/UserFileRepository'; import { DeviceAttributeFileRepository, DeviceKeyFileRepository } from 'spark-protocol'; export default {