Skip to content

Commit

Permalink
feat(data-point-express): migrate to use async/await (#443)
Browse files Browse the repository at this point in the history
re #434
  • Loading branch information
acatl authored Oct 22, 2019
1 parent ca7d270 commit ed41ea9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
5 changes: 3 additions & 2 deletions packages/data-point-express/lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ function createMiddleware(service) {
});
}

function create(options) {
async function create(options) {
const dpInjection = {
DataPoint
};
const dpOptions = Object.assign({}, options, dpInjection);

return Service.create(dpOptions).then(createMiddleware);
const service = await Service.create(dpOptions);
return createMiddleware(service);
}

module.exports = {
Expand Down
6 changes: 2 additions & 4 deletions packages/data-point-express/lib/factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jest.mock("../../data-point-cache/lib/io-redis", () => {
describe("create - all middleware", () => {
let service;
const consoleWarn = console.warn;
beforeAll(() => {
beforeAll(async () => {
console.warn = () => {};
const options = {
entities: {
Expand All @@ -23,9 +23,7 @@ describe("create - all middleware", () => {
})
}
};
return Factory.create(options).then(dpService => {
service = dpService;
});
service = await Factory.create(options);
});

afterAll(() => {
Expand Down
7 changes: 3 additions & 4 deletions packages/data-point-express/lib/inspector-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ function create(dataPoint) {
const router = Express.Router();

router.use(bodyParser.json());
router.get("/", (req, res) => {
InspectorUi.getInspector().then(html => {
res.send(html);
});
router.get("/", async (req, res) => {
const html = await InspectorUi.getInspector();
res.send(html);
});

router.post("/", (req, res, next) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/data-point-express/lib/inspector-ui/inspector-ui.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require("fs");
const path = require("path");
const Promise = require("bluebird");
const util = require("util");

const readFile = Promise.promisify(fs.readFile);
const readFile = util.promisify(fs.readFile);

function getInspector() {
return readFile(path.resolve(__dirname, "inspect.html"), "utf8");
Expand Down
31 changes: 17 additions & 14 deletions packages/data-point-express/lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,23 @@ function createErrorMessage(err) {
};
}

function resolveReducer(dataPoint, reducer, options, res, initialValue = {}) {
dataPoint
.transform(reducer, initialValue, options)
.then(acc => {
if (typeof acc.value === "string") {
res.send(acc.value);
return;
}
res.json(acc.value);
})
.catch(err => {
res.status(400).send(createErrorMessage(err));
})
.done();
async function resolveReducer(
dataPoint,
reducer,
options,
res,
initialValue = {}
) {
try {
const acc = await dataPoint.transform(reducer, initialValue, options);
if (typeof acc.value === "string") {
res.send(acc.value);
return;
}
res.json(acc.value);
} catch (error) {
res.status(400).send(createErrorMessage(error));
}
}

module.exports = {
Expand Down

0 comments on commit ed41ea9

Please sign in to comment.