@rudderjs/session 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +15 -9
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -49,17 +49,21 @@ export default [
49
49
  ]
50
50
  ```
51
51
 
52
- ### 3. Add middleware to routes
52
+ ### 3. That's it the `web` group is auto-wired
53
+
54
+ `SessionProvider` installs the session middleware on the `web` route group
55
+ during `boot()` via `appendToGroup('web', sessionMiddleware(cfg))`. Every
56
+ route loaded through `withRouting({ web })` gets session support automatically —
57
+ you don't need to list it in `bootstrap/app.ts` or attach it per-route.
58
+
59
+ **API routes are stateless by default.** If a specific api route needs session,
60
+ mount `SessionMiddleware()` on just that route:
53
61
 
54
62
  ```ts
55
- // routes/web.ts
56
- import { Route } from '@rudderjs/router'
63
+ // routes/api.ts
57
64
  import { SessionMiddleware } from '@rudderjs/session'
58
65
 
59
- const webMw = [SessionMiddleware()]
60
-
61
- Route.get('/dashboard', handler, webMw)
62
- Route.post('/contact', handler, webMw)
66
+ Route.post('/api/preferences', handler, [SessionMiddleware()])
63
67
  ```
64
68
 
65
69
  ---
@@ -69,11 +73,12 @@ Route.post('/contact', handler, webMw)
69
73
  ### `req.session`
70
74
 
71
75
  ```ts
76
+ // routes/web.ts — session is already on the web group, no per-route wiring
72
77
  Route.get('/profile', (req, res) => {
73
78
  const visits = (req.session.get<number>('visits') ?? 0) + 1
74
79
  req.session.put('visits', visits)
75
80
  res.json({ visits })
76
- }, webMw)
81
+ })
77
82
  ```
78
83
 
79
84
  ### `Session` facade
@@ -155,7 +160,8 @@ Session ID is stored in the cookie; data lives in Redis under `{prefix}{id}`. Re
155
160
 
156
161
  ## Notes
157
162
 
158
- - Apply `SessionMiddleware()` to web routes onlyAPI routes should use stateless auth.
163
+ - The provider auto-installs on the `web` route group. API routes stay stateless opt in per-route with `SessionMiddleware()` if you really need session on an api endpoint.
164
+ - **Don't call `m.use(sessionMiddleware(cfg))` globally.** It doubles up with the auto-install, leaves api routes with an unwanted session, and consumers like `SessionGuard` will read from a different `SessionInstance`. Symptom: session data set in the handler doesn't persist across requests.
159
165
  - Session is saved automatically after the route handler returns; no manual `save()` needed.
160
166
  - The cookie driver stores all data client-side — keep values small. Use Redis for larger payloads.
161
167
  - `SessionMiddleware()` reads config from the DI container. Use `sessionMiddleware(config)` for manual wiring without a provider.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rudderjs/session",
3
- "version": "0.1.1",
3
+ "version": "1.0.0",
4
4
  "rudderjs": {
5
5
  "provider": "SessionProvider",
6
6
  "stage": "infrastructure"
@@ -24,8 +24,8 @@
24
24
  }
25
25
  },
26
26
  "dependencies": {
27
- "@rudderjs/contracts": "0.1.0",
28
- "@rudderjs/core": "0.1.1"
27
+ "@rudderjs/contracts": "^1.0.0",
28
+ "@rudderjs/core": "^1.0.0"
29
29
  },
30
30
  "optionalDependencies": {
31
31
  "ioredis": "^5.3.0"