From 3c7e98642f3307a4269910d9f6c91c2c3b18120a Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 22 Aug 2025 16:25:06 +0200 Subject: [PATCH] add documentation --- README.md | 39 ++++++++++++++++++++++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- src/in.memory.session.service.ts | 20 ++++++++++++++++ 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..2f59cb1 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# InMemory Sessions + +A Service to store Sessions in Memory. + +## Install + +```bash +npm i --save @apihub24/in-memory-sessions +``` + +## Dependencies + +| Exported Injection Keys | +| ------------------------- | +| @apihub24/session_service | + +## Usage + +```typescript +// NestJs usage +import { InMemorySessionsModule } from "@apihub24/in-memory-sessions"; + +@Module({ + imports: [InMemorySessionsModule.forRoot()] +}) +... + +// Other usage +import { InMemorySessionsModule } from "@apihub24/in-memory-sessions"; + +const sessionService = new InMemorySessionService(); +``` + +| Function | Description | +| ------------------------------------------------------------------ | ------------------------------------------------------------------------------- | +| create(account: IAccount): Promise | Create a new Session from a IAccount and returns it | +| getBy(filter: (account: IAccount) => boolean): Promise | get a ISession by a IAccount filter | +| getById(sessionId: string): Promise | get a ISession by a session id. the returned ISession can be null if not exists | +| remove(sessionId: string): Promise | remove the ISession by a session id. | diff --git a/package-lock.json b/package-lock.json index bbcfe83..b7ce727 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@apihub24/in-memory-sessions", - "version": "1.0.2", + "version": "1.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@apihub24/in-memory-sessions", - "version": "1.0.2", + "version": "1.0.3", "license": "MIT", "dependencies": { "@apihub24/token-authentication": "^1.0.4", diff --git a/package.json b/package.json index 52f294e..9e4a74c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@apihub24/in-memory-sessions", - "version": "1.0.2", + "version": "1.0.3", "description": "", "main": "dist/index.js", "types": "./dist/index.d.ts", diff --git a/src/in.memory.session.service.ts b/src/in.memory.session.service.ts index a9ea223..78f1f54 100644 --- a/src/in.memory.session.service.ts +++ b/src/in.memory.session.service.ts @@ -9,6 +9,11 @@ import { Injectable } from "@nestjs/common"; export class InMemorySessionService implements ISessionService { private source: ISession[] = []; + /** + * add a new Session into the Store and returns it. + * @param account a apihub24 token-authentication Account + * @returns a apihub24 token-authentication Session + */ create(account: IAccount): Promise { const s: ISession = { id: crypto.randomUUID().toString(), @@ -19,16 +24,31 @@ export class InMemorySessionService implements ISessionService { return Promise.resolve(s); } + /** + * select all Sessions that matches the given Account Filter + * @param filter a Account Filter Function + * @returns a apihub24 token-authentication Session + */ getBy(filter: (account: IAccount) => boolean): Promise { return Promise.resolve(this.source.filter((x) => filter(x.account))); } + /** + * select the Session by the given id. If no Session with the id exists null was returned! + * @param sessionId a session id + * @returns a apihub24 token-authentication Session or null + */ getById(sessionId: string): Promise { return Promise.resolve( this.source.filter((x) => x.id === sessionId)[0] ?? null ); } + /** + * delete the Session by the given id from the Store + * @param sessionId a session id + * @returns nothing + */ remove(sessionId: string): Promise { this.source = this.source.filter((x) => x.id !== sessionId); return Promise.resolve();