Skip to content

Commit

Permalink
Version 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jormaechea committed Oct 1, 2019
1 parent 045ba76 commit e75f749
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [1.1.0] - 2019-10-01
### Added
- getSessionInstance method to session instance

## [1.0.0] - 2019-09-27
### Added
- Session management
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ const session = new ApiSession({

console.log(`Session created for user ${session.userId} on client ${session.clientCode}.`);

const client = await session.client;
const sessionInjectedModel = session.getSessionInstance(SomeModel);

const sessionInjectedModel = client.getInstance(SomeModel);
console.log(`Session is propagated for user ${sessionInjectedModel.session.userId} on client ${sessionInjectedModel.session.clientCode}.`);

console.log(`Session is propagated for user ${sessionInjectedModel.session.userId} on client ${sessionInjectedModel.session.clientCode}.`):
const client = await sessionInjectedModel.client;

console.log(client);
// Outputs your client object
```
20 changes: 14 additions & 6 deletions lib/api-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,27 @@ class ApiSession {
return client;
}

/**
* Gets an instance injected with the session
*
* @param {Function} TheClass The class
* @return {TheClass} The instance.
*/
getSessionInstance(TheClass) {
const instance = new TheClass();
instance.session = this;

return instance;
}

/**
* Inject the client with methods to propagate this session
*
* @param {object} client The client data
* @param {object} The client data injected
*/
inject(client) {
client.getInstance = TheClass => {
const instance = new TheClass();
instance.session = this;

return instance;
};
client.getInstance = this.getSessionInstance.bind(this);

return client;
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@janiscommerce/api-session",
"version": "1.0.0",
"version": "1.1.0",
"description": "A session manager for APIs",
"main": "lib/index.js",
"scripts": {
Expand Down
14 changes: 13 additions & 1 deletion tests/api-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,19 @@ describe('Api Session', () => {

const testInstance = client.getInstance(Test);

assert.strictEqual(testInstance.session, session);
assert.deepStrictEqual(testInstance.session, session);
});
});

describe('getSessionInstance', () => {

it('Should return an instance with the session injected', () => {

class Test {}

const testInstance = session.getSessionInstance(Test);

assert.deepStrictEqual(testInstance.session, session);
});
});

Expand Down

0 comments on commit e75f749

Please sign in to comment.