-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added typescript lib interface + Airbnb Eslint addition and fixes (#64)
* Added TS support * Added eslint * added knex to prod dependencies instead of dev * Removed unnecessary function input * Fixed lint errors * Fixed error where a bitwise operator was used instead of standard or * Fixed lint errors in test.js Returned KnexStore construction inside the lib's init function, because it must be dynamically created with the session object provided by user * Added testing instructions to README * Removed node v8 from Travis.yml * Fixed an ignored minor lint error: Favor global import * Fixed lint errors in example.js * Running tests locally with passwords, because both MySQL and Postgres refuse all connections without a password Travis should still work without db passwords, should just add ON_TRAVIS=1 in Travis's env * Reverted some of my silly changes in index.js Fixed lint errors in some of example-postgres.js * Fixed the rest of the lint errors in example-postgres.js * Fixed some ignored lint errors * Eslint ignored index.d.ts * Fixed bug in index.d.ts where I forgot to add new to an interface * Added travis env variable that signals to tests that it's in fact running in Travis
- Loading branch information
Showing
11 changed files
with
1,904 additions
and
722 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/**/*.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
env: { | ||
commonjs: true, | ||
es6: true, | ||
node: true, | ||
}, | ||
extends: [ | ||
'airbnb-base', | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,45 @@ | ||
const express = require("express"); | ||
/* eslint-disable import/no-unresolved */ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
/* eslint-disable no-console */ | ||
|
||
const express = require('express'); | ||
|
||
const app = express(); | ||
|
||
const session = require("express-session"); | ||
const KnexSessionStore = require("connect-session-knex")(session); | ||
const session = require('express-session'); | ||
const KnexSessionStore = require('connect-session-knex')(session); | ||
|
||
const Knex = require('knex'); | ||
|
||
const Knex = require("knex"); | ||
const knex = Knex({ | ||
client: "pg", | ||
client: 'pg', | ||
connection: { | ||
host: "127.0.0.1", | ||
user: "postgres", | ||
password: "", | ||
database: "travis_ci_test" | ||
} | ||
host: '127.0.0.1', | ||
user: 'postgres', | ||
password: '', | ||
database: 'travis_ci_test', | ||
}, | ||
}); | ||
|
||
const store = new KnexSessionStore({ | ||
knex: knex, | ||
tablename: "sessions" // optional. Defaults to 'sessions' | ||
knex, | ||
tablename: 'sessions', // optional. Defaults to 'sessions' | ||
}); | ||
|
||
app.use( | ||
session({ | ||
secret: "keyboard cat", | ||
secret: 'keyboard cat', | ||
cookie: { | ||
maxAge: 10000 // ten seconds, for testing | ||
maxAge: 10000, // ten seconds, for testing | ||
}, | ||
store: store | ||
}) | ||
store, | ||
}), | ||
); | ||
|
||
var count = 0; | ||
|
||
app.use("/", function(req, res, next) { | ||
var n = req.session.views || 0; | ||
req.session.views = ++n; | ||
res.end(n + " views"); | ||
app.use('/', (req, res) => { | ||
const n = req.session.views || 0; | ||
req.session.views = n + 1; | ||
res.end(`${n} views`); | ||
}); | ||
|
||
app.listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,41 @@ | ||
const express = require("express"); // Express 4 | ||
const app = express(); | ||
/* eslint-disable import/no-unresolved */ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
/* eslint-disable no-console */ | ||
|
||
const express = require('express'); | ||
|
||
const app = express(); // Express 4 | ||
const session = require('express-session'); | ||
const KnexSessionStore = require('connect-session-knex')(session); | ||
|
||
const session = require("express-session"); | ||
const KnexSessionStore = require("connect-session-knex")(session); | ||
const store = new KnexSessionStore(/* options here */); // defaults to a sqlite3 database | ||
|
||
app.use( | ||
session({ | ||
secret: "keyboard cat", | ||
secret: 'keyboard cat', | ||
cookie: { | ||
maxAge: 30000 // 30 seconds for testing | ||
maxAge: 30000, // 30 seconds for testing | ||
}, | ||
store: store | ||
}) | ||
store, | ||
}), | ||
); | ||
|
||
var count = 0; | ||
|
||
app.use("/", function(req, res, next) { | ||
var n = req.session.views || 0; | ||
req.session.views = ++n; | ||
res.end(n + " views"); | ||
app.use('/', (req, res) => { | ||
const n = req.session.views || 0; | ||
req.session.views = n + 1; | ||
res.end(`${n} views`); | ||
}); | ||
|
||
app.listen(3000); | ||
|
||
setInterval(function() { | ||
store.length().then(function(length) { | ||
console.log("There are " + JSON.stringify(length) + " sessions"); | ||
setInterval(() => { | ||
store.length().then((length) => { | ||
console.log(`There are ${JSON.stringify(length)} sessions`); | ||
}); | ||
}, 2000); | ||
|
||
setInterval(function() { | ||
store.clear().then(function(length) { | ||
console.log("Cleared " + JSON.stringify(length) + " sessions"); | ||
setInterval(() => { | ||
store.clear().then((length) => { | ||
console.log(`Cleared ${JSON.stringify(length)} sessions`); | ||
}); | ||
}, 30000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as Knex from 'knex'; | ||
import expressSession, { Store } from 'express-session'; | ||
|
||
declare module 'connect-session-knex' { | ||
type ConfigType = { | ||
tablename?: string; | ||
sidfieldname?: string; | ||
knex?: Knex; | ||
createTable?: boolean; | ||
clearInterval?: number; | ||
}; | ||
|
||
interface StoreFactory { | ||
new (configs?: ConfigType): Store; | ||
} | ||
|
||
export default function initFunction(session: typeof expressSession): StoreFactory; | ||
} |
Oops, something went wrong.