add documentation

This commit is contained in:
admin 2025-08-22 16:25:06 +02:00
parent 50ee195043
commit 3c7e98642f
4 changed files with 62 additions and 3 deletions

39
README.md Normal file
View File

@ -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<ISession> | Create a new Session from a IAccount and returns it |
| getBy(filter: (account: IAccount) => boolean): Promise<ISession[]> | get a ISession by a IAccount filter |
| getById(sessionId: string): Promise<ISession> | get a ISession by a session id. the returned ISession can be null if not exists |
| remove(sessionId: string): Promise<void> | remove the ISession by a session id. |

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@apihub24/in-memory-sessions", "name": "@apihub24/in-memory-sessions",
"version": "1.0.2", "version": "1.0.3",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@apihub24/in-memory-sessions", "name": "@apihub24/in-memory-sessions",
"version": "1.0.2", "version": "1.0.3",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@apihub24/token-authentication": "^1.0.4", "@apihub24/token-authentication": "^1.0.4",

View File

@ -1,6 +1,6 @@
{ {
"name": "@apihub24/in-memory-sessions", "name": "@apihub24/in-memory-sessions",
"version": "1.0.2", "version": "1.0.3",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",

View File

@ -9,6 +9,11 @@ import { Injectable } from "@nestjs/common";
export class InMemorySessionService implements ISessionService { export class InMemorySessionService implements ISessionService {
private source: ISession[] = []; 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<ISession> { create(account: IAccount): Promise<ISession> {
const s: ISession = { const s: ISession = {
id: crypto.randomUUID().toString(), id: crypto.randomUUID().toString(),
@ -19,16 +24,31 @@ export class InMemorySessionService implements ISessionService {
return Promise.resolve(s); 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<ISession[]> { getBy(filter: (account: IAccount) => boolean): Promise<ISession[]> {
return Promise.resolve(this.source.filter((x) => filter(x.account))); 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<ISession | null> { getById(sessionId: string): Promise<ISession | null> {
return Promise.resolve( return Promise.resolve(
this.source.filter((x) => x.id === sessionId)[0] ?? null 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<void> { remove(sessionId: string): Promise<void> {
this.source = this.source.filter((x) => x.id !== sessionId); this.source = this.source.filter((x) => x.id !== sessionId);
return Promise.resolve(); return Promise.resolve();