Skip to content

Commit

Permalink
Fixed JSDoc Types & Added TypeScript Types
Browse files Browse the repository at this point in the history
  • Loading branch information
kartikk221 committed Jan 21, 2022
1 parent c3ef4d7 commit 4d5f361
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</div>

## Motivation
HyperExpress.Session aims to provide a simple middleware that implements a session engine with high flexibility while being performant. This middleware is unopinionated and can be used with any storage mechanism of your choice.
HyperExpress.Session aims to provide a simple middleware that implements a session engine with high flexibility while being performant. This middleware is unopinionated and can be used with any storage mechanism of your choice. This middleware ships with TypeScript types out of the box, so both vanilla Javascript and TypeScript projects are welcome.

## Installation
HyperExpress and HyperExpressWS can both be installed using node package manager (`npm`)
Expand Down
123 changes: 78 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions src/components/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Session {
/**
* This method asynchronously generates a strong cryptographically random session id.
*
* @returns {Promise} Promise -> String
* @returns {Promise<string>}
*/
async generate_id() {
return await this.#session_engine.methods.id();
Expand All @@ -57,7 +57,7 @@ class Session {
* unsign the the provided id and thus verifies user input.
*
* @param {String} signed_id Signed Session ID
* @param {String} secret Optional (Utilizes SessionEngine.options.cookie.secret by default)
* @param {String=} secret Optional (Utilizes SessionEngine.options.cookie.secret by default)
* @returns {Boolean}
*/
set_signed_id(signed_id, secret) {
Expand Down Expand Up @@ -96,7 +96,7 @@ class Session {
* This method is used to start a session for incoming request.
* Note! This method is asynchronous as it performs the 'read' operation to read session data.
*
* @returns {Promise} Promise
* @returns {Promise}
*/
async start() {
// Return if session has already started
Expand Down Expand Up @@ -143,7 +143,7 @@ class Session {
* Note! This operation performs 2 underlying operations as it first
* deletes old session and then persists session data under new session id.
*
* @returns {Promise} Promise -> Boolean (true || false)
* @returns {Promise<Boolean>}
*/
async roll() {
// Throw not started error if session was not started/ready
Expand All @@ -165,7 +165,7 @@ class Session {
/**
* This method performs the 'touch' operation updating session's expiry in storage.
*
* @returns {Promise} Promise
* @returns {Promise}
*/
touch() {
// Return if no session cookie was sent with request
Expand Down Expand Up @@ -371,6 +371,7 @@ class Session {

/**
* Returns whether session is ready and its data has been retrieved.
* @returns {Boolean}
*/
get ready() {
return this.#ready;
Expand All @@ -379,13 +380,15 @@ class Session {
/**
* Returns whether session has already been stored in database or not.
* This is helpful for choosing between INSERT/UPDATE operations for SQL based implementations.
* @returns {Boolean}
*/
get stored() {
return this.#from_database;
}

/**
* Returns the current session's lifetime duration in milliseconds.
* @returns {Number}
*/
get duration() {
const default_duration = this.#session_engine.options.duration;
Expand All @@ -394,7 +397,8 @@ class Session {
}

/**
* Returns the expiry unix timestamp in milliseconds of current session.
* Returns the expiry UNIX timestamp in milliseconds of current session.
* @returns {Number}
*/
get expires_at() {
return Date.now() + this.duration;
Expand Down
15 changes: 6 additions & 9 deletions src/components/SessionEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ class SessionEngine {
// Ensure the session duration is a valid number
const duration = this.#options.duration;
if (typeof duration !== 'number' || duration < 1)
throw new Error(
'new SessionEngine(options.duration) -> duration must be a valid number in milliseconds.'
);
throw new Error('new SessionEngine(options.duration) -> duration must be a valid number in milliseconds.');

// Ensure user has specified a secret as it is required
const secret = this.#options.cookie.secret;
Expand Down Expand Up @@ -86,14 +84,12 @@ class SessionEngine {
*
* @param {String} type [id, touch, read, write, destroy]
* @param {function(Session):void} handler
* @returns {SessionEngine}
* @returns {SessionEngine} SessionEngine (Chainable)
*/
use(type, handler) {
// Ensure type is valid string that is supported
if (typeof type !== 'string' || this.#methods[type] == undefined)
throw new Error(
'SessionEngine.use(type, handler) -> type must be a string that is a supported operation.'
);
throw new Error('SessionEngine.use(type, handler) -> type must be a string that is a supported operation.');

// Ensure handler is an executable function
if (typeof handler !== 'function')
Expand All @@ -105,7 +101,7 @@ class SessionEngine {
}

/**
* Triggers 'cleanup' operation.
* Triggers 'cleanup' operation based on the assigned "cleanup" handler.
*/
cleanup() {
return this.#methods.cleanup();
Expand All @@ -121,14 +117,15 @@ class SessionEngine {
}

/**
* SessionEngine operation methods.
* SessionEngine assigned operation method handlers.
*/
get methods() {
return this.#methods;
}

/**
* SessionEngine middleware function to be passed into HyperExpress.use() method.
* @returns {Function}
*/
get middleware() {
return this.#middleware;
Expand Down
Loading

0 comments on commit 4d5f361

Please sign in to comment.