# @travetto/rest-session

> Session provider for the travetto rest module.

Latest version **5.1.1** (published 2025-01-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @travetto/rest-session
pnpm add @travetto/rest-session
yarn add @travetto/rest-session
bun add @travetto/rest-session
```

## Health

**Score 40/100 (D)** — status: stable.

Positive: no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 5.1.1 |
| Published | 2025-01-26 |
| First published | 2019-03-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 24.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 23 |
| Author | Travetto Framework |
| Maintainers | arcsine |
| Keywords | rest, session, travetto, typescript |

## Links

- npm: https://www.npmjs.com/package/@travetto/rest-session
- Repository: https://github.com/travetto/travetto
- Homepage: https://travetto.io
- Issues: https://github.com/travetto/travetto/issues
- npm.io page: https://npm.io/package/@travetto/rest-session

## Dependencies (3)

- [@travetto/rest](https://npm.io/package/@travetto/rest.md) ^5.1.0
- [@travetto/model](https://npm.io/package/@travetto/model.md) ^5.1.0
- [@travetto/config](https://npm.io/package/@travetto/config.md) ^5.1.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 5.1.1 (latest) — 2025-01-26
- 5.0.0-rc.15 (rc) — 2024-08-25
- 2.0.0-alpha.20 (alpha) — 2021-05-05
- 1.1.0-rc.0 (next) — 2020-09-20
- 1.0.0-beta.15 (beta) — 2019-10-04
- 5.1.0 — 2025-01-26
- 5.0.21 — 2025-01-16
- 5.0.20 — 2025-01-16
- 5.0.19 — 2025-01-01
- 5.0.18 — 2024-11-16
- 5.0.17 — 2024-10-26
- 5.0.16 — 2024-10-24
- 5.0.15 — 2024-10-20
- 5.0.14 — 2024-10-10
- 5.0.13 — 2024-10-06
- … 286 more at https://npm.io/package/@travetto/rest-session/versions

## README

<!-- This file was generated by @travetto/doc and should not be modified directly -->
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/rest-session/DOC.tsx and execute "npx trv doc" to rebuild -->
# REST Session

## Session provider for the travetto rest module.

**Install: @travetto/rest-session**
```bash
npm install @travetto/rest-session

# or

yarn add @travetto/rest-session
```

This is a module that adds session support to the [RESTful API](https://github.com/travetto/travetto/tree/main/module/rest#readme "Declarative api for RESTful APIs with support for the dependency injection module.") framework.  Sessions allow for persistent data across multiple requests.  Within the framework the sessions are stored against any [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") implementation that provides [ModelExpirySupport](https://github.com/travetto/travetto/tree/main/module/model/src/service/expiry.ts), as the data needs to be able to be expired appropriately.  The list of supported model providers are:
   *  [Redis Model Support](https://github.com/travetto/travetto/tree/main/module/model-redis#readme "Redis backing for the travetto model module.")
   *  [MongoDB Model Support](https://github.com/travetto/travetto/tree/main/module/model-mongo#readme "Mongo backing for the travetto model module.")
   *  [S3 Model Support](https://github.com/travetto/travetto/tree/main/module/model-s3#readme "S3 backing for the travetto model module.")
   *  [DynamoDB Model Support](https://github.com/travetto/travetto/tree/main/module/model-dynamodb#readme "DynamoDB backing for the travetto model module.")
   *  [Elasticsearch Model Source](https://github.com/travetto/travetto/tree/main/module/model-elasticsearch#readme "Elasticsearch backing for the travetto model module, with real-time modeling support for Elasticsearch mappings.")
   *  [File Model Support](https://github.com/travetto/travetto/tree/main/module/model-file#readme "File system backing for the travetto model module.")
   *  [Memory Model Support](https://github.com/travetto/travetto/tree/main/module/model-memory#readme "Memory backing for the travetto model module.")
A session allows for defining the expiration time, what state the session should be in, as well as the payload (session data).  The session and session data are accessible via the [@Context](https://github.com/travetto/travetto/tree/main/module/rest/src/decorator/param.ts#L38) parameter as [Session](https://github.com/travetto/travetto/tree/main/module/rest-session/src/session.ts#L15) and [SessionData](https://github.com/travetto/travetto/tree/main/module/rest-session/src/session.ts#L8) respectively.  Iit can also be accessed via the [Request](https://github.com/travetto/travetto/tree/main/module/rest-session/src/trv.d.ts#L7) as a session property.

**Code: Sample Session Usage**
```typescript
import { InjectableFactory } from '@travetto/di';
import { ModelExpirySupport } from '@travetto/model';
import { Controller, Put, Get } from '@travetto/rest';
import { SessionData, Session, SessionModelSymbol } from '@travetto/rest-session';
import { MemoryModelService } from '@travetto/model-memory';

// Applies to entire execution, not just this file
class SessionConfig {
  /**
   * Session provider must be specified. The memory service is sufficient for simple
   *   workloads, buts falls down when dealing with multiple servers
   */
  @InjectableFactory(SessionModelSymbol)
  static getSessionModel(memory: MemoryModelService): ModelExpirySupport {
    return memory;
  }
}

@Controller('/session')
export class SessionRoutes {

  @Put('/info')
  async storeInfo(data: SessionData) {
    data.age = 20;
    data.name = 'Roger'; // Setting data
  }

  @Get('/logout')
  async logout(session: Session) {
    await session.destroy();
  }

  @Get('/info/age')
  async getInfo(data: SessionData) {
    return data.age;
  }
}
```

This usage should be comparable to [express](https://expressjs.com), [koa](https://koajs.com/) and mostly every other framework.

## Session Configuration
The module supports a general set of configuration that should cover the majority of session behaviors:

**Code: Session Config**
```typescript
import { TimeUtil } from '@travetto/runtime';
import { Config } from '@travetto/config';

/**
 * Rest session config
 */
@Config('rest.session')
export class SessionConfig {
  /**
   * Should the session auto write
   */
  autoCommit = true;
  /**
   * Max age for a given session
   */
  maxAge = TimeUtil.asMillis(30, 'm'); // Half hour
  /**
   * Can the session be renewed
   */
  renew = true;
  /**
   * Should the session support rolling renewals
   */
  rolling = false;

}
```

These are all configurable via the `rest.session.*` config values.  And as a note, in production, a secret is required to be specified.

---
_Source: https://npm.io/package/@travetto/rest-session · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
