Skip to content

Commit 243d281

Browse files
committed
feat: update to es6
1 parent 04a1010 commit 243d281

File tree

15 files changed

+221
-237
lines changed

15 files changed

+221
-237
lines changed

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ module.exports = {
22
"env": {
33
"browser": true,
44
"node": true,
5+
"es6": true
56
},
7+
"parser": "babel-eslint",
68
"extends": "airbnb/legacy",
79
"rules":{
810
"prefer-template": 0,

.travis.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,3 @@ language: node_js
22
node_js:
33
- "6"
44
script: npm run lint
5-
notifications:
6-
email:
7-
recipients:
8-
9-
on_success: never
10-
on_failure: always
11-

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This Node.js sample shows how to start getting notifications from Microsoft Grap
99
* Listen for notifications from Microsoft Graph.
1010
* Request for more information in Microsoft Office 365 using data in the notification.
1111

12-
![Microsoft Graph Webhook Sample for Node.js screenshot](/readme-images/Microsoft-Graph-NodeJs-Webhooks.png)
12+
![Microsoft Graph Webhook Sample for Node.js screenshot](https://user-images.githubusercontent.com/3375461/27248965-40a3e74c-52c0-11e7-95bf-6963696e3d3a.png)
1313

1414
The previous screenshot shows the app in action. After your app gets a subscription, your app gets a notification when events happen in user data. Your app can then react to the event. This sample writes a list item for every notification received by the notification URL.
1515

app.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
33
* See LICENSE in the project root for license information.
44
*/
5-
var express = require('express');
6-
var path = require('path');
7-
var favicon = require('serve-favicon');
8-
var logger = require('morgan');
9-
var bodyParser = require('body-parser');
105

11-
var routes = require('./routes/index');
12-
var listen = require('./routes/listen');
6+
import express from 'express';
7+
import path from 'path';
8+
import favicon from 'serve-favicon';
9+
import logger from 'morgan';
10+
import bodyParser from 'body-parser';
1311

14-
var app = express();
12+
import { authRouter } from './routes/auth';
13+
import { listenRouter } from './routes/listen';
1514

16-
var env = process.env.NODE_ENV || 'development';
15+
export const app = express();
16+
17+
const env = process.env.NODE_ENV || 'development';
1718
app.locals.ENV = env;
18-
app.locals.ENV_DEVELOPMENT = env === 'development';
19+
app.locals.ENV_DEVELOPMENT = (env === 'development');
1920

2021
// view engine setup
2122
app.set('views', path.join(__dirname, 'views'));
@@ -24,18 +25,16 @@ app.set('view engine', 'jade');
2425
app.use(favicon(__dirname + '/public/img/favicon.ico'));
2526
app.use(logger('dev'));
2627
app.use(bodyParser.json());
27-
app.use(bodyParser.urlencoded({
28-
extended: true
29-
}));
28+
app.use(bodyParser.urlencoded({ extended: true }));
3029

3130
app.use(express.static(path.join(__dirname, 'public')));
3231

33-
app.use('/', routes);
34-
app.use('/listen', listen);
32+
app.use('/', authRouter);
33+
app.use('/listen', listenRouter);
3534

3635
// catch 404 and forward to error handler
37-
app.use(function (req, res, next) {
38-
var err = new Error('Not Found');
36+
app.use((req, res, next) => {
37+
let err = new Error('Not Found');
3938
err.status = 404;
4039
next(err);
4140
});
@@ -45,7 +44,7 @@ app.use(function (req, res, next) {
4544
// development error handler
4645
// will print stacktrace
4746
if (app.get('env') === 'development') {
48-
app.use(function (err, req, res) {
47+
app.use((err, req, res) => {
4948
res.status(err.status || 500);
5049
res.render('error', {
5150
message: err.message,
@@ -57,13 +56,11 @@ if (app.get('env') === 'development') {
5756

5857
// production error handler
5958
// no stacktraces leaked to user
60-
app.use(function (err, req, res) {
59+
app.use((err, req, res) => {
6160
res.status(err.status || 500);
6261
res.render('error', {
6362
message: err.message,
64-
error: {},
63+
error: { },
6564
title: 'error'
6665
});
6766
});
68-
69-
module.exports = app;

bin/www

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/usr/bin/env node
2-
var app = require('../app');
32

4-
var dbHelper = new (require('../helpers/dbHelper'))();
5-
dbHelper.createDatabase();
3+
import { app } from '../app';
4+
import { createDatabase } from '../helpers/dbHelper';
5+
6+
createDatabase();
67

78
app.set('port', process.env.PORT || 3000);
89

9-
var server = app.listen(app.get('port'), function() {
10+
const server = app.listen(app.get('port'), () => {
1011
console.log('Express server listening on port ' + server.address().port);
1112
});

gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
33
* See LICENSE in the project root for license information.
44
*/
5+
56
var gulp = require('gulp');
67
var nodemon = require('gulp-nodemon');
78
var plumber = require('gulp-plumber'); // eslint-disable-line no-unused-vars

helpers/authHelper.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
* See LICENSE in the project root for license information.
44
*/
55

6-
var AuthenticationContext = require('adal-node').AuthenticationContext;
7-
var adalConfiguration = require('../constants.js').adalConfiguration;
8-
var resource = 'https://graph.microsoft.com/';
6+
import { AuthenticationContext } from 'adal-node';
7+
import { adalConfiguration } from '../constants';
8+
9+
const resource = 'https://graph.microsoft.com/';
910

1011
/**
1112
* Generate a fully formed uri to use for authentication based on the supplied resource argument
1213
* @return {string} a fully formed uri with which authentication can be completed.
1314
*/
14-
function getAuthUrl() {
15+
export function getAuthUrl() {
1516
return adalConfiguration.authority + '/oauth2/authorize' +
1617
'?client_id=' + adalConfiguration.clientID +
1718
'&response_type=code' +
@@ -24,15 +25,15 @@ function getAuthUrl() {
2425
* @param {string} res A URI that identifies the resource for which the token is valid.
2526
* @param {AcquireTokenCallback} callback The callback function.
2627
*/
27-
function getTokenFromCode(code, callback) {
28-
var authContext = new AuthenticationContext(adalConfiguration.authority);
28+
export function getTokenFromCode(code, callback) {
29+
const authContext = new AuthenticationContext(adalConfiguration.authority);
2930
authContext.acquireTokenWithAuthorizationCode(
3031
code,
3132
adalConfiguration.redirectUri,
3233
resource,
3334
adalConfiguration.clientID,
3435
adalConfiguration.clientSecret,
35-
function (error, token) {
36+
(error, token) => {
3637
if (error) {
3738
callback(error, null);
3839
} else {
@@ -41,6 +42,3 @@ function getTokenFromCode(code, callback) {
4142
}
4243
);
4344
}
44-
45-
exports.getAuthUrl = getAuthUrl;
46-
exports.getTokenFromCode = getTokenFromCode;

helpers/dbHelper.js

Lines changed: 69 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33
* See LICENSE in the project root for license information.
44
*/
55

6-
var sqlite3 = require('sqlite3').verbose();
7-
var fs = require('fs');
8-
var dbFile = './helpers/database.sqlite3';
6+
import fs from 'fs';
7+
import sql from 'sqlite3';
98

10-
function dbHelper() { }
9+
const dbFile = './helpers/database.sqlite3';
10+
const sqlite3 = sql.verbose();
1111

1212
/**
1313
* Create SQLite3 table Subscription.
1414
*/
15-
dbHelper.prototype.createDatabase = function createDatabase() {
16-
var dbExists = fs.existsSync(dbFile);
17-
var db = new sqlite3.Database(dbFile);
18-
var createSubscriptionStatement =
15+
export function createDatabase() {
16+
const dbExists = fs.existsSync(dbFile);
17+
const db = new sqlite3.Database(dbFile);
18+
const createSubscriptionStatement =
1919
'CREATE TABLE Subscription (' +
20-
'UserId TEXT NOT NULL, ' +
21-
'SubscriptionId TEXT NOT NULL, ' +
22-
'AccessToken TEXT NOT NULL, ' +
23-
'Resource TEXT NOT NULL, ' +
24-
'ChangeType TEXT NOT NULL, ' +
25-
'ClientState TEXT NOT NULL, ' +
26-
'NotificationUrl TEXT NOT NULL, ' +
27-
'SubscriptionExpirationDateTime TEXT NOT NULL' +
20+
'UserId TEXT NOT NULL, ' +
21+
'SubscriptionId TEXT NOT NULL, ' +
22+
'AccessToken TEXT NOT NULL, ' +
23+
'Resource TEXT NOT NULL, ' +
24+
'ChangeType TEXT NOT NULL, ' +
25+
'ClientState TEXT NOT NULL, ' +
26+
'NotificationUrl TEXT NOT NULL, ' +
27+
'SubscriptionExpirationDateTime TEXT NOT NULL' +
2828
')';
2929

3030
db.serialize(function createTable() {
@@ -41,20 +41,20 @@ dbHelper.prototype.createDatabase = function createDatabase() {
4141
}
4242
});
4343
db.close();
44-
};
44+
}
4545

46-
dbHelper.prototype.getSubscription = function getSubscription(subscriptionId, callback) {
47-
var db = new sqlite3.Database(dbFile);
48-
var getUserDataStatement =
46+
export function getSubscription(subscriptionId, callback) {
47+
const db = new sqlite3.Database(dbFile);
48+
const getUserDataStatement =
4949
'SELECT ' +
50-
'UserId as userId, ' +
51-
'SubscriptionId as subscriptionId, ' +
52-
'AccessToken as accessToken, ' +
53-
'Resource as resource, ' +
54-
'ChangeType as changeType, ' +
55-
'ClientState as clientState, ' +
56-
'NotificationUrl as notificationUrl, ' +
57-
'SubscriptionExpirationDateTime as subscriptionExpirationDateTime ' +
50+
'UserId as userId, ' +
51+
'SubscriptionId as subscriptionId, ' +
52+
'AccessToken as accessToken, ' +
53+
'Resource as resource, ' +
54+
'ChangeType as changeType, ' +
55+
'ClientState as clientState, ' +
56+
'NotificationUrl as notificationUrl, ' +
57+
'SubscriptionExpirationDateTime as subscriptionExpirationDateTime ' +
5858
'FROM Subscription ' +
5959
'WHERE SubscriptionId = $subscriptionId ' +
6060
'AND SubscriptionExpirationDateTime > datetime(\'now\')';
@@ -68,51 +68,48 @@ dbHelper.prototype.getSubscription = function getSubscription(subscriptionId, ca
6868
callback
6969
);
7070
});
71-
};
71+
}
7272

73-
dbHelper.prototype.saveSubscription =
74-
function saveSubscription(subscriptionData, callback) {
75-
var db = new sqlite3.Database(dbFile);
76-
var insertStatement =
77-
'INSERT INTO Subscription ' +
78-
'(UserId, SubscriptionId, AccessToken, Resource, ChangeType, ' +
79-
'ClientState, NotificationUrl, SubscriptionExpirationDateTime) ' +
80-
'VALUES ($userId, $subscriptionId, $accessToken, $resource, $changeType, ' +
81-
'$clientState, $notificationUrl, $subscriptionExpirationDateTime)';
73+
export function saveSubscription(subscriptionData, callback) {
74+
const db = new sqlite3.Database(dbFile);
75+
const insertStatement =
76+
'INSERT INTO Subscription ' +
77+
'(UserId, SubscriptionId, AccessToken, Resource, ChangeType, ' +
78+
'ClientState, NotificationUrl, SubscriptionExpirationDateTime) ' +
79+
'VALUES ($userId, $subscriptionId, $accessToken, $resource, $changeType, ' +
80+
'$clientState, $notificationUrl, $subscriptionExpirationDateTime)';
8281

83-
db.serialize(function executeInsert() {
84-
db.run(
85-
insertStatement,
86-
{
87-
$userId: subscriptionData.userId,
88-
$subscriptionId: subscriptionData.id,
89-
$accessToken: subscriptionData.accessToken,
90-
$resource: subscriptionData.resource,
91-
$clientState: subscriptionData.clientState,
92-
$changeType: subscriptionData.changeType,
93-
$notificationUrl: subscriptionData.notificationUrl,
94-
$subscriptionExpirationDateTime: subscriptionData.expirationDateTime
95-
},
96-
callback
97-
);
98-
});
99-
};
100-
101-
dbHelper.prototype.deleteSubscription =
102-
function deleteSubscription(subscriptionId, callback) {
103-
var db = new sqlite3.Database(dbFile);
104-
var deleteStatement = 'DELETE FROM Subscription WHERE ' +
105-
'SubscriptionId = $subscriptionId';
82+
db.serialize(function executeInsert() {
83+
db.run(
84+
insertStatement,
85+
{
86+
$userId: subscriptionData.userId,
87+
$subscriptionId: subscriptionData.id,
88+
$accessToken: subscriptionData.accessToken,
89+
$resource: subscriptionData.resource,
90+
$clientState: subscriptionData.clientState,
91+
$changeType: subscriptionData.changeType,
92+
$notificationUrl: subscriptionData.notificationUrl,
93+
$subscriptionExpirationDateTime: subscriptionData.expirationDateTime
94+
},
95+
callback
96+
);
97+
});
98+
}
10699

107-
db.serialize(function executeDelete() {
108-
db.run(
109-
deleteStatement,
110-
{
111-
$subscriptionId: subscriptionId
112-
},
113-
callback
114-
);
115-
});
116-
};
100+
export function deleteSubscription(subscriptionId, callback) {
101+
const db = new sqlite3.Database(dbFile);
102+
const deleteStatement =
103+
'DELETE FROM Subscription WHERE ' +
104+
'SubscriptionId = $subscriptionId';
117105

118-
module.exports = dbHelper;
106+
db.serialize(function executeDelete() {
107+
db.run(
108+
deleteStatement,
109+
{
110+
$subscriptionId: subscriptionId
111+
},
112+
callback
113+
);
114+
});
115+
}

0 commit comments

Comments
 (0)